pageflow 17.0.0 → 17.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,168 @@
1
+ const Backbone = require('backbone');
2
+ const _ = require('underscore');
3
+
4
+ /*!
5
+ * Includes BabySitter
6
+ * https://github.com/marionettejs/backbone.babysitter/
7
+ *
8
+ * Includes Wreqr
9
+ * https://github.com/marionettejs/backbone.wreqr/
10
+ */
11
+
12
+ // Backbone.BabySitter
13
+ // -------------------
14
+ // v0.0.6
15
+ //
16
+ // Copyright (c)2013 Derick Bailey, Muted Solutions, LLC.
17
+ // Distributed under MIT license
18
+ //
19
+ // http://github.com/babysitterjs/backbone.babysitter
20
+
21
+ // ChildViewContainer
22
+ // ---------------------------
23
+ //
24
+ // Provide a container to store, retrieve and
25
+ // shut down child views.
26
+
27
+ module.exports = (function(Backbone, _){
28
+
29
+ // Container Constructor
30
+ // ---------------------
31
+
32
+ var Container = function(views){
33
+ this._views = {};
34
+ this._indexByModel = {};
35
+ this._indexByCustom = {};
36
+ this._updateLength();
37
+
38
+ _.each(views, this.add, this);
39
+ };
40
+
41
+ // Container Methods
42
+ // -----------------
43
+
44
+ _.extend(Container.prototype, {
45
+
46
+ // Add a view to this container. Stores the view
47
+ // by `cid` and makes it searchable by the model
48
+ // cid (and model itself). Optionally specify
49
+ // a custom key to store an retrieve the view.
50
+ add: function(view, customIndex){
51
+ var viewCid = view.cid;
52
+
53
+ // store the view
54
+ this._views[viewCid] = view;
55
+
56
+ // index it by model
57
+ if (view.model){
58
+ this._indexByModel[view.model.cid] = viewCid;
59
+ }
60
+
61
+ // index by custom
62
+ if (customIndex){
63
+ this._indexByCustom[customIndex] = viewCid;
64
+ }
65
+
66
+ this._updateLength();
67
+ },
68
+
69
+ // Find a view by the model that was attached to
70
+ // it. Uses the model's `cid` to find it.
71
+ findByModel: function(model){
72
+ return this.findByModelCid(model.cid);
73
+ },
74
+
75
+ // Find a view by the `cid` of the model that was attached to
76
+ // it. Uses the model's `cid` to find the view `cid` and
77
+ // retrieve the view using it.
78
+ findByModelCid: function(modelCid){
79
+ var viewCid = this._indexByModel[modelCid];
80
+ return this.findByCid(viewCid);
81
+ },
82
+
83
+ // Find a view by a custom indexer.
84
+ findByCustom: function(index){
85
+ var viewCid = this._indexByCustom[index];
86
+ return this.findByCid(viewCid);
87
+ },
88
+
89
+ // Find by index. This is not guaranteed to be a
90
+ // stable index.
91
+ findByIndex: function(index){
92
+ return _.values(this._views)[index];
93
+ },
94
+
95
+ // retrieve a view by it's `cid` directly
96
+ findByCid: function(cid){
97
+ return this._views[cid];
98
+ },
99
+
100
+ // Remove a view
101
+ remove: function(view){
102
+ var viewCid = view.cid;
103
+
104
+ // delete model index
105
+ if (view.model){
106
+ delete this._indexByModel[view.model.cid];
107
+ }
108
+
109
+ // delete custom index
110
+ _.any(this._indexByCustom, function(cid, key) {
111
+ if (cid === viewCid) {
112
+ delete this._indexByCustom[key];
113
+ return true;
114
+ }
115
+ }, this);
116
+
117
+ // remove the view from the container
118
+ delete this._views[viewCid];
119
+
120
+ // update the length
121
+ this._updateLength();
122
+ },
123
+
124
+ // Call a method on every view in the container,
125
+ // passing parameters to the call method one at a
126
+ // time, like `function.call`.
127
+ call: function(method){
128
+ this.apply(method, _.tail(arguments));
129
+ },
130
+
131
+ // Apply a method on every view in the container,
132
+ // passing parameters to the call method one at a
133
+ // time, like `function.apply`.
134
+ apply: function(method, args){
135
+ _.each(this._views, function(view){
136
+ if (_.isFunction(view[method])){
137
+ view[method].apply(view, args || []);
138
+ }
139
+ });
140
+ },
141
+
142
+ // Update the `.length` attribute on this container
143
+ _updateLength: function(){
144
+ this.length = _.size(this._views);
145
+ }
146
+ });
147
+
148
+ // Borrowing this code from Backbone.Collection:
149
+ // http://backbonejs.org/docs/backbone.html#section-106
150
+ //
151
+ // Mix in methods from Underscore, for iteration, and other
152
+ // collection related features.
153
+ var methods = ['forEach', 'each', 'map', 'find', 'detect', 'filter',
154
+ 'select', 'reject', 'every', 'all', 'some', 'any', 'include',
155
+ 'contains', 'invoke', 'toArray', 'first', 'initial', 'rest',
156
+ 'last', 'without', 'isEmpty', 'pluck'];
157
+
158
+ _.each(methods, function(method) {
159
+ Container.prototype[method] = function() {
160
+ var views = _.values(this._views);
161
+ var args = [views].concat(_.toArray(arguments));
162
+ return _[method].apply(_, args);
163
+ };
164
+ });
165
+
166
+ // return the public API
167
+ return Container;
168
+ })(Backbone, _);