aboutsummaryrefslogtreecommitdiff
path: root/client/js/models
diff options
context:
space:
mode:
authorrr-2017-10-01 21:46:53 +0200
committerrr-2017-10-01 21:48:00 +0200
commit1c4c5c5f91dbce7234796daddbb74923638659e7 (patch)
tree1e621ec97c8455613303df066e4d74612f6f1dd0 /client/js/models
parent253e28c1b5e6757057c0dd36a11fd5063e07ff79 (diff)
remove tags.json
Diffstat (limited to 'client/js/models')
-rw-r--r--client/js/models/abstract_list.js11
-rw-r--r--client/js/models/post.js46
-rw-r--r--client/js/models/tag.js25
-rw-r--r--client/js/models/tag_list.js42
4 files changed, 79 insertions, 45 deletions
diff --git a/client/js/models/abstract_list.js b/client/js/models/abstract_list.js
index 6544ed2..10edd61 100644
--- a/client/js/models/abstract_list.js
+++ b/client/js/models/abstract_list.js
@@ -27,6 +27,13 @@ class AbstractList extends events.EventTarget {
return ret;
}
+ sync(plainList) {
+ this.clear();
+ for (let item of (plainList || [])) {
+ this.add(this.constructor._itemClass.fromResponse(item));
+ }
+ }
+
add(item) {
if (item.addEventListener) {
item.addEventListener('delete', e => {
@@ -75,6 +82,10 @@ class AbstractList extends events.EventTarget {
return this._list[index];
}
+ map(...args) {
+ return this._list.map(...args);
+ }
+
[Symbol.iterator]() {
return this._list[Symbol.iterator]();
}
diff --git a/client/js/models/post.js b/client/js/models/post.js
index 71f3eb9..2b63bd6 100644
--- a/client/js/models/post.js
+++ b/client/js/models/post.js
@@ -4,23 +4,18 @@ const api = require('../api.js');
const uri = require('../util/uri.js');
const tags = require('../tags.js');
const events = require('../events.js');
+const TagList = require('./tag_list.js');
const NoteList = require('./note_list.js');
const CommentList = require('./comment_list.js');
const misc = require('../util/misc.js');
-function _syncObservableCollection(target, plainList) {
- target.clear();
- for (let item of (plainList || [])) {
- target.add(target.constructor._itemClass.fromResponse(item));
- }
-}
-
class Post extends events.EventTarget {
constructor() {
super();
this._orig = {};
for (let obj of [this, this._orig]) {
+ obj._tags = new TagList();
obj._notes = new NoteList();
obj._comments = new CommentList();
}
@@ -56,7 +51,6 @@ class Post extends events.EventTarget {
get hasCustomThumbnail() { return this._hasCustomThumbnail; }
set flags(value) { this._flags = value; }
- set tags(value) { this._tags = value; }
set safety(value) { this._safety = value; }
set relations(value) { this._relations = value; }
set newContent(value) { this._newContent = value; }
@@ -94,29 +88,6 @@ class Post extends events.EventTarget {
});
}
- isTaggedWith(tagName) {
- return this._tags
- .map(s => s.toLowerCase())
- .includes(tagName.toLowerCase());
- }
-
- addTag(tagName, addImplications) {
- if (this.isTaggedWith(tagName)) {
- return;
- }
- this._tags.push(tagName);
- if (addImplications !== false) {
- for (let otherTag of tags.getAllImplications(tagName)) {
- this.addTag(otherTag, addImplications);
- }
- }
- }
-
- removeTag(tagName) {
- this._tags = this._tags.filter(
- s => s.toLowerCase() != tagName.toLowerCase());
- }
-
save(anonymous) {
const files = {};
const detail = {version: this._version};
@@ -132,15 +103,14 @@ class Post extends events.EventTarget {
detail.flags = this._flags;
}
if (misc.arraysDiffer(this._tags, this._orig._tags)) {
- detail.tags = this._tags;
+ detail.tags = this._tags.map(tag => tag.names[0]);
}
if (misc.arraysDiffer(this._relations, this._orig._relations)) {
detail.relations = this._relations;
}
if (misc.arraysDiffer(this._notes, this._orig._notes)) {
- detail.notes = [...this._notes].map(note => ({
- polygon: [...note.polygon].map(
- point => [point.x, point.y]),
+ detail.notes = this._notes.map(note => ({
+ polygon: note.polygon.map(point => [point.x, point.y]),
text: note.text,
}));
}
@@ -310,7 +280,6 @@ class Post extends events.EventTarget {
_fileSize: response.fileSize,
_flags: [...response.flags || []],
- _tags: [...response.tags || []],
_relations: [...response.relations || []],
_score: response.score,
@@ -322,8 +291,9 @@ class Post extends events.EventTarget {
});
for (let obj of [this, this._orig]) {
- _syncObservableCollection(obj._notes, response.notes);
- _syncObservableCollection(obj._comments, response.comments);
+ obj._tags.sync(response.tags);
+ obj._notes.sync(response.notes);
+ obj._comments.sync(response.comments);
}
Object.assign(this, map());
diff --git a/client/js/models/tag.js b/client/js/models/tag.js
index ccfe6b2..c743554 100644
--- a/client/js/models/tag.js
+++ b/client/js/models/tag.js
@@ -7,8 +7,16 @@ const misc = require('../util/misc.js');
class Tag extends events.EventTarget {
constructor() {
+ const TagList = require('./tag_list.js');
+
super();
this._orig = {};
+
+ for (let obj of [this, this._orig]) {
+ obj._suggestions = new TagList();
+ obj._implications = new TagList();
+ }
+
this._updateFromResponse({});
}
@@ -24,8 +32,6 @@ class Tag extends events.EventTarget {
set names(value) { this._names = value; }
set category(value) { this._category = value; }
set description(value) { this._description = value; }
- set implications(value) { this._implications = value; }
- set suggestions(value) { this._suggestions = value; }
static fromResponse(response) {
const ret = new Tag();
@@ -54,10 +60,12 @@ class Tag extends events.EventTarget {
detail.description = this._description;
}
if (misc.arraysDiffer(this._implications, this._orig._implications)) {
- detail.implications = this._implications;
+ detail.implications = this._implications.map(
+ relation => relation.names[0]);
}
if (misc.arraysDiffer(this._suggestions, this._orig._suggestions)) {
- detail.suggestions = this._suggestions;
+ detail.suggestions = this._suggestions.map(
+ relation => relation.names[0]);
}
let promise = this._origName ?
@@ -124,13 +132,16 @@ class Tag extends events.EventTarget {
_names: response.names,
_category: response.category,
_description: response.description,
- _implications: response.implications,
- _suggestions: response.suggestions,
_creationTime: response.creationTime,
_lastEditTime: response.lastEditTime,
- _postCount: response.usages,
+ _postCount: response.usages || 0,
};
+ for (let obj of [this, this._orig]) {
+ obj._suggestions.sync(response.suggestions);
+ obj._implications.sync(response.implications);
+ }
+
Object.assign(this, map);
Object.assign(this._orig, map);
}
diff --git a/client/js/models/tag_list.js b/client/js/models/tag_list.js
index 282d4ef..d87b694 100644
--- a/client/js/models/tag_list.js
+++ b/client/js/models/tag_list.js
@@ -22,6 +22,48 @@ class TagList extends AbstractList {
{results: TagList.fromResponse(response.results)}));
});
}
+
+ isTaggedWith(testName) {
+ for (let tag of this._list) {
+ for (let tagName of tag.names) {
+ if (tagName.toLowerCase() === testName.toLowerCase()) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ addByName(tagName, addImplications) {
+ if (this.isTaggedWith(tagName)) {
+ return Promise.resolve();
+ }
+
+ const tag = new Tag();
+ tag.names = [tagName];
+
+ this.add(tag);
+
+ if (addImplications !== false) {
+ return Tag.get(tagName).then(actualTag => {
+ return Promise.all(
+ actualTag.implications.map(
+ relation => this.addByName(relation.names[0], true)));
+ });
+ }
+
+ return Promise.resolve();
+ }
+
+ removeByName(testName) {
+ for (let tag of this._list) {
+ for (let tagName of tag.names) {
+ if (tagName.toLowerCase() === testName.toLowerCase()) {
+ this.remove(tag);
+ }
+ }
+ }
+ }
}
TagList._itemClass = Tag;

© 2015 - 2026 Jakob L. Kreuze