2015-02-22 07:56:27 +01:00
|
|
|
/*globals wp, _, jQuery */
|
|
|
|
|
2015-02-09 01:43:50 +01:00
|
|
|
/**
|
|
|
|
* wp.media.view.MediaFrame
|
|
|
|
*
|
|
|
|
* The frame used to create the media modal.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @augments wp.media.view.Frame
|
|
|
|
* @augments wp.media.View
|
|
|
|
* @augments wp.Backbone.View
|
|
|
|
* @augments Backbone.View
|
|
|
|
* @mixes wp.media.controller.StateMachine
|
|
|
|
*/
|
2015-03-31 04:03:29 +02:00
|
|
|
var Frame = wp.media.view.Frame,
|
2015-02-09 01:43:50 +01:00
|
|
|
$ = jQuery,
|
|
|
|
MediaFrame;
|
|
|
|
|
|
|
|
MediaFrame = Frame.extend({
|
|
|
|
className: 'media-frame',
|
|
|
|
template: wp.template('media-frame'),
|
|
|
|
regions: ['menu','title','content','toolbar','router'],
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'click div.media-frame-title h1': 'toggleMenu'
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @global wp.Uploader
|
|
|
|
*/
|
|
|
|
initialize: function() {
|
|
|
|
Frame.prototype.initialize.apply( this, arguments );
|
|
|
|
|
|
|
|
_.defaults( this.options, {
|
|
|
|
title: '',
|
|
|
|
modal: true,
|
|
|
|
uploader: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Ensure core UI is enabled.
|
|
|
|
this.$el.addClass('wp-core-ui');
|
|
|
|
|
|
|
|
// Initialize modal container view.
|
|
|
|
if ( this.options.modal ) {
|
2015-03-31 04:03:29 +02:00
|
|
|
this.modal = new wp.media.view.Modal({
|
2015-02-09 01:43:50 +01:00
|
|
|
controller: this,
|
|
|
|
title: this.options.title
|
|
|
|
});
|
|
|
|
|
|
|
|
this.modal.content( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the uploader off if the upload limit has been exceeded or
|
|
|
|
// if the browser isn't supported.
|
|
|
|
if ( wp.Uploader.limitExceeded || ! wp.Uploader.browser.supported ) {
|
|
|
|
this.options.uploader = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize window-wide uploader.
|
|
|
|
if ( this.options.uploader ) {
|
2015-03-31 04:03:29 +02:00
|
|
|
this.uploader = new wp.media.view.UploaderWindow({
|
2015-02-09 01:43:50 +01:00
|
|
|
controller: this,
|
|
|
|
uploader: {
|
|
|
|
dropzone: this.modal ? this.modal.$el : this.$el,
|
|
|
|
container: this.$el
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.views.set( '.media-frame-uploader', this.uploader );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on( 'attach', _.bind( this.views.ready, this.views ), this );
|
|
|
|
|
|
|
|
// Bind default title creation.
|
|
|
|
this.on( 'title:create:default', this.createTitle, this );
|
|
|
|
this.title.mode('default');
|
|
|
|
|
|
|
|
this.on( 'title:render', function( view ) {
|
|
|
|
view.$el.append( '<span class="dashicons dashicons-arrow-down"></span>' );
|
|
|
|
});
|
|
|
|
|
|
|
|
// Bind default menu.
|
|
|
|
this.on( 'menu:create:default', this.createMenu, this );
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @returns {wp.media.view.MediaFrame} Returns itself to allow chaining
|
|
|
|
*/
|
|
|
|
render: function() {
|
|
|
|
// Activate the default state if no active state exists.
|
|
|
|
if ( ! this.state() && this.options.state ) {
|
|
|
|
this.setState( this.options.state );
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* call 'render' directly on the parent class
|
|
|
|
*/
|
|
|
|
return Frame.prototype.render.apply( this, arguments );
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param {Object} title
|
|
|
|
* @this wp.media.controller.Region
|
|
|
|
*/
|
|
|
|
createTitle: function( title ) {
|
2015-03-31 04:03:29 +02:00
|
|
|
title.view = new wp.media.View({
|
2015-02-09 01:43:50 +01:00
|
|
|
controller: this,
|
|
|
|
tagName: 'h1'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param {Object} menu
|
|
|
|
* @this wp.media.controller.Region
|
|
|
|
*/
|
|
|
|
createMenu: function( menu ) {
|
2015-03-31 04:03:29 +02:00
|
|
|
menu.view = new wp.media.view.Menu({
|
2015-02-09 01:43:50 +01:00
|
|
|
controller: this
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleMenu: function() {
|
|
|
|
this.$el.find( '.media-menu' ).toggleClass( 'visible' );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} toolbar
|
|
|
|
* @this wp.media.controller.Region
|
|
|
|
*/
|
|
|
|
createToolbar: function( toolbar ) {
|
2015-03-31 04:03:29 +02:00
|
|
|
toolbar.view = new wp.media.view.Toolbar({
|
2015-02-09 01:43:50 +01:00
|
|
|
controller: this
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param {Object} router
|
|
|
|
* @this wp.media.controller.Region
|
|
|
|
*/
|
|
|
|
createRouter: function( router ) {
|
2015-03-31 04:03:29 +02:00
|
|
|
router.view = new wp.media.view.Router({
|
2015-02-09 01:43:50 +01:00
|
|
|
controller: this
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param {Object} options
|
|
|
|
*/
|
|
|
|
createIframeStates: function( options ) {
|
|
|
|
var settings = wp.media.view.settings,
|
|
|
|
tabs = settings.tabs,
|
|
|
|
tabUrl = settings.tabUrl,
|
|
|
|
$postId;
|
|
|
|
|
|
|
|
if ( ! tabs || ! tabUrl ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the post ID to the tab URL if it exists.
|
|
|
|
$postId = $('#post_ID');
|
|
|
|
if ( $postId.length ) {
|
|
|
|
tabUrl += '&post_id=' + $postId.val();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the tab states.
|
|
|
|
_.each( tabs, function( title, id ) {
|
|
|
|
this.state( 'iframe:' + id ).set( _.defaults({
|
|
|
|
tab: id,
|
|
|
|
src: tabUrl + '&tab=' + id,
|
|
|
|
title: title,
|
|
|
|
content: 'iframe',
|
|
|
|
menu: 'default'
|
|
|
|
}, options ) );
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
this.on( 'content:create:iframe', this.iframeContent, this );
|
|
|
|
this.on( 'content:deactivate:iframe', this.iframeContentCleanup, this );
|
|
|
|
this.on( 'menu:render:default', this.iframeMenu, this );
|
|
|
|
this.on( 'open', this.hijackThickbox, this );
|
|
|
|
this.on( 'close', this.restoreThickbox, this );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} content
|
|
|
|
* @this wp.media.controller.Region
|
|
|
|
*/
|
|
|
|
iframeContent: function( content ) {
|
|
|
|
this.$el.addClass('hide-toolbar');
|
2015-03-31 04:03:29 +02:00
|
|
|
content.view = new wp.media.view.Iframe({
|
2015-02-09 01:43:50 +01:00
|
|
|
controller: this
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
iframeContentCleanup: function() {
|
|
|
|
this.$el.removeClass('hide-toolbar');
|
|
|
|
},
|
|
|
|
|
|
|
|
iframeMenu: function( view ) {
|
|
|
|
var views = {};
|
|
|
|
|
|
|
|
if ( ! view ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each( wp.media.view.settings.tabs, function( title, id ) {
|
|
|
|
views[ 'iframe:' + id ] = {
|
|
|
|
text: this.state( 'iframe:' + id ).get('title'),
|
|
|
|
priority: 200
|
|
|
|
};
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
view.set( views );
|
|
|
|
},
|
|
|
|
|
|
|
|
hijackThickbox: function() {
|
|
|
|
var frame = this;
|
|
|
|
|
|
|
|
if ( ! window.tb_remove || this._tb_remove ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._tb_remove = window.tb_remove;
|
|
|
|
window.tb_remove = function() {
|
|
|
|
frame.close();
|
|
|
|
frame.reset();
|
|
|
|
frame.setState( frame.options.state );
|
|
|
|
frame._tb_remove.call( window );
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
restoreThickbox: function() {
|
|
|
|
if ( ! this._tb_remove ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.tb_remove = this._tb_remove;
|
|
|
|
delete this._tb_remove;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Map some of the modal's methods to the frame.
|
|
|
|
_.each(['open','close','attach','detach','escape'], function( method ) {
|
|
|
|
/**
|
|
|
|
* @returns {wp.media.view.MediaFrame} Returns itself to allow chaining
|
|
|
|
*/
|
|
|
|
MediaFrame.prototype[ method ] = function() {
|
|
|
|
if ( this.modal ) {
|
|
|
|
this.modal[ method ].apply( this.modal, arguments );
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2015-02-09 17:01:29 +01:00
|
|
|
module.exports = MediaFrame;
|