Skip to content

Commit 986981d

Browse files
author
Michele Belluco
committed
first commit
1 parent e8085d8 commit 986981d

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
backbone.viewstack
22
==================
3+
4+
Need browserify to work
5+
6+

bower.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "backbone.viewstack",
3+
"version": "0.0.1",
4+
"homepage": "https://github.com/vash15/backbone.viewstack",
5+
"authors": [
6+
"Michele Belluco <michele@belluco.info>"
7+
],
8+
"description": "View stack for Backbone",
9+
"main": "lib/viewstack.js",
10+
"keywords": [
11+
"backbone",
12+
"view",
13+
"viewstack"
14+
],
15+
"license": "MIT",
16+
"ignore": [
17+
"**/.*",
18+
"node_modules",
19+
"bower_components",
20+
"test",
21+
"tests"
22+
]
23+
}

lib/viewstack.js

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
;(function(){
2+
3+
var _ = require("underscore");
4+
var $ = require('jquery');
5+
var BackBone = require("backbone");
6+
7+
var ViewStack = module.exports = BackBone.View.extend({
8+
9+
_stack: null,
10+
_stackReplace: null,
11+
_current: null,
12+
13+
initialize: function initialize(){
14+
this._stack = [];
15+
this._stackReplace = [];
16+
this._current = null;
17+
this.$el.empty();
18+
},
19+
20+
render: function render(){
21+
return this;
22+
},
23+
24+
getView: function getView(){
25+
return this._stack.length > 0 ? this._stack[ this._stack.length - 1 ] : null;
26+
},
27+
28+
getViewActive: function getViewActive(){
29+
return this._current;
30+
},
31+
32+
// pushView
33+
// Aggiunge una view all'applicazione con z-Index più alto
34+
pushView: function pushView( newView, options ){
35+
36+
// Controllo se è la stessa view che sto cercando di pushare
37+
if ( this._current === newView ) return this;
38+
39+
40+
if ( options && options.replace == true ){
41+
42+
if ( this._current ){
43+
44+
var popped = this._stack.pop();
45+
46+
this._stackReplace.push( popped );
47+
48+
popped.$el.detach();
49+
50+
// Calcolo il nuovo z-Index che è incrementale
51+
var zIndex = (this._stack.length*100) + 100;
52+
// Setto lo z-index
53+
if ( newView.setZindex )
54+
newView.setZindex( zIndex );
55+
else
56+
newView.$el.css("z-index", zIndex);
57+
58+
this._stack.push( newView );
59+
60+
this.$el.append( newView.el );
61+
62+
// setto la view Corrente
63+
this._current = newView;
64+
//
65+
newView.render();
66+
67+
68+
}
69+
70+
}else{
71+
72+
if ( this._current.onActivate )
73+
this._current.onActivate();
74+
75+
// Calcolo il nuovo z-Index che è incrementale
76+
var zIndex = (this._stack.length*100) + 100;
77+
// Setto lo z-index
78+
if ( newView.setZindex )
79+
newView.setZindex( zIndex );
80+
else
81+
newView.$el.css("z-index", zIndex);
82+
// aggiungo allo stack
83+
this._stack.push( newView );
84+
// Appendo alla view dell'applicazione
85+
this.$el.append( newView.el );
86+
// setto la view Corrente
87+
this._current = newView;
88+
//
89+
newView.render();
90+
91+
}
92+
93+
// Scateno l'evento active per dire alla view che è attiva
94+
if ( this._current )
95+
this._current.trigger("active");
96+
97+
this
98+
.refreshUrl()
99+
.trigger('pushed', newView);
100+
return this;
101+
},
102+
103+
104+
// popView
105+
// Rimuove l'ultima view dello stack
106+
popView: function popView(){
107+
108+
var self = this,
109+
poppedView = self._current;
110+
111+
if ( !poppedView || !poppedView.onDeactivate )
112+
return self._popView();
113+
114+
poppedView.onDeactivate(function(){
115+
self._popView();
116+
});
117+
return poppedView;
118+
119+
},
120+
121+
// refresh
122+
// Aggiorna l'url se la view corrette ce l'ha settata
123+
refreshUrl: function refreshUrl(){
124+
var shared = this.getShared();
125+
if ( typeof shared !== "undefined" && this._current && this._current.url ){
126+
shared.app.navigate(_.result(this._current, 'url'));
127+
}
128+
return this;
129+
},
130+
131+
// clearStack
132+
// Pulisce tutto lo stack dalle view
133+
clearStack: function clearStack(){
134+
135+
while( this._stack.length > 0 ){
136+
this.popView();
137+
}
138+
139+
return this;
140+
},
141+
142+
143+
//
144+
// Private
145+
//
146+
147+
_popView: function _popView(){
148+
var popped = this._stack.pop();
149+
if (popped) {
150+
popped.destroy();
151+
popped.trigger("deactive");
152+
}
153+
154+
// Assegno la nuova view corrente
155+
this._current = this._stack[ this._stack.length-1 ];
156+
157+
if ( this._stackReplace.length > 0 ){
158+
this.pushView( this._stackReplace.pop() );
159+
}
160+
161+
this
162+
.refreshUrl()
163+
.trigger('popped', popped);
164+
165+
return popped;
166+
}
167+
168+
});
169+
170+
171+
ViewStack.middleware = function middleware(options){
172+
return function(shared, next){
173+
var viewStack = new ViewStack(options);
174+
viewStack.clearStack(); // Pulisco dalle eventuali view appese
175+
viewStack.setShared( shared ); // setto lo shared object
176+
viewStack.render(); // Renderizzo la view base application
177+
shared.viewstack = viewStack;
178+
next();
179+
}
180+
181+
}
182+
183+
})();

0 commit comments

Comments
 (0)