summaryrefslogtreecommitdiff
path: root/client/js/controls/post_metric_list_control.js
blob: b179b12146dfe16138f95470ba131eeccb8cb4dd (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
'use strict';

const events = require('../events.js');
const views = require('../util/views.js');

const postMetricNodeTemplate = views.getTemplate('compact-post-metric-list-item');
const postMetricRangeNodeTemplate = views.getTemplate('compact-post-metric-range-list-item');

class PostMetricListControl extends events.EventTarget {
    constructor(listNode, post) {
        super();
        this._post = post;
        this._listNode = listNode;

        this._refreshContent();
    }

    _refreshContent() {
        this._listNode.innerHTML = '';
        for (let pm of this._post.metrics) {
            const postMetricNode = this._createPostMetricNode(pm);
            this._listNode.appendChild(postMetricNode);
        }
        for (let pmr of this._post.metricRanges) {
            const postMetricRangeNode = this._createPostMetricRangeNode(pmr);
            this._listNode.appendChild(postMetricRangeNode);
        }
    }

    _createPostMetricNode(pm) {
        const tag = this._post.tags.findByName(pm.tagName);
        const node = postMetricNodeTemplate({
            editMode: false,
            postMetric: pm,
            tag: tag,
        });
        return node;
    }

    _createPostMetricRangeNode(pmr) {
        const tag = this._post.tags.findByName(pmr.tagName);
        const node = postMetricRangeNodeTemplate({
            editMode: false,
            postMetricRange: pmr,
            tag: tag,
        });
        return node;
    }
}

module.exports = PostMetricListControl;