editor note references

This commit is contained in:
Mo Bitar
2017-02-27 13:59:11 -06:00
parent e08c284a30
commit 1f90bf0286
6 changed files with 74 additions and 13 deletions

View File

@@ -52,8 +52,10 @@ angular.module('app.frontend')
this.showMenu = false; this.showMenu = false;
this.loadTagsString(); this.loadTagsString();
if(note.editorUrl) { var editor = this.editorForNote(note);
this.customEditor = this.editorForUrl(note.editorUrl);
if(editor) {
this.customEditor = editor;
this.postNoteToExternalEditor(); this.postNoteToExternalEditor();
} else { } else {
this.customEditor = null; this.customEditor = null;
@@ -75,16 +77,27 @@ angular.module('app.frontend')
this.selectedEditor = function(editor) { this.selectedEditor = function(editor) {
this.showEditorMenu = false; this.showEditorMenu = false;
if(editor.default) { if(editor.default) {
if(this.customEditor) {
this.customEditor.removeItemAsRelationship(this.note);
this.customEditor.setDirty(true);
}
this.customEditor = null; this.customEditor = null;
} else { } else {
this.customEditor = editor; this.customEditor = editor;
this.customEditor.addItemAsRelationship(this.note);
this.customEditor.setDirty(true);
} }
this.note.editorUrl = editor.url;
}.bind(this) }.bind(this)
this.editorForUrl = function(url) { this.editorForNote = function(note) {
var editors = modelManager.itemsForContentType("SN|Editor"); 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() { this.postNoteToExternalEditor = function() {

View File

@@ -2,6 +2,9 @@ class Editor extends Item {
constructor(json_obj) { constructor(json_obj) {
super(json_obj); super(json_obj);
if(!this.notes) {
this.notes = [];
}
} }
mapContentToLocalProperties(contentObject) { mapContentToLocalProperties(contentObject) {
@@ -22,6 +25,44 @@ class Editor extends Item {
return params; 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() { toJSON() {
return {uuid: this.uuid} return {uuid: this.uuid}
} }

View File

@@ -62,7 +62,7 @@ class Note extends Item {
_.pull(tag.notes, this); _.pull(tag.notes, this);
}.bind(this)) }.bind(this))
this.tags = []; this.tags = [];
} }
isBeingRemovedLocally() { isBeingRemovedLocally() {
this.tags.forEach(function(tag){ this.tags.forEach(function(tag){

View File

@@ -16,7 +16,7 @@ class ItemParams {
} }
paramsForLocalStorage() { paramsForLocalStorage() {
this.additionalFields = ["updated_at", "dirty", "editorUrl"]; this.additionalFields = ["updated_at", "dirty"];
this.forExportFile = true; this.forExportFile = true;
return this.__params(); return this.__params();
} }

View File

@@ -59,7 +59,9 @@ class ModelManager {
} }
mapResponseItemsToLocalModelsOmittingFields(items, omitFields) { mapResponseItemsToLocalModelsOmittingFields(items, omitFields) {
var models = []; var models = [], processedObjects = [];
// first loop should add and process items
for (var json_obj of items) { for (var json_obj of items) {
json_obj = _.omit(json_obj, omitFields || []) json_obj = _.omit(json_obj, omitFields || [])
var item = this.findItem(json_obj["uuid"]); var item = this.findItem(json_obj["uuid"]);
@@ -80,11 +82,16 @@ class ModelManager {
this.addItem(item); this.addItem(item);
if(json_obj.content) {
this.resolveReferencesForItem(item);
}
models.push(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); this.notifySyncObserversOfModels(models);
@@ -174,6 +181,7 @@ class ModelManager {
return; return;
} }
for(var reference of contentObject.references) { for(var reference of contentObject.references) {
var referencedItem = this.findItem(reference.uuid); var referencedItem = this.findItem(reference.uuid);
if(referencedItem) { if(referencedItem) {

View File

@@ -187,7 +187,6 @@ class SyncManager {
var saved = this.handleItemsResponse(response.saved_items, omitFields); var saved = this.handleItemsResponse(response.saved_items, omitFields);
this.handleUnsavedItemsResponse(response.unsaved) this.handleUnsavedItemsResponse(response.unsaved)
this.writeItemsToLocalStorage(saved, false, null); this.writeItemsToLocalStorage(saved, false, null);
this.writeItemsToLocalStorage(retrieved, false, null); this.writeItemsToLocalStorage(retrieved, false, null);