From fa5c9d82398ff4aa2a67ff534e7855f86bd5bbbf Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Tue, 12 Nov 2019 11:08:01 -0300 Subject: [PATCH] Take tags from notes controller --- js/script.js | 55 ++++++++++++++-------------------------------------- 1 file changed, 15 insertions(+), 40 deletions(-) diff --git a/js/script.js b/js/script.js index 7530d24..3272a3c 100644 --- a/js/script.js +++ b/js/script.js @@ -111,6 +111,16 @@ Notes.prototype = { }); return Ccolors; }, + getTags: function () { + var tags = []; + $.each(this._notes, function(index, note) { + $.each(note.tags, function(index, tag) { + if (tags.findIndex(item => item.id == tag.id) === -1) + tags.push(tag); + }); + }); + return tags; + }, loadAll: function () { var deferred = $.Deferred(); var self = this; @@ -147,38 +157,10 @@ Notes.prototype = { } }; -// this notes object holds all our tags -var Tags = function (baseUrl) { - this._baseUrl = baseUrl; - this._tags = []; - this._loaded = false; -}; - -Tags.prototype = { - loadAll: function () { - var deferred = $.Deferred(); - var self = this; - $.get(this._baseUrl).done(function (tags) { - self._tags = tags.reverse(); - self._loaded = true; - deferred.resolve(); - }).fail(function () { - deferred.reject(); - }); - return deferred.promise(); - }, - isLoaded: function () { - return this._loaded; - }, - getAll: function () { - return this._tags; - } -}; // this will be the view that is used to update the html -var View = function (notes, tags) { +var View = function (notes) { this._notes = notes; - this._tags = tags; }; View.prototype = { @@ -631,7 +613,7 @@ View.prototype = { var id = $("#modal-note-div .quicknote").data('id'); self._notes.load(id); QnDialogs.tags( - self._tags.getAll(), + self._notes.getTags(), self._notes.getActive().tags, function(result, newTags) { if (result === true) { @@ -666,7 +648,7 @@ View.prototype = { var html = Handlebars.templates['navigation']({ colors: this._notes.getColors(), notes: this._notes.getAll(), - tags: this._tags.getAll(), + tags: this._notes.getTags(), newNoteTxt: t('quicknotes', 'New note'), allNotesTxt: t('quicknotes', 'All notes'), colorsTxt: t('quicknotes', 'Colors'), @@ -843,9 +825,7 @@ new OCA.Search(search, function() { * Create modules */ var notes = new Notes(OC.generateUrl('/apps/quicknotes/notes')); -var tags = new Tags(OC.generateUrl('/apps/quicknotes/tags')); - -var view = new View(notes, tags); +var view = new View(notes); /* * Render initial loading view @@ -856,12 +836,7 @@ view.renderContent(); * Loading notes and render final view. */ notes.loadAll().done(function () { - tags.loadAll().done(function () { - view.render(); - }).fail(function () { - alert('Could not load tags'); - }); - // FIXME: So ugly... + view.render(); }).fail(function () { alert('Could not load notes'); });