From 1f90bf028690b3576d9c298feb0f875341d27f65 Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Mon, 27 Feb 2017 13:59:11 -0600 Subject: [PATCH] editor note references --- .../app/frontend/controllers/editor.js | 23 ++++++++--- .../app/frontend/models/app/editor.js | 41 +++++++++++++++++++ .../app/frontend/models/app/note.js | 2 +- .../app/frontend/models/local/itemParams.js | 2 +- .../javascripts/app/services/modelManager.js | 18 +++++--- .../javascripts/app/services/syncManager.js | 1 - 6 files changed, 74 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/app/frontend/controllers/editor.js b/app/assets/javascripts/app/frontend/controllers/editor.js index cc3e8bf4b..04252b04f 100644 --- a/app/assets/javascripts/app/frontend/controllers/editor.js +++ b/app/assets/javascripts/app/frontend/controllers/editor.js @@ -52,8 +52,10 @@ angular.module('app.frontend') this.showMenu = false; this.loadTagsString(); - if(note.editorUrl) { - this.customEditor = this.editorForUrl(note.editorUrl); + var editor = this.editorForNote(note); + + if(editor) { + this.customEditor = editor; this.postNoteToExternalEditor(); } else { this.customEditor = null; @@ -75,16 +77,27 @@ angular.module('app.frontend') this.selectedEditor = function(editor) { this.showEditorMenu = false; if(editor.default) { + if(this.customEditor) { + this.customEditor.removeItemAsRelationship(this.note); + this.customEditor.setDirty(true); + } this.customEditor = null; } else { this.customEditor = editor; + this.customEditor.addItemAsRelationship(this.note); + this.customEditor.setDirty(true); } - this.note.editorUrl = editor.url; }.bind(this) - this.editorForUrl = function(url) { + this.editorForNote = function(note) { var editors = modelManager.itemsForContentType("SN|Editor"); - return editors.filter(function(editor){return editor.url == url})[0]; + for(var editor of editors) { + // console.log(editor.notes, editor.references); + if(_.includes(editor.notes, note)) { + return editor; + } + } + return null; } this.postNoteToExternalEditor = function() { diff --git a/app/assets/javascripts/app/frontend/models/app/editor.js b/app/assets/javascripts/app/frontend/models/app/editor.js index 9cff50789..49e84666d 100644 --- a/app/assets/javascripts/app/frontend/models/app/editor.js +++ b/app/assets/javascripts/app/frontend/models/app/editor.js @@ -2,6 +2,9 @@ class Editor extends Item { constructor(json_obj) { super(json_obj); + if(!this.notes) { + this.notes = []; + } } mapContentToLocalProperties(contentObject) { @@ -22,6 +25,44 @@ class Editor extends Item { return params; } + referenceParams() { + var references = _.map(this.notes, function(note){ + return {uuid: note.uuid, content_type: note.content_type}; + }) + + return references; + } + + addItemAsRelationship(item) { + if(item.content_type == "Note") { + if(!_.find(this.notes, item)) { + this.notes.push(item); + } + } + super.addItemAsRelationship(item); + } + + removeItemAsRelationship(item) { + if(item.content_type == "Note") { + _.pull(this.notes, item); + } + super.removeItemAsRelationship(item); + } + + removeAllRelationships() { + super.removeAllRelationships(); + this.notes = []; + } + + locallyClearAllReferences() { + super.locallyClearAllReferences(); + this.notes = []; + } + + allReferencedObjects() { + return this.notes; + } + toJSON() { return {uuid: this.uuid} } diff --git a/app/assets/javascripts/app/frontend/models/app/note.js b/app/assets/javascripts/app/frontend/models/app/note.js index e79a64682..58cd84292 100644 --- a/app/assets/javascripts/app/frontend/models/app/note.js +++ b/app/assets/javascripts/app/frontend/models/app/note.js @@ -62,7 +62,7 @@ class Note extends Item { _.pull(tag.notes, this); }.bind(this)) this.tags = []; - } + } isBeingRemovedLocally() { this.tags.forEach(function(tag){ diff --git a/app/assets/javascripts/app/frontend/models/local/itemParams.js b/app/assets/javascripts/app/frontend/models/local/itemParams.js index 22d7405ce..396a51bd1 100644 --- a/app/assets/javascripts/app/frontend/models/local/itemParams.js +++ b/app/assets/javascripts/app/frontend/models/local/itemParams.js @@ -16,7 +16,7 @@ class ItemParams { } paramsForLocalStorage() { - this.additionalFields = ["updated_at", "dirty", "editorUrl"]; + this.additionalFields = ["updated_at", "dirty"]; this.forExportFile = true; return this.__params(); } diff --git a/app/assets/javascripts/app/services/modelManager.js b/app/assets/javascripts/app/services/modelManager.js index ef67bc86a..f5b5c04ac 100644 --- a/app/assets/javascripts/app/services/modelManager.js +++ b/app/assets/javascripts/app/services/modelManager.js @@ -59,7 +59,9 @@ class ModelManager { } mapResponseItemsToLocalModelsOmittingFields(items, omitFields) { - var models = []; + var models = [], processedObjects = []; + + // first loop should add and process items for (var json_obj of items) { json_obj = _.omit(json_obj, omitFields || []) var item = this.findItem(json_obj["uuid"]); @@ -80,11 +82,16 @@ class ModelManager { this.addItem(item); - if(json_obj.content) { - this.resolveReferencesForItem(item); - } - models.push(item); + processedObjects.push(json_obj); + } + + // second loop should process references + for (var index in processedObjects) { + var json_obj = processedObjects[index]; + if(json_obj.content) { + this.resolveReferencesForItem(models[index]); + } } this.notifySyncObserversOfModels(models); @@ -174,6 +181,7 @@ class ModelManager { return; } + for(var reference of contentObject.references) { var referencedItem = this.findItem(reference.uuid); if(referencedItem) { diff --git a/app/assets/javascripts/app/services/syncManager.js b/app/assets/javascripts/app/services/syncManager.js index ddabf436d..e8ebb82df 100644 --- a/app/assets/javascripts/app/services/syncManager.js +++ b/app/assets/javascripts/app/services/syncManager.js @@ -187,7 +187,6 @@ class SyncManager { var saved = this.handleItemsResponse(response.saved_items, omitFields); this.handleUnsavedItemsResponse(response.unsaved) - this.writeItemsToLocalStorage(saved, false, null); this.writeItemsToLocalStorage(retrieved, false, null);