home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / js / customize-views.js < prev    next >
Encoding:
JavaScript  |  2017-09-08  |  4.9 KB  |  199 lines

  1. (function( $, wp, _ ) {
  2.  
  3.     if ( ! wp || ! wp.customize ) { return; }
  4.     var api = wp.customize;
  5.  
  6.     /**
  7.      * wp.customize.HeaderTool.CurrentView
  8.      *
  9.      * Displays the currently selected header image, or a placeholder in lack
  10.      * thereof.
  11.      *
  12.      * Instantiate with model wp.customize.HeaderTool.currentHeader.
  13.      *
  14.      * @memberOf wp.customize.HeaderTool
  15.      * @alias wp.customize.HeaderTool.CurrentView
  16.      *
  17.      * @constructor
  18.      * @augments wp.Backbone.View
  19.      */
  20.     api.HeaderTool.CurrentView = wp.Backbone.View.extend(/** @lends wp.customize.HeaderTool.CurrentView.prototype */{
  21.         template: wp.template('header-current'),
  22.  
  23.         initialize: function() {
  24.             this.listenTo(this.model, 'change', this.render);
  25.             this.render();
  26.         },
  27.  
  28.         render: function() {
  29.             this.$el.html(this.template(this.model.toJSON()));
  30.             this.setButtons();
  31.             return this;
  32.         },
  33.  
  34.         setButtons: function() {
  35.             var elements = $('#customize-control-header_image .actions .remove');
  36.             if (this.model.get('choice')) {
  37.                 elements.show();
  38.             } else {
  39.                 elements.hide();
  40.             }
  41.         }
  42.     });
  43.  
  44.  
  45.     /**
  46.      * wp.customize.HeaderTool.ChoiceView
  47.      *
  48.      * Represents a choosable header image, be it user-uploaded,
  49.      * theme-suggested or a special Randomize choice.
  50.      *
  51.      * Takes a wp.customize.HeaderTool.ImageModel.
  52.      *
  53.      * Manually changes model wp.customize.HeaderTool.currentHeader via the
  54.      * `select` method.
  55.      *
  56.      * @memberOf wp.customize.HeaderTool
  57.      * @alias wp.customize.HeaderTool.ChoiceView
  58.      *
  59.      * @constructor
  60.      * @augments wp.Backbone.View
  61.      */
  62.     api.HeaderTool.ChoiceView = wp.Backbone.View.extend(/** @lends wp.customize.HeaderTool.ChoiceView.prototype */{
  63.         template: wp.template('header-choice'),
  64.  
  65.         className: 'header-view',
  66.  
  67.         events: {
  68.             'click .choice,.random': 'select',
  69.             'click .close': 'removeImage'
  70.         },
  71.  
  72.         initialize: function() {
  73.             var properties = [
  74.                 this.model.get('header').url,
  75.                 this.model.get('choice')
  76.             ];
  77.  
  78.             this.listenTo(this.model, 'change:selected', this.toggleSelected);
  79.  
  80.             if (_.contains(properties, api.get().header_image)) {
  81.                 api.HeaderTool.currentHeader.set(this.extendedModel());
  82.             }
  83.         },
  84.  
  85.         render: function() {
  86.             this.$el.html(this.template(this.extendedModel()));
  87.  
  88.             this.toggleSelected();
  89.             return this;
  90.         },
  91.  
  92.         toggleSelected: function() {
  93.             this.$el.toggleClass('selected', this.model.get('selected'));
  94.         },
  95.  
  96.         extendedModel: function() {
  97.             var c = this.model.get('collection');
  98.             return _.extend(this.model.toJSON(), {
  99.                 type: c.type
  100.             });
  101.         },
  102.  
  103.         select: function() {
  104.             this.preventJump();
  105.             this.model.save();
  106.             api.HeaderTool.currentHeader.set(this.extendedModel());
  107.         },
  108.  
  109.         preventJump: function() {
  110.             var container = $('.wp-full-overlay-sidebar-content'),
  111.                 scroll = container.scrollTop();
  112.  
  113.             _.defer(function() {
  114.                 container.scrollTop(scroll);
  115.             });
  116.         },
  117.  
  118.         removeImage: function(e) {
  119.             e.stopPropagation();
  120.             this.model.destroy();
  121.             this.remove();
  122.         }
  123.     });
  124.  
  125.  
  126.     /**
  127.      * wp.customize.HeaderTool.ChoiceListView
  128.      *
  129.      * A container for ChoiceViews. These choices should be of one same type:
  130.      * user-uploaded headers or theme-defined ones.
  131.      *
  132.      * Takes a wp.customize.HeaderTool.ChoiceList.
  133.      *
  134.      * @memberOf wp.customize.HeaderTool
  135.      * @alias wp.customize.HeaderTool.ChoiceListView
  136.      *
  137.      * @constructor
  138.      * @augments wp.Backbone.View
  139.      */
  140.     api.HeaderTool.ChoiceListView = wp.Backbone.View.extend(/** @lends wp.customize.HeaderTool.ChoiceListView.prototype */{
  141.         initialize: function() {
  142.             this.listenTo(this.collection, 'add', this.addOne);
  143.             this.listenTo(this.collection, 'remove', this.render);
  144.             this.listenTo(this.collection, 'sort', this.render);
  145.             this.listenTo(this.collection, 'change', this.toggleList);
  146.             this.render();
  147.         },
  148.  
  149.         render: function() {
  150.             this.$el.empty();
  151.             this.collection.each(this.addOne, this);
  152.             this.toggleList();
  153.         },
  154.  
  155.         addOne: function(choice) {
  156.             var view;
  157.             choice.set({ collection: this.collection });
  158.             view = new api.HeaderTool.ChoiceView({ model: choice });
  159.             this.$el.append(view.render().el);
  160.         },
  161.  
  162.         toggleList: function() {
  163.             var title = this.$el.parents().prev('.customize-control-title'),
  164.                 randomButton = this.$el.find('.random').parent();
  165.             if (this.collection.shouldHideTitle()) {
  166.                 title.add(randomButton).hide();
  167.             } else {
  168.                 title.add(randomButton).show();
  169.             }
  170.         }
  171.     });
  172.  
  173.  
  174.     /**
  175.      * wp.customize.HeaderTool.CombinedList
  176.      *
  177.      * Aggregates wp.customize.HeaderTool.ChoiceList collections (or any
  178.      * Backbone object, really) and acts as a bus to feed them events.
  179.      *
  180.      * @memberOf wp.customize.HeaderTool
  181.      * @alias wp.customize.HeaderTool.CombinedList
  182.      *
  183.      * @constructor
  184.      * @augments wp.Backbone.View
  185.      */
  186.     api.HeaderTool.CombinedList = wp.Backbone.View.extend(/** @lends wp.customize.HeaderTool.CombinedList.prototype */{
  187.         initialize: function(collections) {
  188.             this.collections = collections;
  189.             this.on('all', this.propagate, this);
  190.         },
  191.         propagate: function(event, arg) {
  192.             _.each(this.collections, function(collection) {
  193.                 collection.trigger(event, arg);
  194.             });
  195.         }
  196.     });
  197.  
  198. })( jQuery, window.wp, _ );
  199.