PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/js/customize-models.js

https://gitlab.com/webkod3r/tripolis
JavaScript | 247 lines | 174 code | 41 blank | 32 comment | 20 complexity | 3a799b747d1cc99f440ec4630a32e040 MD5 | raw file
  1. /* global _wpCustomizeHeader */
  2. (function( $, wp ) {
  3. var api = wp.customize;
  4. api.HeaderTool = {};
  5. /**
  6. * wp.customize.HeaderTool.ImageModel
  7. *
  8. * A header image. This is where saves via the Customizer API are
  9. * abstracted away, plus our own AJAX calls to add images to and remove
  10. * images from the user's recently uploaded images setting on the server.
  11. * These calls are made regardless of whether the user actually saves new
  12. * Customizer settings.
  13. *
  14. * @constructor
  15. * @augments Backbone.Model
  16. */
  17. api.HeaderTool.ImageModel = Backbone.Model.extend({
  18. defaults: function() {
  19. return {
  20. header: {
  21. attachment_id: 0,
  22. url: '',
  23. timestamp: _.now(),
  24. thumbnail_url: ''
  25. },
  26. choice: '',
  27. selected: false,
  28. random: false
  29. };
  30. },
  31. initialize: function() {
  32. this.on('hide', this.hide, this);
  33. },
  34. hide: function() {
  35. this.set('choice', '');
  36. api('header_image').set('remove-header');
  37. api('header_image_data').set('remove-header');
  38. },
  39. destroy: function() {
  40. var data = this.get('header'),
  41. curr = api.HeaderTool.currentHeader.get('header').attachment_id;
  42. // If the image we're removing is also the current header, unset
  43. // the latter
  44. if (curr && data.attachment_id === curr) {
  45. api.HeaderTool.currentHeader.trigger('hide');
  46. }
  47. wp.ajax.post( 'custom-header-remove', {
  48. nonce: _wpCustomizeHeader.nonces.remove,
  49. wp_customize: 'on',
  50. theme: api.settings.theme.stylesheet,
  51. attachment_id: data.attachment_id
  52. });
  53. this.trigger('destroy', this, this.collection);
  54. },
  55. save: function() {
  56. if (this.get('random')) {
  57. api('header_image').set(this.get('header').random);
  58. api('header_image_data').set(this.get('header').random);
  59. } else {
  60. if (this.get('header').defaultName) {
  61. api('header_image').set(this.get('header').url);
  62. api('header_image_data').set(this.get('header').defaultName);
  63. } else {
  64. api('header_image').set(this.get('header').url);
  65. api('header_image_data').set(this.get('header'));
  66. }
  67. }
  68. api.HeaderTool.combinedList.trigger('control:setImage', this);
  69. },
  70. importImage: function() {
  71. var data = this.get('header');
  72. if (data.attachment_id === undefined) {
  73. return;
  74. }
  75. wp.ajax.post( 'custom-header-add', {
  76. nonce: _wpCustomizeHeader.nonces.add,
  77. wp_customize: 'on',
  78. theme: api.settings.theme.stylesheet,
  79. attachment_id: data.attachment_id
  80. } );
  81. },
  82. shouldBeCropped: function() {
  83. if (this.get('themeFlexWidth') === true &&
  84. this.get('themeFlexHeight') === true) {
  85. return false;
  86. }
  87. if (this.get('themeFlexWidth') === true &&
  88. this.get('themeHeight') === this.get('imageHeight')) {
  89. return false;
  90. }
  91. if (this.get('themeFlexHeight') === true &&
  92. this.get('themeWidth') === this.get('imageWidth')) {
  93. return false;
  94. }
  95. if (this.get('themeWidth') === this.get('imageWidth') &&
  96. this.get('themeHeight') === this.get('imageHeight')) {
  97. return false;
  98. }
  99. if (this.get('imageWidth') <= this.get('themeWidth')) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. });
  105. /**
  106. * wp.customize.HeaderTool.ChoiceList
  107. *
  108. * @constructor
  109. * @augments Backbone.Collection
  110. */
  111. api.HeaderTool.ChoiceList = Backbone.Collection.extend({
  112. model: api.HeaderTool.ImageModel,
  113. // Ordered from most recently used to least
  114. comparator: function(model) {
  115. return -model.get('header').timestamp;
  116. },
  117. initialize: function() {
  118. var current = api.HeaderTool.currentHeader.get('choice').replace(/^https?:\/\//, ''),
  119. isRandom = this.isRandomChoice(api.get().header_image);
  120. // Overridable by an extending class
  121. if (!this.type) {
  122. this.type = 'uploaded';
  123. }
  124. // Overridable by an extending class
  125. if (typeof this.data === 'undefined') {
  126. this.data = _wpCustomizeHeader.uploads;
  127. }
  128. if (isRandom) {
  129. // So that when adding data we don't hide regular images
  130. current = api.get().header_image;
  131. }
  132. this.on('control:setImage', this.setImage, this);
  133. this.on('control:removeImage', this.removeImage, this);
  134. this.on('add', this.maybeAddRandomChoice, this);
  135. _.each(this.data, function(elt, index) {
  136. if (!elt.attachment_id) {
  137. elt.defaultName = index;
  138. }
  139. if (typeof elt.timestamp === 'undefined') {
  140. elt.timestamp = 0;
  141. }
  142. this.add({
  143. header: elt,
  144. choice: elt.url.split('/').pop(),
  145. selected: current === elt.url.replace(/^https?:\/\//, '')
  146. }, { silent: true });
  147. }, this);
  148. if (this.size() > 0) {
  149. this.addRandomChoice(current);
  150. }
  151. },
  152. maybeAddRandomChoice: function() {
  153. if (this.size() === 1) {
  154. this.addRandomChoice();
  155. }
  156. },
  157. addRandomChoice: function(initialChoice) {
  158. var isRandomSameType = RegExp(this.type).test(initialChoice),
  159. randomChoice = 'random-' + this.type + '-image';
  160. this.add({
  161. header: {
  162. timestamp: 0,
  163. random: randomChoice,
  164. width: 245,
  165. height: 41
  166. },
  167. choice: randomChoice,
  168. random: true,
  169. selected: isRandomSameType
  170. });
  171. },
  172. isRandomChoice: function(choice) {
  173. return (/^random-(uploaded|default)-image$/).test(choice);
  174. },
  175. shouldHideTitle: function() {
  176. return this.size() < 2;
  177. },
  178. setImage: function(model) {
  179. this.each(function(m) {
  180. m.set('selected', false);
  181. });
  182. if (model) {
  183. model.set('selected', true);
  184. }
  185. },
  186. removeImage: function() {
  187. this.each(function(m) {
  188. m.set('selected', false);
  189. });
  190. }
  191. });
  192. /**
  193. * wp.customize.HeaderTool.DefaultsList
  194. *
  195. * @constructor
  196. * @augments wp.customize.HeaderTool.ChoiceList
  197. * @augments Backbone.Collection
  198. */
  199. api.HeaderTool.DefaultsList = api.HeaderTool.ChoiceList.extend({
  200. initialize: function() {
  201. this.type = 'default';
  202. this.data = _wpCustomizeHeader.defaults;
  203. api.HeaderTool.ChoiceList.prototype.initialize.apply(this);
  204. }
  205. });
  206. })( jQuery, window.wp );