From e80ceadce0ce39d246eb042bfee1209e994f460c Mon Sep 17 00:00:00 2001 From: Madhura Jayaratne Date: Sat, 1 Dec 2012 09:19:26 +0530 Subject: [PATCH] Fix CodeMirror functionality of the inline query editor when coming back to the page using the forward button of the browser --- js/functions.js | 104 +++++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 36 deletions(-) diff --git a/js/functions.js b/js/functions.js index 98bd6694ad..c3a78a0150 100644 --- a/js/functions.js +++ b/js/functions.js @@ -25,10 +25,15 @@ var only_once_elements = new Array(); var ajax_message_count = 0; /** - * @var codemirror_editor object containing CodeMirror editor + * @var codemirror_editor object containing CodeMirror editor of the query editor in SQL tab */ var codemirror_editor = false; +/** + * @var codemirror_editor object containing CodeMirror editor of the inline query editor + */ +var codemirror_inline_editor = false; + /** * @var chart_activeTimeouts object active timeouts that refresh the charts. When disabling a realtime chart, this can be used to stop the continuous ajax requests */ @@ -1268,17 +1273,27 @@ function pdfPaperSize(format, axis) */ AJAX.registerTeardown('functions.js', function() { $("a.inline_edit_sql").die('click'); - $("input.btnSave").unbind('click'); - $("input.btnDiscard").unbind('click'); + $("input#sql_query_edit_save").die('click'); + $("input#sql_query_edit_discard").die('click'); $('input.sqlbutton').unbind('click'); $("#export_type").unbind('change'); $('#sqlquery').unbind('keydown'); + + if (codemirror_inline_editor) { + // Copy the sql query to the text area to preserve it. + $('#sql_query_edit').text(codemirror_inline_editor.getValue()); + codemirror_inline_editor.toTextArea(); + codemirror_inline_editor = false; + } }); /** * Jquery Coding for inline editing SQL_QUERY */ AJAX.registerOnload('functions.js', function() { + // If we are coming back to the page by clicking forward button + // of the browser, bind the code mirror to inline query editor. + bindCodeMirrorToInlineEditor(); $("a.inline_edit_sql").live('click', function() { if ($('#sql_query_edit').length) { // An inline query editor is already open, @@ -1286,47 +1301,45 @@ AJAX.registerOnload('functions.js', function() { return false; } - var $form = $(this).prev(); + var $form = $(this).prev('form'); var sql_query = $form.find("input[name='sql_query']").val(); var $inner_sql = $(this).parent().prev().find('.inner_sql'); var old_text = $inner_sql.html(); var new_content = "\n"; - new_content += "\n"; - new_content += "\n"; - $inner_sql.replaceWith(new_content); + new_content += "\n"; + new_content += "\n"; + var $editor_area = $('div#inline_editor'); + if ($editor_area.length == 0) { + $editor_area = $('
'); + $editor_area.insertBefore($inner_sql); + } + $editor_area.html(new_content); + $inner_sql.hide(); - // These settings are duplicated from the .ready()function in functions.js - var height = $('#sql_query_edit').css('height'); - var codemirror_inline_editor = false; - if (typeof CodeMirror !== 'undefined') { - codemirror_inline_editor = CodeMirror.fromTextArea($('textarea[name="sql_query_edit"]')[0], { - lineNumbers: true, - matchBrackets: true, - indentUnit: 4, - mode: "text/x-mysql", - lineWrapping: true - }); - codemirror_inline_editor.getScrollerElement().style.height = height; - codemirror_inline_editor.refresh(); + bindCodeMirrorToInlineEditor(); + return false; + }); + + $("input#sql_query_edit_save").live('click', function() { + if (codemirror_inline_editor) { + var sql_query = codemirror_inline_editor.getValue(); + } else { + var sql_query = $(this).prev().val(); } - $("input.btnSave").click(function() { - if (codemirror_inline_editor) { - var sql_query = codemirror_inline_editor.getValue(); - } else { - var sql_query = $(this).prev().val(); - } - var $fake_form = $('
', {action: 'import.php', method: 'post'}) - .append($form.find("input[name=server], input[name=db], input[name=table], input[name=token]").clone()) - .append($('', {type: 'hidden', name: 'show_query', value: 1})) - .append($('', {type: 'hidden', name: 'sql_query', value: sql_query})); - $fake_form.appendTo($('body')).submit(); - }); - $("input.btnDiscard").click(function() { - $(this).closest(".sql").html("" + old_text + ""); - }); - return false; + $form = $("a.inline_edit_sql").prev('form'); + var $fake_form = $('', {action: 'import.php', method: 'post'}) + .append($form.find("input[name=server], input[name=db], input[name=table], input[name=token]").clone()) + .append($('', {type: 'hidden', name: 'show_query', value: 1})) + .append($('', {type: 'hidden', name: 'sql_query', value: sql_query})); + $fake_form.appendTo($('body')).submit(); + }); + + $("input#sql_query_edit_discard").live('click', function() { + $('div#inline_editor_outer') + .empty() + .siblings('.inner_sql').show(); }); $('input.sqlbutton').click(function(evt) { @@ -1388,6 +1401,25 @@ AJAX.registerOnload('functions.js', function() { } }); +/** + * Binds the CodeMirror to the text area used to inline edit a query. + */ +function bindCodeMirrorToInlineEditor() { + var $inline_editor = $('textarea[name="sql_query_edit"]'); + if ($inline_editor.length > 0 && typeof CodeMirror !== 'undefined') { + var height = $('#sql_query_edit').css('height'); + codemirror_inline_editor = CodeMirror.fromTextArea($inline_editor[0], { + lineNumbers: true, + matchBrackets: true, + indentUnit: 4, + mode: "text/x-mysql", + lineWrapping: true + }); + codemirror_inline_editor.getScrollerElement().style.height = height; + codemirror_inline_editor.refresh(); + } +} + /** * Show a message on the top of the page for an Ajax request *