Docs: Improve JSDoc for media/views/attachments.js
.
Props maartenleenders, herregroen, jipmoors, ireneyoast. Fixes #42832. git-svn-id: https://develop.svn.wordpress.org/trunk@42415 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
e553acf201
commit
2c66a7db75
@ -6620,16 +6620,6 @@ var View = wp.media.View,
|
|||||||
$ = jQuery,
|
$ = jQuery,
|
||||||
Attachments;
|
Attachments;
|
||||||
|
|
||||||
/**
|
|
||||||
* wp.media.view.Attachments
|
|
||||||
*
|
|
||||||
* @memberOf wp.media.view
|
|
||||||
*
|
|
||||||
* @class
|
|
||||||
* @augments wp.media.View
|
|
||||||
* @augments wp.Backbone.View
|
|
||||||
* @augments Backbone.View
|
|
||||||
*/
|
|
||||||
Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
||||||
tagName: 'ul',
|
tagName: 'ul',
|
||||||
className: 'attachments',
|
className: 'attachments',
|
||||||
@ -6638,9 +6628,44 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
tabIndex: -1
|
tabIndex: -1
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the overview of attachments in the Media Library.
|
||||||
|
*
|
||||||
|
* The constructor binds events to the collection this view represents when
|
||||||
|
* adding or removing attachments or resetting the entire collection.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @constructs
|
||||||
|
* @memberof wp.media.view
|
||||||
|
*
|
||||||
|
* @augments wp.media.View
|
||||||
|
*
|
||||||
|
* @listens collection:add
|
||||||
|
* @listens collection:remove
|
||||||
|
* @listens collection:reset
|
||||||
|
* @listens controller:library:selection:add
|
||||||
|
* @listens scrollElement:scroll
|
||||||
|
* @listens this:ready
|
||||||
|
* @listens controller:open
|
||||||
|
*/
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
this.el.id = _.uniqueId('__attachments-view-');
|
this.el.id = _.uniqueId('__attachments-view-');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param refreshSensitivity The time in milliseconds to throttle the scroll
|
||||||
|
* handler.
|
||||||
|
* @param refreshThreshold The amount of pixels that should be scrolled before
|
||||||
|
* loading more attachments from the server.
|
||||||
|
* @param AttachmentView The view class to be used for models in the
|
||||||
|
* collection.
|
||||||
|
* @param sortable A jQuery sortable options object
|
||||||
|
* ( http://api.jqueryui.com/sortable/ ).
|
||||||
|
* @param resize A boolean indicating whether or not to listen to
|
||||||
|
* resize events.
|
||||||
|
* @param idealColumnWidth The width in pixels which a column should have when
|
||||||
|
* calculating the total number of columns.
|
||||||
|
*/
|
||||||
_.defaults( this.options, {
|
_.defaults( this.options, {
|
||||||
refreshSensitivity: wp.media.isTouchDevice ? 300 : 200,
|
refreshSensitivity: wp.media.isTouchDevice ? 300 : 200,
|
||||||
refreshThreshold: 3,
|
refreshThreshold: 3,
|
||||||
@ -6660,6 +6685,10 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
});
|
});
|
||||||
}, this );
|
}, this );
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Find the view to be removed, delete it and call the remove function to clear
|
||||||
|
* any set event handlers.
|
||||||
|
*/
|
||||||
this.collection.on( 'remove', function( attachment ) {
|
this.collection.on( 'remove', function( attachment ) {
|
||||||
var view = this._viewsByCid[ attachment.cid ];
|
var view = this._viewsByCid[ attachment.cid ];
|
||||||
delete this._viewsByCid[ attachment.cid ];
|
delete this._viewsByCid[ attachment.cid ];
|
||||||
@ -6687,24 +6716,63 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
this.on( 'ready', this.bindEvents );
|
this.on( 'ready', this.bindEvents );
|
||||||
this.controller.on( 'open', this.setColumns );
|
this.controller.on( 'open', this.setColumns );
|
||||||
|
|
||||||
// Call this.setColumns() after this view has been rendered in the DOM so
|
/*
|
||||||
// attachments get proper width applied.
|
* Call this.setColumns() after this view has been rendered in the
|
||||||
|
* DOM so attachments get proper width applied.
|
||||||
|
*/
|
||||||
_.defer( this.setColumns, this );
|
_.defer( this.setColumns, this );
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listens to the resizeEvent on the window.
|
||||||
|
*
|
||||||
|
* Adjusts the amount of columns accordingly. First removes any existing event
|
||||||
|
* handlers to prevent duplicate listeners.
|
||||||
|
*
|
||||||
|
* @since 4.0.0
|
||||||
|
*
|
||||||
|
* @listens window:resize
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
bindEvents: function() {
|
bindEvents: function() {
|
||||||
this.$window.off( this.resizeEvent ).on( this.resizeEvent, _.debounce( this.setColumns, 50 ) );
|
this.$window.off( this.resizeEvent ).on( this.resizeEvent, _.debounce( this.setColumns, 50 ) );
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Focuses the first item in the collection.
|
||||||
|
*
|
||||||
|
* @since 4.0.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
attachmentFocus: function() {
|
attachmentFocus: function() {
|
||||||
this.$( 'li:first' ).focus();
|
this.$( 'li:first' ).focus();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restores focus to the selected item in the collection.
|
||||||
|
*
|
||||||
|
* @since 4.0.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
restoreFocus: function() {
|
restoreFocus: function() {
|
||||||
this.$( 'li.selected:first' ).focus();
|
this.$( 'li.selected:first' ).focus();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles events for arrow key presses.
|
||||||
|
*
|
||||||
|
* Focuses the attachment in the direction of the used arrow key if it exists.
|
||||||
|
*
|
||||||
|
* @since 4.0.0
|
||||||
|
*
|
||||||
|
* @param {KeyboardEvent} event The keyboard event that triggered this function.
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
arrowEvent: function( event ) {
|
arrowEvent: function( event ) {
|
||||||
var attachments = this.$el.children( 'li' ),
|
var attachments = this.$el.children( 'li' ),
|
||||||
perRow = this.columns,
|
perRow = this.columns,
|
||||||
@ -6715,7 +6783,7 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Left arrow
|
// Left arrow = 37.
|
||||||
if ( 37 === event.keyCode ) {
|
if ( 37 === event.keyCode ) {
|
||||||
if ( 0 === index ) {
|
if ( 0 === index ) {
|
||||||
return;
|
return;
|
||||||
@ -6723,7 +6791,7 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
attachments.eq( index - 1 ).focus();
|
attachments.eq( index - 1 ).focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Up arrow
|
// Up arrow = 38.
|
||||||
if ( 38 === event.keyCode ) {
|
if ( 38 === event.keyCode ) {
|
||||||
if ( 1 === row ) {
|
if ( 1 === row ) {
|
||||||
return;
|
return;
|
||||||
@ -6731,7 +6799,7 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
attachments.eq( index - perRow ).focus();
|
attachments.eq( index - perRow ).focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Right arrow
|
// Right arrow = 39.
|
||||||
if ( 39 === event.keyCode ) {
|
if ( 39 === event.keyCode ) {
|
||||||
if ( attachments.length === index ) {
|
if ( attachments.length === index ) {
|
||||||
return;
|
return;
|
||||||
@ -6739,7 +6807,7 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
attachments.eq( index + 1 ).focus();
|
attachments.eq( index + 1 ).focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Down arrow
|
// Down arrow = 40.
|
||||||
if ( 40 === event.keyCode ) {
|
if ( 40 === event.keyCode ) {
|
||||||
if ( Math.ceil( attachments.length / perRow ) === row ) {
|
if ( Math.ceil( attachments.length / perRow ) === row ) {
|
||||||
return;
|
return;
|
||||||
@ -6748,18 +6816,33 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears any set event handlers.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
dispose: function() {
|
dispose: function() {
|
||||||
this.collection.props.off( null, null, this );
|
this.collection.props.off( null, null, this );
|
||||||
if ( this.options.resize ) {
|
if ( this.options.resize ) {
|
||||||
this.$window.off( this.resizeEvent );
|
this.$window.off( this.resizeEvent );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Call 'dispose' directly on the parent class.
|
||||||
* call 'dispose' directly on the parent class
|
|
||||||
*/
|
|
||||||
View.prototype.dispose.apply( this, arguments );
|
View.prototype.dispose.apply( this, arguments );
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the amount of columns.
|
||||||
|
*
|
||||||
|
* Calculates the amount of columns and sets it on the data-columns attribute
|
||||||
|
* of .media-frame-content.
|
||||||
|
*
|
||||||
|
* @since 4.0.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
setColumns: function() {
|
setColumns: function() {
|
||||||
var prev = this.columns,
|
var prev = this.columns,
|
||||||
width = this.$el.width();
|
width = this.$el.width();
|
||||||
@ -6773,6 +6856,18 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes jQuery sortable on the attachment list.
|
||||||
|
*
|
||||||
|
* Fails gracefully if jQuery sortable doesn't exist or isn't passed in the
|
||||||
|
* options.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @fires collection:reset
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
initSortable: function() {
|
initSortable: function() {
|
||||||
var collection = this.collection;
|
var collection = this.collection;
|
||||||
|
|
||||||
@ -6784,8 +6879,10 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
// If the `collection` has a `comparator`, disable sorting.
|
// If the `collection` has a `comparator`, disable sorting.
|
||||||
disabled: !! collection.comparator,
|
disabled: !! collection.comparator,
|
||||||
|
|
||||||
// Change the position of the attachment as soon as the
|
/*
|
||||||
// mouse pointer overlaps a thumbnail.
|
* Change the position of the attachment as soon as the mouse pointer overlaps a
|
||||||
|
* thumbnail.
|
||||||
|
*/
|
||||||
tolerance: 'pointer',
|
tolerance: 'pointer',
|
||||||
|
|
||||||
// Record the initial `index` of the dragged model.
|
// Record the initial `index` of the dragged model.
|
||||||
@ -6793,8 +6890,10 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
ui.item.data('sortableIndexStart', ui.item.index());
|
ui.item.data('sortableIndexStart', ui.item.index());
|
||||||
},
|
},
|
||||||
|
|
||||||
// Update the model's index in the collection.
|
/*
|
||||||
// Do so silently, as the view is already accurate.
|
* Update the model's index in the collection. Do so silently, as the view
|
||||||
|
* is already accurate.
|
||||||
|
*/
|
||||||
update: function( event, ui ) {
|
update: function( event, ui ) {
|
||||||
var model = collection.at( ui.item.data('sortableIndexStart') ),
|
var model = collection.at( ui.item.data('sortableIndexStart') ),
|
||||||
comparator = collection.comparator;
|
comparator = collection.comparator;
|
||||||
@ -6818,14 +6917,15 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
// Fire the `reset` event to ensure other collections sync.
|
// Fire the `reset` event to ensure other collections sync.
|
||||||
collection.trigger( 'reset', collection );
|
collection.trigger( 'reset', collection );
|
||||||
|
|
||||||
// If the collection is sorted by menu order,
|
// If the collection is sorted by menu order, update the menu order.
|
||||||
// update the menu order.
|
|
||||||
collection.saveMenuOrder();
|
collection.saveMenuOrder();
|
||||||
}
|
}
|
||||||
}, this.options.sortable ) );
|
}, this.options.sortable ) );
|
||||||
|
|
||||||
// If the `orderby` property is changed on the `collection`,
|
/*
|
||||||
// check to see if we have a `comparator`. If so, disable sorting.
|
* If the `orderby` property is changed on the `collection`, check to see if we
|
||||||
|
* have a `comparator`. If so, disable sorting.
|
||||||
|
*/
|
||||||
collection.props.on( 'change:orderby', function() {
|
collection.props.on( 'change:orderby', function() {
|
||||||
this.$el.sortable( 'option', 'disabled', !! collection.comparator );
|
this.$el.sortable( 'option', 'disabled', !! collection.comparator );
|
||||||
}, this );
|
}, this );
|
||||||
@ -6834,12 +6934,19 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
this.refreshSortable();
|
this.refreshSortable();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables jQuery sortable if collection has a comparator or collection.orderby
|
||||||
|
* equals menuOrder.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
refreshSortable: function() {
|
refreshSortable: function() {
|
||||||
if ( ! this.options.sortable || ! $.fn.sortable ) {
|
if ( ! this.options.sortable || ! $.fn.sortable ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the `collection` has a `comparator`, disable sorting.
|
|
||||||
var collection = this.collection,
|
var collection = this.collection,
|
||||||
orderby = collection.props.get('orderby'),
|
orderby = collection.props.get('orderby'),
|
||||||
enabled = 'menuOrder' === orderby || ! collection.comparator;
|
enabled = 'menuOrder' === orderby || ! collection.comparator;
|
||||||
@ -6848,8 +6955,13 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Creates a new view for an attachment and adds it to _viewsByCid.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
* @param {wp.media.model.Attachment} attachment
|
* @param {wp.media.model.Attachment} attachment
|
||||||
* @returns {wp.media.View}
|
*
|
||||||
|
* @returns {wp.media.View} The created view.
|
||||||
*/
|
*/
|
||||||
createAttachmentView: function( attachment ) {
|
createAttachmentView: function( attachment ) {
|
||||||
var view = new this.options.AttachmentView({
|
var view = new this.options.AttachmentView({
|
||||||
@ -6862,33 +6974,57 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
return this._viewsByCid[ attachment.cid ] = view;
|
return this._viewsByCid[ attachment.cid ] = view;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepares view for display.
|
||||||
|
*
|
||||||
|
* Creates views for every attachment in collection if the collection is not
|
||||||
|
* empty, otherwise clears all views and loads more attachments.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
prepare: function() {
|
prepare: function() {
|
||||||
// Create all of the Attachment views, and replace
|
|
||||||
// the list in a single DOM operation.
|
|
||||||
if ( this.collection.length ) {
|
if ( this.collection.length ) {
|
||||||
this.views.set( this.collection.map( this.createAttachmentView, this ) );
|
this.views.set( this.collection.map( this.createAttachmentView, this ) );
|
||||||
|
|
||||||
// If there are no elements, clear the views and load some.
|
|
||||||
} else {
|
} else {
|
||||||
this.views.unset();
|
this.views.unset();
|
||||||
this.collection.more().done( this.scroll );
|
this.collection.more().done( this.scroll );
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Triggers the scroll function to check if we should query for additional
|
||||||
|
* attachments right away.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
ready: function() {
|
ready: function() {
|
||||||
// Trigger the scroll event to check if we're within the
|
|
||||||
// threshold to query for additional attachments.
|
|
||||||
this.scroll();
|
this.scroll();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles scroll events.
|
||||||
|
*
|
||||||
|
* Shows the spinner if we're close to the bottom. Loads more attachments from
|
||||||
|
* server if we're {refreshThreshold} times away from the bottom.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
scroll: function() {
|
scroll: function() {
|
||||||
var view = this,
|
var view = this,
|
||||||
el = this.options.scrollElement,
|
el = this.options.scrollElement,
|
||||||
scrollTop = el.scrollTop,
|
scrollTop = el.scrollTop,
|
||||||
toolbar;
|
toolbar;
|
||||||
|
|
||||||
// The scroll event occurs on the document, but the element
|
/*
|
||||||
// that should be checked is the document body.
|
* The scroll event occurs on the document, but the element that should be
|
||||||
|
* checked is the document body.
|
||||||
|
*/
|
||||||
if ( el === document ) {
|
if ( el === document ) {
|
||||||
el = document.body;
|
el = document.body;
|
||||||
scrollTop = $(document).scrollTop();
|
scrollTop = $(document).scrollTop();
|
||||||
|
@ -2,16 +2,6 @@ var View = wp.media.View,
|
|||||||
$ = jQuery,
|
$ = jQuery,
|
||||||
Attachments;
|
Attachments;
|
||||||
|
|
||||||
/**
|
|
||||||
* wp.media.view.Attachments
|
|
||||||
*
|
|
||||||
* @memberOf wp.media.view
|
|
||||||
*
|
|
||||||
* @class
|
|
||||||
* @augments wp.media.View
|
|
||||||
* @augments wp.Backbone.View
|
|
||||||
* @augments Backbone.View
|
|
||||||
*/
|
|
||||||
Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
||||||
tagName: 'ul',
|
tagName: 'ul',
|
||||||
className: 'attachments',
|
className: 'attachments',
|
||||||
@ -20,9 +10,44 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
tabIndex: -1
|
tabIndex: -1
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the overview of attachments in the Media Library.
|
||||||
|
*
|
||||||
|
* The constructor binds events to the collection this view represents when
|
||||||
|
* adding or removing attachments or resetting the entire collection.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @constructs
|
||||||
|
* @memberof wp.media.view
|
||||||
|
*
|
||||||
|
* @augments wp.media.View
|
||||||
|
*
|
||||||
|
* @listens collection:add
|
||||||
|
* @listens collection:remove
|
||||||
|
* @listens collection:reset
|
||||||
|
* @listens controller:library:selection:add
|
||||||
|
* @listens scrollElement:scroll
|
||||||
|
* @listens this:ready
|
||||||
|
* @listens controller:open
|
||||||
|
*/
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
this.el.id = _.uniqueId('__attachments-view-');
|
this.el.id = _.uniqueId('__attachments-view-');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param refreshSensitivity The time in milliseconds to throttle the scroll
|
||||||
|
* handler.
|
||||||
|
* @param refreshThreshold The amount of pixels that should be scrolled before
|
||||||
|
* loading more attachments from the server.
|
||||||
|
* @param AttachmentView The view class to be used for models in the
|
||||||
|
* collection.
|
||||||
|
* @param sortable A jQuery sortable options object
|
||||||
|
* ( http://api.jqueryui.com/sortable/ ).
|
||||||
|
* @param resize A boolean indicating whether or not to listen to
|
||||||
|
* resize events.
|
||||||
|
* @param idealColumnWidth The width in pixels which a column should have when
|
||||||
|
* calculating the total number of columns.
|
||||||
|
*/
|
||||||
_.defaults( this.options, {
|
_.defaults( this.options, {
|
||||||
refreshSensitivity: wp.media.isTouchDevice ? 300 : 200,
|
refreshSensitivity: wp.media.isTouchDevice ? 300 : 200,
|
||||||
refreshThreshold: 3,
|
refreshThreshold: 3,
|
||||||
@ -42,6 +67,10 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
});
|
});
|
||||||
}, this );
|
}, this );
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Find the view to be removed, delete it and call the remove function to clear
|
||||||
|
* any set event handlers.
|
||||||
|
*/
|
||||||
this.collection.on( 'remove', function( attachment ) {
|
this.collection.on( 'remove', function( attachment ) {
|
||||||
var view = this._viewsByCid[ attachment.cid ];
|
var view = this._viewsByCid[ attachment.cid ];
|
||||||
delete this._viewsByCid[ attachment.cid ];
|
delete this._viewsByCid[ attachment.cid ];
|
||||||
@ -69,24 +98,63 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
this.on( 'ready', this.bindEvents );
|
this.on( 'ready', this.bindEvents );
|
||||||
this.controller.on( 'open', this.setColumns );
|
this.controller.on( 'open', this.setColumns );
|
||||||
|
|
||||||
// Call this.setColumns() after this view has been rendered in the DOM so
|
/*
|
||||||
// attachments get proper width applied.
|
* Call this.setColumns() after this view has been rendered in the
|
||||||
|
* DOM so attachments get proper width applied.
|
||||||
|
*/
|
||||||
_.defer( this.setColumns, this );
|
_.defer( this.setColumns, this );
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listens to the resizeEvent on the window.
|
||||||
|
*
|
||||||
|
* Adjusts the amount of columns accordingly. First removes any existing event
|
||||||
|
* handlers to prevent duplicate listeners.
|
||||||
|
*
|
||||||
|
* @since 4.0.0
|
||||||
|
*
|
||||||
|
* @listens window:resize
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
bindEvents: function() {
|
bindEvents: function() {
|
||||||
this.$window.off( this.resizeEvent ).on( this.resizeEvent, _.debounce( this.setColumns, 50 ) );
|
this.$window.off( this.resizeEvent ).on( this.resizeEvent, _.debounce( this.setColumns, 50 ) );
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Focuses the first item in the collection.
|
||||||
|
*
|
||||||
|
* @since 4.0.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
attachmentFocus: function() {
|
attachmentFocus: function() {
|
||||||
this.$( 'li:first' ).focus();
|
this.$( 'li:first' ).focus();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restores focus to the selected item in the collection.
|
||||||
|
*
|
||||||
|
* @since 4.0.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
restoreFocus: function() {
|
restoreFocus: function() {
|
||||||
this.$( 'li.selected:first' ).focus();
|
this.$( 'li.selected:first' ).focus();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles events for arrow key presses.
|
||||||
|
*
|
||||||
|
* Focuses the attachment in the direction of the used arrow key if it exists.
|
||||||
|
*
|
||||||
|
* @since 4.0.0
|
||||||
|
*
|
||||||
|
* @param {KeyboardEvent} event The keyboard event that triggered this function.
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
arrowEvent: function( event ) {
|
arrowEvent: function( event ) {
|
||||||
var attachments = this.$el.children( 'li' ),
|
var attachments = this.$el.children( 'li' ),
|
||||||
perRow = this.columns,
|
perRow = this.columns,
|
||||||
@ -97,7 +165,7 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Left arrow
|
// Left arrow = 37.
|
||||||
if ( 37 === event.keyCode ) {
|
if ( 37 === event.keyCode ) {
|
||||||
if ( 0 === index ) {
|
if ( 0 === index ) {
|
||||||
return;
|
return;
|
||||||
@ -105,7 +173,7 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
attachments.eq( index - 1 ).focus();
|
attachments.eq( index - 1 ).focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Up arrow
|
// Up arrow = 38.
|
||||||
if ( 38 === event.keyCode ) {
|
if ( 38 === event.keyCode ) {
|
||||||
if ( 1 === row ) {
|
if ( 1 === row ) {
|
||||||
return;
|
return;
|
||||||
@ -113,7 +181,7 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
attachments.eq( index - perRow ).focus();
|
attachments.eq( index - perRow ).focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Right arrow
|
// Right arrow = 39.
|
||||||
if ( 39 === event.keyCode ) {
|
if ( 39 === event.keyCode ) {
|
||||||
if ( attachments.length === index ) {
|
if ( attachments.length === index ) {
|
||||||
return;
|
return;
|
||||||
@ -121,7 +189,7 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
attachments.eq( index + 1 ).focus();
|
attachments.eq( index + 1 ).focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Down arrow
|
// Down arrow = 40.
|
||||||
if ( 40 === event.keyCode ) {
|
if ( 40 === event.keyCode ) {
|
||||||
if ( Math.ceil( attachments.length / perRow ) === row ) {
|
if ( Math.ceil( attachments.length / perRow ) === row ) {
|
||||||
return;
|
return;
|
||||||
@ -130,18 +198,33 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears any set event handlers.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
dispose: function() {
|
dispose: function() {
|
||||||
this.collection.props.off( null, null, this );
|
this.collection.props.off( null, null, this );
|
||||||
if ( this.options.resize ) {
|
if ( this.options.resize ) {
|
||||||
this.$window.off( this.resizeEvent );
|
this.$window.off( this.resizeEvent );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Call 'dispose' directly on the parent class.
|
||||||
* call 'dispose' directly on the parent class
|
|
||||||
*/
|
|
||||||
View.prototype.dispose.apply( this, arguments );
|
View.prototype.dispose.apply( this, arguments );
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the amount of columns.
|
||||||
|
*
|
||||||
|
* Calculates the amount of columns and sets it on the data-columns attribute
|
||||||
|
* of .media-frame-content.
|
||||||
|
*
|
||||||
|
* @since 4.0.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
setColumns: function() {
|
setColumns: function() {
|
||||||
var prev = this.columns,
|
var prev = this.columns,
|
||||||
width = this.$el.width();
|
width = this.$el.width();
|
||||||
@ -155,6 +238,18 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes jQuery sortable on the attachment list.
|
||||||
|
*
|
||||||
|
* Fails gracefully if jQuery sortable doesn't exist or isn't passed in the
|
||||||
|
* options.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @fires collection:reset
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
initSortable: function() {
|
initSortable: function() {
|
||||||
var collection = this.collection;
|
var collection = this.collection;
|
||||||
|
|
||||||
@ -166,8 +261,10 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
// If the `collection` has a `comparator`, disable sorting.
|
// If the `collection` has a `comparator`, disable sorting.
|
||||||
disabled: !! collection.comparator,
|
disabled: !! collection.comparator,
|
||||||
|
|
||||||
// Change the position of the attachment as soon as the
|
/*
|
||||||
// mouse pointer overlaps a thumbnail.
|
* Change the position of the attachment as soon as the mouse pointer overlaps a
|
||||||
|
* thumbnail.
|
||||||
|
*/
|
||||||
tolerance: 'pointer',
|
tolerance: 'pointer',
|
||||||
|
|
||||||
// Record the initial `index` of the dragged model.
|
// Record the initial `index` of the dragged model.
|
||||||
@ -175,8 +272,10 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
ui.item.data('sortableIndexStart', ui.item.index());
|
ui.item.data('sortableIndexStart', ui.item.index());
|
||||||
},
|
},
|
||||||
|
|
||||||
// Update the model's index in the collection.
|
/*
|
||||||
// Do so silently, as the view is already accurate.
|
* Update the model's index in the collection. Do so silently, as the view
|
||||||
|
* is already accurate.
|
||||||
|
*/
|
||||||
update: function( event, ui ) {
|
update: function( event, ui ) {
|
||||||
var model = collection.at( ui.item.data('sortableIndexStart') ),
|
var model = collection.at( ui.item.data('sortableIndexStart') ),
|
||||||
comparator = collection.comparator;
|
comparator = collection.comparator;
|
||||||
@ -200,14 +299,15 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
// Fire the `reset` event to ensure other collections sync.
|
// Fire the `reset` event to ensure other collections sync.
|
||||||
collection.trigger( 'reset', collection );
|
collection.trigger( 'reset', collection );
|
||||||
|
|
||||||
// If the collection is sorted by menu order,
|
// If the collection is sorted by menu order, update the menu order.
|
||||||
// update the menu order.
|
|
||||||
collection.saveMenuOrder();
|
collection.saveMenuOrder();
|
||||||
}
|
}
|
||||||
}, this.options.sortable ) );
|
}, this.options.sortable ) );
|
||||||
|
|
||||||
// If the `orderby` property is changed on the `collection`,
|
/*
|
||||||
// check to see if we have a `comparator`. If so, disable sorting.
|
* If the `orderby` property is changed on the `collection`, check to see if we
|
||||||
|
* have a `comparator`. If so, disable sorting.
|
||||||
|
*/
|
||||||
collection.props.on( 'change:orderby', function() {
|
collection.props.on( 'change:orderby', function() {
|
||||||
this.$el.sortable( 'option', 'disabled', !! collection.comparator );
|
this.$el.sortable( 'option', 'disabled', !! collection.comparator );
|
||||||
}, this );
|
}, this );
|
||||||
@ -216,12 +316,19 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
this.refreshSortable();
|
this.refreshSortable();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables jQuery sortable if collection has a comparator or collection.orderby
|
||||||
|
* equals menuOrder.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
refreshSortable: function() {
|
refreshSortable: function() {
|
||||||
if ( ! this.options.sortable || ! $.fn.sortable ) {
|
if ( ! this.options.sortable || ! $.fn.sortable ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the `collection` has a `comparator`, disable sorting.
|
|
||||||
var collection = this.collection,
|
var collection = this.collection,
|
||||||
orderby = collection.props.get('orderby'),
|
orderby = collection.props.get('orderby'),
|
||||||
enabled = 'menuOrder' === orderby || ! collection.comparator;
|
enabled = 'menuOrder' === orderby || ! collection.comparator;
|
||||||
@ -230,8 +337,13 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Creates a new view for an attachment and adds it to _viewsByCid.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
* @param {wp.media.model.Attachment} attachment
|
* @param {wp.media.model.Attachment} attachment
|
||||||
* @returns {wp.media.View}
|
*
|
||||||
|
* @returns {wp.media.View} The created view.
|
||||||
*/
|
*/
|
||||||
createAttachmentView: function( attachment ) {
|
createAttachmentView: function( attachment ) {
|
||||||
var view = new this.options.AttachmentView({
|
var view = new this.options.AttachmentView({
|
||||||
@ -244,33 +356,57 @@ Attachments = View.extend(/** @lends wp.media.view.Attachments.prototype */{
|
|||||||
return this._viewsByCid[ attachment.cid ] = view;
|
return this._viewsByCid[ attachment.cid ] = view;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepares view for display.
|
||||||
|
*
|
||||||
|
* Creates views for every attachment in collection if the collection is not
|
||||||
|
* empty, otherwise clears all views and loads more attachments.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
prepare: function() {
|
prepare: function() {
|
||||||
// Create all of the Attachment views, and replace
|
|
||||||
// the list in a single DOM operation.
|
|
||||||
if ( this.collection.length ) {
|
if ( this.collection.length ) {
|
||||||
this.views.set( this.collection.map( this.createAttachmentView, this ) );
|
this.views.set( this.collection.map( this.createAttachmentView, this ) );
|
||||||
|
|
||||||
// If there are no elements, clear the views and load some.
|
|
||||||
} else {
|
} else {
|
||||||
this.views.unset();
|
this.views.unset();
|
||||||
this.collection.more().done( this.scroll );
|
this.collection.more().done( this.scroll );
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Triggers the scroll function to check if we should query for additional
|
||||||
|
* attachments right away.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
ready: function() {
|
ready: function() {
|
||||||
// Trigger the scroll event to check if we're within the
|
|
||||||
// threshold to query for additional attachments.
|
|
||||||
this.scroll();
|
this.scroll();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles scroll events.
|
||||||
|
*
|
||||||
|
* Shows the spinner if we're close to the bottom. Loads more attachments from
|
||||||
|
* server if we're {refreshThreshold} times away from the bottom.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
scroll: function() {
|
scroll: function() {
|
||||||
var view = this,
|
var view = this,
|
||||||
el = this.options.scrollElement,
|
el = this.options.scrollElement,
|
||||||
scrollTop = el.scrollTop,
|
scrollTop = el.scrollTop,
|
||||||
toolbar;
|
toolbar;
|
||||||
|
|
||||||
// The scroll event occurs on the document, but the element
|
/*
|
||||||
// that should be checked is the document body.
|
* The scroll event occurs on the document, but the element that should be
|
||||||
|
* checked is the document body.
|
||||||
|
*/
|
||||||
if ( el === document ) {
|
if ( el === document ) {
|
||||||
el = document.body;
|
el = document.body;
|
||||||
scrollTop = $(document).scrollTop();
|
scrollTop = $(document).scrollTop();
|
||||||
|
Loading…
Reference in New Issue
Block a user