/** * Color used in animation when adding an element. * * Can be 'none' to disable the animation. * * @member {string} */ addColor: '#ffff33',
/** * Color used in animation when deleting an element. * * Can be 'none' to disable the animation. * * @member {string} */ delColor: '#faafaa',
/** * Color used in dim add animation. * * Can be 'none' to disable the animation. * * @member {string} */ dimAddColor: '#ffff33',
/** * Color used in dim delete animation. * * Can be 'none' to disable the animation. * * @member {string} */ dimDelColor: '#ff3333',
/** * Callback that's run before a request is made. * * @callback wpList~confirm * @param {object} this * @param {HTMLElement} list The list DOM element. * @param {object} settings Settings for the current list. * @param {string} action The type of action to perform: 'add', 'delete', or 'dim'. * @param {string} backgroundColor Background color of the list's DOM element. * @return {boolean} Whether to proceed with the action or not. */ confirm: null,
/** * Callback that's run before an item gets added to the list. * * Allows to cancel the request. * * @callback wpList~addBefore * @param {object} settings Settings for the Ajax request. * @return {object|boolean} Settings for the Ajax request or false to abort. */ addBefore: null,
/** * Callback that's run after an item got added to the list. * * @callback wpList~addAfter * @param {XML} returnedResponse Raw response returned from the server. * @param {object} settings Settings for the Ajax request. * @param {jqXHR} settings.xml jQuery XMLHttpRequest object. * @param {string} settings.status Status of the request: 'success', 'notmodified', 'nocontent', 'error', * 'timeout', 'abort', or 'parsererror'. * @param {object} settings.parsed Parsed response object. */ addAfter: null,
/** * Callback that's run before an item gets deleted from the list. * * Allows to cancel the request. * * @callback wpList~delBefore * @param {object} settings Settings for the Ajax request. * @param {HTMLElement} list The list DOM element. * @return {object|boolean} Settings for the Ajax request or false to abort. */ delBefore: null,
/** * Callback that's run after an item got deleted from the list. * * @callback wpList~delAfter * @param {XML} returnedResponse Raw response returned from the server. * @param {object} settings Settings for the Ajax request. * @param {jqXHR} settings.xml jQuery XMLHttpRequest object. * @param {string} settings.status Status of the request: 'success', 'notmodified', 'nocontent', 'error', * 'timeout', 'abort', or 'parsererror'. * @param {object} settings.parsed Parsed response object. */ delAfter: null,
/** * Callback that's run before an item gets dim'd. * * Allows to cancel the request. * * @callback wpList~dimBefore * @param {object} settings Settings for the Ajax request. * @return {object|boolean} Settings for the Ajax request or false to abort. */ dimBefore: null,
/** * Callback that's run after an item got dim'd. * * @callback wpList~dimAfter * @param {XML} returnedResponse Raw response returned from the server. * @param {object} settings Settings for the Ajax request. * @param {jqXHR} settings.xml jQuery XMLHttpRequest object. * @param {string} settings.status Status of the request: 'success', 'notmodified', 'nocontent', 'error', * 'timeout', 'abort', or 'parsererror'. * @param {object} settings.parsed Parsed response object. */ dimAfter: null },
/** * Finds a nonce. * * 1. Nonce in settings. * 2. `_ajax_nonce` value in element's href attribute. * 3. `_ajax_nonce` input field that is a descendant of element. * 4. `_wpnonce` value in element's href attribute. * 5. `_wpnonce` input field that is a descendant of element. * 6. 0 if none can be found. * * @param {jQuery} element Element that triggered the request. * @param {Object} settings Settings for the Ajax request. * @return {string|number} Nonce */ nonce: function( element, settings ) { var url = wpAjax.unserialize( element.attr( 'href' ) ), $element = $( '#' + settings.element );
/** * Extract list item data from a DOM element. * * Example 1: data-wp-lists="delete:the-comment-list:comment-{comment_ID}:66cc66:unspam=1" * Example 2: data-wp-lists="dim:the-comment-list:comment-{comment_ID}:unapproved:e7e7d3:e7e7d3:new=approved" * * Returns an unassociative array with the following data: * data[0] - Data identifier: 'list', 'add', 'delete', or 'dim'. * data[1] - ID of the corresponding list. If data[0] is 'list', the type of list ('comment', 'category', etc). * data[2] - ID of the parent element of all inputs necessary for the request. * data[3] - Hex color to be used in this request. If data[0] is 'dim', dim class. * data[4] - Additional arguments in query syntax that are added to the request. Example: 'post_id=1234'. * If data[0] is 'dim', dim add color. * data[5] - Only available if data[0] is 'dim', dim delete color. * data[6] - Only available if data[0] is 'dim', additional arguments in query syntax that are added to the request. * * Result for Example 1: * data[0] - delete * data[1] - the-comment-list * data[2] - comment-{comment_ID} * data[3] - 66cc66 * data[4] - unspam=1 * * @param {HTMLElement} element The DOM element. * @param {string} type The type of data to look for: 'list', 'add', 'delete', or 'dim'. * @return {Array} Extracted list item data. */ parseData: function( element, type ) { var data = [], wpListsData;
try { wpListsData = $( element ).data( 'wp-lists' ) || ''; wpListsData = wpListsData.match( new RegExp( type + ':[\\S]+' ) );
if ( wpListsData ) { data = wpListsData[0].split( ':' ); } } catch ( error ) {}
return data; },
/** * Calls a confirm callback to verify the action that is about to be performed. * * @param {HTMLElement} list The DOM element. * @param {Object} settings Settings for this list. * @param {string} action The type of action to perform: 'add', 'delete', or 'dim'. * @return {Object|boolean} Settings if confirmed, false if not. */ pre: function( list, settings, action ) { var $element, backgroundColor, confirmed;
/** * Adds an item to the list via Ajax. * * @param {HTMLElement} element The DOM element. * @param {Object} settings Settings for this list. * @return {boolean} Whether the item was added. */ ajaxAdd: function( element, settings ) { var list = this, $element = $( element ), data = wpList.parseData( $element, 'add' ), formValues, formData, parsedResponse, returnedResponse;
/** * Delete an item in the list via Ajax. * * @param {HTMLElement} element A DOM element containing item data. * @param {Object} settings Settings for this list. * @return {boolean} Whether the item was deleted. */ ajaxDel: function( element, settings ) { var list = this, $element = $( element ), data = wpList.parseData( $element, 'delete' ), $eventTarget, parsedResponse, returnedResponse;
/** * Dim an item in the list via Ajax. * * @param {HTMLElement} element A DOM element containing item data. * @param {Object} settings Settings for this list. * @return {boolean} Whether the item was dim'ed. */ ajaxDim: function( element, settings ) { var list = this, $element = $( element ), data = wpList.parseData( $element, 'dim' ), $eventTarget, isClass, color, dimColor, parsedResponse, returnedResponse;
// Prevent hidden links from being clicked by hotkeys. if ( 'none' === $element.parent().css( 'display' ) ) { return false; }
/** @property {string} comment_link Link of the comment to be dimmed. */ if ( 'undefined' !== typeof parsedResponse.responses[0].supplemental.comment_link ) { var $submittedOn = $element.find( '.submitted-on' ), $commentLink = $submittedOn.find( 'a' );
// Comment is approved; link the date field. if ( '' !== parsedResponse.responses[0].supplemental.comment_link ) { $submittedOn.html( $('<a></a>').text( $submittedOn.text() ).prop( 'href', parsedResponse.responses[0].supplemental.comment_link ) );
// Comment is not approved; unlink the date field. } else if ( $commentLink.length ) { $submittedOn.text( $commentLink.text() ); } } };
/** * Returns the background color of the passed element. * * @param {jQuery|string} element Element to check. * @return {string} Background color value in HEX. Default: '#ffffff'. */ getColor: function( element ) { return $( element ).css( 'backgroundColor' ) || '#ffffff'; },
/** * Adds something. * * @param {HTMLElement} element A DOM element containing item data. * @param {Object} settings Settings for this list. * @return {boolean} Whether the item was added. */ add: function( element, settings ) { var $list = $( this ), $element = $( element ), old = false, position, reference;
/** * Clears all input fields within the element passed. * * @param {string} elementId ID of the element to check, including leading #. */ clear: function( elementId ) { var list = this, $element = $( elementId ), type, tagName;
// Bail if we're within the list. if ( list.wpList && $element.parents( '#' + list.id ).length ) { return; }