background.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. (function( $, undefined ) {
  2. 'use strict';
  3. wp.customize.controlConstructor['vamtam-background'] = wp.customize.Control.extend({
  4. ready: function() {
  5. var control = this;
  6. control.setupColor();
  7. // Shortcut so that we don't have to use _.bind every time we add a callback.
  8. _.bindAll( control, 'restoreDefault', 'removeFile', 'openFrame', 'select' );
  9. // Bind events, with delegation to facilitate re-rendering.
  10. control.container.on( 'click keydown', '.upload-button', control.openFrame );
  11. control.container.on( 'click keydown', '.thumbnail-image img', control.openFrame );
  12. control.container.on( 'click keydown', '.default-button', control.restoreDefault );
  13. control.container.on( 'click keydown', '.remove-button', control.removeFile );
  14. control.container.on( 'change', 'select', function() {
  15. var changed = {};
  16. changed[ this.dataset.key ] = this.value;
  17. control.setBackground( changed );
  18. } );
  19. },
  20. setupColor: function() {
  21. var control = this;
  22. var picker = control.container.find('.vamtam-bg-color');
  23. picker.val( control.setting()['background-color'] ).wpColorPicker({
  24. change: function() {
  25. control.setBackground( { 'background-color': picker.val() } );
  26. },
  27. clear: function() {
  28. control.setBackground( { 'background-color': '' } );
  29. },
  30. palettes: false,
  31. });
  32. },
  33. /**
  34. * Open the media modal.
  35. */
  36. openFrame: function( event ) {
  37. if ( wp.customize.utils.isKeydownButNotEnterEvent( event ) ) {
  38. return;
  39. }
  40. event.preventDefault();
  41. if ( ! this.frame ) {
  42. this.initFrame();
  43. }
  44. this.frame.open();
  45. },
  46. /**
  47. * Create a media modal select frame, and store it so the instance can be reused when needed.
  48. */
  49. initFrame: function() {
  50. this.frame = wp.media({
  51. button: {
  52. text: this.params.button_labels.frame_button
  53. },
  54. states: [
  55. new wp.media.controller.Library({
  56. title: this.params.button_labels.frame_title,
  57. library: wp.media.query({ type: this.params.mime_type }),
  58. multiple: false,
  59. date: false
  60. })
  61. ]
  62. });
  63. // When a file is selected, run a callback.
  64. this.frame.on( 'select', this.select );
  65. },
  66. /**
  67. * Callback handler for when an attachment is selected in the media modal.
  68. * Gets the selected image information, and sets it within the control.
  69. */
  70. select: function() {
  71. // Get the attachment from the modal frame.
  72. var attachment = this.frame.state().get( 'selection' ).first().toJSON();
  73. // Set the Customizer setting; the callback takes care of rendering.
  74. this.setImage( attachment );
  75. },
  76. /**
  77. * Reset the setting to the default value.
  78. */
  79. restoreDefault: function( event ) {
  80. if ( wp.customize.utils.isKeydownButNotEnterEvent( event ) ) {
  81. return;
  82. }
  83. event.preventDefault();
  84. this.setImage( this.params.default['background-image'] );
  85. this.setBackground( this.params.default );
  86. },
  87. /**
  88. * Called when the "Remove" link is clicked. Empties the setting.
  89. *
  90. * @param {object} event jQuery Event object
  91. */
  92. removeFile: function( event ) {
  93. if ( wp.customize.utils.isKeydownButNotEnterEvent( event ) ) {
  94. return;
  95. }
  96. event.preventDefault();
  97. this.setImage( {} );
  98. },
  99. setImage: function( attachment ) {
  100. this.params.bg = _.extend( {}, this.params.bg, { 'background-image': attachment } );
  101. this.setting.set( _.extend( {}, this.setting(), { 'background-image': attachment.url || '' } ) );
  102. this.renderContent(); // Render the control to show the new image
  103. this.setupColor();
  104. },
  105. setBackground: function( new_bg ) {
  106. var old_setting = this.setting.get();
  107. this.params.bg = _.extend( {
  108. 'background-repeat': 'no-repeat',
  109. 'background-size': 'auto',
  110. 'background-attachment': 'fixed',
  111. 'background-position': 'left top'
  112. }, this.params.bg, new_bg );
  113. this.setting.set( _.extend( {
  114. 'background-repeat': 'no-repeat',
  115. 'background-size': 'auto',
  116. 'background-attachment': 'fixed',
  117. 'background-position': 'left top'
  118. }, old_setting, new_bg ) );
  119. }
  120. });
  121. })( jQuery );