customize-views.js 4.9 KB

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