Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestDataItem.js « src « summaryWidget « plugins « src - github.com/nasa/openmct.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae005c46d0a21aa0f51835b2f55042ce53b3b2c1 (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
define([
    '../res/testDataItemTemplate.html',
    './input/ObjectSelect',
    './input/KeySelect',
    './eventHelpers',
    '../../../utils/template/templateHelpers',
    'EventEmitter'
], function (
    itemTemplate,
    ObjectSelect,
    KeySelect,
    eventHelpers,
    templateHelpers,
    EventEmitter
) {

    /**
     * An object representing a single mock telemetry value
     * @param {object} itemConfig the configuration for this item, consisting of
     *                            object, key, and value fields
     * @param {number} index the index of this TestDataItem object in the data
     *                 model of its parent {TestDataManager} o be injected into callbacks
     *                 for removes
     * @param {ConditionManager} conditionManager a conditionManager instance
     *                           for populating selects with configuration data
     * @constructor
     */
    function TestDataItem(itemConfig, index, conditionManager) {
        eventHelpers.extend(this);
        this.config = itemConfig;
        this.index = index;
        this.conditionManager = conditionManager;

        this.domElement = templateHelpers.convertTemplateToHTML(itemTemplate)[0];
        this.eventEmitter = new EventEmitter();
        this.supportedCallbacks = ['remove', 'duplicate', 'change'];

        this.deleteButton = this.domElement.querySelector('.t-delete');
        this.duplicateButton = this.domElement.querySelector('.t-duplicate');

        this.selects = {};
        this.valueInputs = [];

        this.remove = this.remove.bind(this);
        this.duplicate = this.duplicate.bind(this);

        const self = this;

        /**
         * A change event handler for this item's select inputs, which also invokes
         * change callbacks registered with this item
         * @param {string} value The new value of this select item
         * @param {string} property The property of this item to modify
         * @private
         */
        function onSelectChange(value, property) {
            if (property === 'key') {
                self.generateValueInput(value);
            }

            self.eventEmitter.emit('change', {
                value: value,
                property: property,
                index: self.index
            });
        }

        /**
         * An input event handler for this item's value field. Invokes any change
         * callbacks associated with this item
         * @param {Event} event The input event that initiated this callback
         * @private
         */
        function onValueInput(event) {
            const elem = event.target;
            const value = (isNaN(elem.valueAsNumber) ? elem.value : elem.valueAsNumber);

            if (elem.tagName.toUpperCase() === 'INPUT') {
                self.eventEmitter.emit('change', {
                    value: value,
                    property: 'value',
                    index: self.index
                });
            }
        }

        this.listenTo(this.deleteButton, 'click', this.remove);
        this.listenTo(this.duplicateButton, 'click', this.duplicate);

        this.selects.object = new ObjectSelect(this.config, this.conditionManager);
        this.selects.key = new KeySelect(
            this.config,
            this.selects.object,
            this.conditionManager,
            function (value) {
                onSelectChange(value, 'key');
            });

        this.selects.object.on('change', function (value) {
            onSelectChange(value, 'object');
        });

        Object.values(this.selects).forEach(function (select) {
            self.domElement.querySelector('.t-configuration').append(select.getDOM());
        });
        this.listenTo(this.domElement, 'input', onValueInput);
    }

    /**
     * Gets the DOM associated with this element's view
     * @return {Element}
     */
    TestDataItem.prototype.getDOM = function (container) {
        return this.domElement;
    };

    /**
     * Register a callback with this item: supported callbacks are remove, change,
     * and duplicate
     * @param {string} event The key for the event to listen to
     * @param {function} callback The function that this rule will envoke on this event
     * @param {Object} context A reference to a scope to use as the context for
     *                         context for the callback function
     */
    TestDataItem.prototype.on = function (event, callback, context) {
        if (this.supportedCallbacks.includes(event)) {
            this.eventEmitter.on(event, callback, context || this);
        }
    };

    /**
     * Implement "off" to complete event emitter interface.
     */
    TestDataItem.prototype.off = function (event, callback, context) {
        this.eventEmitter.off(event, callback, context);
    };

    /**
     * Hide the appropriate inputs when this is the only item
     */
    TestDataItem.prototype.hideButtons = function () {
        this.deleteButton.style.display = 'none';
    };

    /**
     * Remove this item from the configuration. Invokes any registered
     * remove callbacks
     */
    TestDataItem.prototype.remove = function () {
        const self = this;
        this.eventEmitter.emit('remove', self.index);
        this.stopListening();

        Object.values(this.selects).forEach(function (select) {
            select.destroy();
        });
    };

    /**
     * Makes a deep clone of this item's configuration, and invokes any registered
     * duplicate callbacks with the cloned configuration as an argument
     */
    TestDataItem.prototype.duplicate = function () {
        const sourceItem = JSON.parse(JSON.stringify(this.config));
        const self = this;

        this.eventEmitter.emit('duplicate', {
            sourceItem: sourceItem,
            index: self.index
        });
    };

    /**
     * When a telemetry property key is selected, create the appropriate value input
     * and add it to the view
     * @param {string} key The key of currently selected telemetry property
     */
    TestDataItem.prototype.generateValueInput = function (key) {
        const evaluator = this.conditionManager.getEvaluator();
        const inputArea = this.domElement.querySelector('.t-value-inputs');
        const dataType = this.conditionManager.getTelemetryPropertyType(this.config.object, key);
        const inputType = evaluator.getInputTypeById(dataType);

        inputArea.innerHTML = '';
        if (inputType) {
            if (!this.config.value) {
                this.config.value = (inputType === 'number' ? 0 : '');
            }

            const newInput = document.createElement("input");
            newInput.type = `${inputType}`;
            newInput.value = `${this.config.value}`;

            this.valueInput = newInput;
            inputArea.append(this.valueInput);
        }
    };

    return TestDataItem;
});