-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone-viewport.js
332 lines (298 loc) · 10 KB
/
backbone-viewport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*! backbone-viewport - v0.5.3 - 2017-01-12
* https://github.com/dzhdmitry/backbone-viewport
* Copyright (c) 2017 Dmitry Dzhuleba;
* Licensed
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['underscore', 'backbone'], factory);
} else {
root.Viewport = factory(root._, root.Backbone);
}
}(this, function(_, Backbone) {
var Viewport = {};
if (!_) {
console.error("Underscore is required by Backbone-Viewport");
return;
}
if (!Backbone) {
console.error("Backbone is required by Backbone-Viewport");
return;
}
Viewport.View = Backbone.View.extend({
tagName: "div",
initialize: function() {
this.listenTo(this.model, 'render', this.render);
this.listenTo(this.model, 'change:active', this.toggleActive);
},
/**
* Must be overridden.
* Receives model attributes and return rendered string.
*
* @param {Object} data
* @returns {String}
*/
template: function(data) {
return "";
},
/**
* Renders model attributes by .template() and toggles the container by model's `active` attribute.
*
* @returns {Viewport.View}
*/
render: function() {
var html = this.template(this.model.toJSON());
this.$el.html(html);
this.trigger("rendered");
return this;
},
/**
* Set `display: block` css style to page container if `active=true`, or `display:none` if false.
* Override it to use different behaviour.
*
* @param {Boolean} active
*/
toggle: function(active) {
var display = active ? "block" : "none";
this.$el.css("display", display);
},
toggleActive: function() {
this.toggle(this.model.get("active"));
}
});
Viewport.Model = Backbone.Model.extend({
idAttribute: "uri",
defaults: {
active: false // Indicates visibility of a page. When true, page container is set `display: block` css style, and `display:none` if false
// All model's attributes are available in `view.template()`
},
getFetchOptions: function() {
return {};
},
url: function() {
return "/" + this.get("uri");
},
/**
* Set `page.active` property to `true` (must cause view rendering).
* Triggers `shown` event.
*/
show: function() {
this.set("active", true);
if (this.hasChanged("active")) {
this.trigger("shown");
}
},
/**
* Set `page.active` property to `false`.
* Triggers `hidden` event.
*/
hide: function() {
this.set("active", false);
if (this.hasChanged("active")) {
this.trigger("hidden");
}
}
});
Viewport.Collection = Backbone.Collection.extend({
model: Viewport.Model,
view: Viewport.View,
/**
* Create view for given model[s]
*
* @param {Viewport.Model|Viewport.Model[]} model
*/
createView: function(model) {
var self = this;
function fn(page) {
var view = new self.view({model: page});
self.el.append(view.render().el);
view.toggleActive();
}
if (_.isArray(model)) {
_.each(model, fn);
} else {
fn(model);
}
},
/**
* Create page model, add to collection and create view if needed
*
* @param {Object} attributes
* @param {Boolean} createView
* @returns {Viewport.Model}
*/
addPage: function(attributes, createView) {
var model = this.add(attributes);
if (createView) {
this.createView(model);
}
return model;
},
/**
* Merge page with attributes, create view or re-render
*
* @param {Object} attributes
* @param {Boolean} createView
* @returns {Viewport.Model}
*/
mergePage: function(attributes, createView) {
var model = this.add(attributes, {
merge: true
});
if (createView) {
this.createView(model);
} else {
model.trigger("render");
}
return model;
},
/**
* Add page with existing view
*
* @param {Object} attributes `uri` is mandatory
* @param {*} $el
* @returns {Viewport.Model}
*/
pushPage: function(attributes, $el) {
var defaults = {
active: true
},
modelAttributes = _.extend({}, defaults, attributes),
page = new this.model(modelAttributes),
view = new this.view({model: page});
view.setElement($el);
this.add(page);
return page;
},
/**
* Open page with given uri and hide others.
* Find and `show()` page with uri, `hide()` other pages.
*
* @param {String} uri
*/
open: function(uri) {
this.each(function(page) {
if (page.get("uri") == uri) {
page.show();
} else {
page.hide();
}
});
}
});
Viewport.Router = Backbone.Router.extend({
collection: Viewport.Collection,
initialize: function(options) {
var defaults = {
el: Backbone.$('body'),
start: true,
pushState: false,
silent: false,
root: '/',
pages: []
},
settings = _.extend({}, defaults, options);
this.pushState = settings.pushState;
this.silent = settings.silent;
this.root = settings.root;
this.pages = new this.collection();
this.pages.el = settings.el;
this.listenTo(this.pages, 'reset', function(collection) {
collection.each(function(model) {
collection.createView(model);
});
});
this.pages.reset(settings.pages);
if (settings.start) {
this.start();
}
Viewport.Router.__super__.initialize.call(this, options);
},
/**
* Run `Backbone.history.start()` with `pushState` and `root` provided in constructor and overridden by provided directly.
*
* @param {Object=} options
*/
start: function(options) {
var defaults = {
pushState: this.pushState,
root: this.root,
silent: this.silent
},
settings = _.extend({}, defaults, options);
Backbone.history.start(settings);
},
/**
* Stop watching uri changes (Run `Backbone.history.stop()`).
*/
stop: function() {
Backbone.history.stop();
},
/**
* Returns current route depends on router type (pushState or hash)
*
* @returns {String}
*/
getCurrentRoute: function() {
return (this.pushState) ? Backbone.history.getPath() : Backbone.history.getHash();
},
/**
* Read document uri and activate page with given `attributes` (PlainObject).
* If page not exists in collection, it will be created with given `attributes`, and added to collection.
*
* @param {Object} attributes
* @param {Object=} options
*/
go: function(attributes, options) {
var uri = this.getCurrentRoute(),
modelAttributes = _.extend({uri: uri}, attributes),
collection = this.pages;
var settings = _.extend({}, {
force: false,
load: false
}, options);
function openPage() {
collection.open(modelAttributes.uri);
}
function fetchPage(model, createView) {
var defaults = {
success: function(data) {
collection.mergePage(data, createView);
openPage();
}
};
model.fetch(_.extend(defaults, model.getFetchOptions()));
}
if (this.pages.has(modelAttributes.uri)) {
// Page is already in collection...
if (settings.force) {
// ...but re-rendering is required although
if (settings.load) {
// and loading is required too
var existed = this.pages.get(modelAttributes.uri);
fetchPage(existed, false);
} else {
// merge attributes to existing page
this.pages.mergePage(modelAttributes, false);
openPage();
}
} else {
// ...and just needs to be shown
openPage();
}
} else {
// Page is not in collection
if (settings.load) {
// and loading is required
var model = this.pages.addPage(modelAttributes, false);
fetchPage(model, true);
} else {
// and just needs to merge attributes and to be shown
this.pages.addPage(modelAttributes, true);
openPage();
}
}
}
});
return Viewport;
}));