sn-models package

This commit is contained in:
Mo Bitar
2018-06-30 10:13:55 -05:00
parent b4902f03c5
commit 71a3c68d39
19 changed files with 4422 additions and 594 deletions

View File

@@ -1,28 +0,0 @@
class Mfa extends SFItem {
constructor(json_obj) {
super(json_obj);
}
// mapContentToLocalProperties(content) {
// super.mapContentToLocalProperties(content)
// this.serverContent = content;
// }
//
// structureParams() {
// return _.merge(this.serverContent, super.structureParams());
// }
toJSON() {
return {uuid: this.uuid}
}
get content_type() {
return "SF|MFA";
}
doNotEncrypt() {
return true;
}
}

View File

@@ -1,38 +0,0 @@
class ServerExtension extends SFItem {
constructor(json_obj) {
super(json_obj);
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
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;
// }
toJSON() {
return {uuid: this.uuid}
}
get content_type() {
return "SF|Extension";
}
doNotEncrypt() {
return true;
}
}

View File

@@ -1,147 +0,0 @@
class Component extends SFItem {
constructor(json_obj) {
// If making a copy of an existing component (usually during sign in if you have a component active in the session),
// which may have window set, you may get a cross-origin exception since you'll be trying to copy the window. So we clear it here.
json_obj.window = null;
super(json_obj);
if(!this.componentData) {
this.componentData = {};
}
if(!this.disassociatedItemIds) {
this.disassociatedItemIds = [];
}
if(!this.associatedItemIds) {
this.associatedItemIds = [];
}
}
mapContentToLocalProperties(content) {
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;
this.offlineOnly = content.offlineOnly;
if(content.valid_until) {
this.valid_until = new Date(content.valid_until);
}
this.name = content.name;
this.autoupdateDisabled = content.autoupdateDisabled;
this.package_info = content.package_info;
// the location in the view this component is located in. Valid values are currently tags-list, note-tags, and editor-stack`
this.area = content.area;
this.permissions = content.permissions;
if(!this.permissions) {
this.permissions = [];
}
this.active = content.active;
// custom data that a component can store in itself
this.componentData = content.componentData || {};
// items that have requested a component to be disabled in its context
this.disassociatedItemIds = content.disassociatedItemIds || [];
// items that have requested a component to be enabled in its context
this.associatedItemIds = content.associatedItemIds || [];
}
handleDeletedContent() {
super.handleDeletedContent();
this.active = false;
}
structureParams() {
var params = {
url: this.url,
hosted_url: this.hosted_url,
local_url: this.local_url,
valid_until: this.valid_until,
offlineOnly: this.offlineOnly,
name: this.name,
area: this.area,
package_info: this.package_info,
permissions: this.permissions,
active: this.active,
autoupdateDisabled: this.autoupdateDisabled,
componentData: this.componentData,
disassociatedItemIds: this.disassociatedItemIds,
associatedItemIds: this.associatedItemIds,
};
var superParams = super.structureParams();
Object.assign(superParams, params);
return superParams;
}
toJSON() {
return {uuid: this.uuid}
}
get content_type() {
return "SN|Component";
}
isEditor() {
return this.area == "editor-editor";
}
isTheme() {
return this.content_type == "SN|Theme" || this.area == "themes";
}
isDefaultEditor() {
return this.getAppDataItem("defaultEditor") == true;
}
setLastSize(size) {
this.setAppDataItem("lastSize", size);
}
getLastSize() {
return this.getAppDataItem("lastSize");
}
keysToIgnoreWhenCheckingContentEquality() {
return ["active"].concat(super.keysToIgnoreWhenCheckingContentEquality());
}
/*
An associative component depends on being explicitly activated for a given item, compared to a dissaciative component,
which is enabled by default in areas unrelated to a certain item.
*/
static associativeAreas() {
return ["editor-editor"];
}
isAssociative() {
return Component.associativeAreas().includes(this.area);
}
associateWithItem(item) {
this.associatedItemIds.push(item.uuid);
}
isExplicitlyEnabledForItem(item) {
return this.associatedItemIds.indexOf(item.uuid) !== -1;
}
isExplicitlyDisabledForItem(item) {
return this.disassociatedItemIds.indexOf(item.uuid) !== -1;
}
}

View File

@@ -1,103 +0,0 @@
class Editor extends SFItem {
constructor(json_obj) {
super(json_obj);
if(!this.notes) {
this.notes = [];
}
if(!this.data) {
this.data = {};
}
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
this.url = content.url;
this.name = content.name;
this.data = content.data || {};
this.default = content.default;
this.systemEditor = content.systemEditor;
}
structureParams() {
var params = {
url: this.url,
name: this.name,
data: this.data,
default: this.default,
systemEditor: this.systemEditor
};
var superParams = super.structureParams();
Object.assign(superParams, params);
return superParams;
}
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);
}
removeAndDirtyAllRelationships() {
super.removeAndDirtyAllRelationships();
this.notes = [];
}
removeReferencesNotPresentIn(references) {
super.removeReferencesNotPresentIn(references);
var uuids = references.map(function(ref){return ref.uuid});
this.notes.forEach(function(note){
if(!uuids.includes(note.uuid)) {
_.remove(this.notes, {uuid: note.uuid});
}
}.bind(this))
}
potentialItemOfInterestHasChangedItsUUID(newItem, oldUUID, newUUID) {
if(newItem.content_type === "Note" && _.find(this.notes, {uuid: oldUUID})) {
_.remove(this.notes, {uuid: oldUUID});
this.notes.push(newItem);
}
}
toJSON() {
return {uuid: this.uuid}
}
get content_type() {
return "SN|Editor";
}
setData(key, value) {
var dataHasChanged = JSON.stringify(this.data[key]) !== JSON.stringify(value);
if(dataHasChanged) {
this.data[key] = value;
return true;
}
return false;
}
dataForKey(key) {
return this.data[key] || {};
}
}

