From e71a4b6589f01df0ce086542cdccbe8b671b994b Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Tue, 19 Jun 2018 18:34:22 -0500 Subject: [PATCH] Wip --- app/assets/javascripts/app/models/api/item.js | 54 +++++++++++-------- app/assets/javascripts/app/models/api/mfa.js | 16 +++--- .../app/models/api/serverExtension.js | 28 +++++----- .../javascripts/app/models/app/component.js | 6 ++- .../javascripts/app/models/app/editor.js | 5 +- .../javascripts/app/models/app/extension.js | 5 +- app/assets/javascripts/app/models/app/note.js | 7 +-- app/assets/javascripts/app/models/app/tag.js | 5 +- .../javascripts/app/models/app/theme.js | 1 - .../javascripts/app/services/modelManager.js | 14 ++--- 10 files changed, 73 insertions(+), 68 deletions(-) diff --git a/app/assets/javascripts/app/models/api/item.js b/app/assets/javascripts/app/models/api/item.js index 6fe6fdee7..bddd3f158 100644 --- a/app/assets/javascripts/app/models/api/item.js +++ b/app/assets/javascripts/app/models/api/item.js @@ -30,7 +30,9 @@ class Item { } try { - return JSON.parse(this.content); + // console.log("Parsing json", this.content); + this.content = JSON.parse(this.content); + return this.content; } catch (e) { console.log("Error parsing json", e, this); return {}; @@ -58,6 +60,29 @@ class Item { } } + mapContentToLocalProperties(contentObj) { + if(contentObj.appData) { + this.appData = contentObj.appData; + } + if(!this.appData) { this.appData = {}; } + } + + createContentJSONFromProperties() { + return this.structureParams(); + } + + referenceParams() { + // subclasses can override + return this.contentObject.references || []; + } + + structureParams() { + var params = this.contentObject; + params.appData = this.appData; + params.references = this.referenceParams(); + return params; + } + refreshContentObject() { // Before an item can be duplicated or cloned, we must update this.content (if it is an object) with the object's // current physical properties, because updateFromJSON, which is what all new items must go through, @@ -119,28 +144,6 @@ class Item { } } - mapContentToLocalProperties(contentObj) { - if(contentObj.appData) { - this.appData = contentObj.appData; - } - if(!this.appData) { this.appData = {}; } - } - - createContentJSONFromProperties() { - return this.structureParams(); - } - - referenceParams() { - // must override - } - - structureParams() { - return { - references: this.referenceParams(), - appData: this.appData - } - } - addItemAsRelationship(item) { // must override } @@ -172,6 +175,11 @@ class Item { potentialItemOfInterestHasChangedItsUUID(newItem, oldUUID, newUUID) { // optional override + for(var reference of this.content.references) { + if(reference.uuid == oldUUID) { + reference.uuid = newUUID; + } + } } allReferencedObjects() { diff --git a/app/assets/javascripts/app/models/api/mfa.js b/app/assets/javascripts/app/models/api/mfa.js index d3792654d..8c17fcb78 100644 --- a/app/assets/javascripts/app/models/api/mfa.js +++ b/app/assets/javascripts/app/models/api/mfa.js @@ -4,14 +4,14 @@ class Mfa extends Item { super(json_obj); } - mapContentToLocalProperties(content) { - super.mapContentToLocalProperties(content) - this.serverContent = content; - } - - structureParams() { - return _.merge(this.serverContent, super.structureParams()); - } + // mapContentToLocalProperties(content) { + // super.mapContentToLocalProperties(content) + // this.serverContent = content; + // } + // + // structureParams() { + // return _.merge(this.serverContent, super.structureParams()); + // } toJSON() { return {uuid: this.uuid} diff --git a/app/assets/javascripts/app/models/api/serverExtension.js b/app/assets/javascripts/app/models/api/serverExtension.js index 8149c5a95..03c5e95fc 100644 --- a/app/assets/javascripts/app/models/api/serverExtension.js +++ b/app/assets/javascripts/app/models/api/serverExtension.js @@ -9,20 +9,20 @@ class ServerExtension extends Item { this.url = content.url; } - structureParams() { - // There was a bug with the way Base64 content was parsed in previous releases related to this item. - // The bug would not parse the JSON behind the base64 string and thus saved data in an invalid format. - // This is the line: https://github.com/standardnotes/web/commit/1ad0bf73d8e995b7588854f1b1e4e4a02303a42f#diff-15753bac364782a3a5876032bcdbf99aR76 - // We'll remedy this for affected users by trying to parse the content string - if(typeof this.content !== 'object') { - try { - this.content = JSON.parse(this.content); - } catch (e) {} - } - var params = this.content || {}; - _.merge(params, super.structureParams()); - return params; - } + // structureParams() { + // // There was a bug with the way Base64 content was parsed in previous releases related to this item. + // // The bug would not parse the JSON behind the base64 string and thus saved data in an invalid format. + // // This is the line: https://github.com/standardnotes/web/commit/1ad0bf73d8e995b7588854f1b1e4e4a02303a42f#diff-15753bac364782a3a5876032bcdbf99aR76 + // // We'll remedy this for affected users by trying to parse the content string + // if(typeof this.content !== 'object') { + // try { + // this.content = JSON.parse(this.content); + // } catch (e) {} + // } + // var params = this.content || {}; + // _.merge(params, super.structureParams()); + // return params; + // } toJSON() { return {uuid: this.uuid} diff --git a/app/assets/javascripts/app/models/app/component.js b/app/assets/javascripts/app/models/app/component.js index 57e609ca8..25d05cd1b 100644 --- a/app/assets/javascripts/app/models/app/component.js +++ b/app/assets/javascripts/app/models/app/component.js @@ -24,6 +24,7 @@ class Component extends Item { super.mapContentToLocalProperties(content) /* Legacy */ this.url = content.url || content.hosted_url; + /* New */ this.local_url = content.local_url; this.hosted_url = content.hosted_url || content.url; @@ -82,8 +83,9 @@ class Component extends Item { associatedItemIds: this.associatedItemIds, }; - _.merge(params, super.structureParams()); - return params; + var superParams = super.structureParams(); + Object.assign(superParams, params); + return superParams; } toJSON() { diff --git a/app/assets/javascripts/app/models/app/editor.js b/app/assets/javascripts/app/models/app/editor.js index 3a38f0734..a1cb8c5b5 100644 --- a/app/assets/javascripts/app/models/app/editor.js +++ b/app/assets/javascripts/app/models/app/editor.js @@ -28,8 +28,9 @@ class Editor extends Item { systemEditor: this.systemEditor }; - _.merge(params, super.structureParams()); - return params; + var superParams = super.structureParams(); + Object.assign(superParams, params); + return superParams; } referenceParams() { diff --git a/app/assets/javascripts/app/models/app/extension.js b/app/assets/javascripts/app/models/app/extension.js index f990432a5..7df86b99f 100644 --- a/app/assets/javascripts/app/models/app/extension.js +++ b/app/assets/javascripts/app/models/app/extension.js @@ -54,8 +54,9 @@ class Extension extends Component { supported_types: this.supported_types }; - _.merge(params, super.structureParams()); - return params; + var superParams = super.structureParams(); + Object.assign(superParams, params); + return superParams; } } diff --git a/app/assets/javascripts/app/models/app/note.js b/app/assets/javascripts/app/models/app/note.js index e2b981575..9070070aa 100644 --- a/app/assets/javascripts/app/models/app/note.js +++ b/app/assets/javascripts/app/models/app/note.js @@ -35,8 +35,9 @@ class Note extends Item { text: this.text }; - _.merge(params, super.structureParams()); - return params; + var superParams = super.structureParams(); + Object.assign(superParams, params); + return superParams; } addItemAsRelationship(item) { @@ -71,7 +72,7 @@ class Note extends Item { removeReferencesNotPresentIn(references) { this.savedTagsString = null; - + super.removeReferencesNotPresentIn(references); var uuids = references.map(function(ref){return ref.uuid}); diff --git a/app/assets/javascripts/app/models/app/tag.js b/app/assets/javascripts/app/models/app/tag.js index b83585234..04bc11882 100644 --- a/app/assets/javascripts/app/models/app/tag.js +++ b/app/assets/javascripts/app/models/app/tag.js @@ -26,8 +26,9 @@ class Tag extends Item { title: this.title }; - _.merge(params, super.structureParams()); - return params; + var superParams = super.structureParams(); + Object.assign(superParams, params); + return superParams; } addItemAsRelationship(item) { diff --git a/app/assets/javascripts/app/models/app/theme.js b/app/assets/javascripts/app/models/app/theme.js index 7d03cb2c3..0b8fe2c1f 100644 --- a/app/assets/javascripts/app/models/app/theme.js +++ b/app/assets/javascripts/app/models/app/theme.js @@ -2,7 +2,6 @@ class Theme extends Component { constructor(json_obj) { super(json_obj); - this.area = "themes"; } diff --git a/app/assets/javascripts/app/services/modelManager.js b/app/assets/javascripts/app/services/modelManager.js index 8bbb94399..1a1d03610 100644 --- a/app/assets/javascripts/app/services/modelManager.js +++ b/app/assets/javascripts/app/services/modelManager.js @@ -26,10 +26,6 @@ class ModelManager { this.itemsPendingRemoval = []; this.items = []; this._extensions = []; - this.acceptableContentTypes = [ - "Note", "Tag", "Extension", "SN|Editor", "SN|Theme", - "SN|Component", "SF|Extension", "SN|UserPreferences", "SF|MFA" - ]; } resetLocalMemory() { @@ -66,7 +62,6 @@ class ModelManager { this.informModelsOfUUIDChangeForItem(newItem, item.uuid, newItem.uuid); - console.log(item.uuid, "-->", newItem.uuid); var block = () => { @@ -162,16 +157,15 @@ class ModelManager { } let contentType = json_obj["content_type"] || (item && item.content_type); - var unknownContentType = !_.includes(this.acceptableContentTypes, contentType); var isDirtyItemPendingDelete = false; - if(json_obj.deleted == true || unknownContentType) { + if(json_obj.deleted == true) { if(json_obj.deleted && json_obj.dirty) { // Item was marked as deleted but not yet synced // We need to create this item as usual, but just not add it to individual arrays // i.e add to this.items but not this.notes (so that it can be retrieved with getDirtyItems) isDirtyItemPendingDelete = true; } else { - if(item && !unknownContentType) { + if(item) { modelsToNotifyObserversOf.push(item); this.removeItemLocally(item); } @@ -418,9 +412,7 @@ class ModelManager { /* Used when changing encryption key */ setAllItemsDirty(dontUpdateClientDates = true) { - var relevantItems = this.allItems.filter(function(item){ - return _.includes(this.acceptableContentTypes, item.content_type); - }.bind(this)); + var relevantItems = this.allItems; for(var item of relevantItems) { item.setDirty(true, dontUpdateClientDates);