summaryrefslogtreecommitdiffstats
path: root/gui-editors/gui-editor-apex/src/main/resources/webapp/js/contextMenu.js
blob: 8cd6a80b2f8214559a51317ab53c08efb9b2d617 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020-2021 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

//hide the context menu, if it's shown, anytime the user clicks anywhere.
import {apexUtils_emptyElement} from "./ApexUtils";
import {
    editContextSchemaForm_createContextSchema,
    editContextSchemaForm_deleteContextSchema,
    editContextSchemaForm_editContextSchema,
    editContextSchemaForm_viewContextSchema
} from "./ApexContextSchemaEditForm";
import {
    editEventForm_createEvent,
    editEventForm_deleteEvent,
    editEventForm_editEvent,
    editEventForm_viewEvent
} from "./ApexEventEditForm";
import {
    editTaskForm_createTask,
    editTaskForm_deleteTask,
    editTaskForm_editTask,
    editTaskForm_viewTask
} from "./ApexTaskEditForm";
import {
    editPolicyForm_createPolicy,
    editPolicyForm_deletePolicy,
    editPolicyForm_editPolicy,
    editPolicyForm_viewPolicy
} from "./ApexPolicyEditForm";
import {
    editContextAlbumForm_createContextAlbum,
    editContextAlbumForm_deleteContextAlbum,
    editContextAlbumForm_editContextAlbum,
    editContextAlbumForm_viewContextAlbum
} from "./ApexContextAlbumEditForm";

$(document).on("click", function(event) {
    if (document.getElementById("rightClickMenu"))
        document.getElementById("rightClickMenu").className = "ebContextMenu-body_visible_false";
});

// Hack: need to maintain each passed parameter in a way it can be referenced by
// the onContextMenu handler function for
// each iteration inside a loop, after the passed parameter goes out of scope
// and changes.
// see
// http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example
function rightClickMenu_scopePreserver(parent, type, name, version) {
    return function(clickevent) {
        rightClickMenu(clickevent, parent, type, name, version);
    }
}