View File

@@ -1,62 +0,0 @@
class Action {
constructor(json) {
_.merge(this, json);
this.running = false; // in case running=true was synced with server since model is uploaded nondiscriminatory
this.error = false;
if(this.lastExecuted) {
// is string
this.lastExecuted = new Date(this.lastExecuted);
}
}
}
class Extension extends Component {
constructor(json) {
super(json);
if(json.actions) {
this.actions = json.actions.map(function(action){
return new Action(action);
})
}
if(!this.actions) {
this.actions = [];
}
}
actionsWithContextForItem(item) {
return this.actions.filter(function(action){
return action.context == item.content_type || action.context == "Item";
})
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
this.description = content.description;
this.supported_types = content.supported_types;
if(content.actions) {
this.actions = content.actions.map(function(action){
return new Action(action);
})
}
}
get content_type() {
return "Extension";
}
structureParams() {
var params = {
description: this.description,
actions: this.actions.map((a) => {return _.omit(a, ["subrows", "subactions"])}),
supported_types: this.supported_types
};
var superParams = super.structureParams();
Object.assign(superParams, params);
return superParams;
}
}

View File

@@ -1,107 +0,0 @@
export class Note extends SFItem {
constructor(json_obj) {
super(json_obj);
if(!this.text) {
// Some external editors can't handle a null value for text.
// Notes created on mobile with no text have a null value for it,
// so we'll just set a default here.
this.text = "";
}
if(!this.tags) {
this.tags = [];
}
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
this.title = content.title;
this.text = content.text;
}
structureParams() {
var params = {
title: this.title,
text: this.text
};
var superParams = super.structureParams();
Object.assign(superParams, params);
return superParams;
}
addItemAsRelationship(item) {
this.savedTagsString = null;
if(item.content_type == "Tag") {
if(!_.find(this.tags, {uuid: item.uuid})) {
this.tags.push(item);
item.notes.push(this);
}
}
super.addItemAsRelationship(item);
}
removeItemAsRelationship(item) {
this.savedTagsString = null;
if(item.content_type == "Tag") {
_.remove(this.tags, {uuid: item.uuid});
_.remove(item.notes, {uuid: this.uuid});
}
super.removeItemAsRelationship(item);
}
updateLocalRelationships() {
this.savedTagsString = null;
var references = this.content.references;
var uuids = references.map(function(ref){return ref.uuid});
this.tags.slice().forEach(function(tag){
if(!uuids.includes(tag.uuid)) {
_.remove(tag.notes, {uuid: this.uuid});
_.remove(this.tags, {uuid: tag.uuid});
}
}.bind(this))
}
isBeingRemovedLocally() {
this.tags.forEach(function(tag){
_.remove(tag.notes, {uuid: this.uuid});
}.bind(this))
super.isBeingRemovedLocally();
}
static filterDummyNotes(notes) {
var filtered = notes.filter(function(note){return note.dummy == false || note.dummy == null});
return filtered;
}
informReferencesOfUUIDChange(oldUUID, newUUID) {
super.informReferencesOfUUIDChange();
for(var tag of this.tags) {
_.remove(tag.notes, {uuid: oldUUID});
tag.notes.push(this);
}
}
safeText() {
return this.text || "";
}
safeTitle() {
return this.title || "";
}
toJSON() {
return {uuid: this.uuid}
}
tagsString() {
this.savedTagsString = Tag.arrayToDisplayString(this.tags);
return this.savedTagsString;
}
}

View File

@@ -1,45 +0,0 @@
export class Tag extends SFItem {
constructor(json_obj) {
super(json_obj);
if(!this.notes) {
this.notes = [];
}
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
this.title = content.title;
}
structureParams() {
var params = {
title: this.title
};
var superParams = super.structureParams();
Object.assign(superParams, params);
return superParams;
}
isBeingRemovedLocally() {
this.notes.forEach(function(note){
_.remove(note.tags, {uuid: this.uuid});
}.bind(this))
super.isBeingRemovedLocally();
}
informReferencesOfUUIDChange(oldUUID, newUUID) {
for(var note of this.notes) {
_.remove(note.tags, {uuid: oldUUID});
note.tags.push(this);
}
}
static arrayToDisplayString(tags) {
return tags.sort((a, b) => {return a.title > b.title}).map(function(tag, i){
return "#" + tag.title;
}).join(" ");
}
}

View File

@@ -1,11 +0,0 @@
class Theme extends Component {
constructor(json_obj) {
super(json_obj);
this.area = "themes";
}
get content_type() {
return "SN|Theme";
}
}

View File

@@ -1,28 +0,0 @@
class EncryptedStorage extends SFItem {
constructor(json_obj) {
super(json_obj);
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
this.storage = content.storage;
}
structureParams() {
var params = {
storage: this.storage,
};
_.merge(params, super.structureParams());
return params;
}
toJSON() {
return {uuid: this.uuid}
}
get content_type() {
return "SN|EncryptedStorage";
}
}

View File

@@ -33,6 +33,7 @@ class ModelManager extends SFModelManager {
var tag = _.find(this.tags, {title: title})
if(!tag) {
tag = this.createItem({content_type: "Tag", content: {title: title}});
tag.setDirty(true);
this.addItem(tag);
}
return tag;