Skip to content

Commit 665c9a8

Browse files
committed
bar.js: position is saved on event 'unload'
1 parent 44fef9f commit 665c9a8

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

src/Tracy/assets/Bar/bar.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
});
7272
});
7373

74+
window.addEventListener('unload', function() {
75+
_this.savePosition();
76+
});
77+
7478
this.restorePosition();
7579
};
7680

@@ -119,7 +123,6 @@
119123
this.elem.classList.remove(Panel.FLOAT);
120124
this.elem.classList.add(Panel.PEEK);
121125
this.elem.style.display = 'none';
122-
localStorage.removeItem(this.id); // delete position
123126
};
124127

125128
Panel.prototype.toWindow = function() {
@@ -159,7 +162,6 @@
159162
}
160163
});
161164

162-
localStorage.setItem(this.id, JSON.stringify({window: true}));
163165
this.elem.style.display = 'none';
164166
this.elem.classList.remove(Panel.FLOAT);
165167
this.elem.classList.remove(Panel.PEEK);
@@ -168,12 +170,9 @@
168170
};
169171

170172
Panel.prototype.reposition = function() {
171-
if (!this.is(Panel.WINDOW)) {
172-
var pos = getPosition(this.elem);
173-
if (pos.width) { // is visible?
174-
setPosition(this.elem, {right: pos.right, bottom: pos.bottom});
175-
localStorage.setItem(this.id, JSON.stringify({right: pos.right, bottom: pos.bottom}));
176-
}
173+
var pos = getPosition(this.elem);
174+
if (pos.width) { // is visible?
175+
setPosition(this.elem, {right: pos.right, bottom: pos.bottom});
177176
}
178177
};
179178

@@ -184,6 +183,17 @@
184183
coords.bottom = Math.min(Math.max(coords.bottom, -0.2 * el.offsetHeight), height - el.offsetHeight);
185184
};
186185

186+
Panel.prototype.savePosition = function() {
187+
var pos = getPosition(this.elem);
188+
if (this.is(Panel.WINDOW)) {
189+
localStorage.setItem(this.id, JSON.stringify({window: true}));
190+
} else if (pos.width) {
191+
localStorage.setItem(this.id, JSON.stringify({right: pos.right, bottom: pos.bottom}));
192+
} else {
193+
localStorage.removeItem(this.id);
194+
}
195+
};
196+
187197
Panel.prototype.restorePosition = function() {
188198
var pos = JSON.parse(localStorage.getItem(this.id));
189199
if (!pos) {
@@ -213,10 +223,7 @@
213223
draggable(elem, {
214224
rightEdge: true,
215225
bottomEdge: true,
216-
draggedClass: 'tracy-dragged',
217-
stop: function() {
218-
_this.savePosition();
219-
}
226+
draggedClass: 'tracy-dragged'
220227
});
221228

222229
[].forEach.call(elem.querySelectorAll('a'), function(a) {
@@ -239,7 +246,6 @@
239246
right: getPosition(panel.elem).right + Math.round(Math.random() * 100) + 20,
240247
bottom: getPosition(panel.elem).bottom + Math.round(Math.random() * 100) + 20
241248
});
242-
panel.reposition();
243249
}
244250
}
245251
e.preventDefault();
@@ -267,6 +273,10 @@
267273
});
268274
});
269275

276+
window.addEventListener('unload', function() {
277+
_this.savePosition();
278+
});
279+
270280
this.restorePosition();
271281
};
272282

0 commit comments

Comments
 (0)