admin-folders.js
7 months ago
admin-galleries.js
3 years ago
admin-gallery.js
4 years ago
admin-media.js
1 year ago
admin-plugins.js
3 years ago
admin-widgets.js
3 years ago
admin.js
3 years ago
front-basicmasonry.js
3 years ago
front-basicslider.js
1 year ago
front.js
7 months ago
gutenberg.min.js
7 months ago
admin-media.js
499 lines
| 1 | ( function( $ ) { |
| 2 | |
| 3 | // ready event |
| 4 | $( function() { |
| 5 | if ( $.isEmptyObject( wp.media.view ) ) |
| 6 | return; |
| 7 | |
| 8 | var gutenberg_active = typeof rlBlockEditor !== 'undefined'; |
| 9 | |
| 10 | if ( gutenberg_active ) { |
| 11 | RLWPMediaViewMediaFramePostTrigger = wp.media.view.MediaFrame.Post.prototype.trigger; |
| 12 | |
| 13 | // extend MediaFrame.Post |
| 14 | wp.media.view.MediaFrame.Post.prototype.trigger = function( action ) { |
| 15 | RLWPMediaViewMediaFramePostTrigger.apply( this, Array.prototype.slice.call( arguments ) ); |
| 16 | |
| 17 | if ( action === 'open' ) { |
| 18 | if ( $( this.modal.clickedOpenerEl ).hasClass( 'rl-remote-library-media-button' ) ) |
| 19 | this.setState( 'rl-remote-library' ); |
| 20 | else if ( $( this.modal.clickedOpenerEl ).hasClass( 'rl-gallery-media-button' ) ) |
| 21 | this.setState( 'rl-gallery' ); |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // add new media folder filter |
| 27 | var RLWPMediaViewAttachmentFilters = wp.media.view.AttachmentFilters.extend( { |
| 28 | id: 'media-attachment-rl-remote-library-filters', |
| 29 | className: 'attachment-filters attachment-rl-remote-library-filter', |
| 30 | createFilters: function() { |
| 31 | var filters = { |
| 32 | all: { |
| 33 | text: rlRemoteLibraryMedia.allProviders, |
| 34 | priority: 1, |
| 35 | props: { |
| 36 | media_provider: 'all' |
| 37 | } |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | // add active providers |
| 42 | for ( var i = 0; i < rlRemoteLibraryMedia.providersActive.length; i++ ) { |
| 43 | var provider = rlRemoteLibraryMedia.providersActive[i]; |
| 44 | |
| 45 | filters[provider] = { |
| 46 | text: rlRemoteLibraryMedia.providers[provider].name, |
| 47 | priority: i + 2, |
| 48 | props: { |
| 49 | media_provider: provider |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | this.filters = filters; |
| 55 | } |
| 56 | } ); |
| 57 | |
| 58 | var RLWPMediaViewAttachmentsBrowser = wp.media.view.AttachmentsBrowser; |
| 59 | |
| 60 | wp.media.view.AttachmentsBrowser = wp.media.view.AttachmentsBrowser.extend( { |
| 61 | createToolbar: function() { |
| 62 | // load the original toolbar |
| 63 | RLWPMediaViewAttachmentsBrowser.prototype.createToolbar.call( this ); |
| 64 | |
| 65 | if ( this.model.get( 'id' ) === 'rl-remote-library' ) { |
| 66 | this.toolbar.set( 'RLremoteLibraryFilterLabel', new wp.media.view.Label( { |
| 67 | value: rlRemoteLibraryMedia.filterByremoteLibrary, |
| 68 | attributes: { |
| 69 | 'for': 'media-attachment-rl-remote-library-filters' |
| 70 | }, |
| 71 | priority: -75 |
| 72 | } ).render() ); |
| 73 | |
| 74 | this.toolbar.set( 'RLremoteLibraryAttachmentFilters', new RLWPMediaViewAttachmentFilters( { |
| 75 | controller: this.controller, |
| 76 | model: this.collection.props, |
| 77 | priority: -75 |
| 78 | } ).render() ); |
| 79 | } |
| 80 | } |
| 81 | } ); |
| 82 | |
| 83 | var RLRemoteLibraryCollection, |
| 84 | RLRemoteLibraryContentView, |
| 85 | RLWPMediaViewMediaFramePost = wp.media.view.MediaFrame.Post, |
| 86 | attachment_defaults = { |
| 87 | width: 0, |
| 88 | height: 0, |
| 89 | url: '' |
| 90 | }; |
| 91 | |
| 92 | // add new attributes |
| 93 | RLWPMediaViewMediaFramePost.currentAttachment = attachment_defaults; |
| 94 | RLWPMediaViewMediaFramePost.remoteLibraryImage = false; |
| 95 | RLWPMediaViewMediaFramePost.requestHash = ''; |
| 96 | |
| 97 | // extend media frame |
| 98 | wp.media.view.MediaFrame.Post = RLWPMediaViewMediaFramePost.extend( { |
| 99 | initialize: function() { |
| 100 | // calling the initalize method from the current frame before adding new functionality |
| 101 | RLWPMediaViewMediaFramePost.prototype.initialize.apply( this, arguments ); |
| 102 | |
| 103 | // adding new state for remote library image |
| 104 | this.states.add( [ |
| 105 | new wp.media.controller.Library( { |
| 106 | id: 'rl-remote-library', |
| 107 | title: 'Remote Library', |
| 108 | priority: 99, |
| 109 | toolbar: gutenberg_active ? 'select' : 'main-insert', |
| 110 | multiple: false, |
| 111 | editable: true, |
| 112 | allowLocalEdits: true, |
| 113 | library: new wp.media.model.Attachments(), |
| 114 | displaySettings: true, |
| 115 | displayUserSettings: true, |
| 116 | filterable: true, |
| 117 | searchable: true, |
| 118 | content: 'browse', |
| 119 | router: false, |
| 120 | date: false, |
| 121 | sortable: false, |
| 122 | type: 'image', |
| 123 | dragInfo: false, |
| 124 | menu: gutenberg_active ? false : 'default' |
| 125 | } ) |
| 126 | ] ); |
| 127 | |
| 128 | var RLWPMediaEditorSendAttachment = wp.media.editor.send.attachment; |
| 129 | |
| 130 | // replace send attachment |
| 131 | wp.media.editor.send.attachment = function( props, attachment ) { |
| 132 | // remote library simulated attachment? |
| 133 | if ( typeof attachment.remote_library_image !== 'undefined' && attachment.remote_library_image === true ) { |
| 134 | RLWPMediaViewMediaFramePost.remoteLibraryImage = true; |
| 135 | |
| 136 | if ( props.size === 'thumbnail' ) { |
| 137 | RLWPMediaViewMediaFramePost.currentAttachment.width = attachment.thumbnail_width; |
| 138 | RLWPMediaViewMediaFramePost.currentAttachment.height = attachment.thumbnail_height; |
| 139 | RLWPMediaViewMediaFramePost.currentAttachment.url = attachment.thumbnail_url; |
| 140 | } else { |
| 141 | RLWPMediaViewMediaFramePost.currentAttachment.width = attachment.width; |
| 142 | RLWPMediaViewMediaFramePost.currentAttachment.height = attachment.height; |
| 143 | RLWPMediaViewMediaFramePost.currentAttachment.url = attachment.url; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // return original function |
| 148 | return RLWPMediaEditorSendAttachment( props, attachment ); |
| 149 | } |
| 150 | |
| 151 | var RLWPMediaPost = wp.media.post; |
| 152 | |
| 153 | // replace ajax request |
| 154 | wp.media.post = function( action, data ) { |
| 155 | // send attachment to editor action? |
| 156 | if ( action === 'send-attachment-to-editor' && RLWPMediaViewMediaFramePost.remoteLibraryImage === true ) { |
| 157 | var attachmentID = data.attachment.id; |
| 158 | |
| 159 | // replace pseudo ID with generated thumbnial ID |
| 160 | data.attachment.id = parseInt( rlRemoteLibraryMedia.thumbnailID ); |
| 161 | |
| 162 | // set pseudo ID too |
| 163 | data.attachment.att_id = attachmentID; |
| 164 | |
| 165 | // select this image as remote library one |
| 166 | data.attachment.remote_library_image = true; |
| 167 | |
| 168 | // set new dimensions |
| 169 | data.attachment.width = RLWPMediaViewMediaFramePost.currentAttachment.width; |
| 170 | data.attachment.height = RLWPMediaViewMediaFramePost.currentAttachment.height; |
| 171 | |
| 172 | // back to defaults |
| 173 | RLWPMediaViewMediaFramePost.currentAttachment = attachment_defaults; |
| 174 | |
| 175 | // restore defaulkt behavior |
| 176 | RLWPMediaViewMediaFramePost.remoteLibraryImage = false; |
| 177 | |
| 178 | if ( typeof data.attachment.url === 'undefined' ) |
| 179 | data.attachment.rl_url = RLWPMediaViewMediaFramePost.currentAttachment.url; |
| 180 | } |
| 181 | |
| 182 | // return original function |
| 183 | return RLWPMediaPost( action, data ); |
| 184 | } |
| 185 | |
| 186 | // events |
| 187 | this.on( 'activate', this.activateContent, this ); |
| 188 | }, |
| 189 | activateContent: function() { |
| 190 | // get view content |
| 191 | var view = this.content.get(); |
| 192 | |
| 193 | // valid remote library view? |
| 194 | if ( view !== null && 'model' in view && view.model.id === 'rl-remote-library' ) { |
| 195 | var contentState = this.state(); |
| 196 | var contentSelection = contentState.get( 'selection' ); |
| 197 | |
| 198 | // clear selection |
| 199 | contentSelection.reset(); |
| 200 | |
| 201 | var toolbar = this.toolbar.get(); |
| 202 | var controller = this; |
| 203 | var spinner = view.toolbar.get( 'spinner' ); |
| 204 | |
| 205 | // display spinner |
| 206 | spinner.$el.css( 'marginLeft', '6px' ); |
| 207 | spinner.show(); |
| 208 | |
| 209 | // hide uploader view |
| 210 | view.$el.find( '.uploader-inline' ).addClass( 'hidden' ); |
| 211 | |
| 212 | this.selectionStatusToolbar( toolbar ); |
| 213 | |
| 214 | // add upload button |
| 215 | toolbar.set( 'rl-upload-insert', { |
| 216 | style: 'primary', |
| 217 | priority: 20, |
| 218 | text: gutenberg_active ? rlRemoteLibraryMedia.uploadAndSelect : rlRemoteLibraryMedia.uploadAndInsert, |
| 219 | requires: { selection: true }, |
| 220 | click: function() { |
| 221 | var state = controller.state(); |
| 222 | var selection = state.get( 'selection' ); |
| 223 | var image = selection.single(); |
| 224 | var content = controller.content.get(); |
| 225 | var attachment = content.attachments.$el.find( 'li[data-id="' + image.attributes.id + '"] .thumbnail' ); |
| 226 | var attachment_image = attachment.find( '.centered' ); |
| 227 | |
| 228 | attachment_image.css( { opacity: 0.1, transition: 'opacity 500ms' } ); |
| 229 | attachment_image.after( '<div class="media-progress-bar"><div style="width: 20%"></div></div>' ); |
| 230 | |
| 231 | var progress = attachment.find( '.media-progress-bar div' ); |
| 232 | var transition = progress.css( 'transition' ); |
| 233 | |
| 234 | progress.css( 'transition', 'width 10s' ).animate( { width: "100%" }, 0 ); |
| 235 | |
| 236 | $.post( ajaxurl, { |
| 237 | action: 'rl_upload_image', |
| 238 | image: image.attributes, |
| 239 | post_id: rlRemoteLibraryMedia.postID, |
| 240 | rlnonce: rlRemoteLibraryMedia.getUploadNonce |
| 241 | } ).done( function( response ) { |
| 242 | progress.css( 'transition', 'width 0.5s' ).animate( { width: "100%" }, 0, function() { |
| 243 | attachment_image.css( { opacity: 1, transition: '' } ); |
| 244 | |
| 245 | $( this ).css( 'transition', transition ); |
| 246 | |
| 247 | // update attachment data |
| 248 | selection.models[0].attributes.id = parseInt( response.id ); |
| 249 | selection.models[0].attributes.url = response.full[0]; |
| 250 | selection.models[0].attributes.sizes.full.url = response.full[0]; |
| 251 | |
| 252 | // remove progress bar |
| 253 | progress.remove(); |
| 254 | |
| 255 | // close modal |
| 256 | controller.close(); |
| 257 | |
| 258 | // trigger insert event |
| 259 | state.trigger( ( gutenberg_active ? 'select' : 'insert' ), selection ).reset(); |
| 260 | } ); |
| 261 | } ).always( function( data ) { |
| 262 | // |
| 263 | } ); |
| 264 | } |
| 265 | } ); |
| 266 | |
| 267 | RLRemoteLibraryContentView = view; |
| 268 | RLRemoteLibraryContentView.blockScrolling = false; |
| 269 | RLRemoteLibraryContentView.responseData = []; |
| 270 | |
| 271 | var model = view.model.collection.get( 'rl-remote-library' ); |
| 272 | |
| 273 | // set scroll event |
| 274 | this.handleScroll = _.chain( this.handleScroll ).bind( view ).throttle( wp.media.isTouchDevice ? 300 : 200 ).value(); |
| 275 | |
| 276 | // bind scroll event |
| 277 | view.attachments.$el.on( 'scroll', this.handleScroll ); |
| 278 | |
| 279 | // assign model |
| 280 | RLRemoteLibraryCollection = model; |
| 281 | |
| 282 | // run ajax calls for all providers |
| 283 | var promise = remoteQuery( 'all', '', 1, [] ); |
| 284 | |
| 285 | promise.then( |
| 286 | result => { |
| 287 | // any results? |
| 288 | if ( result.images.length ) { |
| 289 | // add images to library |
| 290 | model.attributes.library.push( result.images ); |
| 291 | |
| 292 | // increase page number |
| 293 | RLWPMediaModelAttachments.media_page++; |
| 294 | |
| 295 | RLRemoteLibraryContentView.blockScrolling = false; |
| 296 | RLRemoteLibraryContentView.responseData = result.data; |
| 297 | |
| 298 | // last page? |
| 299 | if ( result.last === false ) |
| 300 | this.handleScroll(); |
| 301 | } |
| 302 | |
| 303 | // hide spinner |
| 304 | view.toolbar.get( 'spinner' ).hide(); |
| 305 | }, |
| 306 | error => { |
| 307 | RLRemoteLibraryContentView.blockScrolling = false; |
| 308 | RLRemoteLibraryContentView.responseData = []; |
| 309 | } |
| 310 | ); |
| 311 | } |
| 312 | }, |
| 313 | handleScroll: function() { |
| 314 | // is another scrolling pending? |
| 315 | if ( RLRemoteLibraryContentView.blockScrolling ) |
| 316 | return; |
| 317 | |
| 318 | var view = this.views.parent, |
| 319 | el = this.attachments.el, |
| 320 | scrollTop = el.scrollTop; |
| 321 | |
| 322 | // the scroll event occurs on the document, but the element that should be checked is the document body |
| 323 | if ( el === document ) { |
| 324 | el = document.body; |
| 325 | scrollTop = $( document ).scrollTop(); |
| 326 | } |
| 327 | |
| 328 | if ( ! $( el ).is( ':visible' ) ) |
| 329 | return; |
| 330 | |
| 331 | // get content view |
| 332 | var content = view.content.get(); |
| 333 | |
| 334 | // show the spinner only if we are close to the bottom. |
| 335 | if ( el.scrollHeight - ( scrollTop + el.clientHeight ) < el.clientHeight / 3 ) |
| 336 | content.toolbar.get( 'spinner' ).show(); |
| 337 | |
| 338 | if ( el.scrollHeight < scrollTop + ( el.clientHeight * 3 ) ) { |
| 339 | RLRemoteLibraryContentView.blockScrolling = true; |
| 340 | |
| 341 | // display spinner |
| 342 | content.toolbar.get( 'spinner' ).show(); |
| 343 | |
| 344 | var promise = remoteQuery( RLWPMediaModelAttachments.media_provider, RLWPMediaModelAttachments.media_search, RLWPMediaModelAttachments.media_page, RLRemoteLibraryContentView.responseData ); |
| 345 | |
| 346 | promise.then( |
| 347 | result => { |
| 348 | // any results? |
| 349 | if ( result.images.length ) { |
| 350 | // add images to library |
| 351 | RLRemoteLibraryCollection.attributes.library.push( result.images ); |
| 352 | |
| 353 | // increase page number |
| 354 | RLWPMediaModelAttachments.media_page++; |
| 355 | |
| 356 | RLRemoteLibraryContentView.blockScrolling = false; |
| 357 | RLRemoteLibraryContentView.responseData = result.data; |
| 358 | |
| 359 | // last page? |
| 360 | if ( result.last === false ) |
| 361 | view.handleScroll( result.data ); |
| 362 | } |
| 363 | |
| 364 | // hide spinner |
| 365 | content.toolbar.get( 'spinner' ).hide(); |
| 366 | }, |
| 367 | error => { |
| 368 | RLRemoteLibraryContentView.blockScrolling = false; |
| 369 | RLRemoteLibraryContentView.responseData = []; |
| 370 | } |
| 371 | ); |
| 372 | } |
| 373 | } |
| 374 | } ); |
| 375 | |
| 376 | var RLWPMediaViewSettingsAttachmentDisplay = wp.media.view.Settings.AttachmentDisplay; |
| 377 | |
| 378 | wp.media.view.Settings.AttachmentDisplay = wp.media.view.Settings.AttachmentDisplay.extend( { |
| 379 | render: function() { |
| 380 | // remove medium size |
| 381 | if ( typeof this.options.attachment.attributes.remote_library_image !== 'undefined' && this.options.attachment.attributes.remote_library_image ) |
| 382 | delete this.options.attachment.attributes.sizes.medium; |
| 383 | |
| 384 | // load the original render function |
| 385 | RLWPMediaViewSettingsAttachmentDisplay.prototype.render.call( this ); |
| 386 | |
| 387 | return this; |
| 388 | } |
| 389 | } ); |
| 390 | |
| 391 | var RLWPMediaModelAttachments = wp.media.model.Attachments; |
| 392 | |
| 393 | // add new attributes |
| 394 | RLWPMediaModelAttachments.media_page = 1; |
| 395 | RLWPMediaModelAttachments.media_provider = 'all'; |
| 396 | RLWPMediaModelAttachments.media_search = ''; |
| 397 | |
| 398 | // extend media frame |
| 399 | wp.media.model.Attachments = RLWPMediaModelAttachments.extend( { |
| 400 | initialize: function() { |
| 401 | // calling the initalize method from the current frame before adding new functionality |
| 402 | RLWPMediaModelAttachments.prototype.initialize.apply( this, arguments ); |
| 403 | |
| 404 | // events |
| 405 | this.props.on( 'change', this.handleFilters ); |
| 406 | }, |
| 407 | handleFilters: function() { |
| 408 | // clear current collection |
| 409 | RLRemoteLibraryCollection.attributes.library.reset(); |
| 410 | |
| 411 | // clear current selection |
| 412 | RLRemoteLibraryCollection.get( 'selection' ).reset(); |
| 413 | |
| 414 | // hide uploader view |
| 415 | RLRemoteLibraryContentView.$el.find( '.uploader-inline' ).addClass( 'hidden' ); |
| 416 | |
| 417 | // reset page to first |
| 418 | RLWPMediaModelAttachments.media_page = 1; |
| 419 | |
| 420 | // clear response data |
| 421 | RLRemoteLibraryContentView.responseData = []; |
| 422 | |
| 423 | // make sure media provider is set |
| 424 | if ( typeof this.attributes.media_provider === 'undefined' ) |
| 425 | RLWPMediaModelAttachments.media_provider = this.attributes.media_provider = 'all'; |
| 426 | else |
| 427 | RLWPMediaModelAttachments.media_provider = this.attributes.media_provider; |
| 428 | |
| 429 | // make sure search phrase is set |
| 430 | if ( typeof this.attributes.search === 'undefined' ) |
| 431 | RLWPMediaModelAttachments.media_search = this.attributes.search = ''; |
| 432 | else |
| 433 | RLWPMediaModelAttachments.media_search = this.attributes.search; |
| 434 | |
| 435 | // disable scroll event |
| 436 | RLRemoteLibraryContentView.blockScrolling = true; |
| 437 | |
| 438 | // display spinner |
| 439 | RLRemoteLibraryContentView.toolbar.get( 'spinner' ).show(); |
| 440 | |
| 441 | var promise = remoteQuery( this.attributes.media_provider, this.attributes.search, RLWPMediaModelAttachments.media_page, RLRemoteLibraryContentView.responseData ); |
| 442 | |
| 443 | promise.then( |
| 444 | result => { |
| 445 | // any results? |
| 446 | if ( result.images.length ) { |
| 447 | // add images to library |
| 448 | RLRemoteLibraryCollection.attributes.library.push( result.images ); |
| 449 | |
| 450 | // increase page number |
| 451 | RLWPMediaModelAttachments.media_page++; |
| 452 | |
| 453 | // allow scrolling |
| 454 | RLRemoteLibraryContentView.blockScrolling = false; |
| 455 | RLRemoteLibraryContentView.responseData = result.data; |
| 456 | |
| 457 | // last page? |
| 458 | if ( result.last === false ) |
| 459 | RLRemoteLibraryContentView.views.parent.handleScroll(); |
| 460 | } |
| 461 | |
| 462 | // hide spinner |
| 463 | RLRemoteLibraryContentView.toolbar.get( 'spinner' ).hide(); |
| 464 | }, |
| 465 | error => { |
| 466 | RLRemoteLibraryContentView.blockScrolling = false; |
| 467 | RLRemoteLibraryContentView.responseData = []; |
| 468 | } |
| 469 | ); |
| 470 | } |
| 471 | } ); |
| 472 | |
| 473 | function remoteQuery( provider, phrase, page, response_data ) { |
| 474 | var promise = new Promise( ( resolve, reject ) => { |
| 475 | // set current request phrase |
| 476 | RLWPMediaViewMediaFramePost.requestHash = 'provider:' + provider + '|phrase:' + phrase; |
| 477 | |
| 478 | $.post( ajaxurl, { |
| 479 | action: 'rl_remote_library_query', |
| 480 | media_provider: provider, |
| 481 | media_search: phrase, |
| 482 | media_page: page, |
| 483 | response_data: response_data |
| 484 | } ).done( function( response ) { |
| 485 | // valid request hash? |
| 486 | if ( RLWPMediaViewMediaFramePost.requestHash === 'provider:' + provider + '|phrase:' + phrase ) |
| 487 | resolve( response ); |
| 488 | else |
| 489 | reject( [] ); |
| 490 | } ).fail( function() { |
| 491 | reject( [] ); |
| 492 | } ); |
| 493 | } ); |
| 494 | |
| 495 | return promise; |
| 496 | } |
| 497 | } ); |
| 498 | |
| 499 | } )( jQuery ); |