forked from webismymind/editablegrid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditablegrid_utils.js
More file actions
579 lines (525 loc) · 18 KB
/
Copy patheditablegrid_utils.js
File metadata and controls
579 lines (525 loc) · 18 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
* EditableGrid_utils.js
*
* Copyright 2010 Webismymind SPRL
*
* This file is part of EditableGrid.
*
* EditableGrid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* EditableGrid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EditableGrid. If not, see http://www.gnu.org/licenses.
*
*/
EditableGrid.prototype.unsort = function(a,b)
{
aa = isNaN(a[3]) ? 0 : parseFloat(a[3]);
bb = isNaN(b[3]) ? 0 : parseFloat(b[3]);
return aa-bb;
};
EditableGrid.prototype.sort_numeric = function(a,b)
{
aa = isNaN(a[0]) ? 0 : parseFloat(a[0]);
bb = isNaN(b[0]) ? 0 : parseFloat(b[0]);
return aa-bb;
};
EditableGrid.prototype.sort_boolean = function(a,b)
{
aa = !a[0] || a[0] == "false" ? 0 : 1;
bb = !b[0] || b[0] == "false" ? 0 : 1;
return aa-bb;
};
EditableGrid.prototype.sort_alpha = function(a,b)
{
if (a[0]==b[0]) return 0;
if (a[0]<b[0]) return -1;
return 1;
};
EditableGrid.prototype.sort_date = function(a,b)
{
date = EditableGrid.prototype.checkDate(a[0]);
aa = typeof date == "object" ? date.sortDate : 0;
date = EditableGrid.prototype.checkDate(b[0]);
bb = typeof date == "object" ? date.sortDate : 0;
return aa-bb;
};
/**
* Returns computed style property for element
* @private
*/
EditableGrid.prototype.getStyle = function(element, stylePropCamelStyle, stylePropCSSStyle)
{
stylePropCSSStyle = stylePropCSSStyle || stylePropCamelStyle;
if (element.currentStyle) return element.currentStyle[stylePropCamelStyle];
else if (window.getComputedStyle) return document.defaultView.getComputedStyle(element,null).getPropertyValue(stylePropCSSStyle);
return element.style[stylePropCamelStyle];
};
/**
* Returns true if the element has a static positioning
* @private
*/
EditableGrid.prototype.isStatic = function (element)
{
var position = this.getStyle(element, 'position');
return (!position || position == "static");
};
/**
* Returns auto width for editor
* @private
*/
EditableGrid.prototype.autoWidth = function (element)
{
var paddingLeft = parseInt(this.getStyle(element, "paddingLeft", "padding-left"));
var paddingRight = parseInt(this.getStyle(element, "paddingRight", "padding-right"));
var borderLeft = parseInt(this.getStyle(element, "borderLeftWidth", "border-left-width"));
var borderRight = parseInt(this.getStyle(element, "borderRightWidth", "border-right-width"));
paddingLeft = isNaN(paddingLeft) ? 0 : paddingLeft;
paddingRight = isNaN(paddingRight) ? 0 : paddingRight;
borderLeft = isNaN(borderLeft) ? 0 : borderLeft;
borderRight = isNaN(borderRight) ? 0 : borderRight;
if (this.Browser.Gecko) paddingLeft += 3; // Firefox: input larger then given size in px!
return element.offsetWidth - paddingLeft - paddingRight - borderLeft - borderRight;
};
/**
* Returns auto height for editor
* @private
*/
EditableGrid.prototype.autoHeight = function (element)
{
var paddingTop = parseInt(this.getStyle(element, "paddingTop", "padding-top"));
var paddingBottom = parseInt(this.getStyle(element, "paddingBottom", "padding-bottom"));
var borderTop = parseInt(this.getStyle(element, "borderTopWidth", "border-top-width"));
var borderBottom = parseInt(this.getStyle(element, "borderBottomWidth", "border-bottom-width"));
paddingTop = isNaN(paddingTop) ? 0 : paddingTop;
paddingBottom = isNaN(paddingBottom) ? 0 : paddingBottom;
borderTop = isNaN(borderTop) ? 0 : borderTop;
borderBottom = isNaN(borderBottom) ? 0 : borderBottom;
return element.offsetHeight - paddingTop - paddingBottom - borderTop - borderBottom;
};
/**
* Detects the directory when the js sources can be found
* @private
*/
EditableGrid.prototype.detectDir = function()
{
var base = location.href;
var e = document.getElementsByTagName('base');
for (var i=0; i<e.length; i++) if(e[i].href) base = e[i].href;
var e = document.getElementsByTagName('script');
for (var i=0; i<e.length; i++) {
if (e[i].src && /(^|\/)editablegrid.*\.js([?#].*)?$/i.test(e[i].src)) {
var src = new URI(e[i].src);
var srcAbs = src.toAbsolute(base);
srcAbs.path = srcAbs.path.replace(/[^\/]+$/, ''); // remove filename
delete srcAbs.query;
delete srcAbs.fragment;
return srcAbs.toString();
}
}
return false;
};
/**
* Detect is 2 values are exactly the same (type and value). Numeric NaN are considered the same.
* @param v1
* @param v2
* @return boolean
*/
EditableGrid.prototype.isSame = function(v1, v2)
{
if (v1 === v2) return true;
if (typeof v1 == 'number' && isNaN(v1) && typeof v2 == 'number' && isNaN(v2)) return true;
return false;
};
/**
* class name manipulation
* @private
*/
EditableGrid.prototype.strip = function(str) { return str.replace(/^\s+/, '').replace(/\s+$/, ''); };
EditableGrid.prototype.hasClassName = function(element, className) { return (element.className.length > 0 && (element.className == className || new RegExp("(^|\\s)" + className + "(\\s|$)").test(element.className))); };
EditableGrid.prototype.addClassName = function(element, className) { if (!this.hasClassName(element, className)) element.className += (element.className ? ' ' : '') + className; };
EditableGrid.prototype.removeClassName = function(element, className) { element.className = this.strip(element.className.replace(new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ')); };
/**
* Useful string methods
* @private
*/
String.prototype.trim = function() { return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, "")); };
String.prototype.startsWith = function(str) { return (this.match("^"+str)==str); };
String.prototype.endsWith = function(str) { return (this.match(str+"$")==str); };
// Accepted formats: (for EU just switch month and day)
//
// mm-dd-yyyy
// mm/dd/yyyy
// mm.dd.yyyy
// mm dd yyyy
// mmm dd yyyy
// mmddyyyy
//
// m-d-yyyy
// m/d/yyyy
// m.d.yyyy,
// m d yyyy
// mmm d yyyy
//
// // m-d-yy
// // m/d/yy
// // m.d.yy
// // m d yy,
// // mmm d yy (yy is 20yy)
/**
* Checks validity of a date string
* @private
*/
EditableGrid.prototype.checkDate = function(strDate, strDatestyle)
{
strDatestyle = strDatestyle || "EU";
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
if (!strDate || strDate.length < 1) return 0;
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) return 1;
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
}
booFound = true;
}
}
if (booFound == false) {
if (strDate.length <= 5) return 1;
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
}
// if (strYear.length == 2) strYear = '20' + strYear;
// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
// get and check day
intday = parseInt(strDay, 10);
if (isNaN(intday)) return 2;
// get and check month
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
}
}
if (isNaN(intMonth)) return 3;
}
if (intMonth>12 || intMonth<1) return 5;
// get and check year
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) return 4;
if (intYear < 1900 || intYear > 2100) return 11;
// check day in month
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) return 6;
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) return 7;
if (intMonth == 2) {
if (intday < 1) return 8;
if (LeapYear(intYear) == true) { if (intday > 29) return 9; }
else if (intday > 28) return 10;
}
// return formatted date
return {
formattedDate: (strDatestyle == "US" ? strMonthArray[intMonth-1] + " " + intday+" " + strYear : intday + " " + strMonthArray[intMonth-1]/*.toLowerCase()*/ + " " + strYear),
sortDate: Date.parse(intMonth + "/" + intday + "/" + intYear),
dbDate: intYear + "-" + intMonth + "-" + intday
};
};
function LeapYear(intYear)
{
if (intYear % 100 == 0) { if (intYear % 400 == 0) return true; }
else if ((intYear % 4) == 0) return true;
return false;
}
// See RFC3986
URI = function(uri)
{
this.scheme = null;
this.authority = null;
this.path = '';
this.query = null;
this.fragment = null;
this.parse = function(uri) {
var m = uri.match(/^(([A-Za-z][0-9A-Za-z+.-]*)(:))?((\/\/)([^\/?#]*))?([^?#]*)((\?)([^#]*))?((#)(.*))?/);
this.scheme = m[3] ? m[2] : null;
this.authority = m[5] ? m[6] : null;
this.path = m[7];
this.query = m[9] ? m[10] : null;
this.fragment = m[12] ? m[13] : null;
return this;
};
this.toString = function() {
var result = '';
if(this.scheme != null) result = result + this.scheme + ':';
if(this.authority != null) result = result + '//' + this.authority;
if(this.path != null) result = result + this.path;
if(this.query != null) result = result + '?' + this.query;
if(this.fragment != null) result = result + '#' + this.fragment;
return result;
};
this.toAbsolute = function(base) {
var base = new URI(base);
var r = this;
var t = new URI;
if(base.scheme == null) return false;
if(r.scheme != null && r.scheme.toLowerCase() == base.scheme.toLowerCase()) {
r.scheme = null;
}
if(r.scheme != null) {
t.scheme = r.scheme;
t.authority = r.authority;
t.path = removeDotSegments(r.path);
t.query = r.query;
} else {
if(r.authority != null) {
t.authority = r.authority;
t.path = removeDotSegments(r.path);
t.query = r.query;
} else {
if(r.path == '') {
t.path = base.path;
if(r.query != null) {
t.query = r.query;
} else {
t.query = base.query;
}
} else {
if(r.path.substr(0,1) == '/') {
t.path = removeDotSegments(r.path);
} else {
if(base.authority != null && base.path == '') {
t.path = '/'+r.path;
} else {
t.path = base.path.replace(/[^\/]+$/,'')+r.path;
}
t.path = removeDotSegments(t.path);
}
t.query = r.query;
}
t.authority = base.authority;
}
t.scheme = base.scheme;
}
t.fragment = r.fragment;
return t;
};
function removeDotSegments(path) {
var out = '';
while(path) {
if(path.substr(0,3)=='../' || path.substr(0,2)=='./') {
path = path.replace(/^\.+/,'').substr(1);
} else if(path.substr(0,3)=='/./' || path=='/.') {
path = '/'+path.substr(3);
} else if(path.substr(0,4)=='/../' || path=='/..') {
path = '/'+path.substr(4);
out = out.replace(/\/?[^\/]*$/, '');
} else if(path=='.' || path=='..') {
path = '';
} else {
var rm = path.match(/^\/?[^\/]*/)[0];
path = path.substr(rm.length);
out = out + rm;
}
}
return out;
}
if(uri) {
this.parse(uri);
}
};
function get_html_translation_table (table, quote_style) {
// http://kevin.vanzonneveld.net
// + original by: Philip Peterson
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: noname
// + bugfixed by: Alex
// + bugfixed by: Marco
// + bugfixed by: madipta
// + improved by: KELAN
// + improved by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: Frank Forte
// + bugfixed by: T.Wild
// + input by: Ratheous
// % note: It has been decided that we're not going to add global
// % note: dependencies to php.js, meaning the constants are not
// % note: real constants, but strings instead. Integers are also supported if someone
// % note: chooses to create the constants themselves.
// * example 1: get_html_translation_table('HTML_SPECIALCHARS');
// * returns 1: {'"': '"', '&': '&', '<': '<', '>': '>'}
var entities = {}, hash_map = {}, decimal = 0, symbol = '';
var constMappingTable = {}, constMappingQuoteStyle = {};
var useTable = {}, useQuoteStyle = {};
// Translate arguments
constMappingTable[0] = 'HTML_SPECIALCHARS';
constMappingTable[1] = 'HTML_ENTITIES';
constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
constMappingQuoteStyle[2] = 'ENT_COMPAT';
constMappingQuoteStyle[3] = 'ENT_QUOTES';
useTable = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';
if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
throw new Error("Table: "+useTable+' not supported');
// return false;
}
entities['38'] = '&';
if (useTable === 'HTML_ENTITIES') {
entities['160'] = ' ';
entities['161'] = '¡';
entities['162'] = '¢';
entities['163'] = '£';
entities['164'] = '¤';
entities['165'] = '¥';
entities['166'] = '¦';
entities['167'] = '§';
entities['168'] = '¨';
entities['169'] = '©';
entities['170'] = 'ª';
entities['171'] = '«';
entities['172'] = '¬';
entities['173'] = '­';
entities['174'] = '®';
entities['175'] = '¯';
entities['176'] = '°';
entities['177'] = '±';
entities['178'] = '²';
entities['179'] = '³';
entities['180'] = '´';
entities['181'] = 'µ';
entities['182'] = '¶';
entities['183'] = '·';
entities['184'] = '¸';
entities['185'] = '¹';
entities['186'] = 'º';
entities['187'] = '»';
entities['188'] = '¼';
entities['189'] = '½';
entities['190'] = '¾';
entities['191'] = '¿';
entities['192'] = 'À';
entities['193'] = 'Á';
entities['194'] = 'Â';
entities['195'] = 'Ã';
entities['196'] = 'Ä';
entities['197'] = 'Å';
entities['198'] = 'Æ';
entities['199'] = 'Ç';
entities['200'] = 'È';
entities['201'] = 'É';
entities['202'] = 'Ê';
entities['203'] = 'Ë';
entities['204'] = 'Ì';
entities['205'] = 'Í';
entities['206'] = 'Î';
entities['207'] = 'Ï';
entities['208'] = 'Ð';
entities['209'] = 'Ñ';
entities['210'] = 'Ò';
entities['211'] = 'Ó';
entities['212'] = 'Ô';
entities['213'] = 'Õ';
entities['214'] = 'Ö';
entities['215'] = '×';
entities['216'] = 'Ø';
entities['217'] = 'Ù';
entities['218'] = 'Ú';
entities['219'] = 'Û';
entities['220'] = 'Ü';
entities['221'] = 'Ý';
entities['222'] = 'Þ';
entities['223'] = 'ß';
entities['224'] = 'à';
entities['225'] = 'á';
entities['226'] = 'â';
entities['227'] = 'ã';
entities['228'] = 'ä';
entities['229'] = 'å';
entities['230'] = 'æ';
entities['231'] = 'ç';
entities['232'] = 'è';
entities['233'] = 'é';
entities['234'] = 'ê';
entities['235'] = 'ë';
entities['236'] = 'ì';
entities['237'] = 'í';
entities['238'] = 'î';
entities['239'] = 'ï';
entities['240'] = 'ð';
entities['241'] = 'ñ';
entities['242'] = 'ò';
entities['243'] = 'ó';
entities['244'] = 'ô';
entities['245'] = 'õ';
entities['246'] = 'ö';
entities['247'] = '÷';
entities['248'] = 'ø';
entities['249'] = 'ù';
entities['250'] = 'ú';
entities['251'] = 'û';
entities['252'] = 'ü';
entities['253'] = 'ý';
entities['254'] = 'þ';
entities['255'] = 'ÿ';
}
if (useQuoteStyle !== 'ENT_NOQUOTES') {
entities['34'] = '"';
}
if (useQuoteStyle === 'ENT_QUOTES') {
entities['39'] = ''';
}
entities['60'] = '<';
entities['62'] = '>';
// ascii decimals to real symbols
for (decimal in entities) {
symbol = String.fromCharCode(decimal);
hash_map[symbol] = entities[decimal];
}
return hash_map;
}
function htmlentities(string, quote_style)
{
var hash_map = {}, symbol = '', tmp_str = '';
tmp_str = string.toString();
if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) return false;
hash_map["'"] = ''';
for (symbol in hash_map) tmp_str = tmp_str.split(symbol).join(hash_map[symbol]);
return tmp_str;
}
function htmlspecialchars(string, quote_style)
{
var hash_map = {}, symbol = '', tmp_str = '';
tmp_str = string.toString();
if (false === (hash_map = this.get_html_translation_table('HTML_SPECIALCHARS', quote_style))) return false;
for (symbol in hash_map) tmp_str = tmp_str.split(symbol).join(hash_map[symbol]);
return tmp_str;
}