You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

387 lines
10 KiB

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