conditional_simple_edit.js
13.4 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* Model Functions
*
* Perform various and sundry operations on the edit-page.
*/
var target_url = '/plugins/ktcore/admin/ajaxSimpleConditionals.php';
function getTargetUrl() {
var base = getElement('kt-core-baseurl');
return base.value + target_url;
}
/** Simple edit: HTML helper functions */
function getConditionalTable() {
return getElement('simple_conditional_edit');
}
// returns the td element representing the row, for use as Parent.
function getColumnForField(field_id) {
return getElement('md_'+field_id);
}
function getButtonsForField(field_id) {
return getElement('buttons_'+field_id);
}
function getNameForField(field_id) {
var h = getElement('header_'+field_id);
//alert(field_id);
return scrapeText(h);
}
function getFieldIdFromColumn(column) {
return column.id.substr(3,column.id.length);
}
// takes "active_fields[]". sets both sets to "active"
// triggered by the "edit button" and its respective callbacks.
function setActiveFields(active_fields) {
var fixed_field = current_fixed; // stored and managed by pushUndoStack.
for (var i=0; i<active_fields.length; i++) {
var column = getColumnForField(active_fields[i]);
setElementClass(column, 'active');
}
}
// takes a field, and sets all items in active_lookups to be active. other items are deleted.
// active_lookups should map to the _values_ of the <option>'s.
function setActiveLookupsForField(field_id, active_lookups) {
simpleLog('DEBUG','function setActiveLookupsForField called with '+active_lookups.length+' items');
var column = getColumnForField(field_id);
// Use an Object as a new associative array.
var active_hash = new Object;
for (var i=0; i<active_lookups.length; i++) {
active_hash[active_lookups[i]] = 1; // everything else is undef.
}
var item_list = getElementsByTagAndClassName('SELECT','item_list',column)[0]; // FIXME check for failure.
for (var i=0; i<item_list.options.length; i++) {
var option = item_list.options[i];
option.selected = false;
if (active_hash[option.value]) {
option.selected = true;
}
}
}
// check in all active (non-edit) fields for lookup_values that are active.
// returns a NESTED array (field_id => array('value'))
// to make the backend a tad simpler.
function getActiveLookups() {
simpleLog('DEBUG','getActiveLookups called');
// first get all edit columns
var rootItem = getConditionalTable();
var potential_sources = getElementsByTagAndClassName('TD','active',rootItem);
if (potential_sources.length == 0) {
simpleLog('ERROR','no active fields located.');
return null;
}
var active_lookups = Array();
for (var i=0; i<potential_sources.length; i++) {
var column = potential_sources[i];
if (!hasElementClass(column, 'editing')) {
var item_list = getElementsByTagAndClassName('SELECT','item_list',column)[0]; // FIXME catch potential failure here (pathalogical)
var field_id = getFieldIdFromColumn(column);
simpleLog('DEBUG','found non-fixed active column-set ('+field_id+')');
active_lookups[field_id] = Array();
for (var j=0; j<item_list.options.length; j++) {
if (item_list.options[j].selected == true) {
active_lookups[field_id].push(item_list.options[j].value);
}
}
}
}
return active_lookups;
}
/** Simple edit: AJAX component */
// extract the "fixed" field, and identify which fields (if any) are active.
function updateActiveFields() {
simpleLog('DEBUG','function updateActiveFields called.');
var req = getXMLHttpRequest();
req.open('GET',getTargetUrl()+'?action=updateActiveFields&active_field='+current_fixed, true);
var deferred = sendXMLHttpRequest(req);
deferred.addCallback(do_updateActiveFields);
deferred.addErrback(partial(do_handleAjaxError, 'updateActiveFields'));
}
function do_handleAjaxError(err_source, err) {
simpleLog('ERROR','AJAX function "'+err_source+'" failed with: \n'+repr(err));
}
// from a selected_lookup, get the fixed_field and pass through, getting the items that selection currently activates.
function updateActiveLookups(selected_lookup, lookup_label) {
simpleLog('DEBUG','function updateActiveLookups called.');
var req = getXMLHttpRequest();
req.open('GET',getTargetUrl()+'?action=updateActiveLookups&active_field='+current_fixed+'&selected_lookup='+selected_lookup, true);
var deferred = sendXMLHttpRequest(req);
deferred.addCallback(partial(do_updateActiveLookups, lookup_label));
deferred.addErrback(partial(do_handleAjaxError, 'updateActiveLookups'));
}
// send a "save" request to the backend, asking it to make the child_lookups the only items parented
// to the selected_lookup (include field_id for selected, and fieldset).
function storeRelationship(selected_lookup, child_lookups) {
var formKeys = Array();
var formValues = Array();
// action
formKeys.push('action');
formValues.push('storeRelationship');
var fieldset_id = getElement('global-fieldset-id').value;
formKeys.push('fieldset_id');
formValues.push(fieldset_id);
// which parent_field
formKeys.push('parent_field');
formValues.push(current_fixed);
// which parent_field
formKeys.push('parent_lookup');
formValues.push(selected_lookup);
// add children to the form.
for (var i=0; i<child_lookups.length; i++) {
if (child_lookups[i]) { // catch undefined items
for (var j=0; j<child_lookups[i].length; j++) {
formKeys.push('child_lookups['+i+'][]'); // field_id and append.
formValues.push(child_lookups[i][j]);
}
}
}
var POSTval = queryString(formKeys, formValues);
simpleLog('DEBUG','query from storeRelationship: \n'+POSTval);
var req = getXMLHttpRequest();
req.open('POST', getTargetUrl(), true);
req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
var deferred = sendXMLHttpRequest(req, POSTval);
// inform the user that something has happened.
// FIXME this isn't i18n friendly.
addInformationNote(_('Dependencies saved (') + new Date() + ')');
deferred.addCallback(do_updateActiveLookups);
deferred.addErrback(partial(do_handleAjaxError, 'storeRelationship'));
}
function addInformationNote(message) {
// this is _NOT_ the best way to handle this ...
var dynamicMessage = getElement('condy-simple-notice');
if (dynamicMessage == null) {
dynamicMessage = DIV({'id':'condy-simple-notice','class':'ktInfo'},null);
var contentBox = getElement('content');
var secondHeaders = contentBox.getElementsByTagName('h2'); // should only be one.
var sH = secondHeaders[0];
contentBox.insertBefore(dynamicMessage, sH);
}
var newStr = createDOM('P',null,message);
replaceChildNodes(dynamicMessage, newStr);
}
// receives a very simple list of fields.
function do_updateActiveFields(req) {
simpleLog('DEBUG','AJAX function do_updateActiveFields triggered');
var active_fields = Array();
var incoming_fields = req.responseXML.getElementsByTagName('active');
for (var i=0; i<incoming_fields.length; i++) {
active_fields.push(incoming_fields[i].getAttribute('field_id'));
}
simpleLog('DEBUG','do_updateActiveFields found active fields: '+repr(active_fields));
setActiveFields(active_fields);
}
// should receive a simple-enough set of "field" elements,
// filled with "lookup" items.
function do_updateActiveLookups(label, req) {
simpleLog('DEBUG','AJAX function do_updateActiveLookups triggered');
var active_fields = Array();
var incoming_fields = req.responseXML.getElementsByTagName('field');
for (var i=0; i<incoming_fields.length; i++) {
var field = incoming_fields[i];
var field_id = field.getAttribute('field_id');
simpleLog('DEBUG','found field: '+field_id);
var infoset = {'field_id':field_id, 'lookups':new Array()};
var active_lookups = field.getElementsByTagName('lookup');
simpleLog('DEBUG',typeof(active_lookups)+' ');
for (var j=0; j < active_lookups.length; j++) {
infoset.lookups.push(active_lookups[j].getAttribute('lookup_id'));
}
active_fields.push(infoset);
}
simpleLog('DEBUG','do_updateActiveLookups found active fields count: '+repr(active_fields.length));
for (var i=0; i<active_fields.length; i++) {
if (active_fields[i]) { // e.g. not undefined.
simpleLog('DEBUG','lookups contain['+i+']: '+active_fields[i].field_id+' - '+active_fields[i].lookups);
setActiveLookupsForField(active_fields[i].field_id, active_fields[i].lookups);
}
}
addInformationNote(_('Dependencies for value "')+label+('" loaded (') + new Date() + ')');
}
/** Simple edit: JS model */
function setExclusiveEditing(field_id) {
var rootItem = getConditionalTable();
var columns = rootItem.getElementsByTagName('TD');
for (var i=0; i<columns.length; i++) {
var col = columns[i];
if (hasElementClass(col, "buttonset")) { setElementClass(col, 'buttonset inactive'); }
else {
setElementClass(columns[i], 'inactive');
var item_list = getElementsByTagAndClassName('SELECT','item_list',columns[i])[0]; // FIXME catch potential failure here (pathalogical)
item_list.multiple=true;
updateNodeAttributes(item_list, {'onchange':null});
}
}
// get the "right" column.
var column = getColumnForField(field_id);
simpleLog('DEBUG','setExclusiveEditing found column' + column + ' for id ' + field_id);
setElementClass(column, 'active editing');
var item_list = getElementsByTagAndClassName('SELECT','item_list',column)[0]; // FIXME catch potential failure here (pathalogical)
item_list.multiple = false;
updateNodeAttributes(item_list, {'onchange':partial(handleChangedSelection, field_id, item_list)});
var buttons = getButtonsForField(field_id);
setElementClass(buttons, 'buttonset active editing');
simpleLog('ERROR','setExclusiveEditing needs to alter the options so nothing is selected.');
}
function editSimpleField(field_id) {
simpleLog('DEBUG','function editSimpleField called.');
// this needs to:
// - make the selected field_id the _only_ editing column.
// (remember to reset multiple on the previously-editable column
// - make the selected field_id's item_list a singular one.
// push the stack.
pushUndoStack(field_id);
// set everything to inactive, except the chosen field.
setExclusiveEditing(field_id);
updateActiveFields(); // trigger an update of the backend.
// rest is asynchronous.
addInformationNote(_('Now editing field "')+getNameForField(field_id)+'".');
}
// extract the currently active lookup, and pass though.
function saveSimpleField(field_id) {
simpleLog('DEBUG','saveSimpleField called with field '+field_id);
var column = getColumnForField(field_id);
var item_list_select = getElementsByTagAndClassName('SELECT','item_list', column);
if (item_list_select.length == 0) {
simpleLog('ERROR','no item_list select found in field '+field_id);
return false;
}
// else
var selected_lookup = item_list_select[0].value;
simpleLog('DEBUG','extracted selected lookup of '+selected_lookup);
// get the active items.
var active_lookups = getActiveLookups();
if (active_lookups == null) {
simpleLog('DEBUG','no selected items found in dependant columns. NOT saving.');
return false;
}
storeRelationship(selected_lookup, active_lookups);
}
function finishSimpleField(field_id) {
popUndoStack();
}
// called when a single-view dropdown is activated.
function handleChangedSelection(field_id, select_input) {
addInformationNote(_('Loading Dependencies for value "')+scrapeText(select_input.options[select_input.selectedIndex])+'" (' + new Date() + ')');
updateActiveLookups(select_input.value, scrapeText(select_input.options[select_input.selectedIndex])); // handles everything for us.
}
// push onto the "fixed" stack the field which is being edited at the moment.
// gracefully handles multiple-calls, ensuring non-immediate replication
// of entries.
var undoStack = Array(); // stores the field_ids.
var current_fixed = null; // current fixed. saves a bit of time...
function pushUndoStack(field_id) {
current_fixed = field_id;
return false;
simpleLog('DEBUG','untested function pushUndoStack called.');
if (current_fixed == null) {
current_fixed = field_id;
return false;
// pre-initialisation.
}
if (undoStack.length == 0) {
undoStack.push(current_fixed);
} else if (undoStack[undoStack.length-1] != field_id) {
undoStack.push(current_fixed);
} else {
return false; // pass
}
simpleLog('DEBUG','undoStack is now: '+repr(undoStack));
current_fixed=field_id;
}
function popUndoStack() {
simpleLog('DEBUG','function popUndoStack called.');
if (undoStack.length == 0 ) {
simpleLog('ERROR','undo stack popped at 0. This should be impossible');
return false;
}
var targetFixed = undoStack.pop();
current_fixed = targetFixed;
setExclusiveEditing(targetFixed);
updateActiveFields(); // trigger an update of the backend.
if (undoStack.length == 0) {
undoStack.push(targetFixed);
simpleLog('ERROR','undo stack popped to 0, re-pushing this last item. ');
}
simpleLog('DEBUG','undoStack is now: '+repr(undoStack));
}