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

WidgetDnD.js « src « summaryWidget « plugins « src - github.com/nasa/openmct.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e9ee2f04009dac06afa0ef7ff443b5568af23af6 (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
define([
    '../res/ruleImageTemplate.html',
    'EventEmitter',
    'zepto'
], function (
    ruleImageTemplate,
    EventEmitter,
    $
) {

    /**
     * Manages the Sortable List interface for reordering rules by drag and drop
     * @param {Element} container The DOM element that contains this Summary Widget's view
     * @param {string[]} ruleOrder An array of rule IDs representing the current rule order
     * @param {Object} rulesById An object mapping rule IDs to rule configurations
     */
    function WidgetDnD(container, ruleOrder, rulesById) {
        this.container = container;
        this.ruleOrder = ruleOrder;
        this.rulesById = rulesById;

        this.imageContainer = $(ruleImageTemplate);
        this.image = $('.t-drag-rule-image', this.imageContainer);
        this.draggingId = '';
        this.draggingRulePrevious = '';
        this.eventEmitter = new EventEmitter();
        this.supportedCallbacks = ['drop'];

        this.drag = this.drag.bind(this);
        this.drop = this.drop.bind(this);

        $(this.container).on('mousemove', this.drag);
        $(document).on('mouseup', this.drop);
        $(this.container).before(this.imageContainer);
        $(this.imageContainer).hide();
    }

    /**
     * Remove event listeners registered to elements external to the widget
     */
    WidgetDnD.prototype.destroy = function () {
        $(this.container).off('mousemove', this.drag);
        $(document).off('mouseup', this.drop);
    };

    /**
     * Register a callback with this WidgetDnD: supported callback is drop
     * @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
     */
    WidgetDnD.prototype.on = function (event, callback, context) {
        if (this.supportedCallbacks.includes(event)) {
            this.eventEmitter.on(event, callback, context || this);
        }
    };

    /**
     * Sets the image for the dragged element to the given DOM element
     * @param {Element} image The HTML element to set as the drap image
     */
    WidgetDnD.prototype.setDragImage = function (image) {
        this.image.html(image);
    };

    /**
     * Calculate where this rule has been dragged relative to the other rules
     * @param {Event} event The mousemove or mouseup event that triggered this
                            event handler
     * @return {string} The ID of the rule whose drag indicator should be displayed
     */
    WidgetDnD.prototype.getDropLocation = function (event) {
        const ruleOrder = this.ruleOrder;
        const rulesById = this.rulesById;
        const draggingId = this.draggingId;
        let offset;
        let y;
        let height;
        const dropY = event.pageY;
        let target = '';

        ruleOrder.forEach(function (ruleId, index) {
            offset = rulesById[ruleId].getDOM().offset();
            y = offset.top;
            height = offset.height;
            if (index === 0) {
                if (dropY < y + 7 * height / 3) {
                    target = ruleId;
                }
            } else if (index === ruleOrder.length - 1 && ruleId !== draggingId) {
                if (y + height / 3 < dropY) {
                    target = ruleId;
                }
            } else {
                if (y + height / 3 < dropY && dropY < y + 7 * height / 3) {
                    target = ruleId;
                }
            }
        });

        return target;
    };

    /**
     * Called by a {Rule} instance that initiates a drag gesture
     * @param {string} ruleId The identifier of the rule which is being dragged
     */
    WidgetDnD.prototype.dragStart = function (ruleId) {
        const ruleOrder = this.ruleOrder;
        this.draggingId = ruleId;
        this.draggingRulePrevious = ruleOrder[ruleOrder.indexOf(ruleId) - 1];
        this.rulesById[this.draggingRulePrevious].showDragIndicator();
        this.imageContainer.show();
        this.imageContainer.offset({
            top: event.pageY - this.image.height() / 2,
            left: event.pageX - $('.t-grippy', this.image).width()
        });
    };

    /**
     * An event handler for a mousemove event, once a rule has begun a drag gesture
     * @param {Event} event The mousemove event that triggered this callback
     */
    WidgetDnD.prototype.drag = function (event) {
        let dragTarget;
        if (this.draggingId && this.draggingId !== '') {
            event.preventDefault();
            dragTarget = this.getDropLocation(event);
            this.imageContainer.offset({
                top: event.pageY - this.image.height() / 2,
                left: event.pageX - $('.t-grippy', this.image).width()
            });
            if (this.rulesById[dragTarget]) {
                this.rulesById[dragTarget].showDragIndicator();
            } else {
                this.rulesById[this.draggingRulePrevious].showDragIndicator();
            }
        }
    };

    /**
     * Handles the mouseup event that corresponds to the user dropping the rule
     * in its final location. Invokes any registered drop callbacks with the dragged
     * rule's ID and the ID of the target rule that the dragged rule should be
     * inserted after
     * @param {Event} event The mouseup event that triggered this callback
     */
    WidgetDnD.prototype.drop = function (event) {
        let dropTarget = this.getDropLocation(event);
        const draggingId = this.draggingId;

        if (this.draggingId && this.draggingId !== '') {
            if (!this.rulesById[dropTarget]) {
                dropTarget = this.draggingId;
            }

            this.eventEmitter.emit('drop', {
                draggingId: draggingId,
                dropTarget: dropTarget
            });
            this.draggingId = '';
            this.draggingRulePrevious = '';
            this.imageContainer.hide();
        }
    };

    return WidgetDnD;
});