model manager refactor

This commit is contained in:
Mo Bitar
2017-01-05 00:55:23 -06:00
parent b494a4da4a
commit 4a35e78765
16 changed files with 775 additions and 685 deletions

View File

@@ -1,54 +1,12 @@
class Item {
constructor(json_obj) {
var content;
Object.defineProperty(this, "content", {
get: function() {
return content;
},
set: function(value) {
var finalValue = value;
if(typeof value === 'string') {
try {
var decodedValue = JSON.parse(value);
finalValue = decodedValue;
}
catch(e) {
finalValue = value;
}
}
content = finalValue;
},
enumerable: true,
});
_.merge(this, json_obj);
if(this.created_at) {
this.created_at = new Date(this.created_at);
this.updated_at = new Date(this.updated_at);
} else {
this.created_at = new Date();
this.updated_at = new Date();
}
this.updateFromJSON(json_obj);
if(!this.uuid) {
this.uuid = Neeto.crypto.generateUUID();
}
this.setContentRaw = function(rawContent) {
content = rawContent;
}
if(!this.content) {
this.content = {};
}
if(!this.content.references) {
this.content.references = [];
}
}
static sortItemsByDate(items) {
@@ -57,31 +15,67 @@ class Item {
});
}
addReference(reference) {
this.content.references.push(reference);
this.content.references = _.uniq(this.content.references);
this.updateReferencesLocalMapping();
get contentObject() {
// console.log("getting content object from content", this.content);
if(!this.content) {
return {};
}
if(this.content !== null && typeof this.content === 'object') {
// this is the case when mapping localStorage content, in which case the content is already parsed
return this.content;
}
return JSON.parse(this.content);
}
removeReference(reference) {
_.remove(this.content.references, _.find(this.content.references, {uuid: reference.uuid}));
this.updateReferencesLocalMapping();
updateFromJSON(json) {
_.merge(this, json);
if(this.created_at) {
this.created_at = new Date(this.created_at);
this.updated_at = new Date(this.updated_at);
} else {
this.created_at = new Date();
this.updated_at = new Date();
}
if(json.content) {
this.mapContentToLocalProperties(this.contentObject);
}
}
referencesMatchingContentType(contentType) {
return this.content.references.filter(function(reference){
return reference.content_type == contentType;
});
mapContentToLocalProperties(contentObj) {
}
createContentJSONFromProperties() {
return this.structureParams();
}
referenceParams() {
// must override
}
structureParams() {
return {references: this.referenceParams()}
}
addItemAsRelationship(item) {
// must override
}
removeItemAsRelationship(item) {
// must override
}
removeFromRelationships() {
// must override
}
mergeMetadataFromItem(item) {
_.merge(this, _.omit(item, ["content"]));
}
updateReferencesLocalMapping() {
// should be overriden to manage local properties
}
referencesAffectedBySharingChange() {
// should be overriden to determine which references should be decrypted/encrypted
return null;
@@ -92,7 +86,7 @@ class Item {
}
isEncrypted() {
return this.encryptionEnabled() && typeof this.content === 'string' ? true : false;
return this.encryptionEnabled() && this.content.substring(0, 3) === '001' ? true : false;
}
encryptionEnabled() {

View File

@@ -1,5 +1,6 @@
class Extension extends Item {
constructor(json) {
super(json);
_.merge(this, json);
this.actions = this.actions.map(function(action){

View File

@@ -6,14 +6,52 @@ class Note extends Item {
if(!this.tags) {
this.tags = [];
}
}
if(!this.content.title) {
this.content.title = "";
}
mapContentToLocalProperties(contentObject) {
super.mapContentToLocalProperties(contentObject)
this.title = contentObject.title;
this.text = contentObject.text;
}
if(!this.content.text) {
this.content.text = "";
referenceParams() {
var references = _.map(this.tags, function(tag){
return {uuid: tag.uuid, content_type: tag.content_type};
})
return references;
}
structureParams() {
var params = {
title: this.title,
text: this.text
};
_.merge(params, super.structureParams());
return params;
}
addItemAsRelationship(item) {
if(item.content_type == "Tag") {
if(!_.find(this.tags, item)) {
this.tags.push(item);
}
}
super.addItemAsRelationship(item);
}
removeItemAsRelationship(item) {
if(item.content_type == "Tag") {
_.pull(this.tags, item);
}
super.removeItemAsRelationship(item);
}
removeFromRelationships() {
this.tags.forEach(function(tag){
_.pull(tag.notes, this);
tag.dirty = true;
})
}
static filterDummyNotes(notes) {
@@ -21,11 +59,6 @@ class Note extends Item {
return filtered;
}
updateReferencesLocalMapping() {
super.updateReferencesLocalMapping();
this.tags = this.referencesMatchingContentType("Tag");
}
referencesAffectedBySharingChange() {
return super.referencesAffectedBySharingChange();
}
@@ -39,6 +72,14 @@ class Note extends Item {
return false;
}
safeText() {
return this.text || "";
}
safeTitle() {
return this.title || "";
}
toJSON() {
return {uuid: this.uuid}
}

View File

@@ -6,22 +6,57 @@ class Tag extends Item {
if(!this.notes) {
this.notes = [];
}
}
if(!this.content.title) {
this.content.title = "";
mapContentToLocalProperties(contentObject) {
super.mapContentToLocalProperties(contentObject)
this.title = contentObject.title;
}
referenceParams() {
var references = _.map(this.notes, function(note){
return {uuid: note.uuid, content_type: note.content_type};
})
return references;
}
structureParams() {
var params = {
title: this.title
};
_.merge(params, super.structureParams());
return params;
}
addItemAsRelationship(item) {
if(item.content_type == "Note") {
if(!_.find(this.notes, item)) {
this.notes.unshift(item);
}
}
super.addItemAsRelationship(item);
}
removeItemAsRelationship(item) {
if(item.content_type == "Note") {
_.pull(this.notes, item);
}
super.removeItemAsRelationship(item);
}
removeFromRelationships() {
this.notes.forEach(function(note){
_.pull(note.tags, this);
note.dirty = true;
})
}
get content_type() {
return "Tag";
}
updateReferencesLocalMapping() {
super.updateReferencesLocalMapping();
this.notes = this.referencesMatchingContentType("Note");
}
referencesAffectedBySharingChange() {
return this.referencesMatchingContentType("Note");
return this.notes;
}
}