blob: 2f2732dc353ac3e41d867155d402637022f4c0f9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
"use strict";
const misc = require("./util/misc.js");
const TagCategoryList = require("./models/tag_category_list.js");
const Tag = require("./models/tag.js");
let _stylesheet = null;
function refreshCategoryColorMap() {
return TagCategoryList.get().then((response) => {
if (_stylesheet) {
document.head.removeChild(_stylesheet);
}
_stylesheet = document.createElement("style");
document.head.appendChild(_stylesheet);
for (let category of response.results) {
const ruleName = misc.makeCssName(category.name, "tag");
_stylesheet.sheet.insertRule(
`.${ruleName} { color: ${category.color}; border-color: ${category.color} }`,
_stylesheet.sheet.cssRules.length
);
_stylesheet.sheet.insertRule(
`.${ruleName}.selected { color: white; background-color: ${category.color} }`,
_stylesheet.sheet.cssRules.length
);
}
});
}
function parseTagAndCategory(text) {
let nameAndCat = text.split(":");
if (nameAndCat.length > 1) {
// "cat:my:tag" should parse to category "cat" and tag "my:tag"
let category = nameAndCat.shift();
let name = nameAndCat.join(":");
return {name: name, category: category};
} else {
return {name: text, category: null};
}
}
function resolveTagAndCategory(text) {
let tagData = parseTagAndCategory(text);
return _createTagByCategoryAndName(tagData.category, tagData.name);
}
function _createTagByCategoryAndName(category, name) {
category = category ? category.trim() : "default";
name = name.trim();
if (!name) {
return Promise.reject(new Error("Empty tag name"));
}
// if tag with this name already exists, existing category will be used
return Tag.get(name).then((tag) => {
return Promise.resolve(tag);
}, () => {
const tag = new Tag();
tag.names = [name];
tag.category = category;
return tag.save().then(() => Promise.resolve(tag));
});
}
module.exports = {
refreshCategoryColorMap: refreshCategoryColorMap,
parseTagAndCategory: parseTagAndCategory,
resolveTagAndCategory: resolveTagAndCategory,
};
|