diff options
author | Madhura Jayaratne <madhura.cj@gmail.com> | 2015-05-21 07:16:49 +0300 |
---|---|---|
committer | Madhura Jayaratne <madhura.cj@gmail.com> | 2015-05-21 07:16:49 +0300 |
commit | 62f07fef15349c3d2f3fff308649d6b5fa914169 (patch) | |
tree | 31b1767bb662100fae10c489e6c4383a66b473d2 /js/functions.js | |
parent | ad289aab6c9e9593a6b00ac8f30f1258109da45d (diff) |
Use a single function to initialize CodeMirror editors
Signed-off-by: Madhura Jayaratne <madhura.cj@gmail.com>
Diffstat (limited to 'js/functions.js')
-rw-r--r-- | js/functions.js | 133 |
1 files changed, 52 insertions, 81 deletions
diff --git a/js/functions.js b/js/functions.js index b6cc7f729a..c12aea5ff3 100644 --- a/js/functions.js +++ b/js/functions.js @@ -93,6 +93,46 @@ $.ajaxPrefilter(function (options, originalOptions, jqXHR) { }); /** + * Creates an SQL editor which supports auto completing etc. + * + * @param $textarea jQuery object wrapping the textarea to be made the editor + * @param options optional options for CodeMirror + */ +function PMA_getSQLEditor($textarea, options) { + if ($textarea.length > 0 && typeof CodeMirror !== 'undefined') { + + // merge options for CodeMirror + var defaults = { + lineNumbers: true, + matchBrackets: true, + extraKeys: {"Ctrl-Space": "autocomplete"}, + hintOptions: {"completeSingle": false, "completeOnSingleClick": true}, + indentUnit: 4, + mode: "text/x-mysql", + lineWrapping: true + }; + $.extend(true, defaults, options); + + // create CodeMirror editor + var codemirrorEditor = CodeMirror.fromTextArea($textarea[0], defaults); + // allow resizing + $(codemirrorEditor.getWrapperElement()) + .css('resize', 'vertical') + .resizable({ + handles: 'n, s', + resize: function() { + codemirrorEditor.setSize($(this).width(), $(this).height()); + } + }); + // enable autocomplete + codemirrorEditor.on("inputRead", codemirrorAutocompleteOnInputRead); + + return codemirrorEditor; + } + return null; +} + +/** * Clear text selection */ function PMA_clearSelection() { @@ -1853,37 +1893,17 @@ function bindCodeMirrorToInlineEditor() { var $inline_editor = $('#sql_query_edit'); if ($inline_editor.length > 0) { if (typeof CodeMirror !== 'undefined') { - var height = $('#sql_query_edit').css('height'); - codemirror_inline_editor = CodeMirror.fromTextArea($inline_editor[0], { - lineNumbers: true, - matchBrackets: true, - extraKeys: {"Ctrl-Space": "autocomplete"}, - hintOptions: {"completeSingle": false, "completeOnSingleClick": true}, - indentUnit: 4, - mode: "text/x-mysql", - lineWrapping: true - }); - $(codemirror_inline_editor.getWrapperElement()) - .css('resize', 'vertical') - .resizable({ - handles: 'n, s', - resize: function() { - codemirror_inline_editor.setSize($(this).width(), $(this).height()); - } - }); - codemirror_inline_editor.on("inputRead", codemirrorAutocompleteOnInputRead); + var height = $inline_editor.css('height'); + codemirror_inline_editor = PMA_getSQLEditor($inline_editor); codemirror_inline_editor.getWrapperElement().style.height = height; codemirror_inline_editor.refresh(); codemirror_inline_editor.focus(); - $(codemirror_inline_editor.getWrapperElement()).bind( - 'keydown', - catchKeypressesFromSqlTextboxes - ); + $(codemirror_inline_editor.getWrapperElement()) + .bind('keydown', catchKeypressesFromSqlTextboxes); } else { - $inline_editor.focus().bind( - 'keydown', - catchKeypressesFromSqlTextboxes - ); + $inline_editor + .focus() + .bind('keydown', catchKeypressesFromSqlTextboxes); } } } @@ -4067,31 +4087,11 @@ AJAX.registerOnload('functions.js', function () { var $elm = $('#sqlquery'); if ($elm.length > 0) { if (typeof CodeMirror != 'undefined') { - // for codemirror - codemirror_editor = CodeMirror.fromTextArea($elm[0], { - lineNumbers: true, - matchBrackets: true, - extraKeys: {"Ctrl-Space": "autocomplete"}, - hintOptions: {"completeSingle": false, "completeOnSingleClick": true}, - indentUnit: 4, - mode: "text/x-mysql", - lineWrapping: true - }); + codemirror_editor = PMA_getSQLEditor($elm); + codemirror_editor.focus(); $(codemirror_editor.getWrapperElement()) - .css('resize', 'vertical') - .resizable({ - handles: 'n, s', - resize: function() { - codemirror_editor.setSize($(this).width(), $(this).height()); - } - }); - codemirror_editor.on("inputRead", codemirrorAutocompleteOnInputRead); + .bind('keydown', catchKeypressesFromSqlTextboxes); codemirror_editor.on("blur", updateQueryParameters); - codemirror_editor.focus(); - $(codemirror_editor.getWrapperElement()).bind( - 'keydown', - catchKeypressesFromSqlTextboxes - ); } else { // without codemirror $elm.focus() @@ -4291,24 +4291,7 @@ AJAX.registerOnload('functions.js', function () { }); // end $(document).on() } - var $elm = $('textarea[name="view[as]"]'); - if ($elm.length > 0) { - if (typeof CodeMirror != 'undefined') { - syntaxHighlighter = CodeMirror.fromTextArea( - $elm[0], - { - lineNumbers: true, - matchBrackets: true, - extraKeys: {"Ctrl-Space": "autocomplete"}, - hintOptions: {"completeSingle": false, "completeOnSingleClick": true}, - indentUnit: 4, - mode: "text/x-mysql", - lineWrapping: true - } - ); - syntaxHighlighter.on("inputRead", codemirrorAutocompleteOnInputRead); - } - } + syntaxHighlighter = PMA_getSQLEditor($('textarea[name="view[as]"]')); $(document).on('change', '#fkc_checkbox', function () { if ($(this).prop("checked")) { @@ -4357,19 +4340,7 @@ function PMA_createViewDialog($this) } }); // Attach syntax highlighted editor - if (typeof CodeMirror !== 'undefined') { - var $elm = $dialog.find('textarea'); - syntaxHighlighter = CodeMirror.fromTextArea($elm[0], { - lineNumbers: true, - matchBrackets: true, - extraKeys: {"Ctrl-Space": "autocomplete"}, - hintOptions: {"completeSingle": false, "completeOnSingleClick": true}, - indentUnit: 4, - mode: "text/x-mysql", - lineWrapping: true - }); - syntaxHighlighter.on("inputRead", codemirrorAutocompleteOnInputRead); - } + syntaxHighlighter = PMA_getSQLEditor($dialog.find('textarea')); $('input:visible[type=text]', $dialog).first().focus(); } else { PMA_ajaxShowMessage(data.error); |