Keine Beschreibung

admin.js 6.3KB

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