// Context menu when user right-clicks inside one of the display tabs
function rightClickMenu(event, parent, type, name, version) {
    var rclickdiv = document.getElementById('rightClickMenu');
    if (rclickdiv == null) {
        rclickdiv = document.createElement("div");
        document.body.appendChild(rclickdiv);
    }
    // clear the div
    apexUtils_emptyElement('rightClickMenu');
    rclickdiv.setAttribute("id", "rightClickMenu");
    rclickdiv.setAttribute("title", type + " menu");
    rclickdiv.addEventListener("contextmenu", function(e) {
        e.preventDefault();
    }, false);

    var ul = document.createElement("div");
    ul.setAttribute("class", "ebComponentList")
    rclickdiv.appendChild(ul);

    var li1 = document.createElement("div");
    li1.setAttribute("class", "ebComponentList-item")
    li1.onclick = function() {
        return rightClickMenuCreate(parent, type);
    };
    li1.innerHTML = "Create new <type>" + type + "<type>";
    if (type.toUpperCase() == "KEYINFORMATION") {
        li1.setAttribute("class", "ebComponentList-item ebComponentList-item_disabled");
    }
    ul.appendChild(li1);

    if (name) {
        var value = name + ":" + version;

        var li4 = document.createElement("div");
        li4.setAttribute("class", "ebComponentList-item")
        li4.onclick = function() {
            return rightClickMenuView(parent, type, name, version);
        };
        li4.innerHTML = "View <type>" + type + "</type> <value>" + value + "<value>";
        ul.appendChild(li4);

        var li2 = document.createElement("div");
        li2.setAttribute("class", "ebComponentList-item")
        li2.onclick = function() {
            return rightClickMenuEdit(parent, type, name, version);
        };
        li2.innerHTML = "Edit <type>" + type + "</type> <value>" + value + "<value>";
        ul.appendChild(li2);

        var li3 = document.createElement("div");
        li3.setAttribute("class", "ebComponentList-item")
        li3.onclick = function() {
            return rightClickMenuDelete(parent, type, name, version);
        };
        li3.innerHTML = "Delete <type>" + type + "</type> <value>" + value + "<value>";
        ul.appendChild(li3);
        if (type.toUpperCase() == "KEYINFORMATION") {
            li3.setAttribute("class", "ebComponentList-item ebComponentList-item_disabled");
        }
    }

    // rclickdiv.setAttribute("class", "contextmenu_show"); ewatkmi: swapped
    // with ebContextMenu
    rclickdiv.setAttribute("class",
            "ebContextMenu-body ebContextMenu-body_corner_default ebContextMenu-body_visible_true");
    rclickdiv.style.position = "absolute";
    rclickdiv.style.top = mouseY(event);
    rclickdiv.style.left = mouseX(event);

    // prevent event bubbling up to parent elements for their on-click
    event.stopPropagation();
    // prevent default context menu
    event.returnValue = false;
    event.preventDefault();
    return false;
}
function rightClickMenuCreate(parent, type) {
    document.getElementById("rightClickMenu").className = "ebContextMenu-body_visible_false";
    if (type.toUpperCase() == "CONTEXTSCHEMA") {
        editContextSchemaForm_createContextSchema(parent);
    } else if (type.toUpperCase() == "EVENT") {
        editEventForm_createEvent(parent);
    } else if (type.toUpperCase() == "TASK") {
        editTaskForm_createTask(parent);
    } else if (type.toUpperCase() == "POLICY") {
        editPolicyForm_createPolicy(parent);
    } else if (type.toUpperCase() == "CONTEXTALBUM") {
        editContextAlbumForm_createContextAlbum(parent);
    } else {
        alert("So you want to create a new " + type);
    }
}
function rightClickMenuView(parent, type, value_name, value_version) {
    document.getElementById("rightClickMenu").className = "ebContextMenu-body_visible_false";
    if (type.toUpperCase() == "CONTEXTSCHEMA") {
        editContextSchemaForm_viewContextSchema(parent, value_name, value_version);
    } else if (type.toUpperCase() == "EVENT") {
        editEventForm_viewEvent(parent, value_name, value_version);
    } else if (type.toUpperCase() == "TASK") {
        editTaskForm_viewTask(parent, value_name, value_version);
    } else if (type.toUpperCase() == "POLICY") {
        editPolicyForm_viewPolicy(parent, value_name, value_version);
    } else if (type.toUpperCase() == "CONTEXTALBUM") {
        editContextAlbumForm_viewContextAlbum(parent, value_name, value_version);
    } else {
        alert("So you want to view " + type + " " + value_name + ":" + value_version);
    }
}
function rightClickMenuEdit(parent, type, value_name, value_version) {
    document.getElementById("rightClickMenu").className = "ebContextMenu-body_visible_false";
    if (type.toUpperCase() == "CONTEXTSCHEMA") {
        editContextSchemaForm_editContextSchema(parent, value_name, value_version);
    } else if (type.toUpperCase() == "EVENT") {
        editEventForm_editEvent(parent, value_name, value_version);
    } else if (type.toUpperCase() == "TASK") {
        editTaskForm_editTask(parent, value_name, value_version);
    } else if (type.toUpperCase() == "POLICY") {
        editPolicyForm_editPolicy(parent, value_name, value_version);
    } else if (type.toUpperCase() == "CONTEXTALBUM") {
        editContextAlbumForm_editContextAlbum(parent, value_name, value_version);
    } else {
        alert("So you want to edit " + type + " " + value_name + ":" + value_version);
    }
}
function rightClickMenuDelete(parent, type, value_name, value_version) {
    document.getElementById("rightClickMenu").className = "ebContextMenu-body_visible_false";
    if (type.toUpperCase() == "CONTEXTSCHEMA") {
        editContextSchemaForm_deleteContextSchema(parent, value_name, value_version);
    } else if (type.toUpperCase() == "EVENT") {
        editEventForm_deleteEvent(parent, value_name, value_version);
    } else if (type.toUpperCase() == "TASK") {
        editTaskForm_deleteTask(parent, value_name, value_version);
    } else if (type.toUpperCase() == "POLICY") {
        editPolicyForm_deletePolicy(parent, value_name, value_version);
    } else if (type.toUpperCase() == "CONTEXTALBUM") {
        editContextAlbumForm_deleteContextAlbum(parent, value_name, value_version);
    } else {
        alert("So you want to delete " + type + " " + value_name + ":" + value_version);
    }
}
function mouseX(evt) {
    if (evt.pageX) {
        return evt.pageX;
    } else if (evt.clientX) {
        return evt.clientX
                + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    } else {
        return null;
    }
}
function mouseY(evt) {
    if (evt.pageY) {
        return evt.pageY;
    } else if (evt.clientY) {
        return evt.clientY
                + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    } else {
        return null;
    }
}

export {
    rightClickMenu_scopePreserver,rightClickMenu,rightClickMenuCreate,rightClickMenuView,rightClickMenuEdit,rightClickMenuDelete
}