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