home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-includes / js / customize-models.js < prev    next >
Encoding:
JavaScript  |  2017-10-04  |  6.6 KB  |  278 lines

  1. /* global _wpCustomizeHeader */
  2. (function( $, wp ) {
  3.     var api = wp.customize;
  4.     /** @namespace wp.customize.HeaderTool */
  5.     api.HeaderTool = {};
  6.  
  7.  
  8.     /**
  9.      * wp.customize.HeaderTool.ImageModel
  10.      *
  11.      * A header image. This is where saves via the Customizer API are
  12.      * abstracted away, plus our own AJAX calls to add images to and remove
  13.      * images from the user's recently uploaded images setting on the server.
  14.      * These calls are made regardless of whether the user actually saves new
  15.      * Customizer settings.
  16.      *
  17.      * @memberOf wp.customize.HeaderTool
  18.      * @alias wp.customize.HeaderTool.ImageModel
  19.      *
  20.      * @constructor
  21.      * @augments Backbone.Model
  22.      */
  23.     api.HeaderTool.ImageModel = Backbone.Model.extend(/** @lends wp.customize.HeaderTool.ImageModel.prototype */{
  24.         defaults: function() {
  25.             return {
  26.                 header: {
  27.                     attachment_id: 0,
  28.                     url: '',
  29.                     timestamp: _.now(),
  30.                     thumbnail_url: ''
  31.                 },
  32.                 choice: '',
  33.                 selected: false,
  34.                 random: false
  35.             };
  36.         },
  37.  
  38.         initialize: function() {
  39.             this.on('hide', this.hide, this);
  40.         },
  41.  
  42.         hide: function() {
  43.             this.set('choice', '');
  44.             api('header_image').set('remove-header');
  45.             api('header_image_data').set('remove-header');
  46.         },
  47.  
  48.         destroy: function() {
  49.             var data = this.get('header'),
  50.                 curr = api.HeaderTool.currentHeader.get('header').attachment_id;
  51.  
  52.             // If the image we're removing is also the current header, unset
  53.             // the latter
  54.             if (curr && data.attachment_id === curr) {
  55.                 api.HeaderTool.currentHeader.trigger('hide');
  56.             }
  57.  
  58.             wp.ajax.post( 'custom-header-remove', {
  59.                 nonce: _wpCustomizeHeader.nonces.remove,
  60.                 wp_customize: 'on',
  61.                 theme: api.settings.theme.stylesheet,
  62.                 attachment_id: data.attachment_id
  63.             });
  64.  
  65.             this.trigger('destroy', this, this.collection);
  66.         },
  67.  
  68.         save: function() {
  69.             if (this.get('random')) {
  70.                 api('header_image').set(this.get('header').random);
  71.                 api('header_image_data').set(this.get('header').random);
  72.             } else {
  73.                 if (this.get('header').defaultName) {
  74.                     api('header_image').set(this.get('header').url);
  75.                     api('header_image_data').set(this.get('header').defaultName);
  76.                 } else {
  77.                     api('header_image').set(this.get('header').url);
  78.                     api('header_image_data').set(this.get('header'));
  79.                 }
  80.             }
  81.  
  82.             api.HeaderTool.combinedList.trigger('control:setImage', this);
  83.         },
  84.  
  85.         importImage: function() {
  86.             var data = this.get('header');
  87.             if (data.attachment_id === undefined) {
  88.                 return;
  89.             }
  90.  
  91.             wp.ajax.post( 'custom-header-add', {
  92.                 nonce: _wpCustomizeHeader.nonces.add,
  93.                 wp_customize: 'on',
  94.                 theme: api.settings.theme.stylesheet,
  95.                 attachment_id: data.attachment_id
  96.             } );
  97.         },
  98.  
  99.         shouldBeCropped: function() {
  100.             if (this.get('themeFlexWidth') === true &&
  101.                         this.get('themeFlexHeight') === true) {
  102.                 return false;
  103.             }
  104.  
  105.             if (this.get('themeFlexWidth') === true &&
  106.                 this.get('themeHeight') === this.get('imageHeight')) {
  107.                 return false;
  108.             }
  109.  
  110.             if (this.get('themeFlexHeight') === true &&
  111.                 this.get('themeWidth') === this.get('imageWidth')) {
  112.                 return false;
  113.             }
  114.  
  115.             if (this.get('themeWidth') === this.get('imageWidth') &&
  116.                 this.get('themeHeight') === this.get('imageHeight')) {
  117.                 return false;
  118.             }
  119.  
  120.             if (this.get('imageWidth') <= this.get('themeWidth')) {
  121.                 return false;
  122.             }
  123.  
  124.             return true;
  125.         }
  126.     });
  127.  
  128.  
  129.     /**
  130.      * wp.customize.HeaderTool.ChoiceList
  131.      *
  132.      * @memberOf wp.customize.HeaderTool
  133.      * @alias wp.customize.HeaderTool.ChoiceList
  134.      *
  135.      * @constructor
  136.      * @augments Backbone.Collection
  137.      */
  138.     api.HeaderTool.ChoiceList = Backbone.Collection.extend({
  139.         model: api.HeaderTool.ImageModel,
  140.  
  141.         // Ordered from most recently used to least
  142.         comparator: function(model) {
  143.             return -model.get('header').timestamp;
  144.         },
  145.  
  146.         initialize: function() {
  147.             var current = api.HeaderTool.currentHeader.get('choice').replace(/^https?:\/\//, ''),
  148.                 isRandom = this.isRandomChoice(api.get().header_image);
  149.  
  150.             // Overridable by an extending class
  151.             if (!this.type) {
  152.                 this.type = 'uploaded';
  153.             }
  154.  
  155.             // Overridable by an extending class
  156.             if (typeof this.data === 'undefined') {
  157.                 this.data = _wpCustomizeHeader.uploads;
  158.             }
  159.  
  160.             if (isRandom) {
  161.                 // So that when adding data we don't hide regular images
  162.                 current = api.get().header_image;
  163.             }
  164.  
  165.             this.on('control:setImage', this.setImage, this);
  166.             this.on('control:removeImage', this.removeImage, this);
  167.             this.on('add', this.maybeRemoveOldCrop, this);
  168.             this.on('add', this.maybeAddRandomChoice, this);
  169.  
  170.             _.each(this.data, function(elt, index) {
  171.                 if (!elt.attachment_id) {
  172.                     elt.defaultName = index;
  173.                 }
  174.  
  175.                 if (typeof elt.timestamp === 'undefined') {
  176.                     elt.timestamp = 0;
  177.                 }
  178.  
  179.                 this.add({
  180.                     header: elt,
  181.                     choice: elt.url.split('/').pop(),
  182.                     selected: current === elt.url.replace(/^https?:\/\//, '')
  183.                 }, { silent: true });
  184.             }, this);
  185.  
  186.             if (this.size() > 0) {
  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 );
  207.             }
  208.         },
  209.  
  210.         maybeAddRandomChoice: function() {
  211.             if (this.size() === 1) {
  212.                 this.addRandomChoice();
  213.             }
  214.         },
  215.  
  216.         addRandomChoice: function(initialChoice) {
  217.             var isRandomSameType = RegExp(this.type).test(initialChoice),
  218.                 randomChoice = 'random-' + this.type + '-image';
  219.  
  220.             this.add({
  221.                 header: {
  222.                     timestamp: 0,
  223.                     random: randomChoice,
  224.                     width: 245,
  225.                     height: 41
  226.                 },
  227.                 choice: randomChoice,
  228.                 random: true,
  229.                 selected: isRandomSameType
  230.             });
  231.         },
  232.  
  233.         isRandomChoice: function(choice) {
  234.             return (/^random-(uploaded|default)-image$/).test(choice);
  235.         },
  236.  
  237.         shouldHideTitle: function() {
  238.             return this.size() < 2;
  239.         },
  240.  
  241.         setImage: function(model) {
  242.             this.each(function(m) {
  243.                 m.set('selected', false);
  244.             });
  245.  
  246.             if (model) {
  247.                 model.set('selected', true);
  248.             }
  249.         },
  250.  
  251.         removeImage: function() {
  252.             this.each(function(m) {
  253.                 m.set('selected', false);
  254.             });
  255.         }
  256.     });
  257.  
  258.  
  259.     /**
  260.      * wp.customize.HeaderTool.DefaultsList
  261.      *
  262.      * @memberOf wp.customize.HeaderTool
  263.      * @alias wp.customize.HeaderTool.DefaultsList
  264.      *
  265.      * @constructor
  266.      * @augments wp.customize.HeaderTool.ChoiceList
  267.      * @augments Backbone.Collection
  268.      */
  269.     api.HeaderTool.DefaultsList = api.HeaderTool.ChoiceList.extend({
  270.         initialize: function() {
  271.             this.type = 'default';
  272.             this.data = _wpCustomizeHeader.defaults;
  273.             api.HeaderTool.ChoiceList.prototype.initialize.apply(this);
  274.         }
  275.     });
  276.  
  277. })( jQuery, window.wp );
  278.