/** * @fileoverview functions used wherever an sql query form is used * * @requires jQuery * @requires js/functions.js * * @test-module Sql */ /* global isStorageSupported */ // js/config.js /* global codeMirrorEditor */ // js/functions.js /* global makeGrid */ // js/makegrid.js /* global sqlBoxLocked */ // js/functions.js var Sql = {}; /** * decode a string URL_encoded * * @param {string} str * @return {string} the URL-decoded string */ Sql.urlDecode = function (str) { if (typeof str !== 'undefined') { return decodeURIComponent(str.replace(/\+/g, '%20')); } }; /** * encode a string URL_decoded * * @param {string} str * @return {string} the URL-encoded string */ Sql.urlEncode = function (str) { if (typeof str !== 'undefined') { return encodeURIComponent(str).replace(/%20/g, '+'); } }; /** * Saves SQL query in local storage or cookie * * @param {string} query SQL query * @return {void} */ Sql.autoSave = function (query) { if (query) { var key = Sql.getAutoSavedKey(); if (isStorageSupported('localStorage')) { window.localStorage.setItem(key, query); } else { Cookies.set(key, query); } } }; /** * Saves SQL query in local storage or cookie * * @param {string} db database name * @param {string} table table name * @param {string} query SQL query * @return {void} */ Sql.showThisQuery = function (db, table, query) { var showThisQueryObject = { 'db': db, 'table': table, 'query': query }; if (isStorageSupported('localStorage')) { window.localStorage.showThisQuery = 1; window.localStorage.showThisQueryObject = JSON.stringify(showThisQueryObject); } else { Cookies.set('showThisQuery', 1); Cookies.set('showThisQueryObject', JSON.stringify(showThisQueryObject)); } }; /** * Set query to codemirror if show this query is * checked and query for the db and table pair exists */ Sql.setShowThisQuery = function () { var db = $('input[name="db"]').val(); var table = $('input[name="table"]').val(); if (isStorageSupported('localStorage')) { if (window.localStorage.showThisQueryObject !== undefined) { var storedDb = JSON.parse(window.localStorage.showThisQueryObject).db; var storedTable = JSON.parse(window.localStorage.showThisQueryObject).table; var storedQuery = JSON.parse(window.localStorage.showThisQueryObject).query; } if (window.localStorage.showThisQuery !== undefined && window.localStorage.showThisQuery === '1') { $('input[name="show_query"]').prop('checked', true); if (db === storedDb && table === storedTable) { if (codeMirrorEditor) { codeMirrorEditor.setValue(storedQuery); } else if (document.sqlform) { document.sqlform.sql_query.value = storedQuery; } } } else { $('input[name="show_query"]').prop('checked', false); } } }; /** * Saves SQL query with sort in local storage or cookie * * @param {string} query SQL query * @return {void} */ Sql.autoSaveWithSort = function (query) { if (query) { if (isStorageSupported('localStorage')) { window.localStorage.setItem('autoSavedSqlSort', query); } else { Cookies.set('autoSavedSqlSort', query); } } }; /** * Clear saved SQL query with sort in local storage or cookie * * @return {void} */ Sql.clearAutoSavedSort = function () { if (isStorageSupported('localStorage')) { window.localStorage.removeItem('autoSavedSqlSort'); } else { Cookies.set('autoSavedSqlSort', ''); } }; /** * Get the field name for the current field. Required to construct the query * for grid editing * * @param $tableResults enclosing results table * @param $thisField jQuery object that points to the current field's tr * * @return {string} */ Sql.getFieldName = function ($tableResults, $thisField) { var thisFieldIndex = $thisField.index(); // ltr or rtl direction does not impact how the DOM was generated // check if the action column in the left exist var leftActionExist = !$tableResults.find('th').first().hasClass('draggable'); // number of column span for checkbox and Actions var leftActionSkip = leftActionExist ? $tableResults.find('th').first().attr('colspan') - 1 : 0; // If this column was sorted, the text of the a element contains something // like 1 that is useful to indicate the order in case // of a sort on multiple columns; however, we dont want this as part // of the column name so we strip it ( .clone() to .end() ) var fieldName = $tableResults.find('thead').find('th').eq(thisFieldIndex - leftActionSkip).find('a').clone() // clone the element .children() // select all the children .remove() // remove all of them .end() // go back to the selected element .text(); // grab the text // happens when just one row (headings contain no a) if (fieldName === '') { var $heading = $tableResults.find('thead').find('th').eq(thisFieldIndex - leftActionSkip).children('span'); // may contain column comment enclosed in a span - detach it temporarily to read the column name var $tempColComment = $heading.children().detach(); fieldName = $heading.text(); // re-attach the column comment $heading.append($tempColComment); } fieldName = fieldName.trim(); return fieldName; }; /** * Unbind all event handlers before tearing down a page */ AJAX.registerTeardown('sql.js', function () { $(document).off('click', 'a.delete_row.ajax'); $(document).off('submit', '.bookmarkQueryForm'); $('input#bkm_label').off('input'); $(document).off('makegrid', '.sqlqueryresults'); $('#togglequerybox').off('click'); $(document).off('click', '#button_submit_query'); $(document).off('change', '#id_bookmark'); $('input[name=\'bookmark_variable\']').off('keypress'); $(document).off('submit', '#sqlqueryform.ajax'); $(document).off('click', 'input[name=navig].ajax'); $(document).off('submit', 'form[name=\'displayOptionsForm\'].ajax'); $(document).off('mouseenter', 'th.column_heading.pointer'); $(document).off('mouseleave', 'th.column_heading.pointer'); $(document).off('click', 'th.column_heading.marker'); $(document).off('scroll', window); $(document).off('keyup', '.filter_rows'); if (codeMirrorEditor) { codeMirrorEditor.off('change'); } else { $('#sqlquery').off('input propertychange'); } $('body').off('click', '.navigation .showAllRows'); $('body').off('click', 'a.browse_foreign'); $('body').off('click', '#simulate_dml'); $('body').off('keyup', '#sqlqueryform'); $('body').off('click', 'form[name="resultsForm"].ajax button[name="submit_mult"], form[name="resultsForm"].ajax input[name="submit_mult"]'); $(document).off('submit', '.maxRowsForm'); $(document).off('click', '#view_as'); $(document).off('click', '#sqlquery'); }); /** * @description
Ajax scripts for sql and browse pages
* * Actions ajaxified here: *