Extract Functions.highlightSql() into modules/sql-highlight.js

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
Maurício Meneghini Fauth 2022-12-03 15:02:06 -03:00
parent 93e7f10929
commit 7efd78e603
No known key found for this signature in database
GPG Key ID: 6A16FD38AFC89CC8
13 changed files with 152 additions and 138 deletions

View File

@ -2,6 +2,7 @@ import $ from 'jquery';
import { AJAX } from '../modules/ajax.js';
import { Functions } from '../modules/functions.js';
import { CommonParams } from '../modules/common.js';
import highlightSql from '../modules/sql-highlight.js';
/**
* JavaScript functions used on Database Search page
@ -142,7 +143,7 @@ AJAX.registerOnload('database/search.js', function () {
window.makeGrid(this, true, true, true, true);
});
$('#browse-results').show();
Functions.highlightSql($('#browse-results'));
highlightSql($('#browse-results'));
$('html, body')
.animate({
scrollTop: $('#browse-results').offset().top

View File

@ -2,6 +2,7 @@ import $ from 'jquery';
import { AJAX } from './modules/ajax.js';
import { Functions } from './modules/functions.js';
import { CommonParams } from './modules/common.js';
import highlightSql from './modules/sql-highlight.js';
/**
* Functions used in the export tab
@ -239,7 +240,7 @@ AJAX.registerOnload('export.js', function () {
modal.modal('show');
modal.on('shown.bs.modal', function () {
$('#showSqlQueryModalLabel').first().html(window.Messages.strQuery);
Functions.highlightSql(modal);
highlightSql(modal);
});
});

View File

@ -3,6 +3,7 @@ import { AJAX } from './modules/ajax.js';
import { Functions } from './modules/functions.js';
import { CommonParams } from './modules/common.js';
import tooltip from './modules/tooltip.js';
import highlightSql from './modules/sql-highlight.js';
/* global Sql */
/* global firstDayOfCalendar */ // templates/javascript/variables.twig
@ -1424,7 +1425,7 @@ window.makeGrid = function (t, enableResize, enableReorder, enableVisib, enableG
} else {
$existingQuery.append(sqlOuter + tools);
}
Functions.highlightSql($existingQuery);
highlightSql($existingQuery);
}
}
// hide and/or update the successfully saved cells

View File

