Skip to content

Commit 1eb4d8d

Browse files
SonoIoSonoIo
authored andcommitted
Better popViewToInstance
1 parent efc5948 commit 1eb4d8d

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

lib/viewstack.js

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var ViewStack = module.exports = Backbone.View.extend({
99
_stackReplace: null,
1010
_current: null,
1111
_length: null,
12+
_baseZIndex: 0,
1213

1314
initialize: function initialize(options){
1415
if (!options || (!options.context && !options.shared))
@@ -53,6 +54,7 @@ var ViewStack = module.exports = Backbone.View.extend({
5354
// Controllo se è la stessa view che sto cercando di pushare
5455
if ( this._current === newView ) return this;
5556

57+
var zIndex = this.getZIndex();
5658

5759
if ( options && options.replace == true ){
5860

@@ -64,16 +66,14 @@ var ViewStack = module.exports = Backbone.View.extend({
6466

6567
popped.$el.detach();
6668

67-
// Calcolo il nuovo z-Index che è incrementale
68-
var zIndex = (this._stack.length*100) + 100;
6969
// Setto lo z-index
7070
if ( newView.setZindex )
7171
newView.setZindex( zIndex );
7272
else
7373
newView.$el.css("z-index", zIndex);
7474

7575
this._stack.push( newView );
76-
76+
7777
this.$el.append( newView.el );
7878

7979
// Lancio l'evento onDeactivate se è impostato
@@ -84,14 +84,10 @@ var ViewStack = module.exports = Backbone.View.extend({
8484
this._current = newView;
8585
//
8686
newView.render();
87-
88-
8987
}
9088

9189
}else{
9290

93-
// Calcolo il nuovo z-Index che è incrementale
94-
var zIndex = (this._stack.length*100) + 100;
9591
// Setto lo z-index
9692
if ( newView.setZindex )
9793
newView.setZindex( zIndex );
@@ -144,53 +140,39 @@ var ViewStack = module.exports = Backbone.View.extend({
144140
if (!poppedView)
145141
poppedView = currentView;
146142

147-
if ( _.isFunction(poppedView.confirmPop) ){
148-
return poppedView.confirmPop( function(confirm){ return pop(confirm); } );
149-
}else{
150-
return pop();
143+
if (!options.animated){
144+
if ( poppedView.onPop ) poppedView.onPop();
145+
poppedView.trigger('pop');
146+
return self._popView(poppedView);
151147
}
152148

153-
154-
function pop( confirm ){
155-
if ( confirm === false )
156-
throw new Error("The popView was interrupted");
157-
158-
if (!options.animated){
159-
if ( poppedView.onPop ) poppedView.onPop();
160-
poppedView.trigger('pop');
161-
return self._popView(poppedView);
162-
}
163-
164-
self._popView(poppedView, false);
149+
self._popView(poppedView, false);
165150

166-
if ( !options.animatedName ){
167-
poppedView.$el.one('webkitAnimationEnd mozAnimationEnd msAnimationEnd animationend', function (e) {
151+
if ( !options.animatedName ){
152+
poppedView.$el.one('webkitAnimationEnd mozAnimationEnd msAnimationEnd animationend', function (e) {
153+
if ( poppedView.onPop )
154+
poppedView.onPop();
155+
poppedView.trigger('pop');
156+
poppedView.destroy();
157+
poppedView.trigger("deactive");
158+
});
159+
}else{
160+
// Viene usata nei casi di più animazioni nella view. Cos' mi assicuro di distruggerla solo dopo aver
161+
// terminato l'animazione desiderata
162+
poppedView.$el.on('webkitAnimationEnd mozAnimationEnd msAnimationEnd animationend', function (e) {
163+
if ( e && e.originalEvent && e.originalEvent.animationName == options.animatedName ){
164+
poppedView.$el.off('webkitAnimationEnd mozAnimationEnd msAnimationEnd animationend');
168165
if ( poppedView.onPop )
169166
poppedView.onPop();
170167
poppedView.trigger('pop');
171168
poppedView.destroy();
172169
poppedView.trigger("deactive");
173-
});
174-
}else{
175-
// Viene usata nei casi di più animazioni nella view. Cos' mi assicuro di distruggerla solo dopo aver
176-
// terminato l'animazione desiderata
177-
poppedView.$el.on('webkitAnimationEnd mozAnimationEnd msAnimationEnd animationend', function (e) {
178-
if ( e && e.originalEvent && e.originalEvent.animationName == options.animatedName ){
179-
poppedView.$el.off('webkitAnimationEnd mozAnimationEnd msAnimationEnd animationend');
180-
if ( poppedView.onPop )
181-
poppedView.onPop();
182-
poppedView.trigger('pop');
183-
poppedView.destroy();
184-
poppedView.trigger("deactive");
185-
}
186-
});
187-
}
188-
poppedView.$el.addClass('pop');
189-
190-
return poppedView;
170+
}
171+
});
191172
}
173+
poppedView.$el.addClass('pop');
192174

193-
return this;
175+
return poppedView;
194176
},
195177

196178
// Update page URL
@@ -296,12 +278,37 @@ var ViewStack = module.exports = Backbone.View.extend({
296278
},
297279

298280
popViewToInstance: function popViewToInstance(instance, options) {
281+
options = _.defaults(options || {}, {
282+
popInstance: true
283+
});
299284
var indexOfViewToPop = this.indexOf(instance);
300285
if (indexOfViewToPop === -1) return;
301-
for (var i = this._stack.length - 1; i >= indexOfViewToPop; i--) {
286+
for (var i = this._stack.length - 1; shouldPopView(i); i--) {
302287
this.popView(this._stack[i], options);
303288
}
304289
return this;
290+
291+
function shouldPopView(indexOfView) {
292+
if (indexOfView > indexOfViewToPop)
293+
return true;
294+
else if (options.popInstance && indexOfView === indexOfViewToPop)
295+
return true;
296+
else
297+
return false;
298+
}
299+
},
300+
301+
getZIndex: function () {
302+
return (this._stack.length * 100) + 100 + this._baseZIndex;
303+
},
304+
305+
setBaseZIndex: function (value) {
306+
this._baseZIndex = parseInt(value);
307+
return this;
308+
},
309+
310+
getBaseZIndex: function () {
311+
return this._baseZIndex;
305312
}
306313

307314
});

0 commit comments

Comments
 (0)