From dbc36aebd4c8c74351f095c55cde37188dfd6c70 Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Tue, 26 May 2020 21:55:14 -0300 Subject: [PATCH] Add confirmation dialog to cancel. Part of issue #27 --- js/script.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/js/script.js b/js/script.js index 5774a86..0195ed5 100644 --- a/js/script.js +++ b/js/script.js @@ -161,6 +161,9 @@ Notes.prototype = { // this will be the view that is used to update the html var View = function (notes) { this._notes = notes; + + this._editor = undefined; + this._changed = false; }; View.prototype = { @@ -184,6 +187,7 @@ View.prototype = { return digits[1] + '#' + rgb.toString(16).toUpperCase(); }, editNote: function (id) { + var self = this; var modal = $('#modal-note-div'); var modaltitle = $('#modal-note-div #title-editable'); var modalcontent = $('#modal-note-div #content-editable'); @@ -237,6 +241,12 @@ View.prototype = { } }); + editor.subscribe('editableInput', function(event, editorElement) { + self._changed = true; + }); + + this._editor = editor; + /* var shareSelect = $('.note-share-select'); shareSelect.select2({ @@ -350,15 +360,21 @@ View.prototype = { modalcontent.html(""); modaltags.html(""); + self._editor.destroy(); + self._changed = false; + self.render(); }).fail(function () { alert('DOh!. Could not update note!.'); }); + }, cancelEdit: function () { + var self = this; var modal = $('#modal-note-div'); var modaltitle = $('#modal-note-div #title-editable'); var modalcontent = $('#modal-note-dive #content-editable'); + var modaltags = $('#modal-note-div .note-tags'); var modalcolortools = $("#modal-note-div .circle-toolbar"); var modalnote = $("#modal-note-div .quicknote"); @@ -385,12 +401,18 @@ View.prototype = { modalnote.data('id', -1); modaltitle.html(""); modalcontent.html(""); + modaltags.html(""); $.each(modalcolortools, function(i, colortool) { $(colortool).removeClass('icon-checkmark'); }); + + self._editor.destroy(); + self._changed = false; } ); + + this._changed = false; }, renderContent: function () { // Remove all event handlers to prevent double events. @@ -451,7 +473,20 @@ View.prototype = { // Cancel when click outside the modal. $('#app-content').on('click', '.modal-note-background', function (event) { event.stopPropagation(); - self.cancelEdit(); + if (!self._changed) { + self.cancelEdit(); + return; + } + OC.dialogs.confirm( + t('facerecognition', 'Do you want to discard the changes?'), + t('facerecognition', 'Unsaved changes'), + function(result) { + if (result) { + self.cancelEdit(); + } + }, + true + ); }); // Handle hotkeys @@ -459,7 +494,20 @@ View.prototype = { $(document).on("keyup", function(event) { if (event.keyCode == 27) { event.stopPropagation(); - self.cancelEdit(); + if (!self._changed) { + self.cancelEdit(); + return; + } + OC.dialogs.confirm( + t('facerecognition', 'Do you want to discard the changes?'), + t('facerecognition', 'Unsaved changes'), + function(result) { + if (result) { + self.cancelEdit(); + } + }, + true + ); } else if (event.keyCode == 13 && event.altKey) { event.preventDefault();