@ -3,6 +3,7 @@ import { Functions } from './functions.js';
import { Navigation } from './navigation.js';
import { CommonParams } from './common.js';
import { initializeTopMenuResizer } from './menu-resizer.js';
import highlightSql from './sql-highlight.js';
/**
* This object handles ajax requests for pages. It also
@ -375,7 +376,7 @@ const AJAX = {
if (data.displayMessage) {
$('#page_content').prepend(data.displayMessage);
Functions.highlightSql($('#page_content'));
highlightSql($('#page_content'));
}
$('#pma_errors').remove();
@ -532,7 +533,7 @@ const AJAX = {
$('#page_content').replaceWith(
'<div id=\'page_content\'>' + data.message + '</div>'
);
Functions.highlightSql($('#page_content'));
highlightSql($('#page_content'));
Functions.checkNumberOfFields();
}
@ -558,7 +559,7 @@ const AJAX = {
}
if (data.displayMessage) {
$('#page_content').prepend(data.displayMessage);
Functions.highlightSql($('#page_content'));
highlightSql($('#page_content'));
}
$('#pma_errors').remove();

View File

@ -2,15 +2,15 @@ import $ from 'jquery';
import { AJAX } from './ajax.js';
import { Navigation } from './navigation.js';
import { CommonActions, CommonParams } from './common.js';
import { mysqlDocKeyword, mysqlDocBuiltin } from './doc-links.js';
import { Indexes } from './indexes.js';
import { Config } from './config.js';
import { resizeTopMenu } from './menu-resizer.js';
import tooltip from './tooltip.js';
import highlightSql from './sql-highlight.js';
/* global ChartType, ColumnType, DataTable, JQPlotChartFactory */ // js/chart.js
/* global DatabaseStructure */ // js/database/structure.js
/* global firstDayOfCalendar, maxInputVars, mysqlDocTemplate, themeImagePath */ // templates/javascript/variables.twig
/* global firstDayOfCalendar, maxInputVars, themeImagePath */ // templates/javascript/variables.twig
/**
* General functions, usually for data manipulation pages.
@ -1378,108 +1378,6 @@ Functions.catchKeypressesFromSqlInlineEdit = function (event) {
}
};
/**
* Adds doc link to single highlighted SQL element
*
* @param $elm
* @param params
*/
Functions.documentationAdd = function ($elm, params) {
if (typeof mysqlDocTemplate === 'undefined') {
return;
}
var url = window.sprintf(
decodeURIComponent(mysqlDocTemplate),
params[0]
);
if (params.length > 1) {
// The # needs to be escaped to be part of the destination URL
url += encodeURIComponent('#') + params[1];
}
var content = $elm.text();
$elm.text('');
$elm.append('<a target="mysql_doc" class="cm-sql-doc" href="' + url + '">' + content + '</a>');
};
/**
* Generates doc links for keywords inside highlighted SQL
*
* @param idx
* @param elm
*/
Functions.documentationKeyword = function (idx, elm) {
var $elm = $(elm);
/* Skip already processed ones */
if ($elm.find('a').length > 0) {
return;
}
var keyword = $elm.text().toUpperCase();
var $next = $elm.next('.cm-keyword');
if ($next) {
var nextKeyword = $next.text().toUpperCase();
var full = keyword + ' ' + nextKeyword;
var $next2 = $next.next('.cm-keyword');
if ($next2) {
var next2Keyword = $next2.text().toUpperCase();
var full2 = full + ' ' + next2Keyword;
if (full2 in mysqlDocKeyword) {
Functions.documentationAdd($elm, mysqlDocKeyword[full2]);
Functions.documentationAdd($next, mysqlDocKeyword[full2]);
Functions.documentationAdd($next2, mysqlDocKeyword[full2]);
return;
}
}
if (full in mysqlDocKeyword) {
Functions.documentationAdd($elm, mysqlDocKeyword[full]);
Functions.documentationAdd($next, mysqlDocKeyword[full]);
return;
}
}
if (keyword in mysqlDocKeyword) {
Functions.documentationAdd($elm, mysqlDocKeyword[keyword]);
}
};
/**
* Generates doc links for builtins inside highlighted SQL
*
* @param idx
* @param elm
*/
Functions.documentationBuiltin = function (idx, elm) {
var $elm = $(elm);
var builtin = $elm.text().toUpperCase();
if (builtin in mysqlDocBuiltin) {
Functions.documentationAdd($elm, mysqlDocBuiltin[builtin]);
}
};
/**
* Higlights SQL using CodeMirror.
*
* @param $base
*/
Functions.highlightSql = function ($base) {
var $elm = $base.find('code.sql');
$elm.each(function () {
var $sql = $(this);
var $pre = $sql.find('pre');
/* We only care about visible elements to avoid double processing */
if ($pre.is(':visible')) {
var $highlight = $('<div class="sql-highlight cm-s-default"></div>');
$sql.append($highlight);
if (typeof window.CodeMirror !== 'undefined') {
window.CodeMirror.runMode($sql.text(), 'text/x-mysql', $highlight[0]);
$pre.hide();
$highlight.find('.cm-keyword').each(Functions.documentationKeyword);
$highlight.find('.cm-builtin').each(Functions.documentationBuiltin);
}
}
});
};
/**
* Updates an element containing code.
*
@ -1655,7 +1553,7 @@ Functions.ajaxShowMessage = function (message = null, timeout = null, type = nul
if (msg !== window.Messages.strLoading) {
$retval.css('background-image', 'none');
}
Functions.highlightSql($retval);
highlightSql($retval);
return $retval;
};
@ -1706,7 +1604,7 @@ Functions.previewSql = function ($form) {
$('#previewSqlModal').find('.modal-body').first().html(response.sql_data);
$('#previewSqlModalLabel').first().html(window.Messages.strPreviewSQL);
$('#previewSqlModal').on('shown.bs.modal', function () {
Functions.highlightSql($('#previewSqlModal'));
highlightSql($('#previewSqlModal'));
});
} else {
Functions.ajaxShowMessage(response.message);
@ -1738,7 +1636,7 @@ Functions.confirmPreviewSql = function (sqlData, url, callback) {
$('#previewSqlConfirmModalLabel').first().html(window.Messages.strPreviewSQL);
$('#previewSqlConfirmCode').first().text(sqlData);
$('#previewSqlConfirmModal').on('shown.bs.modal', function () {
Functions.highlightSql($('#previewSqlConfirmModal'));
highlightSql($('#previewSqlConfirmModal'));
});
$('#previewSQLConfirmOkButton').on('click', function () {
callback(url);
@ -2341,7 +2239,7 @@ Functions.onloadCreateTableEvents = function () {
if (typeof data !== 'undefined' && data.success) {
var $pageContent = $('#page_content');
$pageContent.html(data.message);
Functions.highlightSql($pageContent);
highlightSql($pageContent);
Functions.verifyColumnsProperties();
Functions.hideShowConnection($('.create_table_form select[name=tbl_storage_engine]'));
Functions.ajaxRemoveMessage($msgbox);
@ -2547,7 +2445,7 @@ Functions.onloadChangePasswordEvents = function () {
var $pageContent = $('#page_content');
$pageContent.prepend(data.message);
Functions.highlightSql($pageContent);
highlightSql($pageContent);
$('#change_password_dialog').hide().remove();
$('#edit_user_dialog').dialog('close').remove();
Functions.ajaxRemoveMessage($msgbox);
@ -3118,7 +3016,7 @@ Functions.indexDialogModal = function (routeUrl, url, title, callbackSuccess, ca
}
modalBody.innerHTML = response.sql_data;
Functions.highlightSql($('#indexDialogPreviewModal'));
highlightSql($('#indexDialogPreviewModal'));
},
error: () => {
modalBody.innerHTML = '<div class="alert alert-danger" role="alert">' + window.Messages.strErrorProcessingRequest + '</div>';
@ -3147,7 +3045,7 @@ Functions.indexDialogModal = function (routeUrl, url, title, callbackSuccess, ca
}
if (typeof data !== 'undefined' && data.success === true) {
Functions.ajaxShowMessage(data.message);
Functions.highlightSql($('.result_query'));
highlightSql($('.result_query'));
$('.result_query .alert').remove();
/* Reload the field form*/
$('#table_index').remove();
@ -3523,7 +3421,7 @@ Functions.slidingMessage = function (msg, $object) {
$obj
.append('<div>' + msg + '</div>');
// highlight any sql before taking height;
Functions.highlightSql($obj);
highlightSql($obj);
$obj.find('div')
.first()
.hide();
@ -3541,7 +3439,7 @@ Functions.slidingMessage = function (msg, $object) {
$obj.width('100%')
.html('<div>' + msg + '</div>');
// highlight any sql before taking height;
Functions.highlightSql($obj);
highlightSql($obj);
var h = $obj
.find('div')
.first()
@ -3588,7 +3486,7 @@ Functions.onloadCodeMirrorEditor = () => {
$elm.trigger('focus').on('blur', Functions.updateQueryParameters);
}
}
Functions.highlightSql($('body'));
highlightSql($('body'));
};
/**

View File

@ -3,6 +3,7 @@ import { AJAX } from './ajax.js';
import { Functions } from './functions.js';
import { Navigation } from './navigation.js';
import { CommonActions, CommonParams } from './common.js';
import highlightSql from './sql-highlight.js';
/**
* @fileoverview function used for index manipulation pages
@ -656,7 +657,7 @@ Indexes.on = () => function () {
$('<div class="result_query"></div>')
.html(data.sql_query)
.prependTo('#structure_content');
Functions.highlightSql($('#page_content'));
highlightSql($('#page_content'));
}
Navigation.reload();
CommonActions.refreshMain('index.php?route=/table/structure');

View File

@ -1,8 +1,11 @@
import $ from 'jquery';
/* global mysqlDocTemplate */ // templates/javascript/variables.twig
/**
* Definition of links to MySQL documentation.
*/
export const mysqlDocKeyword = {
const mysqlDocKeyword = {
/* Multi word */
'CHARACTER SET': ['charset'],
'SHOW AUTHORS': ['show-authors'],
@ -331,7 +334,7 @@ export const mysqlDocKeyword = {
'NOT_IN': ['comparison-operators', 'function_not-in']
};
export const mysqlDocBuiltin = {
const mysqlDocBuiltin = {
'TINYINT': ['numeric-types'],
'SMALLINT': ['numeric-types'],
'MEDIUMINT': ['numeric-types'],
@ -364,3 +367,105 @@ export const mysqlDocBuiltin = {
'ENUM': ['string-types'],
'SET': ['string-types']
};
/**
* Adds doc link to single highlighted SQL element
*
* @param $elm
* @param params
*/
function documentationAdd ($elm, params) {
if (typeof mysqlDocTemplate === 'undefined') {
return;
}
var url = window.sprintf(
decodeURIComponent(mysqlDocTemplate),
params[0]
);
if (params.length > 1) {
// The # needs to be escaped to be part of the destination URL
url += encodeURIComponent('#') + params[1];
}
var content = $elm.text();
$elm.text('');
$elm.append('<a target="mysql_doc" class="cm-sql-doc" href="' + url + '">' + content + '</a>');
}
/**
* Generates doc links for keywords inside highlighted SQL
*
* @param idx
* @param elm
*/
function documentationKeyword (idx, elm) {
var $elm = $(elm);
/* Skip already processed ones */
if ($elm.find('a').length > 0) {
return;
}
var keyword = $elm.text().toUpperCase();
var $next = $elm.next('.cm-keyword');
if ($next) {
var nextKeyword = $next.text().toUpperCase();
var full = keyword + ' ' + nextKeyword;
var $next2 = $next.next('.cm-keyword');
if ($next2) {
var next2Keyword = $next2.text().toUpperCase();
var full2 = full + ' ' + next2Keyword;
if (full2 in mysqlDocKeyword) {
documentationAdd($elm, mysqlDocKeyword[full2]);
documentationAdd($next, mysqlDocKeyword[full2]);
documentationAdd($next2, mysqlDocKeyword[full2]);
return;
}
}
if (full in mysqlDocKeyword) {
documentationAdd($elm, mysqlDocKeyword[full]);
documentationAdd($next, mysqlDocKeyword[full]);
return;
}
}
if (keyword in mysqlDocKeyword) {
documentationAdd($elm, mysqlDocKeyword[keyword]);
}
}
/**
* Generates doc links for builtins inside highlighted SQL
*
* @param idx
* @param elm
*/
function documentationBuiltin (idx, elm) {
var $elm = $(elm);
var builtin = $elm.text().toUpperCase();
if (builtin in mysqlDocBuiltin) {
documentationAdd($elm, mysqlDocBuiltin[builtin]);
}
}
/**
* Higlights SQL using CodeMirror.
*
* @param $base
*/
export default function highlightSql ($base) {
var $elm = $base.find('code.sql');
$elm.each(function () {
var $sql = $(this);
var $pre = $sql.find('pre');
/* We only care about visible elements to avoid double processing */
if ($pre.is(':visible')) {
var $highlight = $('<div class="sql-highlight cm-s-default"></div>');
$sql.append($highlight);
if (typeof window.CodeMirror !== 'undefined') {
window.CodeMirror.runMode($sql.text(), 'text/x-mysql', $highlight[0]);
$pre.hide();
$highlight.find('.cm-keyword').each(documentationKeyword);
$highlight.find('.cm-builtin').each(documentationBuiltin);
}
}
});
}

View File

@ -2,6 +2,7 @@ import $ from 'jquery';
import { AJAX } from '../../modules/ajax.js';
import { Functions } from '../../modules/functions.js';
import { CommonParams } from '../../modules/common.js';
import highlightSql from '../../modules/sql-highlight.js';
/**
* Server Status Processes
@ -101,7 +102,7 @@ var processList = {
if (data.hasOwnProperty('success') && data.success) {
var $newTable = $(data.message);
$('#tableprocesslist').html($newTable.html());
Functions.highlightSql($('#tableprocesslist'));
highlightSql($('#tableprocesslist'));
}
processList.refreshTimeout = setTimeout(
processList.refresh,

View File

@ -4,6 +4,7 @@ import { Functions } from './modules/functions.js';
import { Navigation } from './modules/navigation.js';
import { CommonActions, CommonParams } from './modules/common.js';
import { Config } from './modules/config.js';
import highlightSql from './modules/sql-highlight.js';
/**
* @fileoverview functions used wherever an sql query form is used
@ -800,7 +801,7 @@ AJAX.registerOnload('sql.js', function () {
$sqlqueryresultsouter
.show()
.html(data.message);
Functions.highlightSql($sqlqueryresultsouter);
highlightSql($sqlqueryresultsouter);
if (data.menu) {
history.replaceState({
@ -841,7 +842,7 @@ AJAX.registerOnload('sql.js', function () {
$('#sqlqueryresultsouter')
.show()
.html(data.message);
Functions.highlightSql($('#sqlqueryresultsouter'));
highlightSql($('#sqlqueryresultsouter'));
});
}
@ -884,7 +885,7 @@ AJAX.registerOnload('sql.js', function () {
$sqlqueryresults
.html(data.message)
.trigger('makegrid');
Functions.highlightSql($sqlqueryresults);
highlightSql($sqlqueryresults);
}); // end $.post()
}); // end displayOptionsForm handler
@ -997,7 +998,7 @@ AJAX.registerOnload('sql.js', function () {
modal.modal('show');
modal.find('.modal-body').first().html($dialogContent);
modal.on('shown.bs.modal', function () {
Functions.highlightSql(modal);
highlightSql(modal);
});
} else {
Functions.ajaxShowMessage(response.error);

View File

@ -3,6 +3,7 @@ import { AJAX } from '../modules/ajax.js';
import { Functions } from '../modules/functions.js';
import { Navigation } from '../modules/navigation.js';
import { CommonActions, CommonParams } from '../modules/common.js';
import highlightSql from '../modules/sql-highlight.js';
/**
* Unbind all event handlers before tearing down a page
@ -55,7 +56,7 @@ var confirmAndPost = function (linkObject, action) {
Functions.ajaxShowMessage(data.message);
$('<div class="sqlqueryresults ajax"></div>').prependTo('#page_content');
$('.sqlqueryresults').html(data.sql_query);
Functions.highlightSql($('#page_content'));
highlightSql($('#page_content'));
} else {
Functions.ajaxShowMessage(data.error, false);
}
@ -164,7 +165,7 @@ AJAX.registerOnload('table/operations.js', function () {
CommonParams.set('table', data.params.table);
CommonActions.refreshMain(false, function () {
$('#page_content').html(data.message);
Functions.highlightSql($('#page_content'));
highlightSql($('#page_content'));
});
// Refresh navigation when the table is renamed
Navigation.reload();
@ -207,7 +208,7 @@ AJAX.registerOnload('table/operations.js', function () {
Functions.ajaxShowMessage(data.message);
$('<div class=\'sqlqueryresults ajax\'></div>').prependTo('#page_content');
$('.sqlqueryresults').html(data.sql_query);
Functions.highlightSql($('#page_content'));
highlightSql($('#page_content'));
scrollToTop();
} else if (typeof data !== 'undefined' && data.success === true) {
$tempDiv = $('<div id=\'temp_div\'></div>');
@ -216,7 +217,7 @@ AJAX.registerOnload('table/operations.js', function () {
Functions.ajaxShowMessage($success);
$('<div class=\'sqlqueryresults ajax\'></div>').prependTo('#page_content');
$('.sqlqueryresults').html(data.message);
Functions.highlightSql($('#page_content'));
highlightSql($('#page_content'));
$('.sqlqueryresults').children('fieldset,br').remove();
scrollToTop();
} else {

View File

@ -2,6 +2,7 @@ import $ from 'jquery';
import { AJAX } from '../modules/ajax.js';
import { Functions } from '../modules/functions.js';
import { CommonParams } from '../modules/common.js';
import highlightSql from '../modules/sql-highlight.js';
/**
* @fileoverview JavaScript functions used on /table/search
@ -179,7 +180,7 @@ AJAX.registerOnload('table/select.js', function () {
} else {
$('#sqlqueryresultsouter').html(data.error);
}
Functions.highlightSql($('#sqlqueryresultsouter'));
highlightSql($('#sqlqueryresultsouter'));
}); // end $.post()
});

View File

@ -3,6 +3,7 @@ import { AJAX } from '../modules/ajax.js';
import { Functions } from '../modules/functions.js';
import { Navigation } from '../modules/navigation.js';
import { CommonActions, CommonParams } from '../modules/common.js';
import highlightSql from '../modules/sql-highlight.js';
/**
* @fileoverview functions used on the table structure page
@ -87,7 +88,7 @@ AJAX.registerOnload('table/structure.js', function () {
.empty()
.append(data.message)
.show();
Functions.highlightSql($('#page_content'));
highlightSql($('#page_content'));
$('.result_query .alert-primary').remove();
if (typeof data.structure_refresh_route !== 'string') {
// Do not reload the form when the code below freshly filled it
@ -208,7 +209,7 @@ AJAX.registerOnload('table/structure.js', function () {
$('<div class="result_query"></div>')
.html(data.sql_query)
.prependTo('#structure_content');
Functions.highlightSql($('#page_content'));
highlightSql($('#page_content'));
}
// Adjust the row numbers
for (var $row = $currRow.next(); $row.length > 0; $row = $row.next()) {
@ -337,7 +338,7 @@ AJAX.registerOnload('table/structure.js', function () {
}
modalBody.innerHTML = response.sql_data;
Functions.highlightSql($('#designerModalPreviewModal'));
highlightSql($('#designerModalPreviewModal'));
},
error: () => {
modalBody.innerHTML = '<div class="alert alert-danger" role="alert">' + window.Messages.strErrorProcessingRequest + '</div>';

View File

@ -2,6 +2,7 @@ import $ from 'jquery';
import { AJAX } from '../modules/ajax.js';
import { Functions } from '../modules/functions.js';
import { CommonParams } from '../modules/common.js';
import highlightSql from '../modules/sql-highlight.js';
// TODO: change the axis
/**
@ -401,7 +402,7 @@ AJAX.registerOnload('table/zoom_plot_jqplot.js', function () {
}, function (data) {
if (typeof data !== 'undefined' && data.success === true) {
$('#sqlqueryresultsouter').html(data.sql_query);
Functions.highlightSql($('#sqlqueryresultsouter'));
highlightSql($('#sqlqueryresultsouter'));
} else {
Functions.ajaxShowMessage(data.error, false);
}