forked from tonytomov/jqGrid
-
Notifications
You must be signed in to change notification settings - Fork 198
/
jquery.fmatter.js
987 lines (966 loc) · 35.4 KB
/
jquery.fmatter.js
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
/**
* formatter for values but most of the values if for jqGrid
* Some of this was inspired and based on how YUI does the table datagrid but in jQuery fashion
* we are trying to keep it as light as possible
* Joshua Burnett josh@9ci.com
* http://www.greenbill.com
*
* Changes from Tony Tomov tony@trirand.com
* Changed by Oleg Kiriljuk, oleg.kiriljuk@ok-soft-gmbh.com
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl-2.0.html
*
**/
/*jshint eqeqeq:false */
/*jslint
browser, devel, for, multivar, this, white
*/
/*global jQuery, define */
(function (factory) {
"use strict";
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(["jquery", "./grid.base"], factory);
} else if (typeof exports === "object") {
// Node/CommonJS
factory(require("jquery"));
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
"use strict";
$.jgrid = $.jgrid || {};
var jgrid = $.jgrid, getGridRes = jgrid.getMethod("getGridRes"), base = $.fn.jqGrid;
// begin module jquery.fmatter
$.fmatter = $.fmatter || {};
var fmatter = $.fmatter,
getOptionByName = function (colModel, name) {
var option = colModel.formatoptions || {};
if (option.hasOwnProperty(name)) {
return option[name];
} else {
return (colModel.editoptions || {})[name];
}
},
parseCheckboxOptions = function (options) {
var colModel = options.colModel || options.cm, checked, unchecked,
title = colModel.title !== false ?
" title='" + (options.colName || colModel.name) + "'" :
"",
getOption = function (name) {
return getOptionByName(colModel, name);
},
checkedClasses = getOption("checkedClass"),
uncheckedClasses = getOption("uncheckedClass"),
value = getOption("value"),
yes = typeof value === "string" ? (value.split(":")[0] || "Yes") : "Yes",
no = typeof value === "string" ? (value.split(":")[1] || "No") : "No",
buildCheckbox = function (classes) {
return "<i class='" + classes + "'" + title + "></i>";
},
disabled = getOption("disabled");
if (disabled === undefined) {
disabled = jgrid.formatter.checkbox.disabled;
}
if (disabled === true && base.isInCommonIconClass.call(this, "fa")) {
checkedClasses = checkedClasses || "fa fa-check-square-o fa-lg";
checked = buildCheckbox(checkedClasses);
unchecked = buildCheckbox(uncheckedClasses || "fa fa-square-o fa-lg");
} else if (disabled === true && base.isInCommonIconClass.call(this, "glyphicon")) {
checkedClasses = checkedClasses || "glyphicon glyphicon-check";
checked = buildCheckbox(checkedClasses);
unchecked = buildCheckbox(uncheckedClasses || "glyphicon glyphicon-unchecked");
} else {
checkedClasses = "";
title += disabled === true ? " disabled='disabled'" : "";
checked = "<input type='checkbox' checked='checked'" + title + " />";
unchecked = "<input type='checkbox'" + title + " />";
}
return {
checkedClasses: checkedClasses,
checked: checked,
unchecked: unchecked,
yes: yes,
no: no
};
},
// http://jsperf.com/regex-vs-indexof-vs-in/12
/*YesObject = Object.create(null, {
1: { value: 1 },
x: { value: 1 },
"true": { value: 1 },
yes: { value: 1 },
on: { value: 1 }
}),
NoObject = Object.create(null, {
0: { value: 1 },
"false": { value: 1 },
no: { value: 1 },
off: { value: 1 }
});*/
// one can use typeof Object.create != "function" and use either
// Object.create or simple object firm, but the performance differences
// are so low, that the compatibility to IE8 is more important
YesObject = { 1: 1, x: 1, "true": 1, yes: 1, on: 1 },
NoObject = { 0: 1, "false": 1, no: 1, off: 1 };
$.extend(true, jgrid, {
formatter: { // setting common formatter settings, which are independent from the language and locale
date: {
parseRe: /[#%\\\/:_;.,\t\s\-]/,
masks: {
ISO8601Long: "Y-m-d H:i:s",
ISO8601Short: "Y-m-d",
SortableDateTime: "Y-m-d\\TH:i:s",
UniversalSortableDateTime: "Y-m-d H:i:sO"
},
reformatAfterEdit: true,
userLocalTime: false
},
baseLinkUrl: "",
showAction: "",
target: "",
checkbox: { disabled: true },
idName: "id"
},
cmTemplate: {
integerStr: {
formatter: "integer", align: "right", sorttype: "integer",
searchoptions: { sopt: ["eq", "ne", "lt", "le", "gt", "ge"] }
},
integer: {
formatter: "integer", align: "right", sorttype: "integer",
convertOnSave: function (options) {
var nData = options.newValue;
return isNaN(nData) ? nData : parseInt(nData, 10);
},
searchoptions: { sopt: ["eq", "ne", "lt", "le", "gt", "ge"] }
},
numberStr: {
formatter: "number", align: "right", sorttype: "number",
searchoptions: { sopt: ["eq", "ne", "lt", "le", "gt", "ge"] }
},
number: {
formatter: "number", align: "right", sorttype: "number",
convertOnSave: function (options) {
var nData = options.newValue;
return isNaN(nData) ? nData : parseFloat(nData);
},
searchoptions: { sopt: ["eq", "ne", "lt", "le", "gt", "ge"] }
},
booleanCheckbox: {
align: "center", formatter: "checkbox",
edittype: "checkbox", editoptions: { value: "true:false", defaultValue: "false" },
convertOnSave: function (options) {
var newValue = options.newValue,
checkboxOptions = parseCheckboxOptions.call(this, options),
lowerCaseNewData = String(newValue).toLowerCase();
if (YesObject[lowerCaseNewData] || lowerCaseNewData === checkboxOptions.yes.toLowerCase()) {
newValue = true;
} else if (NoObject[lowerCaseNewData] || lowerCaseNewData === checkboxOptions.no.toLowerCase()) {
newValue = false;
}
return newValue;
},
stype: "select", searchoptions: { sopt: ["eq", "ne"], value: ":Any;true:Yes;false:No" }
},
// TODO: add cmTemplate for currency and date
actions: function () {
var p = this.p;
return {
formatter: "actions",
width: (p != null && (base.isInCommonIconClass.call(this, "fa") || base.isInCommonIconClass.call(this, "glyphicon")) ?
($(this).jqGrid("isBootstrapGuiStyle") ? 45 : 39) : 40) + (jgrid.cellWidth() ? 5 : 0),
align: "center",
label: "",
autoResizable: false,
frozen: true,
fixed: true,
hidedlg: true,
resizable: false,
sortable: false,
search: false,
editable: false,
viewable: false
};
}
}
});
jgrid.cmTemplate.booleanCheckboxFa = jgrid.cmTemplate.booleanCheckbox;
//opts can be id:row id for the row, rowdata:the data for the row, colmodel:the column model for this column
//example {id:1234,}
$.extend(fmatter, {
// one can consider to use $.type instead of some functions below (see http://api.jquery.com/jQuery.type/)
isObject: function (o) {
return (o && (typeof o === "object" || $.isFunction(o))) || false;
},
isNumber: function (o) {
// probably Number.isFinite can be used instead.
return typeof o === "number" && isFinite(o); // return false for +infinity, -infinity, or NaN
},
isValue: function (o) {
return (this.isObject(o) || typeof o === "string" || this.isNumber(o) || typeof o === "boolean");
},
isEmpty: function (o) {
if (typeof o !== "string" && this.isValue(o)) {
return false;
}
if (!this.isValue(o)) {
return true;
}
o = $.trim(o).replace(/ /ig, "").replace(/ /ig, "");
return o === "";
},
NumberFormat: function (nData, opts) {
var isNumber = fmatter.isNumber;
if (!isNumber(nData)) {
nData *= 1;
}
if (isNumber(nData)) {
var bNegative = (nData < 0);
var sOutput = String(nData);
var sDecimalSeparator = opts.decimalSeparator || ".";
var nDotIndex;
if (isNumber(opts.decimalPlaces)) {
// Round to the correct decimal place
var nDecimalPlaces = opts.decimalPlaces;
var nDecimal = Math.pow(10, nDecimalPlaces);
sOutput = String(Math.round(nData * nDecimal) / nDecimal);
nDotIndex = sOutput.lastIndexOf(".");
if (nDecimalPlaces > 0) {
// Add the decimal separator
if (nDotIndex < 0) {
sOutput += sDecimalSeparator;
nDotIndex = sOutput.length - 1;
} else if (sDecimalSeparator !== ".") { // Replace the "."
sOutput = sOutput.replace(".", sDecimalSeparator);
}
// Add missing zeros
while ((sOutput.length - 1 - nDotIndex) < nDecimalPlaces) {
sOutput += "0";
}
}
}
if (opts.thousandsSeparator) {
var sThousandsSeparator = opts.thousandsSeparator;
nDotIndex = sOutput.lastIndexOf(sDecimalSeparator);
nDotIndex = (nDotIndex > -1) ? nDotIndex : sOutput.length;
// we cut the part after the point for integer numbers
// it will prevent storing/restoring of wrong numbers during inline editing
var sNewOutput = opts.decimalSeparator === undefined ? "" : sOutput.substring(nDotIndex);
var nCount = -1, i;
for (i = nDotIndex; i > 0; i--) {
nCount++;
if ((nCount % 3 === 0) && (i !== nDotIndex) && (!bNegative || (i > 1))) {
sNewOutput = sThousandsSeparator + sNewOutput;
}
sNewOutput = sOutput.charAt(i - 1) + sNewOutput;
}
sOutput = sNewOutput;
}
return sOutput;
}
return nData;
}
});
var $FnFmatter = function (formatType, cellval, opts, rwd, act) {
// build main options before element iteration
var v = cellval;
opts = $.extend({}, getGridRes.call($(this), "formatter"), opts);
try {
v = $.fn.fmatter[formatType].call(this, cellval, opts, rwd, act);
} catch (ignore) { }
return v;
};
$.fn.fmatter = $FnFmatter;
$FnFmatter.getCellBuilder = function (formatType, opts, act) {
var cellBuilder = $.fn.fmatter[formatType] != null ? $.fn.fmatter[formatType].getCellBuilder : null;
return $.isFunction(cellBuilder) ?
cellBuilder.call(this, $.extend({}, getGridRes.call($(this), "formatter"), opts), act) :
null;
};
$FnFmatter.defaultFormat = function (cellval, opts) {
return (fmatter.isValue(cellval) && cellval !== "") ? cellval : opts.defaultValue || " ";
};
var defaultFormat = $FnFmatter.defaultFormat,
formatCheckboxValue = function (cellValue, checkboxOptions, colModel) {
if (cellValue === undefined || fmatter.isEmpty(cellValue)) {
var defaultValue = getOptionByName(colModel, "defaultValue");
if (defaultValue === undefined) {
cellValue = checkboxOptions.no;
}
cellValue = defaultValue;
}
// see http://jsperf.com/regex-vs-indexof-vs-in/12
cellValue = String(cellValue).toLowerCase();
return YesObject[cellValue] || cellValue === checkboxOptions.yes.toLowerCase() ?
checkboxOptions.checked :
checkboxOptions.unchecked;
};
$FnFmatter.email = function (cellval, opts) {
if (!fmatter.isEmpty(cellval)) {
return "<a href='mailto:" + cellval + "'>" + cellval + "</a>";
}
return defaultFormat(cellval, opts);
};
$FnFmatter.checkbox = function (cellValue, options) {
var checkboxOptions = parseCheckboxOptions.call(this, options);
return formatCheckboxValue(cellValue, checkboxOptions, options.colModel);
};
$FnFmatter.checkbox.getCellBuilder = function (options) {
var checkboxOptions, colModel = options.colModel;
options.colName = options.colName || this.p.colNames[options.pos];
checkboxOptions = parseCheckboxOptions.call(this, options);
return function (cellValue) {
return formatCheckboxValue(cellValue, checkboxOptions, colModel);
};
};
$FnFmatter.checkbox.unformat = function (cellValue, options, elem) {
var checkboxOptions = parseCheckboxOptions.call(this, options),
$elem = $(elem);
return (checkboxOptions.checkedClasses ?
jgrid.hasAllClasses($elem.children("i"), checkboxOptions.checkedClasses) :
$elem.children("input").is(":checked")) ?
checkboxOptions.yes :
checkboxOptions.no;
};
$FnFmatter.checkboxFontAwesome4 = $FnFmatter.checkbox;
$FnFmatter.checkboxFontAwesome4.getCellBuilder = $FnFmatter.checkbox.getCellBuilder;
$FnFmatter.checkboxFontAwesome4.unformat = $FnFmatter.checkbox.unformat;
$FnFmatter.link = function (cellval, opts) {
var colModel = opts.colModel, target = "", op = { target: opts.target };
if (colModel != null) {
op = $.extend({}, op, colModel.formatoptions || {});
}
if (op.target) { target = "target=" + op.target; }
if (!fmatter.isEmpty(cellval)) {
return "<a " + target + " href='" + cellval + "'>" + cellval + "</a>";
}
return defaultFormat(cellval, op);
};
$FnFmatter.showlink = function (cellval, opts, rowData) {
var self = this, colModel = opts.colModel,
op = {
baseLinkUrl: opts.baseLinkUrl,
showAction: opts.showAction,
addParam: opts.addParam || "",
target: opts.target,
idName: opts.idName,
hrefDefaultValue: "#"
},
target = "",
idUrl,
idParam,
addParam,
getOptionValue = function (option) {
return $.isFunction(option) ?
option.call(self, {
cellValue: cellval,
rowid: opts.rowId,
rowData: rowData,
options: op
}) :
option || "";
};
if (colModel != null) {
op = $.extend({}, op, colModel.formatoptions || {});
}
if (op.target) {
target = "target=" + getOptionValue(op.target);
}
idUrl = getOptionValue(op.baseLinkUrl) + getOptionValue(op.showAction);
idParam = op.idName ? encodeURIComponent(getOptionValue(op.idName)) + "=" + encodeURIComponent(getOptionValue(op.rowId) || opts.rowId) : "";
addParam = getOptionValue(op.addParam);
if (typeof addParam === "object" && addParam !== null) {
// add "&" only in case of usage object for of addParam
addParam = (idParam !== "" ? "&" : "") + $.param(addParam);
}
idUrl += !idParam && !addParam ? "" : "?" + idParam + addParam;
if (idUrl === "") {
idUrl = getOptionValue(op.hrefDefaultValue);
}
if (typeof cellval === "string" || fmatter.isNumber(cellval) || $.isFunction(op.cellValue)) {
//add this one even if cellval is blank string
return "<a " + target + " href='" + idUrl + "'>" +
($.isFunction(op.cellValue) ? getOptionValue(op.cellValue) : cellval) +
"</a>";
}
// the code below will be called typically for undefined cellval or
// if cellval have null value or some other unclear value like an object
// and no cellValue callback function are defined "to decode" the value
return defaultFormat(cellval, op);
};
$FnFmatter.showlink.getCellBuilder = function (opts1) {
var op = {
baseLinkUrl: opts1.baseLinkUrl,
showAction: opts1.showAction,
addParam: opts1.addParam || "",
target: opts1.target,
idName: opts1.idName,
hrefDefaultValue: "#"
},
colModel = opts1.colModel;
if (colModel != null) {
op = $.extend({}, op, colModel.formatoptions || {});
}
return function (cellval, opts, rowData) {
var self = this, rowid = opts.rowId, target = "", idUrl, idParam, addParam,
getOptionValue = function (option) {
return $.isFunction(option) ?
option.call(self, {
cellValue: cellval,
rowid: rowid,
rowData: rowData,
options: op
}) :
option || "";
};
if (op.target) {
target = "target=" + getOptionValue(op.target);
}
idUrl = getOptionValue(op.baseLinkUrl) + getOptionValue(op.showAction);
idParam = op.idName ? encodeURIComponent(getOptionValue(op.idName)) + "=" + encodeURIComponent(getOptionValue(rowid) || opts.rowId) : "";
addParam = getOptionValue(op.addParam);
if (typeof addParam === "object" && addParam !== null) {
// add "&" only in case of usage object for of addParam
addParam = (idParam !== "" ? "&" : "") + $.param(addParam);
}
idUrl += !idParam && !addParam ? "" : "?" + idParam + addParam;
if (idUrl === "") {
idUrl = getOptionValue(op.hrefDefaultValue);
}
if (typeof cellval === "string" || fmatter.isNumber(cellval) || $.isFunction(op.cellValue)) {
//add this one even if cellval is blank string
return "<a " + target + " href='" + idUrl + "'>" +
($.isFunction(op.cellValue) ? getOptionValue(op.cellValue) : cellval) +
"</a>";
}
// the code below will be called typically for undefined cellval or
// if cellval have null value or some other unclear value like an object
// and no cellValue callback function are defined "to decode" the value
return defaultFormat(cellval, op);
};
};
$FnFmatter.showlink.pageFinalization = function (iCol) {
var $self = $(this), p = this.p, colModel = p.colModel, cm = colModel[iCol], iRow, rows = this.rows, nRows = rows.length, row, td,
onClick = function (e) {
var $tr = $(this).closest(".jqgrow");
if ($tr.length > 0) {
return cm.formatoptions.onClick.call($self[0], {
iCol: iCol,
iRow: $tr[0].rowIndex,
rowid: $tr.attr("id"),
cm: cm,
cmName: cm.name,
cellValue: $(this).text(),
a: this,
event: e
});
}
};
if (cm.formatoptions != null && $.isFunction(cm.formatoptions.onClick)) {
for (iRow = 0; iRow < nRows; iRow++) {
row = rows[iRow];
if ($(row).hasClass("jqgrow")) {
td = row.cells[iCol];
if (cm.autoResizable && td != null && $(td.firstChild).hasClass(p.autoResizing.wrapperClassName)) {
td = td.firstChild;
}
if (td != null) {
$(td.firstChild).bind("click", onClick);
}
}
}
}
};
var insertPrefixAndSuffix = function (sOutput, opts) {
// Prepend prefix
sOutput = (opts.prefix) ? opts.prefix + sOutput : sOutput;
// Append suffix
return (opts.suffix) ? sOutput + opts.suffix : sOutput;
},
numberHelper = function (cellval, opts, formatType) {
var colModel = opts.colModel, op = $.extend({}, opts[formatType]);
if (colModel != null) {
op = $.extend({}, op, colModel.formatoptions || {});
}
if (fmatter.isEmpty(cellval)) {
return insertPrefixAndSuffix(op.defaultValue, op);
}
return insertPrefixAndSuffix(fmatter.NumberFormat(cellval, op), op);
};
$FnFmatter.integer = function (cellval, opts) {
return numberHelper(cellval, opts, "integer");
};
$FnFmatter.number = function (cellval, opts) {
return numberHelper(cellval, opts, "number");
};
$FnFmatter.currency = function (cellval, opts) {
return numberHelper(cellval, opts, "currency");
};
var numberCellBuilder = function (opts, formatType) {
var colModel = opts.colModel, op = $.extend({}, opts[formatType]);
if (colModel != null) {
op = $.extend({}, op, colModel.formatoptions || {});
}
var numberFormat = fmatter.NumberFormat,
defaultValue = op.defaultValue ? insertPrefixAndSuffix(op.defaultValue, op) : "";
return function (cellValue) {
if (fmatter.isEmpty(cellValue)) { return defaultValue; }
return insertPrefixAndSuffix(numberFormat(cellValue, op), op);
};
};
$FnFmatter.integer.getCellBuilder = function (options) {
return numberCellBuilder(options, "integer");
};
$FnFmatter.number.getCellBuilder = function (options) {
return numberCellBuilder(options, "number");
};
$FnFmatter.currency.getCellBuilder = function (options) {
return numberCellBuilder(options, "currency");
};
$FnFmatter.date = function (cellval, opts, rwd, act) {
var colModel = opts.colModel, op = $.extend({}, opts.date);
if (colModel != null) {
op = $.extend({}, op, colModel.formatoptions || {});
}
if (!op.reformatAfterEdit && act === "edit") {
return defaultFormat(cellval, op);
}
if (!fmatter.isEmpty(cellval)) {
return jgrid.parseDate.call(this, op.srcformat, cellval, op.newformat, op);
}
return defaultFormat(cellval, op);
};
$FnFmatter.date.getCellBuilder = function (opts, act) {
var op = $.extend({}, opts.date);
if (opts.colModel != null) {
op = $.extend({}, op, opts.colModel.formatoptions || {});
}
var parseDate = jgrid.parseDate,
srcformat = op.srcformat, newformat = op.newformat;
if (!op.reformatAfterEdit && act === "edit") {
return function (cellValue) {
return defaultFormat(cellValue, op);
};
}
return function (cellValue) {
return fmatter.isEmpty(cellValue) ?
defaultFormat(cellValue, op) :
parseDate.call(this, srcformat, cellValue, newformat, op);
};
};
$FnFmatter.select = function (cellval, opts) {
var ret = [], colModel = opts.colModel, defaultValue,
op = $.extend({}, colModel.editoptions || {}, colModel.formatoptions || {}),
oSelect = op.value, sep = op.separator || ":", delim = op.delimiter || ";";
if (oSelect) {
var msl = op.multiple === true ? true : false, scell = [], sv,
mapFunc = function (n, j) { if (j > 0) { return n; } };
if (msl) {
scell = $.map(String(cellval).split(","), function (n) { return $.trim(n); });
}
if (typeof oSelect === "string") {
// maybe here we can use some caching with care ????
var so = oSelect.split(delim), i, v;
for (i = 0; i < so.length; i++) {
sv = so[i].split(sep);
if (sv.length > 2) {
sv[1] = $.map(sv, mapFunc).join(sep);
}
v = $.trim(sv[0]);
if (op.defaultValue === v) {
defaultValue = sv[1];
}
if (msl) {
if ($.inArray(v, scell) > -1) {
ret.push(sv[1]);
}
} else if (v === $.trim(cellval)) {
ret = [sv[1]];
break;
}
}
} else if (fmatter.isObject(oSelect)) {
defaultValue = oSelect[op.defaultValue];
if (msl) {
ret = $.map(scell, function (n) {
return oSelect[n];
});
} else {
ret = [oSelect[cellval] === undefined ? "" : oSelect[cellval]];
}
}
}
cellval = ret.join(", ");
return cellval !== "" ? cellval :
(op.defaultValue !== undefined ? defaultValue : defaultFormat(cellval, op));
};
$FnFmatter.select.getCellBuilder = function (opts) {
// jqGrid specific
var colModel = opts.colModel, $fnDefaultFormat = $FnFmatter.defaultFormat,
op = $.extend({}, colModel.editoptions || {}, colModel.formatoptions || {}),
oSelect = op.value, sep = op.separator || ":", delim = op.delimiter || ";",
defaultValue, defaultValueDefined = op.defaultValue !== undefined,
isMultiple = op.multiple === true ? true : false, sv, so, i, nOpts, selOptions = {},
mapFunc = function (n, j) { if (j > 0) { return n; } };
if (typeof oSelect === "string") {
// maybe here we can use some caching with care ????
so = oSelect.split(delim);
nOpts = so.length;
for (i = nOpts - 1; i >= 0; i--) {
sv = so[i].split(sep);
if (sv.length > 2) {
sv[1] = $.map(sv, mapFunc).join(sep);
}
selOptions[$.trim(sv[0])] = sv[1];
}
} else if (fmatter.isObject(oSelect)) {
selOptions = oSelect;
} else {
return function (cellValue) {
return cellValue ? String(cellValue) : $fnDefaultFormat(cellValue, op);
};
}
if (defaultValueDefined) {
defaultValue = selOptions[op.defaultValue];
}
return isMultiple ?
function (cellValue) {
var ret = [], iOpt,
splitedCell = $.map(String(cellValue).split(","), function (n) { return $.trim(n); });
for (iOpt = 0; iOpt < splitedCell.length; iOpt++) {
cellValue = splitedCell[iOpt];
if (selOptions.hasOwnProperty(cellValue)) {
ret.push(selOptions[cellValue]);
}
}
cellValue = ret.join(", ");
return cellValue !== "" ? cellValue :
(defaultValueDefined ? defaultValue : $fnDefaultFormat(cellValue, op));
} :
function (cellValue) {
var ret = selOptions[String(cellValue)];
return ret !== "" && ret !== undefined ? ret :
(defaultValueDefined ? defaultValue : $fnDefaultFormat(cellValue, op));
};
};
$FnFmatter.rowactions = function (e, act) {
var $tr = $(this).closest("tr.jqgrow"), rid = $tr.attr("id"),
$id = $(this).closest("table.ui-jqgrid-btable").attr("id").replace(/_frozen([^_]*)$/, "$1"),
$grid = $("#" + jgrid.jqID($id)), $t = $grid[0], p = $t.p, i, n, customAction, actop,
getTop = function () {
var tr = $tr[0], gbox = $grid.closest(".ui-jqgrid")[0];
if (tr.getBoundingClientRect != null && gbox.getBoundingClientRect != null) {
return tr.getBoundingClientRect().top + $tr.outerHeight() - gbox.getBoundingClientRect().top;
}
return $tr.offset().top + $tr.outerHeight() - $(gbox).offset().top;
},
cm = p.colModel[jgrid.getCellIndex(this)],
op = $.extend(true, { extraparam: {} }, jgrid.actionsNav || {}, p.actionsNavOptions || {}, cm.formatoptions || {});
if (p.editOptions !== undefined) {
op.editOptions = $.extend(true, op.editOptions || {}, p.editOptions);
}
if (p.delOptions !== undefined) {
op.delOptions = p.delOptions;
}
if ($tr.hasClass("jqgrid-new-row")) {
op.extraparam[p.prmNames.oper] = p.prmNames.addoper;
}
actop = {
keys: op.keys,
oneditfunc: op.onEdit,
successfunc: op.onSuccess,
url: op.url,
extraparam: op.extraparam,
aftersavefunc: op.afterSave,
errorfunc: op.onError,
afterrestorefunc: op.afterRestore,
restoreAfterError: op.restoreAfterError,
mtype: op.mtype
};
if ((!p.multiselect && rid !== p.selrow) || (p.multiselect && $.inArray(rid, p.selarrrow) < 0)) {
$grid.jqGrid("setSelection", rid, true, e);
} else {
jgrid.fullBoolFeedback.call($t, "onSelectRow", "jqGridSelectRow", rid, true, e);
}
switch (act) {
case "edit":
$grid.jqGrid("editRow", rid, actop);
break;
case "save":
$grid.jqGrid("saveRow", rid, actop);
break;
case "cancel":
$grid.jqGrid("restoreRow", rid, op.afterRestore);
break;
case "del":
op.delOptions = op.delOptions || {};
if (op.delOptions.top === undefined) {
op.delOptions.top = getTop();
}
$grid.jqGrid("delGridRow", rid, op.delOptions);
break;
case "formedit":
op.editOptions = op.editOptions || {};
if (op.editOptions.top === undefined) {
op.editOptions.top = getTop();
op.editOptions.recreateForm = true;
}
$grid.jqGrid("editGridRow", rid, op.editOptions);
break;
default:
if (op.custom != null && op.custom.length > 0) {
n = op.custom.length;
for (i = 0; i < n; i++) {
customAction = op.custom[i];
if (customAction.action === act && $.isFunction(customAction.onClick)) {
customAction.onClick.call($t, { rowid: rid, event: e, action: act, options: customAction });
}
}
}
}
if (e.stopPropagation) {
e.stopPropagation();
}
return false; // prevent other processing of the click on the row
};
$FnFmatter.actions = function (cellval, opts, rwd, act) {
var rowid = opts.rowId, str = "", $t = this, p = $t.p, $self = $($t), i, customAction, info, displayMask = {},
edit = getGridRes.call($self, "edit") || {},
op = $.extend({
editbutton: true,
delbutton: true,
editformbutton: false,
commonIconClass: "ui-icon",
editicon: "ui-icon-pencil",
delicon: "ui-icon-trash",
saveicon: "ui-icon-disk",
cancelicon: "ui-icon-cancel",
savetitle: edit.bSubmit || "",
canceltitle: edit.bCancel || ""
},
getGridRes.call($self, "nav") || {},
jgrid.nav || {},
p.navOptions || {},
getGridRes.call($self, "actionsNav") || {},
jgrid.actionsNav || {},
p.actionsNavOptions || {},
opts.colModel.formatoptions || {}),
cssIconClass = function (name) {
return jgrid.mergeCssClasses(op.commonIconClass, op[name + "icon"]);
},
hoverClass = $self.jqGrid("getGuiStyles", "states.hover"),
hoverAttributes = "onmouseover=\"jQuery(this).addClass('" + hoverClass +
"');\" onmouseout=\"jQuery(this).removeClass('" + hoverClass + "');\"",
buttonInfos = [
{ action: "edit", actionName: "formedit", display: op.editformbutton },
{ action: "edit", display: !op.editformbutton && op.editbutton },
{ action: "del", idPrefix: "Delete", display: op.delbutton },
{ action: "save", display: op.editformbutton || op.editbutton, hidden: true },
{ action: "cancel", display: op.editformbutton || op.editbutton, hidden: true }
],
actionButton = function (options) {
var action = options.action, actionName = options.actionName || action,
idPrefix = options.idPrefix !== undefined ? options.idPrefix : (action.charAt(0).toUpperCase() + action.substring(1));
return "<div title='" + op[action + "title"] +
(options.hidden ? "' style='display:none;" : "") +
"' class='" + $self.jqGrid("getGuiStyles", "actionsButton", "ui-pg-div ui-inline-" + action) + "' " +
(idPrefix !== null ? "id='j" + idPrefix + "Button_" + rowid : "") +
"' onclick=\"return jQuery.fn.fmatter.rowactions.call(this,event,'" + actionName + "');\" " +
(options.noHovering ? "" : hoverAttributes) + "><span class='" +
cssIconClass(action) + "'></span></div>";
},
n = op.custom != null ? op.custom.length - 1 : -1;
if (rowid === undefined || fmatter.isEmpty(rowid)) { return ""; }
if ($.isFunction(op.isDisplayButtons)) {
try {
displayMask = op.isDisplayButtons.call($t, opts, rwd, act) || {};
} catch (ignore) {}
}
while (n >= 0) {
customAction = op.custom[n--];
buttonInfos[customAction.position === "first" ? "unshift" : "push"](customAction);
}
for (i = 0, n = buttonInfos.length; i < n; i++) {
info = $.extend({}, buttonInfos[i], displayMask[buttonInfos[i].action] || {});
if (info.display !== false) {
str += actionButton(info);
}
}
return "<div class='" + $self.jqGrid("getGuiStyles", "actionsDiv", "ui-jqgrid-actions") + "'>" + str + "</div>";
};
$FnFmatter.actions.pageFinalization = function (iCol) {
var $self = $(this), p = this.p, colModel = p.colModel, cm = colModel[iCol],
showHideEditDelete = function (show, rowid) {
var maxfrozen = 0, tr, $actionsDiv, len = colModel.length, i;
for (i = 0; i < len; i++) {
// from left, no breaking frozen
if (colModel[i].frozen !== true) {
break;
}
maxfrozen = i;
}
tr = $self.jqGrid("getGridRowById", rowid);
if (tr != null && tr.cells != null) {
//$actionsDiv = cm.frozen ? $("tr#"+jgrid.jqID(rid)+" td:eq("+jgrid.getCellIndex(this)+") > div",$grid) :$(this).parent(),
iCol = p.iColByName[cm.name];
$actionsDiv = $(tr.cells[iCol]).children(".ui-jqgrid-actions");
if (cm.frozen && p.frozenColumns && iCol <= maxfrozen) {
// uses the corresponding tr from frozen div with the same rowIndex ADDITIONALLY
// to the standard action div
$actionsDiv = $actionsDiv
.add($($self[0].grid.fbRows[tr.rowIndex].cells[iCol])
.children(".ui-jqgrid-actions"));
}
if (show) {
$actionsDiv.find(">.ui-inline-edit,>.ui-inline-del").show();
$actionsDiv.find(">.ui-inline-save,>.ui-inline-cancel").hide();
} else {
$actionsDiv.find(">.ui-inline-edit,>.ui-inline-del").hide();
$actionsDiv.find(">.ui-inline-save,>.ui-inline-cancel").show();
}
}
},
showEditDelete = function (e, rowid) {
showHideEditDelete(true, rowid);
return false;
},
hideEditDelete = function (e, rowid) {
showHideEditDelete(false, rowid);
return false;
};
if (cm.formatoptions == null || !cm.formatoptions.editformbutton) {
// we use unbind to be sure that we don't register the same events multiple times
$self.unbind("jqGridInlineAfterRestoreRow.jqGridFormatter jqGridInlineAfterSaveRow.jqGridFormatter", showEditDelete);
$self.bind("jqGridInlineAfterRestoreRow.jqGridFormatter jqGridInlineAfterSaveRow.jqGridFormatter", showEditDelete);
$self.unbind("jqGridInlineEditRow.jqGridFormatter", hideEditDelete);
$self.bind("jqGridInlineEditRow.jqGridFormatter", hideEditDelete);
}
};
$.unformat = function (cellval, options, pos, cnt) {
// specific for jqGrid only
var ret, colModel = options.colModel, formatType = colModel.formatter, p = this.p,
op = colModel.formatoptions || {},// sep,
//re = /([\.\*\_\'\(\)\{\}\+\?\\])/g,
unformatFunc = colModel.unformat || ($FnFmatter[formatType] && $FnFmatter[formatType].unformat);
if (cellval instanceof jQuery && cellval.length > 0) {
cellval = cellval[0];
}
if (p.treeGrid && cellval != null && $(cellval.firstChild).hasClass("tree-wrap") && ($(cellval.lastChild).hasClass("cell-wrapper") || $(cellval.lastChild).hasClass("cell-wrapperleaf"))) {
cellval = cellval.lastChild;
}
if (colModel.autoResizable && cellval != null && $(cellval.firstChild).hasClass(p.autoResizing.wrapperClassName)) {
cellval = cellval.firstChild;
}
if (unformatFunc !== undefined && $.isFunction(unformatFunc)) {
ret = unformatFunc.call(this, $(cellval).text(), options, cellval);
} else if (formatType !== undefined && typeof formatType === "string") {
//var opts = $.extend(true, {}, getRes(locales[p.locale], "formatter"), jgrid.formatter || {}), stripTag;
var $self = $(this), //stripTag, //opts = getGridRes.call($self, "formatter"),
getFormaterOption = function (formatterName, optionName) {
return op[optionName] !== undefined ?
op[optionName] :
getGridRes.call($self, "formatter." + formatterName + "." + optionName);
},
cutThousandsSeparator = function (formatterName, val) {
var separator = getFormaterOption(formatterName, "thousandsSeparator")
.replace(/([\.\*\_\'\(\)\{\}\+\?\\])/g, "\\$1");
return val.replace(new RegExp(separator, "g"), "");
};
switch (formatType) {
case "integer":
ret = cutThousandsSeparator("integer", $(cellval).text());
break;
case "number":
ret = cutThousandsSeparator("number", $(cellval).text())
.replace(getFormaterOption("number", "decimalSeparator"), ".");
break;
case "currency":
ret = $(cellval).text();
var prefix = getFormaterOption("currency", "prefix"),
suffix = getFormaterOption("currency", "suffix");
if (prefix && prefix.length) {
ret = ret.substr(prefix.length);
}
if (suffix && suffix.length) {
ret = ret.substr(0, ret.length - suffix.length);
}
ret = cutThousandsSeparator("number", ret)
.replace(getFormaterOption("number", "decimalSeparator"), ".");
break;
case "checkbox":
ret = $FnFmatter.checkbox.unformat(cellval, options, cellval);
break;
case "select":
ret = $.unformat.select(cellval, options, pos, cnt);
break;
case "actions":
return "";
default:
ret = $(cellval).text();
}
}
ret = ret !== undefined ? ret : cnt === true ? $(cellval).text() : jgrid.htmlDecode($(cellval).html());
return ret;
};
$.unformat.select = function (cellval, options, pos, cnt) {
// Spacial case when we have local data and perform a sort
// cnt is set to true only in sortDataArray
var ret = [], cell = $(cellval).text(), colModel = options.colModel;
if (cnt === true) { return cell; }
var op = $.extend({}, colModel.editoptions || {}, colModel.formatoptions || {}),
sep = op.separator === undefined ? ":" : op.separator,
delim = op.delimiter === undefined ? ";" : op.delimiter;
if (op.value) {
var oSelect = op.value,
msl = op.multiple === true ? true : false,
scell = [], sv, mapFunc = function (n, k) { if (k > 0) { return n; } };
if (msl) { scell = cell.split(","); scell = $.map(scell, function (n) { return $.trim(n); }); }
if (typeof oSelect === "string") {
var so = oSelect.split(delim), j = 0, i;
for (i = 0; i < so.length; i++) {
sv = so[i].split(sep);
if (sv.length > 2) {
sv[1] = $.map(sv, mapFunc).join(sep);
}
if (msl) {
if ($.inArray($.trim(sv[1]), scell) > -1) {
ret[j] = sv[0];
j++;
}
} else if ($.trim(sv[1]) === $.trim(cell)) {
ret[0] = sv[0];
break;
}
}
} else if (fmatter.isObject(oSelect) || $.isArray(oSelect)) {
if (!msl) { scell[0] = cell; }
ret = $.map(scell, function (n) {
var rv;
$.each(oSelect, function (k, val) {
if (val === n) {
rv = k;
return false;
}
});
if (rv !== undefined) { return rv; }
});
}
return ret.join(", ");
}
return cell || "";
};
$.unformat.date = function (cellval, opts) {
// TODO
var op = $.extend(true, {},
getGridRes.call($(this), "formatter.date"),
jgrid.formatter.date || {},
opts.formatoptions || {});
return !fmatter.isEmpty(cellval) ?
jgrid.parseDate.call(this, op.newformat, cellval, op.srcformat, op) :
"";
};
// end module jquery.fmatter
}));