site-logo-control.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * JS for handling the Site Logo Customizer control.
  3. */
  4. (function( wp, $ ){
  5. // nice shortcut
  6. var api = wp.customize;
  7. /**
  8. * The Customizer looks for wp.customizer.controlConstructor[type] functions
  9. * where type == the type member of a WP_Customize_Control
  10. */
  11. api.controlConstructor.site_logo = api.Control.extend({
  12. /**
  13. * This method is called when the control is ready to run.
  14. * Do all of your setup and event binding here.
  15. */
  16. ready: function() {
  17. // this.container is a jQuery object of your container
  18. // grab the bits of data from the title for specifying this control
  19. var data = this.container.find( '.customize-control-title' ).data();
  20. // Use specific l10n data for this control where available
  21. this.l10n = data.l10n;
  22. // Grab mime type
  23. this.mime = data.mime;
  24. // Set up image container and button elements. Cache for re-use.
  25. this.$imgContainer = $( '#customize-control-site_logo .current' );
  26. this.$btnContainer = $( '#customize-control-site_logo .actions' );
  27. this.$img = $( '<img class="site-logo-thumbnail" />' ).prependTo( this.$imgContainer );
  28. this.$placeholder = $( '<span>' + this.l10n.placeholder + '</span>' ).prependTo( this.$imgContainer );
  29. this.$btnAdd = $( '<button type="button" class="button new">' + this.l10n.upload + '</button>' ).prependTo( this.$btnContainer );
  30. this.$btnChange = $( '<button type="button" class="button change">' + this.l10n.change + '</button>' ).prependTo( this.$btnContainer );
  31. this.$btnRemove = $( '<button type="button" class="button remove">' + this.l10n.remove + '</button>' ).prependTo( this.$btnContainer );
  32. // handy shortcut so we don't have to us _.bind every time we add a callback
  33. _.bindAll( this, 'removeImg', 'upload', 'render', 'pick' );
  34. this.$btnAdd.on( 'click', this.upload );
  35. this.$btnChange.on( 'click', this.upload );
  36. this.$btnRemove.on( 'click', this.removeImg );
  37. // Call render method whenever setting is changed.
  38. this.setting.bind( 'change', this.render );
  39. // Do initial rendering.
  40. this.render();
  41. },
  42. /**
  43. * Remember that _.bind was used to maintain `this` as the control
  44. * object rather than the usual jQuery way of binding to the DOM element.
  45. */
  46. upload: function( event ) {
  47. event.preventDefault();
  48. if ( ! this.frame ) {
  49. this.initFrame();
  50. }
  51. this.frame.open();
  52. },
  53. /**
  54. * Set the media frame so that it can be reused and accessed when needed.
  55. */
  56. initFrame: function() {
  57. this.frame = wp.media({
  58. // The title of the media modal
  59. title: this.l10n.choose,
  60. // restrict to specified mime type
  61. library: {
  62. type: this.mime
  63. },
  64. // Customize the submit button.
  65. button: {
  66. // Set the text of the button.
  67. text: this.l10n.set
  68. },
  69. // Just one, thanks.
  70. multiple: false
  71. });
  72. // When an image is selected, run a callback.
  73. this.frame.on( 'select', this.pick );
  74. },
  75. /**
  76. * Fired when an image is selected in the media modal. Gets the selected
  77. * image information, and sets it within the control.
  78. */
  79. pick: function() {
  80. // get the attachment from the modal frame
  81. var attachment = this.frame.state().get( 'selection' ).single();
  82. if ( 'image' === attachment.get( 'type' ) ) {
  83. // set the setting - the callback will take care of rendering
  84. this.setting( this.reduceMembers( attachment.toJSON() ) );
  85. }
  86. },
  87. /**
  88. * Reduces the attachment object to just the few desired members.
  89. * @param {object} attachment An attachment object provided by the
  90. * medial modal.
  91. * @return {object} A reduced media object.
  92. */
  93. reduceMembers: function( attachment ) {
  94. var desired = [
  95. 'id',
  96. 'sizes',
  97. 'url'
  98. ],
  99. output = {};
  100. $.each( desired, function( i, key ){
  101. output[key] = attachment[key];
  102. });
  103. return output;
  104. },
  105. /**
  106. * Called on init and whenever a setting is changed. Shows the thumbnail
  107. * when there is one or the upload button when there isn't.
  108. */
  109. render: function() {
  110. var value = this.setting();
  111. if ( value && value.url ) {
  112. this.$placeholder.hide();
  113. if ( ! value.sizes || ! value.sizes.medium ) {
  114. this.$img.attr( 'src', value.url );
  115. } else {
  116. this.$img.attr( 'src', value.sizes.medium.url );
  117. }
  118. this.$img.show();
  119. this.$btnRemove.show();
  120. this.$btnChange.show();
  121. this.$btnAdd.hide();
  122. } else {
  123. this.$img.hide();
  124. this.$placeholder.show();
  125. this.$btnRemove.hide();
  126. this.$btnChange.hide();
  127. this.$btnAdd.show();
  128. }
  129. },
  130. /**
  131. * Called when the "Remove Image" link is clicked. Sets thes setting back
  132. * to its default state.
  133. * @param {object} event jQuery Event object from click event
  134. */
  135. removeImg: function( event ) {
  136. event.preventDefault();
  137. this.setting( {
  138. url: '',
  139. id: 0
  140. } );
  141. }
  142. });
  143. })( this.wp, jQuery );