-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathfield-edit-widget.js
More file actions
135 lines (104 loc) · 3.28 KB
/
Copy pathfield-edit-widget.js
File metadata and controls
135 lines (104 loc) · 3.28 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
'use strict';
// *****************************************************************************
// Custom Modules
import { HTMLCreator } from './dom';
import { WindowEvents, GlobalEvents } from './global-events';
// *****************************************************************************
// Global Variables
var counterID = 0;
export function FieldEditWdiget(options) {
var DomElem = HTMLCreator();
// ------------------------------------------------------------------------
// Create UI
// Context will be the session folderID or bookmarkID
var context = undefined;
// Context data represents fields that need to be edited
var context_data = null;
var widgetID = counterID++;
var fields = {};
var first_field = null;
// Edit session widget
var widget = DomElem('div', { class: 'field-edit-widget' });
var wfields = DomElem('div', { class: 'fields' });
var save = DomElem('div', { class: 'button save' });
var cancel = DomElem('div', { class: 'button cancel' });
save.textContent = 'Save';
cancel.textContent = 'Cancel';
widget.appendChild(wfields);
widget.appendChild(cancel);
widget.appendChild(save);
// ------------------------------------------------------------------------
// Methods
function addField(options) {
var group = DomElem('div', { class: 'group' });
var input = DomElem('input');
var label = DomElem('label');
label.textContent = options.label;
group.appendChild(label);
group.appendChild(input);
wfields.appendChild(group);
fields[options.name] = input;
if (first_field == null) {
first_field = input;
}
}
function keyBoardShortcut(e) {
if (e.keyCode == 13) {
saveValues();
}
}
function hideWidget() {
if (first_field) first_field.blur();
widget.removeAttribute('data-active');
document.removeEventListener('keydown', keyBoardShortcut);
}
var hideIfNotInvoked = (ID) => {
if (widgetID !== ID) hideWidget();
};
function invokeWidget(data) {
context_data = data.fields;
context = data.context;
for (var key in data.fields) {
if (data.fields[key]) {
fields[key].value = data.fields[key];
} else {
if (Object.prototype.hasOwnProperty.call(fields, key)) {
fields[key].value = '';
data.fields[key] = '';
}
}
}
widget.setAttribute('data-active', '');
document.addEventListener('keydown', keyBoardShortcut);
WindowEvents.emit(document, 'FieldWidgetInvoked', widgetID);
if (first_field) {
first_field.focus();
first_field.select();
}
}
function saveValues() {
var obj = {};
var prop_save = 0;
for (var key in fields) {
if (context_data[key] != fields[key].value) {
prop_save++;
obj[key] = fields[key].value;
}
}
if (prop_save) {
WindowEvents.emit(document, options.name + '-Save', { context: context, fields: obj });
}
hideWidget();
}
// ------------------------------------------------------------------------
// Create UI
cancel.addEventListener('click', hideWidget);
save.addEventListener('click', saveValues);
GlobalEvents.on('BookmarkRemoved', hideWidget);
WindowEvents.on(document, options.name + '-Invoke', invokeWidget);
WindowEvents.on(document, 'FieldWidgetInvoked', hideIfNotInvoked);
// ------------------------------------------------------------------------
// Public properties
this.DOMRoot = widget;
this.addField = addField;
}