芝麻web文件管理V1.00
编辑当前文件:/home2/sdektunc/cepali.edu.mx/wp-includes/blocks/post-terms/autosave.js
/** * @output wp-includes/js/autosave.js */ /* global tinymce, wpCookies, autosaveL10n, switchEditors */ // Back-compat. window.autosave = function() { return true; }; /** * Adds autosave to the window object on dom ready. * * @since 3.9.0 * * @param {jQuery} $ jQuery object. * @param {window} The window object. * */ ( function( $, window ) { /** * Auto saves the post. * * @since 3.9.0 * * @return {Object} * {{ * getPostData: getPostData, * getCompareString: getCompareString, * disableButtons: disableButtons, * enableButtons: enableButtons, * local: ({hasStorage, getSavedPostData, save, suspend, resume}|*), * server: ({tempBlockSave, triggerSave, postChanged, suspend, resume}|*) * }} * The object with all functions for autosave. */ function autosave() { var initialCompareString, initialCompareData = {}, lastTriggerSave = 0, $document = $( document ); /** * Sets the initial compare data. * * @since 5.6.1 */ function setInitialCompare() { initialCompareData = { post_title: $( '#title' ).val() || '', content: $( '#content' ).val() || '', excerpt: $( '#excerpt' ).val() || '' }; initialCompareString = getCompareString( initialCompareData ); } /** * Returns the data saved in both local and remote autosave. * * @since 3.9.0 * * @param {string} type The type of autosave either local or remote. * * @return {Object} Object containing the post data. */ function getPostData( type ) { var post_name, parent_id, data, time = ( new Date() ).getTime(), cats = [], editor = getEditor(); // Don't run editor.save() more often than every 3 seconds. // It is resource intensive and might slow down typing in long posts on slow devices. if ( editor && editor.isDirty() && ! editor.isHidden() && time - 3000 > lastTriggerSave ) { editor.save(); lastTriggerSave = time; } data = { post_id: $( '#post_ID' ).val() || 0, post_type: $( '#post_type' ).val() || '', post_author: $( '#post_author' ).val() || '', post_title: $( '#title' ).val() || '', content: $( '#content' ).val() || '', excerpt: $( '#excerpt' ).val() || '' }; if ( type === 'local' ) { return data; } $( 'input[id^="in-category-"]:checked' ).each( function() { cats.push( this.value ); }); data.catslist = cats.join(','); if ( post_name = $( '#post_name' ).val() ) { data.post_name = post_name; } if ( parent_id = $( '#parent_id' ).val() ) { data.parent_id = parent_id; } if ( $( '#comment_status' ).prop( 'checked' ) ) { data.comment_status = 'open'; } if ( $( '#ping_status' ).prop( 'checked' ) ) { data.ping_status = 'open'; } if ( $( '#auto_draft' ).val() === '1' ) { data.auto_draft = '1'; } return data; } /** * Concatenates the title, content and excerpt. This is used to track changes * when auto-saving. * * @since 3.9.0 * * @param {Object} postData The object containing the post data. * * @return {string} A concatenated string with title, content and excerpt. */ function getCompareString( postData ) { if ( typeof postData === 'object' ) { return ( postData.post_title || '' ) + '::' + ( postData.content || '' ) + '::' + ( postData.excerpt || '' ); } return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' ); } /** * Disables save buttons. * * @since 3.9.0 * * @return {void} */ function disableButtons() { $document.trigger('autosave-disable-buttons'); // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions. setTimeout( enableButtons, 5000 ); } /** * Enables save buttons. * * @since 3.9.0 * * @return {void} */ function enableButtons() { $document.trigger( 'autosave-enable-buttons' ); } /** * Gets the content editor. * * @since 4.6.0 * * @return {boolean|*} Returns either false if the editor is undefined, * or the instance of the content editor. */ function getEditor() { return typeof tinymce !== 'undefined' && tinymce.get('content'); } /** * Autosave in localStorage. * * @since 3.9.0 * * @return { * { * hasStorage: *, * getSavedPostData: getSavedPostData, * save: save, * suspend: suspend, * resume: resume * } * } * The object with all functions for local storage autosave. */ function autosaveLocal() { var blog_id, post_id, hasStorage, intervalTimer, lastCompareString, isSuspended = false; /** * Checks if the browser supports sessionStorage and it's not disabled. * * @since 3.9.0 * * @return {boolean} True if the sessionStorage is supported and enabled. */ function checkStorage() { var test = Math.random().toString(), result = false; try { window.sessionStorage.setItem( 'wp-test', test ); result = window.sessionStorage.getItem( 'wp-test' ) === test; window.sessionStorage.removeItem( 'wp-test' ); } catch(e) {} hasStorage = result; return result; } /** * Initializes the local storage. * * @since 3.9.0 * * @return {boolean|Object} False if no sessionStorage in the browser or an Object * containing all postData for this blog. */ function getStorage() { var stored_obj = false; // Separate local storage containers for each blog_id. if ( hasStorage && blog_id ) { stored_obj = sessionStorage.getItem( 'wp-autosave-' + blog_id ); if ( stored_obj ) { stored_obj = JSON.parse( stored_obj ); } else { stored_obj = {}; } } return stored_obj; } /** * Sets the storage for this blog. Confirms that the data was saved * successfully. * * @since 3.9.0 * * @return {boolean} True if the data was saved successfully, false if it wasn't saved. */ function setStorage( stored_obj ) { var key; if ( hasStorage && blog_id ) { key = 'wp-autosave-' + blog_id; sessionStorage.setItem( key, JSON.stringify( stored_obj ) ); return sessionStorage.getItem( key ) !== null; } return false; } /** * Gets the saved post data for the current post. * * @since 3.9.0 * * @return {boolean|Object} False if no storage or no data or the postData as an Object. */ function getSavedPostData() { var stored = getStorage(); if ( ! stored || ! post_id ) { return false; } return stored[ 'post_' + post_id ] || false; } /** * Sets (save or delete) post data in the storage. * * If stored_data evaluates to 'false' the storage key for the current post will be removed. * * @since 3.9.0 * * @param {Object|boolean|null} stored_data The post data to store or null/false/empty to delete the key. * * @return {boolean} True if data is stored, false if data was removed. */ function setData( stored_data ) { var stored = getStorage(); if ( ! stored || ! post_id ) { return false; } if ( stored_data ) { stored[ 'post_' + post_id ] = stored_data; } else if ( stored.hasOwnProperty( 'post_' + post_id ) ) { delete stored[ 'post_' + post_id ]; } else { return false; } return setStorage( stored ); } /** * Sets isSuspended to true. * * @since 3.9.0 * * @return {void} */ function suspend() { isSuspended = true; } /** * Sets isSuspended to false. * * @since 3.9.0 * * @return {void} */ function resume() { isSuspended = false; } /** * Saves post data for the current post. * * Runs on a 15 seconds interval, saves when there are differences in the post title or content. * When the optional data is provided, updates the last saved post data. * * @since 3.9.0 * * @param {Object} data The post data for saving, minimum 'post_title' and 'content'. * * @return {boolean} Returns