-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixAjax.js
More file actions
458 lines (416 loc) · 12.8 KB
/
Copy pathmatrixAjax.js
File metadata and controls
458 lines (416 loc) · 12.8 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
var delay = (function () {
var timer = 0;
return function (callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
// save Editable so matrix html element is focused and saveable
function sE(editableObj) {
$(editableObj).css("background", "#edd");
document.execCommand("selectAll", false, null); // select text
$(editableObj).keydown(function (e) {
// User can cancel edit by pressing escape
if (e.which == 27) {
$(editableObj).css("background", "#ddd");
}
// User can submit by pressing enter (shift enter is carriage return)
if (e.which == 13 && !e.shiftKey) {
$(editableObj).trigger("blur");
}
});
}
// focus and save Editable fluid (non-matrix) html element
function sEf(editableObj, table, updCol, pcol1, pid1) {
// cursor to end if input field textarea
// var eType = editableObj.tagName;
// eType = eType.toLowerCase();
// if(eType == 'textarea') {
// editableObj.selectionStart = editableObj.selectionEnd = editableObj.value.length;
// }
// workaround to stop lastpass interfering with enter key
$(editableObj).keydown(function (e) {
// otherwise error: assertion failed: input argument is not an htmlinputelement
if (e.which == 13) {
e.stopPropagation();
}
});
// autosave if x seconds after last keystroke
// just have to be careful not to close window before autosave, swapping fields or windows is fine
var timeoutId;
var wait = 1000;
$(editableObj).keyup(function (e) {
// avoid triggering on irrelevant window switch keystrokes
// adding inclusions that fall within excluded range
//alert(e.which);return false;
var speccy = [
8, // delete
46, // backspace
173, // - hyphen
191, // ?
219, // [ or {
221, // ] or }
// , 224 // command button undo/redo/paste but also switch window
];
// filter irrelevant keystrokes
if (
(e.which < 32 ||
e.which > 127 || // outside alphanumeric/characters (32-127)
(e.which >= 37 && e.which <= 40)) && // cursor keys (37-40)
// adding inclusions
!speccy.includes(e.which)
) {
return false;
}
// change colour to indicate editing
// once colour disappears, content is saved
$(editableObj).css("background", "#F3F3e0");
// if a timer was already started, clear it
if (timeoutId) clearTimeout(timeoutId);
// wait to send ajax save query
timeoutId = setTimeout(function () {
sT(editableObj, table, updCol, pcol1, pid1);
}, wait);
});
// unnecessary unless wait time is long
// save if blur away
// $(editableObj).blur(function () {
// sT(editableObj,table,updCol,pcol1,pid1);
// });
}
// saveTo from any html element type even if not in matrix.php, i.e. from fluid
function sT(editableObj, table, updCol, pcol1, pid1, col2, id2, col3, id3, col4, id4, col5, id5, updVal) {
// saveTo
//
// define vars
//
var eType = editableObj.tagName;
eType = eType.toLowerCase();
var bgcol = "#fff";
// alert(eType);
// set element style
if (eType == "select" || eType == "input") {
$(editableObj).css("outline-style", "dotted");
$(editableObj).css("outline-width", "medium");
$(editableObj).css("outline-color", "#c22");
} else if (eType == "textarea") {
bgcol = "#fff";
} else if (eType == "td" || eType == "div") {
bgcol = "#ded";
}
// call element content
if (updVal === "" || typeof updVal == "undefined") {
if (eType == "textarea" || eType == "select" || eType == "input") {
updVal = editableObj.value;
} else if (eType == "td" || eType == "div") {
//updVal = editableObj.innerHTML; // adds unwanted html tags
updVal = editableObj.innerText;
} else {
alert("error: unrecognised element type " + eType);
return;
}
}
// basic clean, helps avoid numbers with hidden space
updVal = updVal.trim(); // remove leading/trailing spaces
// exception handling
if (
table == "itemstatus" &&
updCol == "dateCreated" &&
(updVal == "\n" || updVal === "" || typeof updVal == "undefined")
)
updVal = "NULL";
if (
table == "itemattributes" &&
updCol == "deadline" &&
(updVal == "\n" || updVal === "" || typeof updVal == "undefined")
)
updVal = "NULL";
// alert(updVal);
//if (updVal == 'dateCompleteNullRandom34Lror') updVal = 'NULL';
//alert(table+','+updCol+','+pcol1+','+pid1+','+updVal);
// shorthand
if (table == "lq") table = "lookupqualities";
if (updCol == "val") updCol = "value";
if (pcol1 == "qaId") pcol1 = "qaId";
if (col2 == "vId") col2 = "visId";
if (col3 == "iId") col3 = "itemId";
if (col4 == "qId") col4 = "qId";
if (col5 == "iT") col5 = "itemType";
//
// data validation
//
if (col4 == "qId") id4 = parseInt(id4);
// the special days calc ids, to trigger recalculation of Effort / Year, Travel / Year, and Cost / Year
let calcDaysIds = [21, 22, 515, 516, 517, 518, 519, 520, 621, 623, 624, 625, 626, 627];
let calcDaysFlag = false;
if (col4 == "qId" && calcDaysIds.includes(id4)) calcDaysFlag = true;
// the special month calc ids, to trigger recalculation of Sum / Season
let calcMonthFlag = false;
// months
let calcMonthNames = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
let calcMonthIds = [583, 584];
if (col4 == "qId" && calcMonthIds.includes(id4)) {
calcMonthFlag = true;
updVal = updVal.replace(/(<br\s*[\/]?>|\r\n\t|\n|\r\t)/gm, ""); // remove line breaks and <br>
updVal = toTitleCase(updVal);
if (!calcMonthNames.includes(updVal)) return;
}
// probability scores
calcMonthIds = [587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598];
if (col4 == "qId" && calcMonthIds.includes(id4)) calcMonthFlag = true;
// the special career calc ids, to trigger recalculation of Career scores
// also if any related ids in calcDaysFlag
let calcCareerIds = [
/* non-specifically career items*/
51, 52, 53, 54 /* Personal Fit */, 521, 522 /* Sum-Cr */ /* specifically career items*/, 722, 723, 724, 725, 726,
727 /* Career Capital */, 771, 772, 773, 774, 775, 776, 777, 778 /* Impact Scale */, 781, 782, 783, 784, 785,
786 /* Impact Neglectedness */, 791, 792 /* Impact Solvability */, 812, 813, 814, 815, 816,
817 /* Conditions Sum */, 822, 823 /* Conditions Needs */, 831, 832, 833, 834, 835, 836 /* Conditions Locality */,
841, 842, 843, 844 /* Conditions Income */, 853, 854, 855, 856 /* Personal Fit */,
];
let calcCareerFlag = false;
if ((col4 == "qId" && calcCareerIds.includes(id4)) || calcDaysFlag) calcCareerFlag = true;
//
// ajax query
//
var ajaxFailed = false; // flag variable to track if ajax request failed
$.ajax({
url: "matrixSave.php",
type: "POST",
data:
"table=" +
table +
"&updCol=" +
updCol +
"&pcol1=" +
pcol1 +
"&pid1=" +
pid1 +
"&col2=" +
col2 +
"&id2=" +
id2 +
"&col3=" +
col3 +
"&id3=" +
id3 +
"&id4=" +
id4 +
"&col4=" +
col4 +
"&col5=" +
col5 +
"&id5=" +
id5 +
"&updVal=" +
encodeURIComponent(updVal),
success: function (result) {
// validate query
// compare update request with actual recorded value in db
$.post(
"matrixQuery.php",
{
table: table,
updCol: updCol,
pcol1: pcol1,
pid1: pid1,
col2: col2,
id2: id2,
col3: col3,
id3: id3,
col4: col4,
id4: id4,
col5: col5,
id5: id5,
},
function (resVal) {
// sanitise
resVal = resVal.replace(/(<br\s*[\/]?>|\r\n\t|\n|\r\t)/gm, ""); // remove line breaks and <br>
updVal = updVal.replace(/(<br\s*[\/]?>|\r\n\t|\n|\r\t)/gm, ""); // remove line breaks and <br>
updVal = updVal.replace(/(&)/gm, "&"); // convert html characters, ignore ",',\,/,
updVal = updVal.replace(/(<)/gm, "<");
updVal = updVal.replace(/(>)/gm, ">");
updVal = updVal.replace(/( .LIST)/gm, "");
updVal = updVal.replace(/( .CL)/gm, "");
// exception when saving NULL to db
if (resVal == "" && updVal == "NULL") resVal = "NULL";
// confirm request and result match
if (resVal == updVal) {
// alert('Posting success\r\n\t' + resVal + '\r\n\t' +updVal);
$("#ajaxResp").html(resVal.substring(0, 10));
$(editableObj).css("background", bgcol);
$(editableObj).css("outline-color", "#6a6");
if (col2 == "visId") calcFormulae(id2);
// if one of the special days calc ids, recalculate
if (calcDaysFlag) calcDays(col2, id2, col3, id3, col5, id5);
// if one of the special month calc ids, recalculate
if (calcMonthFlag) calcMonths(col2, id2, col3, id3, col5, id5);
// if one of the special career calc ids, recalculate
if (calcCareerFlag) calcCareer(col2, id2, col3, id3, col5, id5);
// otherwise throw warning
} else {
// throw warning only if ajax request has not failed previously
if (!ajaxFailed) {
alert("Posting failed\r\n\t" + resVal + "\r\n\t" + updVal);
// alert('Posting failed\r\n\nManually submit item then refresh page');
ajaxFailed = true;
}
}
// if no query returned at all throw warning
},
).fail(function () {
if (!ajaxFailed) {
alert("Query failed");
ajaxFailed = true;
}
});
},
});
// known issue of multiple query requests sent to php when attribute edited more than once, very difficult to understand, don't bother fixing. Not harmful to database.
}
function calcDays(col2, id2, col3, id3, col5, id5) {
let table = "lookupqualities";
let pcol1 = "qaId";
let updCol = "value";
let col4 = "qId";
let id4 = "";
$.ajax({
url: "matrixSaveDays.php",
type: "POST",
data:
"table=" +
table +
"&updCol=" +
updCol +
"&pcol1=" +
pcol1 +
"&col2=" +
col2 +
"&id2=" +
id2 +
"&col3=" +
col3 +
"&id3=" +
id3 +
"&id4=" +
id4 +
"&col4=" +
col4 +
"&col5=" +
col5 +
"&id5=" +
id5,
});
}
function calcMonths(col2, id2, col3, id3, col5, id5) {
let table = "lookupqualities";
let pcol1 = "qaId";
let updCol = "value";
let col4 = "qId";
let id4 = "";
$.ajax({
url: "matrixSaveMonths.php",
type: "POST",
data:
"table=" +
table +
"&updCol=" +
updCol +
"&pcol1=" +
pcol1 +
"&col2=" +
col2 +
"&id2=" +
id2 +
"&col3=" +
col3 +
"&id3=" +
id3 +
"&id4=" +
id4 +
"&col4=" +
col4 +
"&col5=" +
col5 +
"&id5=" +
id5,
});
}
function calcCareer(col2, id2, col3, id3, col5, id5) {
let table = "lookupqualities";
let pcol1 = "qaId";
let updCol = "value";
let col4 = "qId";
let id4 = "";
$.ajax({
url: "matrixSaveCareer.php",
type: "POST",
data:
"table=" +
table +
"&updCol=" +
updCol +
"&pcol1=" +
pcol1 +
"&col2=" +
col2 +
"&id2=" +
id2 +
"&col3=" +
col3 +
"&id3=" +
id3 +
"&id4=" +
id4 +
"&col4=" +
col4 +
"&col5=" +
col5 +
"&id5=" +
id5,
});
}
function cB(editableObj, table, updCol, pcol1, pid1, col2, id2, col3, id3, col4, id4, col5, id5) {
if ($(editableObj).is(":checked")) {
var updVal = "y";
// this only triggers if called
if (updCol == "dateCompleted") {
var now = new Date();
var m = now.getMonth() + 1;
var d = now.getDate();
var y = now.getFullYear();
m = m < 10 ? "0" + m : m;
d = d < 10 ? "0" + d : d;
updVal = "" + y + "-" + m + "-" + d;
}
} else {
var updVal = "n";
if (updCol == "dateCompleted") updVal = "NULL";
}
sT(editableObj, table, updCol, pcol1, pid1, col2, id2, col3, id3, col4, id4, col5, id5, updVal);
}
function calcCL(checklistId) {
delay(function () {
// call CL calculation within delay cycle to allow CL item write to occur first
$.ajax({
url: "matrixSaveCL.php",
type: "POST",
data: "listId=" + checklistId,
success: function (result) {
// notification action?
// alert(result);
},
});
}, 200);
// alert(checklistId);
}
function editableCol(tableClass, nthChild, isEdit) {
// untested
if (nthChild.length < 1) return;
$("table." + tableClass + " td:nth-child(" + nthChild + ")").attr("contenteditable", isEdit);
}
function toTitleCase(str) {
return str.toLowerCase().replace(/\b\w/g, function (char) {
return char.toUpperCase();
});
}