author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1 |
/** @namespace wp */ |
0 | 2 |
window.wp = window.wp || {}; |
3 |
||
4 |
(function ($) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6 |
* Create the WordPress Backbone namespace. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
8 |
* @namespace wp.Backbone |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
9 |
*/ |
0 | 10 |
wp.Backbone = {}; |
11 |
||
12 |
||
13 |
// wp.Backbone.Subviews |
|
14 |
// -------------------- |
|
15 |
// |
|
16 |
// A subview manager. |
|
17 |
wp.Backbone.Subviews = function( view, views ) { |
|
18 |
this.view = view; |
|
19 |
this._views = _.isArray( views ) ? { '': views } : views || {}; |
|
20 |
}; |
|
21 |
||
22 |
wp.Backbone.Subviews.extend = Backbone.Model.extend; |
|
23 |
||
24 |
_.extend( wp.Backbone.Subviews.prototype, { |
|
25 |
// ### Fetch all of the subviews |
|
26 |
// |
|
27 |
// Returns an array of all subviews. |
|
28 |
all: function() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
return _.flatten( _.values( this._views ) ); |
0 | 30 |
}, |
31 |
||
32 |
// ### Get a selector's subviews |
|
33 |
// |
|
34 |
// Fetches all subviews that match a given `selector`. |
|
35 |
// |
|
36 |
// If no `selector` is provided, it will grab all subviews attached |
|
37 |
// to the view's root. |
|
38 |
get: function( selector ) { |
|
39 |
selector = selector || ''; |
|
40 |
return this._views[ selector ]; |
|
41 |
}, |
|
42 |
||
43 |
// ### Get a selector's first subview |
|
44 |
// |
|
45 |
// Fetches the first subview that matches a given `selector`. |
|
46 |
// |
|
47 |
// If no `selector` is provided, it will grab the first subview |
|
48 |
// attached to the view's root. |
|
49 |
// |
|
50 |
// Useful when a selector only has one subview at a time. |
|
51 |
first: function( selector ) { |
|
52 |
var views = this.get( selector ); |
|
53 |
return views && views.length ? views[0] : null; |
|
54 |
}, |
|
55 |
||
56 |
// ### Register subview(s) |
|
57 |
// |
|
58 |
// Registers any number of `views` to a `selector`. |
|
59 |
// |
|
60 |
// When no `selector` is provided, the root selector (the empty string) |
|
61 |
// is used. `views` accepts a `Backbone.View` instance or an array of |
|
62 |
// `Backbone.View` instances. |
|
63 |
// |
|
64 |
// --- |
|
65 |
// |
|
66 |
// Accepts an `options` object, which has a significant effect on the |
|
67 |
// resulting behavior. |
|
68 |
// |
|
69 |
// `options.silent` – *boolean, `false`* |
|
70 |
// > If `options.silent` is true, no DOM modifications will be made. |
|
71 |
// |
|
72 |
// `options.add` – *boolean, `false`* |
|
73 |
// > Use `Views.add()` as a shortcut for setting `options.add` to true. |
|
74 |
// |
|
75 |
// > By default, the provided `views` will replace |
|
76 |
// any existing views associated with the selector. If `options.add` |
|
77 |
// is true, the provided `views` will be added to the existing views. |
|
78 |
// |
|
79 |
// `options.at` – *integer, `undefined`* |
|
80 |
// > When adding, to insert `views` at a specific index, use |
|
81 |
// `options.at`. By default, `views` are added to the end of the array. |
|
82 |
set: function( selector, views, options ) { |
|
83 |
var existing, next; |
|
84 |
||
85 |
if ( ! _.isString( selector ) ) { |
|
86 |
options = views; |
|
87 |
views = selector; |
|
88 |
selector = ''; |
|
89 |
} |
|
90 |
||
91 |
options = options || {}; |
|
92 |
views = _.isArray( views ) ? views : [ views ]; |
|
93 |
existing = this.get( selector ); |
|
94 |
next = views; |
|
95 |
||
96 |
if ( existing ) { |
|
97 |
if ( options.add ) { |
|
98 |
if ( _.isUndefined( options.at ) ) { |
|
99 |
next = existing.concat( views ); |
|
100 |
} else { |
|
101 |
next = existing; |
|
102 |
next.splice.apply( next, [ options.at, 0 ].concat( views ) ); |
|
103 |
} |
|
104 |
} else { |
|
105 |
_.each( next, function( view ) { |
|
106 |
view.__detach = true; |
|
107 |
}); |
|
108 |
||
109 |
_.each( existing, function( view ) { |
|
110 |
if ( view.__detach ) |
|
111 |
view.$el.detach(); |
|
112 |
else |
|
113 |
view.remove(); |
|
114 |
}); |
|
115 |
||
116 |
_.each( next, function( view ) { |
|
117 |
delete view.__detach; |
|
118 |
}); |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
this._views[ selector ] = next; |
|
123 |
||
124 |
_.each( views, function( subview ) { |
|
125 |
var constructor = subview.Views || wp.Backbone.Subviews, |
|
126 |
subviews = subview.views = subview.views || new constructor( subview ); |
|
127 |
subviews.parent = this.view; |
|
128 |
subviews.selector = selector; |
|
129 |
}, this ); |
|
130 |
||
131 |
if ( ! options.silent ) |
|
132 |
this._attach( selector, views, _.extend({ ready: this._isReady() }, options ) ); |
|
133 |
||
134 |
return this; |
|
135 |
}, |
|
136 |
||
137 |
// ### Add subview(s) to existing subviews |
|
138 |
// |
|
139 |
// An alias to `Views.set()`, which defaults `options.add` to true. |
|
140 |
// |
|
141 |
// Adds any number of `views` to a `selector`. |
|
142 |
// |
|
143 |
// When no `selector` is provided, the root selector (the empty string) |
|
144 |
// is used. `views` accepts a `Backbone.View` instance or an array of |
|
145 |
// `Backbone.View` instances. |
|
146 |
// |
|
147 |
// Use `Views.set()` when setting `options.add` to `false`. |
|
148 |
// |
|
149 |
// Accepts an `options` object. By default, provided `views` will be |
|
150 |
// inserted at the end of the array of existing views. To insert |
|
151 |
// `views` at a specific index, use `options.at`. If `options.silent` |
|
152 |
// is true, no DOM modifications will be made. |
|
153 |
// |
|
154 |
// For more information on the `options` object, see `Views.set()`. |
|
155 |
add: function( selector, views, options ) { |
|
156 |
if ( ! _.isString( selector ) ) { |
|
157 |
options = views; |
|
158 |
views = selector; |
|
159 |
selector = ''; |
|
160 |
} |
|
161 |
||
162 |
return this.set( selector, views, _.extend({ add: true }, options ) ); |
|
163 |
}, |
|
164 |
||
165 |
// ### Stop tracking subviews |
|
166 |
// |
|
167 |
// Stops tracking `views` registered to a `selector`. If no `views` are |
|
168 |
// set, then all of the `selector`'s subviews will be unregistered and |
|
169 |
// removed. |
|
170 |
// |
|
171 |
// Accepts an `options` object. If `options.silent` is set, `remove` |
|
172 |
// will *not* be triggered on the unregistered views. |
|
173 |
unset: function( selector, views, options ) { |
|
174 |
var existing; |
|
175 |
||
176 |
if ( ! _.isString( selector ) ) { |
|
177 |
options = views; |
|
178 |
views = selector; |
|
179 |
selector = ''; |
|
180 |
} |
|
181 |
||
182 |
views = views || []; |
|
183 |
||
184 |
if ( existing = this.get( selector ) ) { |
|
185 |
views = _.isArray( views ) ? views : [ views ]; |
|
186 |
this._views[ selector ] = views.length ? _.difference( existing, views ) : []; |
|
187 |
} |
|
188 |
||
189 |
if ( ! options || ! options.silent ) |
|
190 |
_.invoke( views, 'remove' ); |
|
191 |
||
192 |
return this; |
|
193 |
}, |
|
194 |
||
195 |
// ### Detach all subviews |
|
196 |
// |
|
197 |
// Detaches all subviews from the DOM. |
|
198 |
// |
|
199 |
// Helps to preserve all subview events when re-rendering the master |
|
200 |
// view. Used in conjunction with `Views.render()`. |
|
201 |
detach: function() { |
|
202 |
$( _.pluck( this.all(), 'el' ) ).detach(); |
|
203 |
return this; |
|
204 |
}, |
|
205 |
||
206 |
// ### Render all subviews |
|
207 |
// |
|
208 |
// Renders all subviews. Used in conjunction with `Views.detach()`. |
|
209 |
render: function() { |
|
210 |
var options = { |
|
211 |
ready: this._isReady() |
|
212 |
}; |
|
213 |
||
214 |
_.each( this._views, function( views, selector ) { |
|
215 |
this._attach( selector, views, options ); |
|
216 |
}, this ); |
|
217 |
||
218 |
this.rendered = true; |
|
219 |
return this; |
|
220 |
}, |
|
221 |
||
222 |
// ### Remove all subviews |
|
223 |
// |
|
224 |
// Triggers the `remove()` method on all subviews. Detaches the master |
|
225 |
// view from its parent. Resets the internals of the views manager. |
|
226 |
// |
|
227 |
// Accepts an `options` object. If `options.silent` is set, `unset` |
|
228 |
// will *not* be triggered on the master view's parent. |
|
229 |
remove: function( options ) { |
|
230 |
if ( ! options || ! options.silent ) { |
|
231 |
if ( this.parent && this.parent.views ) |
|
232 |
this.parent.views.unset( this.selector, this.view, { silent: true }); |
|
233 |
delete this.parent; |
|
234 |
delete this.selector; |
|
235 |
} |
|
236 |
||
237 |
_.invoke( this.all(), 'remove' ); |
|
238 |
this._views = []; |
|
239 |
return this; |
|
240 |
}, |
|
241 |
||
242 |
// ### Replace a selector's subviews |
|
243 |
// |
|
244 |
// By default, sets the `$target` selector's html to the subview `els`. |
|
245 |
// |
|
246 |
// Can be overridden in subclasses. |
|
247 |
replace: function( $target, els ) { |
|
248 |
$target.html( els ); |
|
249 |
return this; |
|
250 |
}, |
|
251 |
||
252 |
// ### Insert subviews into a selector |
|
253 |
// |
|
254 |
// By default, appends the subview `els` to the end of the `$target` |
|
255 |
// selector. If `options.at` is set, inserts the subview `els` at the |
|
256 |
// provided index. |
|
257 |
// |
|
258 |
// Can be overridden in subclasses. |
|
259 |
insert: function( $target, els, options ) { |
|
260 |
var at = options && options.at, |
|
261 |
$children; |
|
262 |
||
263 |
if ( _.isNumber( at ) && ($children = $target.children()).length > at ) |
|
264 |
$children.eq( at ).before( els ); |
|
265 |
else |
|
266 |
$target.append( els ); |
|
267 |
||
268 |
return this; |
|
269 |
}, |
|
270 |
||
271 |
// ### Trigger the ready event |
|
272 |
// |
|
273 |
// **Only use this method if you know what you're doing.** |
|
274 |
// For performance reasons, this method does not check if the view is |
|
275 |
// actually attached to the DOM. It's taking your word for it. |
|
276 |
// |
|
277 |
// Fires the ready event on the current view and all attached subviews. |
|
278 |
ready: function() { |
|
279 |
this.view.trigger('ready'); |
|
280 |
||
281 |
// Find all attached subviews, and call ready on them. |
|
282 |
_.chain( this.all() ).map( function( view ) { |
|
283 |
return view.views; |
|
284 |
}).flatten().where({ attached: true }).invoke('ready'); |
|
285 |
}, |
|
286 |
||
287 |
// #### Internal. Attaches a series of views to a selector. |
|
288 |
// |
|
289 |
// Checks to see if a matching selector exists, renders the views, |
|
290 |
// performs the proper DOM operation, and then checks if the view is |
|
291 |
// attached to the document. |
|
292 |
_attach: function( selector, views, options ) { |
|
293 |
var $selector = selector ? this.view.$( selector ) : this.view.$el, |
|
294 |
managers; |
|
295 |
||
296 |
// Check if we found a location to attach the views. |
|
297 |
if ( ! $selector.length ) |
|
298 |
return this; |
|
299 |
||
300 |
managers = _.chain( views ).pluck('views').flatten().value(); |
|
301 |
||
302 |
// Render the views if necessary. |
|
303 |
_.each( managers, function( manager ) { |
|
304 |
if ( manager.rendered ) |
|
305 |
return; |
|
306 |
||
307 |
manager.view.render(); |
|
308 |
manager.rendered = true; |
|
309 |
}, this ); |
|
310 |
||
311 |
// Insert or replace the views. |
|
312 |
this[ options.add ? 'insert' : 'replace' ]( $selector, _.pluck( views, 'el' ), options ); |
|
313 |
||
314 |
// Set attached and trigger ready if the current view is already |
|
315 |
// attached to the DOM. |
|
316 |
_.each( managers, function( manager ) { |
|
317 |
manager.attached = true; |
|
318 |
||
319 |
if ( options.ready ) |
|
320 |
manager.ready(); |
|
321 |
}, this ); |
|
322 |
||
323 |
return this; |
|
324 |
}, |
|
325 |
||
326 |
// #### Internal. Checks if the current view is in the DOM. |
|
327 |
_isReady: function() { |
|
328 |
var node = this.view.el; |
|
329 |
while ( node ) { |
|
330 |
if ( node === document.body ) |
|
331 |
return true; |
|
332 |
node = node.parentNode; |
|
333 |
} |
|
334 |
||
335 |
return false; |
|
336 |
} |
|
337 |
}); |
|
338 |
||
339 |
||
340 |
// wp.Backbone.View |
|
341 |
// ---------------- |
|
342 |
// |
|
343 |
// The base view class. |
|
344 |
wp.Backbone.View = Backbone.View.extend({ |
|
345 |
// The constructor for the `Views` manager. |
|
346 |
Subviews: wp.Backbone.Subviews, |
|
347 |
||
5 | 348 |
constructor: function( options ) { |
0 | 349 |
this.views = new this.Subviews( this, this.views ); |
350 |
this.on( 'ready', this.ready, this ); |
|
351 |
||
5 | 352 |
this.options = options || {}; |
353 |
||
0 | 354 |
Backbone.View.apply( this, arguments ); |
355 |
}, |
|
356 |
||
357 |
remove: function() { |
|
358 |
var result = Backbone.View.prototype.remove.apply( this, arguments ); |
|
359 |
||
360 |
// Recursively remove child views. |
|
361 |
if ( this.views ) |
|
362 |
this.views.remove(); |
|
363 |
||
364 |
return result; |
|
365 |
}, |
|
366 |
||
367 |
render: function() { |
|
368 |
var options; |
|
369 |
||
370 |
if ( this.prepare ) |
|
371 |
options = this.prepare(); |
|
372 |
||
373 |
this.views.detach(); |
|
374 |
||
375 |
if ( this.template ) { |
|
376 |
options = options || {}; |
|
377 |
this.trigger( 'prepare', options ); |
|
378 |
this.$el.html( this.template( options ) ); |
|
379 |
} |
|
380 |
||
381 |
this.views.render(); |
|
382 |
return this; |
|
383 |
}, |
|
384 |
||
385 |
prepare: function() { |
|
386 |
return this.options; |
|
387 |
}, |
|
388 |
||
389 |
ready: function() {} |
|
390 |
}); |
|
391 |
}(jQuery)); |