wp/wp-includes/js/customize-models.js
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     1 /* globals _wpCustomizeHeader, _ */
     1 /* global _wpCustomizeHeader */
     2 (function( $, wp ) {
     2 (function( $, wp ) {
     3 	var api = wp.customize;
     3 	var api = wp.customize;
       
     4 	/** @namespace wp.customize.HeaderTool */
     4 	api.HeaderTool = {};
     5 	api.HeaderTool = {};
     5 
     6 
     6 
     7 
     7 	/**
     8 	/**
     8 	 * wp.customize.HeaderTool.ImageModel
     9 	 * wp.customize.HeaderTool.ImageModel
    11 	 * abstracted away, plus our own AJAX calls to add images to and remove
    12 	 * abstracted away, plus our own AJAX calls to add images to and remove
    12 	 * images from the user's recently uploaded images setting on the server.
    13 	 * images from the user's recently uploaded images setting on the server.
    13 	 * These calls are made regardless of whether the user actually saves new
    14 	 * These calls are made regardless of whether the user actually saves new
    14 	 * Customizer settings.
    15 	 * Customizer settings.
    15 	 *
    16 	 *
       
    17 	 * @memberOf wp.customize.HeaderTool
       
    18 	 * @alias wp.customize.HeaderTool.ImageModel
       
    19 	 *
    16 	 * @constructor
    20 	 * @constructor
    17 	 * @augments Backbone.Model
    21 	 * @augments Backbone.Model
    18 	 */
    22 	 */
    19 	api.HeaderTool.ImageModel = Backbone.Model.extend({
    23 	api.HeaderTool.ImageModel = Backbone.Model.extend(/** @lends wp.customize.HeaderTool.ImageModel.prototype */{
    20 		defaults: function() {
    24 		defaults: function() {
    21 			return {
    25 			return {
    22 				header: {
    26 				header: {
    23 					attachment_id: 0,
    27 					attachment_id: 0,
    24 					url: '',
    28 					url: '',
   123 
   127 
   124 
   128 
   125 	/**
   129 	/**
   126 	 * wp.customize.HeaderTool.ChoiceList
   130 	 * wp.customize.HeaderTool.ChoiceList
   127 	 *
   131 	 *
       
   132 	 * @memberOf wp.customize.HeaderTool
       
   133 	 * @alias wp.customize.HeaderTool.ChoiceList
       
   134 	 *
   128 	 * @constructor
   135 	 * @constructor
   129 	 * @augments Backbone.Collection
   136 	 * @augments Backbone.Collection
   130 	 */
   137 	 */
   131 	api.HeaderTool.ChoiceList = Backbone.Collection.extend({
   138 	api.HeaderTool.ChoiceList = Backbone.Collection.extend({
   132 		model: api.HeaderTool.ImageModel,
   139 		model: api.HeaderTool.ImageModel,
   155 				current = api.get().header_image;
   162 				current = api.get().header_image;
   156 			}
   163 			}
   157 
   164 
   158 			this.on('control:setImage', this.setImage, this);
   165 			this.on('control:setImage', this.setImage, this);
   159 			this.on('control:removeImage', this.removeImage, this);
   166 			this.on('control:removeImage', this.removeImage, this);
       
   167 			this.on('add', this.maybeRemoveOldCrop, this);
   160 			this.on('add', this.maybeAddRandomChoice, this);
   168 			this.on('add', this.maybeAddRandomChoice, this);
   161 
   169 
   162 			_.each(this.data, function(elt, index) {
   170 			_.each(this.data, function(elt, index) {
   163 				if (!elt.attachment_id) {
   171 				if (!elt.attachment_id) {
   164 					elt.defaultName = index;
   172 					elt.defaultName = index;
   175 				}, { silent: true });
   183 				}, { silent: true });
   176 			}, this);
   184 			}, this);
   177 
   185 
   178 			if (this.size() > 0) {
   186 			if (this.size() > 0) {
   179 				this.addRandomChoice(current);
   187 				this.addRandomChoice(current);
       
   188 			}
       
   189 		},
       
   190 
       
   191 		maybeRemoveOldCrop: function( model ) {
       
   192 			var newID = model.get( 'header' ).attachment_id || false,
       
   193 			 	oldCrop;
       
   194 
       
   195 			// Bail early if we don't have a new attachment ID.
       
   196 			if ( ! newID ) {
       
   197 				return;
       
   198 			}
       
   199 
       
   200 			oldCrop = this.find( function( item ) {
       
   201 				return ( item.cid !== model.cid && item.get( 'header' ).attachment_id === newID );
       
   202 			} );
       
   203 
       
   204 			// If we found an old crop, remove it from the collection.
       
   205 			if ( oldCrop ) {
       
   206 				this.remove( oldCrop );
   180 			}
   207 			}
   181 		},
   208 		},
   182 
   209 
   183 		maybeAddRandomChoice: function() {
   210 		maybeAddRandomChoice: function() {
   184 			if (this.size() === 1) {
   211 			if (this.size() === 1) {
   229 	});
   256 	});
   230 
   257 
   231 
   258 
   232 	/**
   259 	/**
   233 	 * wp.customize.HeaderTool.DefaultsList
   260 	 * wp.customize.HeaderTool.DefaultsList
       
   261 	 *
       
   262 	 * @memberOf wp.customize.HeaderTool
       
   263 	 * @alias wp.customize.HeaderTool.DefaultsList
   234 	 *
   264 	 *
   235 	 * @constructor
   265 	 * @constructor
   236 	 * @augments wp.customize.HeaderTool.ChoiceList
   266 	 * @augments wp.customize.HeaderTool.ChoiceList
   237 	 * @augments Backbone.Collection
   267 	 * @augments Backbone.Collection
   238 	 */
   268 	 */