From 4f15c036863a2df3ae2a806563cb8dd39bb57e0f Mon Sep 17 00:00:00 2001 From: ilvalle Date: Wed, 12 Jun 2013 13:18:48 +0200 Subject: [PATCH] fire a warning before closing an unsaved file --- applications/admin/static/js/ajax_editor.js | 5 ++--- applications/admin/views/default/edit.html | 16 +++++++++++----- applications/admin/views/default/edit_js.html | 5 +++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/applications/admin/static/js/ajax_editor.js b/applications/admin/static/js/ajax_editor.js index 7377af25..f02adb7b 100644 --- a/applications/admin/static/js/ajax_editor.js +++ b/applications/admin/static/js/ajax_editor.js @@ -66,9 +66,8 @@ function doClickSave() { 'doClickSave'); }, success: function(json,text,xhr){ - - - + jQuery(editor).data('saved', true); // Set as saved + editor.on("change", store_changes_function); // Re-enable change watcher // reenable disabled submit button var t=jQuery("input[name='save']"); t.attr('class',''); diff --git a/applications/admin/views/default/edit.html b/applications/admin/views/default/edit.html index c4a5ff42..877fe869 100644 --- a/applications/admin/views/default/edit.html +++ b/applications/admin/views/default/edit.html @@ -55,12 +55,18 @@ jQuery(document).on('shown click', 'a[data-toggle="tab"]', function (e) { jQuery(document).on('click', '#filesTab button[class="close"]', function (e) { var tab_body = jQuery(jQuery(this).parent().attr("href")); // it should be a div var tab_header = jQuery(this).parent().parent(); // it should be a li - - if (tab_header.hasClass('active') === true) { //Set active an other tab - jQuery(tab_header).prev().children('a[data-toggle="tab"]').tab('show'); // Select first tab + var saved = jQuery(tab_body.find('textarea').data('editor')).data('saved'); + var close = true; + if (saved === false) { + close = confirm("You are closing an unsaved file") + } + if (close) { + if (tab_header.hasClass('active') === true) { //Set active an other tab + jQuery(tab_header).prev().children('a[data-toggle="tab"]').tab('show'); // Select first tab + } + tab_header.remove(); //remove li of tab + tab_body.remove(); //remove li of tab } - tab_header.remove(); //remove li of tab - tab_body.remove(); //remove li of tab }); // Revert current file diff --git a/applications/admin/views/default/edit_js.html b/applications/admin/views/default/edit_js.html index 7f6a0722..adc910d7 100644 --- a/applications/admin/views/default/edit_js.html +++ b/applications/admin/views/default/edit_js.html @@ -113,6 +113,11 @@ CodeMirror.showHint(cm, CodeMirror.pythonHint); } {{pass}} + store_changes_function = function(instance, changeObj) { + jQuery(instance).data('saved', false); + instance.off("change", store_changes_function); + } + editor.on("change", store_changes_function); // save the editor as textarea data attribute jQuery("#{{=id}} textarea").data('editor', editor); var hlLine = editor.addLineClass(0, "background", "activeline");