summaryrefslogtreecommitdiff
path: root/client/js/models/tag.js
blob: eb24ccd5c9cf02b9816f2a08081f44ee43ae2fb7 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"use strict";

const api = require("../api.js");
const uri = require("../util/uri.js");
const events = require("../events.js");
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({});
    }

    get names() {
        return this._names;
    }

    get category() {
        return this._category;
    }

    get description() {
        return this._description;
    }

    get suggestions() {
        return this._suggestions;
    }

    get implications() {
        return this._implications;
    }

    get postCount() {
        return this._postCount;
    }

    get creationTime() {
        return this._creationTime;
    }

    get lastEditTime() {
        return this._lastEditTime;
    }

    set names(value) {
        this._names = value;
    }

    set category(value) {
        this._category = value;
    }

    set description(value) {
        this._description = value;
    }

    get metric() {
        return this._metric;
    }

    set metric(value) {
        this._metric = value;
    }

    static fromResponse(response) {
        const ret = new Tag();
        ret._updateFromResponse(response);
        return ret;
    }

    static get(name) {
        return api.get(uri.formatApiLink("tag", name)).then((response) => {
            return Promise.resolve(Tag.fromResponse(response));
        });
    }

    save() {
        const detail = { version: this._version };

        // send only changed fields to avoid user privilege violation
        if (misc.arraysDiffer(this._names, this._orig._names, true)) {
            detail.names = this._names;
        }
        if (this._category !== this._orig._category) {
            detail.category = this._category;
        }
        if (this._description !== this._orig._description) {
            detail.description = this._description;
        }
        if (misc.arraysDiffer(this._implications, this._orig._implications)) {
            detail.implications = this._implications.map(
                (relation) => relation.names[0]
            );
        }
        if (misc.arraysDiffer(this._suggestions, this._orig._suggestions)) {
            detail.suggestions = this._suggestions.map(
                (relation) => relation.names[0]
            );
        }
        if (this._metric !== this._orig._metric) {
            detail.metric = {
                min: this._metric.min,
                max: this._metric.max
            };
        }

        let promise = this._origName
            ? api.put(uri.formatApiLink("tag", this._origName), detail)
            : api.post(uri.formatApiLink("tags"), detail);
        return promise.then((response) => {
            this._updateFromResponse(response);
            this.dispatchEvent(
                new CustomEvent("change", {
                    detail: {
                        tag: this,
                    },
                })
            );
            return Promise.resolve();
        });
    }

    merge(targetName, addAlias) {
        return api
            .get(uri.formatApiLink("tag", targetName))
            .then((response) => {
                return api.post(uri.formatApiLink("tag-merge"), {
                    removeVersion: this._version,
                    remove: this._origName,
                    mergeToVersion: response.version,
                    mergeTo: targetName,
                });
            })
            .then((response) => {
                if (!addAlias) {
                    return Promise.resolve(response);
                }
                return api.put(uri.formatApiLink("tag", targetName), {
                    version: response.version,
                    names: response.names.concat(this._names),
                });
            })
            .then((response) => {
                this._updateFromResponse(response);
                this.dispatchEvent(
                    new CustomEvent("change", {
                        detail: {
                            tag: this,
                        },
                    })
                );
                return Promise.resolve();
            });
    }

    delete() {
        return api
            .delete(uri.formatApiLink("tag", this._origName), {
                version: this._version,
            })
            .then((response) => {
                this.dispatchEvent(
                    new CustomEvent("delete", {
                        detail: {
                            tag: this,
                        },
                    })
                );
                return Promise.resolve();
            });
    }

    deleteMetric() {
        return api.delete(
            uri.formatApiLink("metric", this._origName),
            {version: this.metric.version})
            .then((response) => {
                this.dispatchEvent(
                    new CustomEvent("delete", {
                        detail: {
                            metric: this.metric,
                        },
                    })
                );
                return Promise.resolve();
            });
    }

    _updateFromResponse(response) {
        const map = {
            _version: response.version,
            _origName: response.names ? response.names[0] : null,
            _names: response.names || [],
            _category: response.category,
            _description: response.description,
            _creationTime: response.creationTime,
            _lastEditTime: response.lastEditTime,
            _postCount: response.usages || 0,
            _metric: response.metric,
        };

        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);
    }
}

module.exports = Tag;