customize-views.js 4.9 KB

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