summaryrefslogtreecommitdiff
path: root/client/js/models/pool_category.js
blob: 8c7df46eb4db2910e619d652a2d00f0f986216f8 (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
"use strict";

const api = require("../api.js");
const uri = require("../util/uri.js");
const events = require("../events.js");

class PoolCategory extends events.EventTarget {
    constructor() {
        super();
        this._name = "";
        this._color = "#000000";
        this._poolCount = 0;
        this._isDefault = false;
        this._origName = null;
        this._origColor = null;
    }

    get name() {
        return this._name;
    }

    get color() {
        return this._color;
    }

    get poolCount() {
        return this._poolCount;
    }

    get isDefault() {
        return this._isDefault;
    }

    get isTransient() {
        return !this._origName;
    }

    set name(value) {
        this._name = value;
    }

    set color(value) {
        this._color = value;
    }

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

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

        if (this.name !== this._origName) {
            detail.name = this.name;
        }
        if (this.color !== this._origColor) {
            detail.color = this.color;
        }

        if (!Object.keys(detail).length) {
            return Promise.resolve();
        }

        let promise = this._origName
            ? api.put(
                  uri.formatApiLink("pool-category", this._origName),
                  detail
              )
            : api.post(uri.formatApiLink("pool-categories"), detail);

        return promise.then((response) => {
            this._updateFromResponse(response);
            this.dispatchEvent(
                new CustomEvent("change", {
                    detail: {
                        poolCategory: this,
                    },
                })
            );
            return Promise.resolve();
        });
    }

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

    _updateFromResponse(response) {
        this._version = response.version;
        this._name = response.name;
        this._color = response.color;
        this._isDefault = response.default;
        this._poolCount = response.usages;
        this._origName = this.name;
        this._origColor = this.color;
    }
}

module.exports = PoolCategory;