From f08db2e8a86db50ce6170e2616fe7e5f065b8f82 Mon Sep 17 00:00:00 2001 From: Piyush Vijay Date: Thu, 23 Aug 2018 07:28:10 +0530 Subject: [PATCH] Some code structured for PMA Console Signed-Off-By: Piyush Vijay --- js/microhistory.js | 335 ------------------ js/src/classes/Console/PMA_ConsoleResizer.js | 14 +- .../classes/Console/PMA_consoleBookmarks.js | 31 +- js/src/classes/Console/PMA_consoleDebug.js | 16 +- js/src/classes/Console/PMA_consoleInput.js | 31 +- js/src/classes/Console/PMA_consoleMessages.js | 36 +- js/src/console.js | 34 ++ 7 files changed, 151 insertions(+), 346 deletions(-) delete mode 100644 js/microhistory.js diff --git a/js/microhistory.js b/js/microhistory.js deleted file mode 100644 index 46c98a88ff..0000000000 --- a/js/microhistory.js +++ /dev/null @@ -1,335 +0,0 @@ -/* vim: set expandtab sw=4 ts=4 sts=4: */ -/** - * An implementation of a client-side page cache. - * This object also uses the cache to provide a simple microhistory, - * that is the ability to use the back and forward buttons in the browser - */ -PMA_MicroHistory = { - /** - * @var int The maximum number of pages to keep in the cache - */ - MAX: 6, - /** - * @var object A hash used to prime the cache with data about the initially - * loaded page. This is set in the footer, and then loaded - * by a double-queued event further down this file. - */ - primer: {}, - /** - * @var array Stores the content of the cached pages - */ - pages: [], - /** - * @var int The index of the currently loaded page - * This is used to know at which point in the history we are - */ - current: 0, - /** - * Saves a new page in the cache - * - * @param string hash The hash part of the url that is being loaded - * @param array scripts A list of scripts that is required for the page - * @param string menu A hash that links to a menu stored - * in a dedicated menu cache - * @param array params A list of parameters used by PMA_commonParams() - * @param string rel A relationship to the current page: - * 'samepage': Forces the response to be treated as - * the same page as the current one - * 'newpage': Forces the response to be treated as - * a new page - * undefined: Default behaviour, 'samepage' if the - * selflinks of the two pages are the same. - * 'newpage' otherwise - * - * @return void - */ - add: function (hash, scripts, menu, params, rel) { - if (this.pages.length > PMA_MicroHistory.MAX) { - // Trim the cache, to the maximum number of allowed entries - // This way we will have a cached menu for every page - for (var i = 0; i < this.pages.length - this.MAX; i++) { - delete this.pages[i]; - } - } - while (this.current < this.pages.length) { - // trim the cache if we went back in the history - // and are now going forward again - this.pages.pop(); - } - if (rel === 'newpage' || - ( - typeof rel === 'undefined' && ( - typeof this.pages[this.current - 1] === 'undefined' || - this.pages[this.current - 1].hash !== hash - ) - ) - ) { - this.pages.push({ - hash: hash, - content: $('#page_content').html(), - scripts: scripts, - selflink: $('#selflink').html(), - menu: menu, - params: params - }); - PMA_SetUrlHash(this.current, hash); - this.current++; - } - }, - /** - * Restores a page from the cache. This is called when the hash - * part of the url changes and it's structure appears to be valid - * - * @param string index Which page from the history to load - * - * @return void - */ - navigate: function (index) { - if (typeof this.pages[index] === 'undefined' || - typeof this.pages[index].content === 'undefined' || - typeof this.pages[index].menu === 'undefined' || - ! PMA_MicroHistory.menus.get(this.pages[index].menu) - ) { - PMA_ajaxShowMessage( - '
' + PMA_messages.strInvalidPage + '
', - false - ); - } else { - AJAX.active = true; - var record = this.pages[index]; - AJAX.scriptHandler.reset(function () { - $('#page_content').html(record.content); - $('#selflink').html(record.selflink); - PMA_MicroHistory.menus.replace(PMA_MicroHistory.menus.get(record.menu)); - PMA_commonParams.setAll(record.params); - AJAX.scriptHandler.load(record.scripts); - PMA_MicroHistory.current = ++index; - }); - } - }, - /** - * Resaves the content of the current page in the cache. - * Necessary in order not to show the user some outdated version of the page - * - * @return void - */ - update: function () { - var page = this.pages[this.current - 1]; - if (page) { - page.content = $('#page_content').html(); - } - }, - /** - * @var object Dedicated menu cache - */ - menus: { - /** - * Returns the number of items in an associative array - * - * @return int - */ - size: function (obj) { - var size = 0; - var key; - for (key in obj) { - if (obj.hasOwnProperty(key)) { - size++; - } - } - return size; - }, - /** - * @var hash Stores the content of the cached menus - */ - data: {}, - /** - * Saves a new menu in the cache - * - * @param string hash The hash (trimmed md5) of the menu to be saved - * @param string content The HTML code of the menu to be saved - * - * @return void - */ - add: function (hash, content) { - if (this.size(this.data) > PMA_MicroHistory.MAX) { - // when the cache grows, we remove the oldest entry - var oldest; - var key; - var init = 0; - for (var i in this.data) { - if (this.data[i]) { - if (! init || this.data[i].timestamp.getTime() < oldest.getTime()) { - oldest = this.data[i].timestamp; - key = i; - init = 1; - } - } - } - delete this.data[key]; - } - this.data[hash] = { - content: content, - timestamp: new Date() - }; - }, - /** - * Retrieves a menu given its hash - * - * @param string hash The hash of the menu to be retrieved - * - * @return string - */ - get: function (hash) { - if (this.data[hash]) { - return this.data[hash].content; - } else { - // This should never happen as long as the number of stored menus - // is larger or equal to the number of pages in the page cache - return ''; - } - }, - /** - * Prepares part of the parameter string used during page requests, - * this is necessary to tell the server which menus we have in the cache - * - * @return string - */ - getRequestParam: function () { - var param = ''; - var menuHashes = []; - for (var i in this.data) { - menuHashes.push(i); - } - var menuHashesParam = menuHashes.join('-'); - if (menuHashesParam) { - param = PMA_commonParams.get('arg_separator') + 'menuHashes=' + menuHashesParam; - } - return param; - }, - /** - * Replaces the menu with new content - * - * @return void - */ - replace: function (content) { - $('#floating_menubar').html(content) - // Remove duplicate wrapper - // TODO: don't send it in the response - .children().first().remove(); - $('#topmenu').menuResizer(PMA_mainMenuResizerCallback); - } - } -}; - -/** - * URL hash management module. - * Allows direct bookmarking and microhistory. - */ -PMA_SetUrlHash = (function (jQuery, window) { - 'use strict'; - /** - * Indictaes whether we have already completed - * the initialisation of the hash - * - * @access private - */ - var ready = false; - /** - * Stores a hash that needed to be set when we were not ready - * - * @access private - */ - var savedHash = ''; - /** - * Flag to indicate if the change of hash was triggered - * by a user pressing the back/forward button or if - * the change was triggered internally - * - * @access private - */ - var userChange = true; - - // Fix favicon disappearing in Firefox when setting location.hash - function resetFavicon () { - if (navigator.userAgent.indexOf('Firefox') > -1) { - // Move the link tags for the favicon to the bottom - // of the head element to force a reload of the favicon - $('head > link[href="favicon\\.ico"]').appendTo('head'); - } - } - - /** - * Sets the hash part of the URL - * - * @access public - */ - function setUrlHash (index, hash) { - /* - * Known problem: - * Setting hash leads to reload in webkit: - * http://www.quirksmode.org/bugreports/archives/2005/05/Safari_13_visual_anomaly_with_windowlocationhref.html - * - * so we expect that users are not running an ancient Safari version - */ - - userChange = false; - if (ready) { - window.location.hash = 'PMAURL-' + index + ':' + hash; - resetFavicon(); - } else { - savedHash = 'PMAURL-' + index + ':' + hash; - } - } - /** - * Start initialisation - */ - var urlhash = window.location.hash; - if (urlhash.substring(0, 8) === '#PMAURL-') { - // We have a valid hash, let's redirect the user - // to the page that it's pointing to - var colon_position = urlhash.indexOf(':'); - var questionmark_position = urlhash.indexOf('?'); - if (colon_position !== -1 && questionmark_position !== -1 && colon_position < questionmark_position) { - var hash_url = urlhash.substring(colon_position + 1, questionmark_position); - if (PMA_gotoWhitelist.indexOf(hash_url) !== -1) { - window.location = urlhash.substring( - colon_position + 1 - ); - } - } - } else { - // We don't have a valid hash, so we'll set it up - // when the page finishes loading - jQuery(function () { - /* Check if we should set URL */ - if (savedHash !== '') { - window.location.hash = savedHash; - savedHash = ''; - resetFavicon(); - } - // Indicate that we're done initialising - ready = true; - }); - } - /** - * Register an event handler for when the url hash changes - */ - jQuery(function () { - jQuery(window).hashchange(function () { - if (userChange === false) { - // Ignore internally triggered hash changes - userChange = true; - } else if (/^#PMAURL-\d+:/.test(window.location.hash)) { - // Change page if the hash changed was triggered by a user action - var index = window.location.hash.substring( - 8, window.location.hash.indexOf(':') - ); - PMA_MicroHistory.navigate(index); - } - }); - }); - /** - * Publicly exposes a reference to the otherwise private setUrlHash function - */ - return setUrlHash; -}(jQuery, window)); diff --git a/js/src/classes/Console/PMA_ConsoleResizer.js b/js/src/classes/Console/PMA_ConsoleResizer.js index 0f2c1193a2..19896ffaa6 100644 --- a/js/src/classes/Console/PMA_ConsoleResizer.js +++ b/js/src/classes/Console/PMA_ConsoleResizer.js @@ -4,9 +4,12 @@ * Resizer object * Careful: this object UI logics highly related with functions under PMA_console * Resizing min-height is 32, if small than it, console will collapse - * @namespace ConsoleResizer + * @class ConsoleResizer */ export default class ConsoleResizer { + /** + * @param {Object} instance Instance of PMA Console + */ constructor (instance) { this._posY = 0; this._height = 0; @@ -18,10 +21,16 @@ export default class ConsoleResizer { this._mouseup = this._mouseup.bind(this); this.setPmaConsole(instance); } + + /** + * @param {Object} instance Instance of PMA Console + * @return {void} + */ setPmaConsole (instance) { this.pmaConsole = instance; this.initialize(); } + /** * Mousedown event handler for bind to resizer * @@ -40,6 +49,7 @@ export default class ConsoleResizer { return false; }); } + /** * Mousemove event handler for bind to resizer * @@ -65,6 +75,7 @@ export default class ConsoleResizer { } } } + /** * Mouseup event handler for bind to resizer * @@ -77,6 +88,7 @@ export default class ConsoleResizer { $(document).off('mouseup'); $(document).off('selectstart'); } + /** * Used for console resizer initialize * diff --git a/js/src/classes/Console/PMA_consoleBookmarks.js b/js/src/classes/Console/PMA_consoleBookmarks.js index 8cbeb08a83..a6296b8af7 100644 --- a/js/src/classes/Console/PMA_consoleBookmarks.js +++ b/js/src/classes/Console/PMA_consoleBookmarks.js @@ -1,11 +1,21 @@ /* vim: set expandtab sw=4 ts=4 sts=4: */ + +/** + * Module import + */ import CommonParams from '../../variables/common_params'; import { PMA_Messages as messages } from '../../variables/export_variables'; + /** - * @namespace ConsoleBookmarks + * @class ConsoleBookmarks * Console bookmarks card, and bookmarks items management object */ export default class ConsoleBookmarks { + /** + * @constructor + * @param {Object} instance Instance of the PMA Console + * @return {void} + */ constructor (instance) { this._bookmarks = []; this.pmaConsole = null; @@ -15,10 +25,23 @@ export default class ConsoleBookmarks { this.initialize = this.initialize.bind(this); this.setPmaConsole(instance); } + + /** + * @param {Object} instance Instance of PMA Console + * @return {void} + */ setPmaConsole (instance) { this.pmaConsole = instance; this.initialize(); } + + /** + * @param {string} queryString Query string to be bookmarked + * @param {string} targetDb Target database for the query string + * @param {string} label Label for the query + * @param {bool} isShared Is the query shared + * @return {void} + */ addBookmark (queryString, targetDb, label, isShared) { $('#pma_bookmarks').find('.add [name=shared]').prop('checked', false); $('#pma_bookmarks').find('.add [name=label]').val(''); @@ -43,6 +66,11 @@ export default class ConsoleBookmarks { break; } } + + /** + * Method to refresh the bookmak list + * @return {void} + */ refresh () { $.get('import.php', { ajax_request: true, @@ -55,6 +83,7 @@ export default class ConsoleBookmarks { } }.bind(this)); } + /** * Used for console bookmarks initialize * message events are already binded by PMA_consoleMsg._msgEventBinds diff --git a/js/src/classes/Console/PMA_consoleDebug.js b/js/src/classes/Console/PMA_consoleDebug.js index 29523db8dc..9f0728fcf0 100644 --- a/js/src/classes/Console/PMA_consoleDebug.js +++ b/js/src/classes/Console/PMA_consoleDebug.js @@ -9,9 +9,13 @@ import { escapeHtml } from '../../utils/Sanitise'; /** * Console debug object - * @namespace ConsoleDebug + * @class ConsoleDebug */ export default class ConsoleDebug { + /** + * @constructor + * @param {Object} instance Instance of PMA Console + */ constructor (instance) { this.pmaConsole = null; this._config = { @@ -36,10 +40,20 @@ export default class ConsoleDebug { this.refresh = this.refresh.bind(this); this.setPmaConsole(instance); } + + /** + * @param {Object} instance Instance of PMA Console + * @return {void} + */ setPmaConsole (instance) { this.pmaConsole = instance; this.initialize(); } + + /** + * Used for Console Debug initialise + * @return {void} + */ initialize () { var self = this; // Try to get debug info after every AJAX request diff --git a/js/src/classes/Console/PMA_consoleInput.js b/js/src/classes/Console/PMA_consoleInput.js index ebf82ae7c7..2b668e760a 100644 --- a/js/src/classes/Console/PMA_consoleInput.js +++ b/js/src/classes/Console/PMA_consoleInput.js @@ -11,15 +11,14 @@ import CommonParams from '../../variables/common_params'; /** * Console input object - * @namespace ConsoleInput + * @class ConsoleInput */ export default class ConsoleInput { /** * @constructor - * - * @param {object} pmaConsoleInstance Instance of pma console + * @param {object} instance Instance of PMA Console */ - constructor (pmaConsoleInstance) { + constructor (instance) { /** * @var array, contains Codemirror objects or input jQuery objects * @access private @@ -61,12 +60,22 @@ export default class ConsoleInput { this.setText = this.setText.bind(this); this.getText = this.getText.bind(this); - this.setPmaConsole(pmaConsoleInstance); + this.setPmaConsole(instance); } + + /** + * @param {Object} instance Instance of PMA Console + * @return {void} + */ setPmaConsole (instance) { this.pmaConsole = instance; this.initialize(); } + + /** + * Used for Console Input initialise + * @return {void} + */ initialize () { // _cm object can't be reinitialize if (this._inputs !== null) { @@ -119,6 +128,11 @@ export default class ConsoleInput { } $('#pma_console').find('.console_query_input').keydown(this._keydown); } + + /** + * @param {jQueryEvent} event + * @return {void} + */ _historyNavigate (event) { if (event.keyCode === 38 || event.keyCode === 40) { var upPermitted = false; @@ -172,6 +186,7 @@ export default class ConsoleInput { } } } + /** * Mousedown event handler for bind to input * Shortcut is Ctrl+Enter key or just ENTER, depending on console's @@ -192,6 +207,7 @@ export default class ConsoleInput { } } } + /** * Used for send text to PMA_console.execute() * @@ -204,6 +220,7 @@ export default class ConsoleInput { this.pmaConsole.execute(this._inputs.console.val()); } } + /** * Used for clear the input * @@ -213,6 +230,7 @@ export default class ConsoleInput { clear (target) { this.setText('', target); } + /** * Used for set focus to input * @@ -221,6 +239,7 @@ export default class ConsoleInput { focus () { this._inputs.console.focus(); } + /** * Used for blur input * @@ -233,6 +252,7 @@ export default class ConsoleInput { this._inputs.console.blur(); } } + /** * Used for set text in input * @@ -262,6 +282,7 @@ export default class ConsoleInput { } } } + /** * Used for getting the text of input * diff --git a/js/src/classes/Console/PMA_consoleMessages.js b/js/src/classes/Console/PMA_consoleMessages.js index 289d6fec70..6af0e01737 100644 --- a/js/src/classes/Console/PMA_consoleMessages.js +++ b/js/src/classes/Console/PMA_consoleMessages.js @@ -1,15 +1,20 @@ /* vim: set expandtab sw=4 ts=4 sts=4: */ -import CodeMirror from 'codemirror'; -import PMA_commonParams from '../../variables/common_params'; + /** * Module import */ +import CodeMirror from 'codemirror'; +import PMA_commonParams from '../../variables/common_params'; import { PMA_Messages as messages } from '../../variables/export_variables'; + /** * Console messages, and message items management object - * @namespace ConsoleMessages + * @class ConsoleMessages */ export default class ConsoleMessages { + /** + * @param {Object} instance Instance of PMA Console + */ constructor (instance) { this.pmaConsole = null; this.clear = this.clear.bind(this); @@ -25,10 +30,16 @@ export default class ConsoleMessages { this.setPmaConsole = this.setPmaConsole.bind(this); this.setPmaConsole(instance); } + + /** + * @param {Object} instance Instance of PMA Console + * @return {void} + */ setPmaConsole (instance) { this.pmaConsole = instance; this.initialize(); } + /** * Used for clear the messages * @@ -39,6 +50,7 @@ export default class ConsoleMessages { $('#pma_console').find('.content .console_message_container .message.failed').remove(); $('#pma_console').find('.content .console_message_container .message.expanded').find('.action.collapse').click(); } + /** * Used for show history messages * @@ -47,6 +59,7 @@ export default class ConsoleMessages { showHistory () { $('#pma_console').find('.content .console_message_container .message.hide').removeClass('hide'); } + /** * Used for getting a perticular history query * @@ -63,6 +76,7 @@ export default class ConsoleMessages { return $query.text(); } } + /** * Used to show the correct message depending on which key * combination executes the query (Ctrl+Enter or Enter). @@ -76,6 +90,7 @@ export default class ConsoleMessages { $welcomeMsg.children('[id^=instructions]').hide(); $welcomeMsg.children('#instructions-' + enterExecutes).show(); } + /** * Used for log new message * @@ -117,6 +132,7 @@ export default class ConsoleMessages { return { message_id: msgId, $message: $newMessage.appendTo('#pma_console .content .console_message_container') }; } + /** * Used for log new query * @@ -151,6 +167,7 @@ export default class ConsoleMessages { } return targetMessage; } + _msgEventBinds ($targetMessage) { var self = this; // Leave unbinded elements, remove binded. @@ -258,6 +275,12 @@ export default class ConsoleMessages { }); } } + + /** + * @param {string} msgId Id of the message + * @param {string} msgString Message string + * @param {string} msgType Type of message + */ msgAppend (msgId, msgString, msgType) { var $targetMessage = $('#pma_console').find('.content .console_message_container .message[msgid=' + msgId + ']'); if ($targetMessage.length === 0 || isNaN(parseInt(msgId)) || typeof(msgString) !== 'string') { @@ -265,6 +288,12 @@ export default class ConsoleMessages { } $targetMessage.append('
' + msgString + '
'); } + + /** + * @param {string} msgId Id of the message + * @param {bool} isSuccessed is update succeded + * @param {Object} queryData Data associated with the query + */ updateQuery (msgId, isSuccessed, queryData) { var $targetMessage = $('#pma_console').find('.console_message_container .message[msgid=' + parseInt(msgId) + ']'); if ($targetMessage.length === 0 || isNaN(parseInt(msgId))) { @@ -292,6 +321,7 @@ export default class ConsoleMessages { $targetMessage.addClass('failed'); } } + /** * Used for console messages initialize * diff --git a/js/src/console.js b/js/src/console.js index b6a90fa379..ef2ff4cbef 100644 --- a/js/src/console.js +++ b/js/src/console.js @@ -1,14 +1,20 @@ /* vim: set expandtab sw=4 ts=4 sts=4: */ + /** * Used in or for console * * @package phpMyAdmin-Console */ + +/** + * Module import + */ import ConsoleBookmarks from './classes/Console/PMA_consoleBookmarks'; import ConsoleDebug from './classes/Console/PMA_consoleDebug'; import ConsoleInput from './classes/Console/PMA_consoleInput'; import ConsoleMessages from './classes/Console/PMA_consoleMessages'; import ConsoleResizer from './classes/Console/PMA_ConsoleResizer'; + /** * Console object */ @@ -54,11 +60,27 @@ var Console = { * @access private */ isInitialized: false, + /** + * @var object, instance of PMA Console Resizer + */ pmaConsoleResizer: null, + /** + * @var object, instance of PMA Console Input + */ pmaConsoleInput: null, + /** + * @var object, instance of PMA Console Messages + */ pmaConsoleMessages: null, + /** + * @var object, instance of PMA Console Bookmaks + */ pmaConsoleBookmarks: null, + /** + * @var object, instance of PMA Console Debug + */ pmaConsoleDebug: null, + /** * Used for console initialize, reinit is ok, just some variable assignment * @@ -216,6 +238,7 @@ var Console = { Console.setConfig('Mode', 'info'); } }, + /** * Execute query and show results in console * @@ -251,6 +274,7 @@ var Console = { Console.pmaConsoleInput.clear(); PMA_reloadNavigation(); }, + ajaxCallback: function (data) { if (data && data.console_message_id) { Console.pmaConsoleMessages.updateQuery(data.console_message_id, data.success, @@ -262,6 +286,7 @@ var Console = { } } }, + /** * Change console to collapse mode * @@ -281,6 +306,7 @@ var Console = { }); Console.hideCard(); }, + /** * Show console * @@ -306,6 +332,7 @@ var Console = { } }); }, + /** * Change console to SQL information mode * this mode shows current SQL query @@ -317,6 +344,7 @@ var Console = { // Under construction Console.collapse(); }, + /** * Toggle console mode between collapse/show * Used for toggle buttons and shortcuts @@ -336,6 +364,7 @@ var Console = { PMA_consoleInitialize(); } }, + /** * Scroll console to bottom * @@ -344,6 +373,7 @@ var Console = { scrollBottom: function () { Console.$consoleContent.scrollTop(Console.$consoleContent.prop('scrollHeight')); }, + /** * Show card * @@ -373,6 +403,7 @@ var Console = { Console.showCard($card.parents('.card')); } }, + /** * Scroll console to bottom * @@ -389,6 +420,7 @@ var Console = { $targetCard.removeClass('show'); } }, + /** * Used for update console config * @@ -407,10 +439,12 @@ var Console = { $('#pma_console').find('>.content').removeClass('console_dark_theme'); } }, + setConfig: function (key, value) { Console.config[key] = value; configSet('Console/' + key, value); }, + isSelect: function (queryString) { var reg_exp = /^SELECT\s+/i; return reg_exp.test(queryString);