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