admin.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* jshint onevar: false, multistr: true */
  2. /* global _wpMediaViewsL10n, _wpGalleryWidgetAdminSettings */
  3. (function($){
  4. var $ids;
  5. var $thumbs;
  6. $(function(){
  7. $( document.body ) .on( 'click', '.gallery-widget-choose-images', function( event ) {
  8. event.preventDefault();
  9. var widget_form = $( this ).closest( 'form, .form' );
  10. $ids = widget_form.find( '.gallery-widget-ids' );
  11. $thumbs = widget_form.find( '.gallery-widget-thumbs' );
  12. var idsString = $ids.val();
  13. var attachments = getAttachments( idsString );
  14. var selection = null;
  15. var editing = false;
  16. if ( attachments ) {
  17. selection = getSelection( attachments );
  18. editing = true;
  19. }
  20. var options = {
  21. state: 'gallery-edit',
  22. title: wp.media.view.l10n.addMedia,
  23. multiple: true,
  24. editing: editing,
  25. selection: selection
  26. };
  27. var workflow = getWorkflow( options );
  28. workflow.open();
  29. });
  30. // Setup an onchange handler to toggle various options when changing style. The different style options
  31. // require different form inputs to be presented in the widget; this event will keep the UI in sync
  32. // with the selected style
  33. $( '.widget-inside' ).on( 'change', '.gallery-widget-style', setupStyleOptions);
  34. // Setup the Link To options for all forms currently on the page. Does the same as the onChange handler, but
  35. // is called once to display the correct form inputs for each widget on the page
  36. setupStyleOptions();
  37. });
  38. var media = wp.media,
  39. l10n;
  40. // Link any localized strings.
  41. l10n = media.view.l10n = typeof _wpMediaViewsL10n === 'undefined' ? {} : _wpMediaViewsL10n;
  42. /**
  43. * wp.media.view.MediaFrame.GalleryWidget
  44. *
  45. * This behavior can be very nearly had by setting the workflow's state to 'gallery-edit', but
  46. * we cannot use the custom WidgetGalleryEdit controller with it (must overide createStates(),
  47. * which is necessary to disable the sidebar gallery settings in the media browser)
  48. */
  49. media.view.MediaFrame.GalleryWidget = media.view.MediaFrame.Post.extend({
  50. createStates: function() {
  51. var options = this.options;
  52. // `CollectionEdit` and `CollectionAdd` were only introduced in r27214-core,
  53. // so they may not be available yet.
  54. if ( 'CollectionEdit' in media.controller ) {
  55. this.states.add([
  56. new media.controller.CollectionEdit({
  57. type: 'image',
  58. collectionType: 'gallery',
  59. title: l10n.editGalleryTitle,
  60. SettingsView: media.view.Settings.Gallery,
  61. library: options.selection,
  62. editing: options.editing,
  63. menu: 'gallery'
  64. }),
  65. new media.controller.CollectionAdd({
  66. type: 'image',
  67. collectionType: 'gallery',
  68. title: l10n.addToGalleryTitle
  69. })
  70. ]);
  71. } else {
  72. // If `CollectionEdit` is not available, then use the old approach.
  73. if ( ! ( 'WidgetGalleryEdit' in media.controller ) ) {
  74. // Remove the gallery settings sidebar when editing widgets.
  75. media.controller.WidgetGalleryEdit = media.controller.GalleryEdit.extend({
  76. gallerySettings: function( /*browser*/ ) {
  77. return;
  78. }
  79. });
  80. }
  81. this.states.add([
  82. new media.controller.WidgetGalleryEdit({
  83. library: options.selection,
  84. editing: options.editing,
  85. menu: 'gallery'
  86. }),
  87. new media.controller.GalleryAdd({ })
  88. ]);
  89. }
  90. }
  91. });
  92. function setupStyleOptions(){
  93. $( '.widget-inside .gallery-widget-style' ).each( function( /*i*/ ){
  94. var style = $( this ).val();
  95. var form = $( this ).parents( 'form' );
  96. switch ( style ) {
  97. case 'slideshow':
  98. form.find( '.gallery-widget-link-wrapper' ).hide();
  99. form.find( '.gallery-widget-columns-wrapper' ).hide();
  100. break;
  101. default:
  102. form.find( '.gallery-widget-link-wrapper' ).show();
  103. form.find( '.gallery-widget-columns-wrapper' ).show();
  104. }
  105. });
  106. }
  107. /**
  108. * Take a given Selection of attachments and a thumbs wrapper div (jQuery object)
  109. * and fill it with thumbnails
  110. */
  111. function setupThumbs( selection, wrapper ){
  112. wrapper.empty();
  113. var imageSize = _wpGalleryWidgetAdminSettings.thumbSize;
  114. selection.each( function( model ){
  115. var sizedUrl = model.get('url') + '?w=' + imageSize + '&h=' + imageSize + '&crop=true';
  116. var thumb = jQuery('<img>', { 'src' : sizedUrl, 'alt': model.get('title'), 'title': model.get('title'), 'width': imageSize, 'height': imageSize, 'class': 'thumb' });
  117. wrapper.append( thumb );
  118. });
  119. }
  120. /**
  121. * Take a csv string of ids (as stored in db) and fetch a full Attachments collection
  122. */
  123. function getAttachments( idsString ) {
  124. if ( ! idsString ) {
  125. return null;
  126. }
  127. // Found in /wp-includes/js/media-editor.js
  128. var shortcode = wp.shortcode.next( 'gallery', '[gallery ids="' + idsString + '"]' );
  129. // Ignore the rest of the match object, to give attachments() below what it expects
  130. shortcode = shortcode.shortcode;
  131. var attachments = wp.media.gallery.attachments( shortcode );
  132. return attachments;
  133. }
  134. /**
  135. * Take an Attachments collection and return a corresponding Selection model that can be
  136. * passed to a MediaFrame to prepopulate the gallery picker
  137. */
  138. function getSelection( attachments ) {
  139. var selection = new wp.media.model.Selection( attachments.models, {
  140. props: attachments.props.toJSON(),
  141. multiple: true
  142. });
  143. selection.gallery = attachments.gallery;
  144. // Fetch the query's attachments, and then break ties from the
  145. // query to allow for sorting.
  146. selection.more().done( function() {
  147. // Break ties with the query.
  148. selection.props.set( { query: false } );
  149. selection.unmirror();
  150. selection.props.unset( 'orderby' );
  151. });
  152. return selection;
  153. }
  154. /**
  155. * Create a media 'workflow' (MediaFrame). This is the main entry point for the media picker
  156. */
  157. function getWorkflow( options ) {
  158. var workflow = new wp.media.view.MediaFrame.GalleryWidget( options );
  159. workflow.on( 'update', function( selection ) {
  160. var state = workflow.state();
  161. selection = selection || state.get( 'selection' );
  162. if ( ! selection ) {
  163. return;
  164. }
  165. // Map the Models down into a simple array of ids that can be easily imploded to a csv string
  166. var ids = selection.map( function( model ){
  167. return model.get( 'id' );
  168. } );
  169. var id_string = ids.join( ',' );
  170. $ids.val( id_string ).trigger( 'change' );
  171. setupThumbs( selection, $thumbs );
  172. }, this );
  173. workflow.setState( workflow.options.state );
  174. return workflow;
  175. }
  176. })(jQuery);