es6, itemManager
This commit is contained in:
@@ -1,3 +1,13 @@
|
||||
var Group = function (json_obj) {
|
||||
_.merge(this, json_obj);
|
||||
};
|
||||
class Group extends Item {
|
||||
constructor(json_obj) {
|
||||
_.merge(this, json_obj);
|
||||
}
|
||||
|
||||
updateReferencesLocalMapping() {
|
||||
super.updateReferencesLocalMapping();
|
||||
this.notes = this.referencesMatchingContentType("Note");
|
||||
this.notes.sort(function(a,b){
|
||||
return new Date(b.created_at) - new Date(a.created_at);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
29
app/assets/javascripts/app/frontend/models/item.js
Normal file
29
app/assets/javascripts/app/frontend/models/item.js
Normal file
@@ -0,0 +1,29 @@
|
||||
class Item {
|
||||
|
||||
referencesMatchingContentType(contentType) {
|
||||
return this.references.filter(function(reference){
|
||||
return reference.content_type == content_type;
|
||||
});
|
||||
}
|
||||
|
||||
updateReferencesLocalMapping() {
|
||||
// should be overriden to manage local properties
|
||||
}
|
||||
|
||||
/* Returns true if note is shared individually or via group */
|
||||
isPublic() {
|
||||
return this.presentation;
|
||||
}
|
||||
|
||||
isEncrypted() {
|
||||
return this.encryptionEnabled() && typeof this.content === 'string' ? true : false;
|
||||
}
|
||||
|
||||
encryptionEnabled() {
|
||||
return this.loc_eek;
|
||||
}
|
||||
|
||||
presentationURL() {
|
||||
return this.presentation.url;
|
||||
}
|
||||
}
|
||||
55
app/assets/javascripts/app/frontend/models/itemManager.js
Normal file
55
app/assets/javascripts/app/frontend/models/itemManager.js
Normal file
@@ -0,0 +1,55 @@
|
||||
class ItemManager() {
|
||||
|
||||
set items(items) {
|
||||
this.items = items;
|
||||
resolveReferences();
|
||||
}
|
||||
|
||||
referencesForItemId(itemId) {
|
||||
return _.find(this.items, {uuid: itemId});
|
||||
}
|
||||
|
||||
resolveReferences() {
|
||||
this.items.forEach(function(item){
|
||||
// build out references
|
||||
_.map(item.references, function(reference){
|
||||
return referencesForItemId(reference.uuid);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
itemsForContentType(contentType) {
|
||||
this.items.filter(function(item){
|
||||
return item.content_type == contentType;
|
||||
});
|
||||
}
|
||||
|
||||
// returns dirty item references that need saving
|
||||
deleteItem(item) {
|
||||
_.remove(this.items, item);
|
||||
item.references.forEach(function(reference){
|
||||
removeReferencesFromItem(reference, item);
|
||||
})
|
||||
|
||||
return item.references;
|
||||
}
|
||||
|
||||
removeReferencesBetweenItems(itemOne, itemTwo) {
|
||||
_.remove(itemOne.references, _.find(itemOne.references, {uuid: itemTwo.uuid}));
|
||||
_.remove(itemTwo.references, _.find(itemTwo.references, {uuid: itemOne.uuid}));
|
||||
return [itemOne, itemTwo];
|
||||
}
|
||||
|
||||
removeReferencesBetweenItems(itemOne, itemTwo) {
|
||||
itemOne.references.push(itemTwo);
|
||||
itemTwo.references.push(itemOne);
|
||||
return [itemOne, itemTwo];
|
||||
}
|
||||
|
||||
createReferencesBetweenItems(itemOne, itemTwo) {
|
||||
itemOne.references.push(itemTwo);
|
||||
itemTwo.references.push(itemOne);
|
||||
return [itemOne, itemTwo];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,57 +1,63 @@
|
||||
var Note = function (json_obj) {
|
||||
var content;
|
||||
class Note extends Item {
|
||||
constructor(json_obj) {
|
||||
var content;
|
||||
|
||||
Object.defineProperty(this, "content", {
|
||||
get: function() {
|
||||
return content;
|
||||
},
|
||||
set: function(value) {
|
||||
var finalValue = value;
|
||||
Object.defineProperty(this, "content", {
|
||||
get: function() {
|
||||
return content;
|
||||
},
|
||||
set: function(value) {
|
||||
var finalValue = value;
|
||||
|
||||
if(typeof value === 'string') {
|
||||
try {
|
||||
decodedValue = JSON.parse(value);
|
||||
finalValue = decodedValue;
|
||||
}
|
||||
catch(e) {
|
||||
finalValue = value;
|
||||
if(typeof value === 'string') {
|
||||
try {
|
||||
decodedValue = JSON.parse(value);
|
||||
finalValue = decodedValue;
|
||||
}
|
||||
catch(e) {
|
||||
finalValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
content = finalValue;
|
||||
},
|
||||
enumerable: true,
|
||||
});
|
||||
|
||||
this.setContentRaw = function(rawContent) {
|
||||
content = rawContent;
|
||||
}
|
||||
|
||||
_.merge(this, json_obj);
|
||||
|
||||
if(!this.content) {
|
||||
this.content = {title: "", text: ""};
|
||||
}
|
||||
}
|
||||
|
||||
filterDummyNotes(notes) {
|
||||
var filtered = notes.filter(function(note){return note.dummy == false || note.dummy == null});
|
||||
return filtered;
|
||||
}
|
||||
|
||||
updateReferencesLocalMapping() {
|
||||
super.updateReferencesLocalMapping();
|
||||
this.groups = this.referencesMatchingContentType("Group");
|
||||
}
|
||||
|
||||
get hasOnePublicGroup() {
|
||||
var hasPublicGroup = false;
|
||||
this.groups.forEach(function(group){
|
||||
if(group.isPublic()) {
|
||||
hasPublicGroup = true;
|
||||
return;
|
||||
}
|
||||
})
|
||||
|
||||
content = finalValue;
|
||||
},
|
||||
enumerable: true,
|
||||
});
|
||||
|
||||
this.setContentRaw = function(rawContent) {
|
||||
content = rawContent;
|
||||
return hasPublicGroup;
|
||||
}
|
||||
|
||||
_.merge(this, json_obj);
|
||||
|
||||
if(!this.content) {
|
||||
this.content = {title: "", text: ""};
|
||||
function isPublic() {
|
||||
return super.isPublic() || this.hasOnePublicGroup;
|
||||
}
|
||||
};
|
||||
|
||||
Note.filterDummyNotes = function(notes) {
|
||||
var filtered = notes.filter(function(note){return note.dummy == false || note.dummy == null});
|
||||
return filtered;
|
||||
}
|
||||
|
||||
/* Returns true if note is shared individually or via group */
|
||||
Note.prototype.isPublic = function() {
|
||||
return this.presentation || (this.group && this.group.presentation);
|
||||
};
|
||||
|
||||
Note.prototype.isEncrypted = function() {
|
||||
return this.encryptionEnabled() && typeof this.content === 'string' ? true : false;
|
||||
}
|
||||
|
||||
Note.prototype.encryptionEnabled = function() {
|
||||
return this.loc_eek;
|
||||
}
|
||||
|
||||
Note.prototype.presentationURL = function() {
|
||||
return this.presentation.url;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
var User = function (json_obj) {
|
||||
_.merge(this, json_obj);
|
||||
class User {
|
||||
constructor(json_obj) {
|
||||
_.merge(this, json_obj);
|
||||
|
||||
this.notes = _.map(this.notes, function(json_obj) {
|
||||
return new Note(json_obj);
|
||||
});
|
||||
this.itemManager = new ItemManager();
|
||||
this.itemManager.items = this.items;
|
||||
this.items = null;
|
||||
|
||||
this.groups = _.map(this.groups, function(json_obj) {
|
||||
return new Group(json_obj);
|
||||
});
|
||||
|
||||
this.groups.forEach(function(group){
|
||||
var notes = this.notes.filter(function(note){return note.group_id && note.group_id == group.id});
|
||||
notes.forEach(function(note){
|
||||
note.group = group;
|
||||
this.notes = _.map(this.itemManager.itemsForContentType("Note"), function(json_obj) {
|
||||
return new Note(json_obj);
|
||||
})
|
||||
group.notes = notes;
|
||||
}.bind(this))
|
||||
};
|
||||
|
||||
User.prototype.filteredNotes = function() {
|
||||
return Note.filterDummyNotes(this.notes);
|
||||
this.groups = _.map(this.itemManager.itemsForContentType("Group"), function(json_obj) {
|
||||
var group = Group(json_obj);
|
||||
group.updateReferencesLocalMapping();
|
||||
return group;
|
||||
})
|
||||
}
|
||||
|
||||
filteredNotes() {
|
||||
return Note.filterDummyNotes(this.notes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user