-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathlock.js
More file actions
253 lines (234 loc) · 8.75 KB
/
lock.js
File metadata and controls
253 lines (234 loc) · 8.75 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Locked Questions plugin.
*
* Lets you lock questions by setting vellum:lock on their bind.
*
* 'all': Nothing about this question can be changed.
*/
import $ from "jquery";
import "vellum/core";
import _ from "underscore";
import widgets from "vellum/widgets";
const LOCKED_BIND_ATTR = "vellum:lock";
const LOCKED_UNEDITABLE_MSG_KEY = "mug-locked-cannot-edit";
const LOCKED_CHILDREN_MSG_KEY = "mug-has-locked-children";
const SELECT_CLASSES = ["Select", "MSelect", "Choice"];
$.vellum.plugin("lock", {}, {
init: function () {
const data = this.data.lock;
data.locks = {};
},
loadXML: function () {
this.__callOld();
// we don't know whether there are any locks until XML is loaded
// tools menu items are already defined at this point, so target what we want and remove them here
if (_.some(this.data.lock.locks) && !this.opts().features.edit_locked_questions) {
const $menu = this.$f.find('.fd-tools-menu').parent();
$menu.find('a:contains("' + gettext("Edit Source XML") + '")').parent().remove();
$menu.find('a:contains("' + gettext("Edit Bulk Translations") + '")').parent().remove();
}
},
parseBindElement: function (form, el, path) {
this.__callOld();
const locked = el.xmlAttr(LOCKED_BIND_ATTR);
if (locked && locked === 'all') {
const mug = form.getMugByPath(path);
mug.p.set('locked', true);
if (!this.opts().features.edit_locked_questions) {
const message = {
key: LOCKED_UNEDITABLE_MSG_KEY,
message: gettext(
"This question is locked and can only be edited by a user with the locked " +
"questions permission."
),
level: mug.INFO,
};
mug.addMessage('locked', message);
}
}
},
handleMugParseFinish: function (mug) {
this.__callOld();
setControlOnlyChildrenLocked(mug);
},
setTreeActions: function (mug) {
if (!mug.options.hasOwnProperty('_originalCanAddChoices')) {
mug.options._originalCanAddChoices = mug.options.canAddChoices;
}
mug.options.canAddChoices = mug.p.locked ? false : mug.options._originalCanAddChoices;
this.__callOld();
},
setTreeExtraIcons: function (mug) {
this.__callOld();
const node = mug.ufid && this.jstree('get_node', mug.ufid);
if (!node) { return; }
let icon = null;
if (mug.p.locked) {
icon = treeLockIcon(mug);
}
if (node.data.extraIcons?.lock !== icon) {
node.data.extraIcons = node.data.extraIcons || {};
node.data.extraIcons.lock = icon;
this.jstree('redraw_node', node);
}
},
getMainProperties: function () {
const properties = this.__callOld();
properties.splice(1 + properties.indexOf('requiredAttr'), 0, 'locked');
return properties;
},
getMugSpec: function () {
const _this = this;
const spec = this.__callOld();
const isEditable = this.opts().features.edit_locked_questions;
spec.databind.locked = {
lstring: gettext("Locked"),
visibility: isEditable ? 'visible' : 'visible_if_present',
presence: 'optional',
widget: widgets.checkbox,
enabled: () => isEditable,
help: gettext("A locked question cannot be edited, moved, or deleted from the form."),
helpURL: "https://www.example.com", // placeholder for public documentation
serialize: () => {},
deserialize: (data, key, mug, context) => {
if (mug.p.rawBindAttributes && mug.p.rawBindAttributes[LOCKED_BIND_ATTR]) {
delete mug.p.rawBindAttributes[LOCKED_BIND_ATTR];
}
},
setter: function (mug, attr, value) {
if (value === true) {
mug.p.rawBindAttributes = mug.p.rawBindAttributes || {};
mug.p.rawBindAttributes[LOCKED_BIND_ATTR] = 'all';
_this.data.lock.locks[mug.ufid] = 'all';
} else {
delete mug.p.rawBindAttributes[LOCKED_BIND_ATTR];
delete _this.data.lock.locks[mug.ufid];
}
mug.p.set(attr, value);
_this.setTreeExtraIcons(mug);
if (_.contains(["Select", "MSelect"], mug.__className)) {
setControlOnlyChildrenLocked(mug);
}
if (!mug.form.isLoadingXForm) {
if (mug.__className === "Group") {
cascadeLockToChildren(mug, value);
} else if (_.contains(["Select", "MSelect"], mug.__className)) {
_this.setTreeActions(mug);
}
if (_this.getCurrentlySelectedMug() === mug) {
_this.displayMugProperties(mug);
}
if (mug.parentMug) {
updateParentTreeIcons(mug.parentMug);
}
}
},
};
return spec;
},
checkMove: function (srcId, srcType, dstId, dstType, position) {
const form = this.data.core.form;
const sourceMug = form.getMugByUFID(srcId);
if (sourceMug.p.locked) {
return false;
}
const canMove = this.__callOld();
if (canMove) {
const destinationMug = form.getMugByUFID(dstId);
if (destinationMug) {
return !(destinationMug.p.locked && _.contains(SELECT_CLASSES, dstType));
}
return true;
}
return false;
},
getInsertTargetAndPosition: function (refMug, qType, after) {
if (refMug?.p.locked && _.contains(SELECT_CLASSES, refMug.__className)) {
return null;
}
return this.__callOld();
},
isPropertyLocked: function (mug, propertyPath) {
const locked = this.__callOld();
if (locked || mug.p.locked) {
return true;
}
if (propertyPath === 'nodeID' && !this.isMugPathMoveable(mug)) {
const message = {
key: LOCKED_CHILDREN_MSG_KEY,
message: gettext(
"This group contains locked questions and cannot be moved or deleted from the form."
),
level: mug.INFO,
};
mug.addMessage('nodeID', message);
return true;
}
return false;
},
isMugPathMoveable: function (mug) {
return this.__callOld() && !mug.p.locked && !hasLockedChildren(mug);
},
isMugRemoveable: function (mug) {
return this.__callOld() && !mug.p.locked && !hasLockedChildren(mug);
},
isMugTypeChangeable: function (mug) {
return this.__callOld() && !mug.p.locked;
},
});
function hasLockedChildren(mug) {
return mug.form.getChildren(mug).some(child =>
child.p.locked || hasLockedChildren(child)
);
}
function hasUnlockedChildren(mug) {
return mug.form.getChildren(mug).some(child =>
!child.p.locked || hasUnlockedChildren(child)
);
}
function setControlOnlyChildrenLocked(mug) {
mug.form.getChildren(mug).forEach(child => {
if (child.options.isControlOnly) {
child.p.set('locked', mug.p.locked);
}
});
}
function cascadeLockToChildren(mug, value) {
mug.form.getChildren(mug).forEach(child => {
if (child.p.locked !== value) {
child.p.locked = value;
} else if (child.__className === "Group") {
cascadeLockToChildren(child, value);
}
});
}
function updateParentTreeIcons(parent) {
while (parent) {
if (parent.p.locked && parent.__className === "Group") {
parent.form.vellum.setTreeExtraIcons(parent);
}
parent = parent.parentMug;
}
}
function treeLockIcon(mug) {
if (mug.options.isControlOnly) {
return;
}
let iconClass = "fa-lock";
let tooltipText = gettext("Only a user with permission can edit this question.");
if (mug.__className === "Group") {
if (hasUnlockedChildren(mug)) {
iconClass = "fa-unlock";
tooltipText = gettext(
"Only a user with permission can edit this group. " +
"Some questions are unlocked."
);
} else {
tooltipText = gettext(
"Only a user with permission can edit this group. " +
"All questions are locked."
);
}
}
return `<i class="fa ${iconClass}" title="${tooltipText}"></i> `;
}