PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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