-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjquery.sheet.js
11583 lines (10267 loc) · 475 KB
/
jquery.sheet.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
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @project jQuery.sheet() The Ajax Spreadsheet - http://code.google.com/p/jquerysheet/
* @author RobertLeePlummerJr@gmail.com
* $Id: jquery.sheet.js 943 2013-09-04 14:57:33Z robertleeplummerjr $
* Licensed under MIT
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
/**
* @namespace
* @type {Object|Function}
*/
jQuery = jQuery || window.jQuery;
(function($, doc, win, Date, String, Number, Boolean, Math, RegExp, Error) {
/**
* @namespace
* @type {Object}
* @name jQuery()
*/
$.fn.extend({
/**
* @memberOf jQuery()
* @function
* @returns {jQuery()}
* @description
* <pre>
* The jQuery.sheet plugin
* Supports the following jQuery events
*
* sheetAddRow - occurs just after a row has been added
* arguments: e (jQuery event), jS, i (row index), isBefore, qty
* example:
* $(obj).sheet({
* sheetAddRow: function(e, jS, i, isBefore, qty) {
*
* }
* });
* or:
* $(obj).bind('sheetAddRow', function(e, jS, i, isBefore, qty) {
*
* })
* .sheet();
*
* sheetAddColumn - occurs just after a column has been added
* arguments: e (jQuery event), jS, i (column index), isBefore, qty
* example:
* $(obj).sheet({
* sheetAddColumn: function(e, jS, i, isBefore, qty) {
*
* }
* });
* or:
* $(obj).bind('sheetAddColumn', function(e, jS, i, isBefore, qty) {
*
* })
* .sheet();
*
* sheetSwitch - occurs after a spreadsheet has been switched
* arguments: e (jQuery event), jS (jQuery.sheet instance), i (spreadsheet index)
* example:
* $(obj).sheet({
* sheetSwitch: function(e, jS, i) {
*
* }
* });
* or:
* $(obj).bind('sheetSwitch', function(e, jS, i) {
*
* })
* .sheet();
*
* sheetRename - occurs just after a spreadsheet is renamed, to obtain new title jS.obj.table().attr('title');
* arguments: e (jQuery event), jS (jQuery.sheet instance), i (spreadsheet index)
* example:
* $(obj).sheet({
* sheetRename: function(e, jS, i) {
*
* }
* });
* or:
* $(obj).bind('sheetRename', function(e, jS, i) {
*
* })
* .sheet();
*
* sheetTabSortStart - occurs at the beginning of a sort for moving a spreadsheet around in order
* arguments: e (jQuery event), jS (jQuery.sheet instance), E (jQuery sortable event), ui, (jQuery ui event)
* example:
* $(obj).sheet({
* sheetTabSortStart: function(e, jS, E, ui) {
*
* }
* });
* or:
* $(obj).bind('sheetTabSortStart',NPER: function(e, jS, E, ui) {
*
* })
* .sheet();
*
* sheetTabSortUpdate - occurs after a sort of a spreadsheet has been completed
* arguments: e (jQuery event), jS (jQuery.sheet instance), E (jQuery sotable event), ui, (jQuery ui event), i (original index)
* example:
* $(obj).sheet({
* sheetTabSortUpdate: function(e, jS, E, ui) {
*
* }
* });
* or:
* $(obj).bind('sheetTabSortUpdate', function(e, jS, E, ui) {
*
* })
* .sheet();
*
* sheetFormulaKeydown - occurs just after keydown on either inline or static formula
* arguments: e (jQuery event)
* example:
* $(obj).sheet({
* sheetFormulaKeydown: function(e) {
*
* }
* });
* or:
* $(obj).bind('sheetFormulaKeydown') {
*
* })
* .sheet();
* sheetCellEdit - occurs just before a cell has been a cell is started to edit
* arguments: e (jQuery event), jS (jQuery.sheet instance), cell (jQuery.sheet.instance.spreadsheet cell)
* example:
* $(obj).sheet({
* sheetCellEdit: function(e, jS, cell) {
*
* }
* });
* or:
* $(obj).bind('sheetCellEdit', function(e, jS, cell) {
*
* })
* .sheet();
*
* sheetCellEdited - occurs just after a cell has been updated
* arguments: e (jQuery event), jS (jQuery.sheet instance), cell (jQuery.sheet.instance.spreadsheet cell)
* example:
* $(obj).sheet({
* sheetCellEdited: function(e, jS, cell) {
*
* }
* });
* or:
* $(obj).bind('sheetCellEdited', function(e, jS, cell) {
*
* })
* .sheet();
*
* sheetCalculation - occurs just after a spreadsheet has been fully calculated
* arguments: e (jQuery event), jS (jQuery.sheet instance)
* example:
* $(obj).sheet({
* sheetCalculation: function(e, jS) {
*
* }
* });
* or:
* $(obj).bind('sheetCalculation', function(e, jS) {
*
* })
* .sheet();
*
* sheetAdd - occurs just after a spreadsheet has been added
* arguments: e (jQuery event), jS (jQuery.sheet instance), i (new sheet index)
* example:
* $(obj).sheet({
* sheetAdd: function(e, jS, i) {
*
* }
* });
* or:
* $(obj).bind('sheetAdd', function(e, jS, i) {
*
* })
* .sheet();
*
* sheetDelete - occurs just after a spreadsheet has been deleted
* arguments: e (jQuery event), jS (jQuery.sheet instance), i (old sheet index)
* example:
* $(obj).sheet({
* sheetDelete: function(e, jS, i) {
*
* }
* });
* or:
* $(obj).bind('sheetDelete', function(e, jS, i) {
*
* })
* .sheet();
*
* sheetDeleteRow - occurs just after a row has been deleted
* arguments: e (jQuery event), jS (jQuery.sheet instance), i (old row index)
* example:
* $(obj).sheet({
* sheetDeleteRow: function(e, jS, i) {
*
* }
* });
* or:
* $(obj).bind('sheetDeleteRow', function(e, jS, i) {
*
* })
* .sheet();
*
* sheetDeleteColumn - occurs just after a column as been deleted
* arguments: e (jQuery event), jS (jQuery.sheet instance), i (old column index)
* example:
* $(obj).sheet({
* sheetDeleteColumn: function(e, jS, i) {
*
* }
* });
* or:
* $(obj).bind('sheetDeleteColumn', function(e, jS, i) {
*
* })
* .sheet();
*
* sheetOpen - occurs just after a single sheet within a set of sheets has been opened, this is triggered when calling sheet, so it needs to be bound beforehand
* arguments: e (jQuery event), jS (jQuery.sheet instance), i (new sheet index)
* example:
* $(obj).sheet({
* sheetOpen: function(e, jS, i) {
*
* }
* });
* or:
* $(obj).bind('sheetOpen', function(e, jS, i) {
*
* })
* .sheet();
*
* sheetAllOpened - occurs just after all sheets have been loaded and complete user interface has been created, this is triggered when calling sheet, so it needs to be bound beforehand
* arguments: e (jQuery event), jS (jQuery.sheet instance)
* example:
* $(obj).sheet({
* sheetAllOpened: function(e, jS) {
*
* }
* });
* or:
* $(obj).bind('sheetAllOpened', function(e, jS) {
*
* })
* .sheet();
*
* sheetSave - an assistance event called when calling jS.toggleState(), but not tied to anything internally
* arguments: e (jQuery event), jS (jQuery.sheet instance), tables (tables from spreadsheet)
* example:
* $(obj).sheet({
* sheetSave: function(e, jS, tables) {
*
* });
* }
* or:
* $(obj).bind('sheetSave', function(e, jS, tables) {
*
* })
* .sheet();
*
* sheetFullScreen - triggered when the sheet goes full screen
* arguments: e (jQuery event), jS (jQuery.sheet instance), isFullScreen (boolean, true if full screen, false if not)
* example:
* $(obj).sheet({
* sheetFullScreen: function(e, jS, isFullScreen) {
*
* });
* }
* or:
* $(obj).bind('sheetFullScreen', function(e, jS, isFullScreen) {
*
* })
* .sheet();
* </pre>
*
* @param {Object} [settings] supports the following properties/methods:
* <pre>
* editable {Boolean}, default true, Makes the sheet editable or viewable
*
* editableNames {Boolean}, default true, Allows sheets to have their names changed, depends on settings.editable being true
*
* barMenus {Boolean}, default true, Turns bar menus on/off
*
* freezableCells {Boolean}, default true, Turns ability to freeze cells on/off
*
* allowToggleState {Boolean}, default true, allows the spreadsheet to be toggled from write/read
*
* newColumnWidth {Number}, default 120, width of new columns
*
* title {String|Function}, title of spreadsheet, if function, expects string and is sent jS
*
* menuRight {String|Function}, default '', 'this' is jQuery.sheet instance. If ul object, will attempt to create menu
*
* menuLeft {String|Function}, default '', 'this' is jQuery.sheet instance. If ul object, will attempt to create menu
*
* calcOff {Boolean} default false, turns turns off ability to calculate
*
* lockFormulas {Boolean} default false, turns on/off the ability to edit formulas
*
* colMargin {Number} default 18, size of height of new cells, and width of cell bars
*
* boxModelCorrection {Number} default 2, if box model is detected, it adds these pixels to ensure the size of the spreadsheet controls are correct
*
* formulaFunctions {Object} default {}, Additional functions for formulas. Will overwrite default functions if named the same.
* Javascript Example:
* $(obj).sheet({
* formulaFunctions: {
* NEWFUNCTION: function(arg1, arg2) {
* //this = the parser's cell object object
* return 'string'; //can return a string
* return { //can also return an object {value: '', html: ''}
* value: 'my value seen by other cells or if accessed directly',
* html: $('What the end user will see on the cell this is called in')
* }
* }
* }
* });
*
* Formula Example:
* =NEWFUNCTION(A1:B1, C3);
*
* formulaVariables {Object} default {}, Additional variables that formulas can access.
* Javascript Example:
* $(obj).sheet({
* formulaVariables: {
* newVariable: 100
* }
* });
*
* Formula Example (will output 200)
* =newVariable + 100
*
* cellSelectModel {String} default 'excel', accepts 'excel', 'oo', or 'gdrive', makes the select model act differently
*
* autoAddCells {Boolean} default true, allows you to add cells by selecting the last row/column and add cells by pressing either tab (column) or enter (row)
*
* resizableCells {Boolean} default true, turns resizing on and off for cells, depends on jQuery ui
*
* resizableSheet {Boolean} default true, turns resizing on and off for sheet, depends on jQuery ui
*
* autoFiller {Boolean} default true, turns on/off the auto filler, the little square that follows the active cell around that you can drag and fill the values of other cells in with.
*
* minSize {Object} default {rows: 1, cols: 1}, the minimum size of a spreadsheet
*
* error {Function} default function(e) { return e.error; }, is triggered on errors from the formula engine
*
* encode {Function} default is a special characters handler for strings only, is a 1 way encoding of the html if entered manually by the editor. If you want to use html with a function, return an object rather than a string
*
* frozenAt {Object} default [{row: 0,col: 0}], Gives the ability to freeze cells at a certain row/col
*
* contextmenuTop {Object} default is standard list of commands for context menus when right click or click on menu dropdown
* Javascript example:
* {
* "What I want my command to say": function() {}
* }
*
* contextmenuLeft {Object} default is standard list of commands for context menus when right click
* Javascript example:
* {
* "What I want my command to say": function() {}
* }
*
* contextmenuCell {Object} default is standard list of commands for context menus when right click or click on menu dropdown
* Javascript example:
* {
* "What I want my command to say": function() {}
* }
*
* hiddenRows {Array} default [], Hides certain rows from being displayed initially. [sheet Index][row index]. example: [[1]] hides first row in first spreadsheet; [[]],[1]] hides first row in second spreadsheet
*
* hiddenColumns {Array} default [], Hides certain columns from being displayed initially. [sheet Index][column index]. example: [[1]] hides first column in first spreadsheet; [[],[1]] hides first column in second spreadsheet
*
* confirm
*
* alert
*
* prompt
* </pre>
*/
sheet:function (settings) {
var n = isNaN,
events = $.sheet.events;
settings = settings || {};
$(this).each(function () {
var globalize = Globalize,
me = $(this),
defaults = {
editable:true,
editableNames:true,
barMenus:true,
freezableCells:true,
allowToggleState:true,
menuLeft:null,
menuRight:null,
newColumnWidth:120,
title:null,
calcOff:false,
lockFormulas:false,
parent:me,
colMargin:18,
boxModelCorrection:2,
formulaFunctions:{},
formulaVariables:{},
cellSelectModel:'excel',
autoAddCells:true,
resizableCells:true,
resizableSheet:true,
autoFiller:true,
minSize:{rows:1, cols:1},
error:function (e) {
return e.error;
},
endOfNumber: false,
encode:function (val) {
switch (typeof val) {
case 'object':
//check if it is a date
if (val.getMonth) {
return globalize.format(val, 'd');
}
return val;
}
if (!val) {
return val || '';
}
if (!val.replace) {
return val || '';
}
/*var num = $.trim(val) * 1;
if (!isNaN(num)) {
return globalize.format(num, "n10").replace(this.endOfNumber, function (orig, radix, num) {
return (num ? radix : '') + (num || '');
});
}*/
return val
.replace(/&/gi, '&')
.replace(/>/gi, '>')
.replace(/</gi, '<')
.replace(/\n/g, '\n<br>')
.replace(/\t/g, '   ')
.replace(/ /g, ' ');
},
frozenAt:[],
contextmenuTop:{
"Toggle freeze columns to here":function (jS) {
var col = jS.getTdLocation(jS.obj.tdActive()).col;
jS.frozenAt().col = (jS.frozenAt().col == col ? 0 : col);
},
"Insert column after":function (jS) {
jS.controlFactory.addColumn(jS.colLast);
return false;
},
"Insert column before":function (jS) {
jS.controlFactory.addColumn(jS.colLast, true);
return false;
},
"Add column to end":function (jS) {
jS.controlFactory.addColumn();
return false;
},
"Delete this column":function (jS) {
jS.deleteColumn();
return false;
},
"Hide column":function (jS) {
jS.toggleHide.column();
return false;
}
},
contextmenuLeft:{
"Toggle freeze rows to here":function (jS) {
var row = jS.getTdLocation(jS.obj.tdActive()).row;
jS.frozenAt().row = (jS.frozenAt().row == row ? 0 : row);
},
"Insert row after":function (jS) {
jS.controlFactory.addRow(jS.rowLast);
return false;
},
"Insert row before":function (jS) {
jS.controlFactory.addRow(jS.rowLast, true);
return false;
},
"Add row to end":function (jS) {
jS.controlFactory.addRow();
return false;
},
"Delete this row":function (jS) {
jS.deleteRow();
return false;
},
"Hide row":function (jS) {
jS.toggleHide.row();
return false;
}
},
contextmenuCell:{
"Copy":false,
"Cut":false,
"Insert column after":function (jS) {
jS.controlFactory.addColumn(jS.colLast);
return false;
},
"Insert column before":function (jS) {
jS.controlFactory.addColumn(jS.colLast, true);
return false;
},
"Add column to end":function (jS) {
jS.controlFactory.addColumn();
return false;
},
"Delete this column":function (jS) {
jS.deleteColumn();
return false;
},
"line1":"line",
"Insert row after":function (jS) {
jS.controlFactory.addRow(jS.rowLast);
return false;
},
"Insert row before":function (jS) {
jS.controlFactory.addRow(jS.rowLast, true);
return false;
},
"Add row to end":function (jS) {
jS.controlFactory.addRow();
return false;
},
"Delete this row":function (jS) {
jS.deleteRow();
return false;
},
"line2":'line',
"Add spreadsheet":function (jS) {
jS.addSheet();
},
"Delete spreadsheet":function (jS) {
jS.deleteSheet();
}
},
hiddenRows:[],
hiddenColumns:[],
cellStartingHandlers: {
'$':function(val, ch) {
return jFN.DOLLAR.call(this, val.substring(1).replace(globalize.culture().numberFormat[','], ''), 2, ch || '$');
},
'£':function(val) {
return jS.s.cellStartingHandlers['$'].call(this, val, '£');
}
},
cellEndHandlers: {
'%': function(value) {
return value.substring(0, this.value.length - 1) / 100;
}
},
cellTypeHandlers: {
percent: function(value) {
var num = (n(value) ? globalize.parseFloat(value) : value),
result;
if (!n(num)) {//success
result = new Number(num);
result.html = globalize.format(num, 'p');
return result;
}
return value;
},
date: function(value) {
var date = globalize.parseDate(value);
date.html = globalize.format(date, 'd');
return date;
},
time: function(value) {
var date = globalize.parseDate(value);
date.html = globalize.format(date, 't');
return date;
},
currency: function(value) {
var num = (n(value) ? globalize.parseFloat(value) : value),
result;
if (!n(num)) {//success
result = new Number(num);
result.html = globalize.format(num, 'c');
return result;
}
return num;
},
number: function(value) {
var radix, result;
if (!settings.endOfNumber) {
radix = globalize.culture().numberFormat['.'];
settings.endOfNumber = new RegExp("([" + (radix == '.' ? "\." : radix) + "])([0-9]*?[1-9]+)?(0)*$");
}
if (!n(value)) {//success
result = new Number(value);
result.html = globalize.format(value + '', "n10")
.replace(settings.endOfNumber, function (orig, radix, num) {
return (num ? radix : '') + (num || '');
});
return result;
}
return value;
}
}
};
//destroy already existing spreadsheet
var jS = this.jS;
if (jS) {
var tables = me.children().detach();
jS.kill();
me.html(tables);
for (var event in events) {
if (settings[events[event]]) {
me.unbind(events[event]);
}
}
}
if ((this.className || '').match(/\bnotEditable\b/i) != null) {
settings['editable'] = false;
}
for (var i in events) {
if (settings[events[i]]) me.bind(events[i], settings[events[i]]);
}
if (!$.sheet.instance.length) $.sheet.instance = [];
this.jS = jS = $.sheet.createInstance($.extend(defaults, settings), $.sheet.instance.length);
$.sheet.instance.push(jS);
});
return this;
},
/**
* @memberOf jQuery()
* @method
* @returns {HTMLElement}
*/
disableSelectionSpecial:function () {
this.each(function () {
this.onselectstart = function () {
return false;
};
this.unselectable = "on";
this.style['-moz-user-select'] = 'none';
});
return this;
},
/**
* @memberOf jQuery()
* @returns {jS}
*/
getSheet:function () {
var me = this[0],
jS = (me.jS || {});
return jS;
},
/**
* Get cell value
* @memberOf jQuery()
* @param {Number} rowIndex
* @param {Number} colIndex
* @param {Number} [sheetIndex] defaults to 0
* @returns {String|Date|Number|Boolean|Null}
*/
getCellValue:function (rowIndex, colIndex, sheetIndex) {
var me = this[0],
jS = (me.jS || {});
sheetIndex = (sheetIndex || 0);
if (jS.updateCellValue) {
try {
return jS.updateCellValue(sheetIndex, rowIndex, colIndex);
} catch (e) {}
}
return null;
},
/**
* Set cell value
* @memberOf jQuery()
* @param {String|Number} value
* @param {Number} rowIndex
* @param {Number} colIndex
* @param {Number} [sheetIndex] defaults to 0
* @returns {Boolean}
*/
setCellValue:function (value, rowIndex, colIndex, sheetIndex) {
var me = this[0],
jS = (me.jS || {}),
cell;
sheetIndex = (sheetIndex || 0);
if (
jS.getCell
&& (cell = jS.getCell(rowIndex, colIndex, sheetIndex))
) {
try {
cell.value = value;
cell.valueOverride = cell.formula = '';
cell.calcLast = cell.calcDependenciesLast = 0;
jS.updateCellValue(sheetIndex, rowIndex, colIndex);
return true;
} catch (e) {}
}
return false;
},
/**
* Set cell formula
* @memberOf jQuery()
* @param {String} formula
* @param {Number} rowIndex
* @param {Number} colIndex
* @param {Number} [sheetIndex] defaults to 0
* @returns {Boolean}
*/
setCellFormula:function (formula, rowIndex, colIndex, sheetIndex) {
var me = this[0],
jS = (me.jS || {}),
cell;
sheetIndex = (sheetIndex || 0);
if (
jS.getCell
&& (cell = jS.getCell(rowIndex, colIndex, sheetIndex))
) {
try {
cell.formula = formula;
cell.valueOverride = cell.value = '';
cell.calcLast = cell.calcDependenciesLast = 0;
jS.updateCellValue(sheetIndex, rowIndex, colIndex);
} catch (e) {}
}
return this;
},
/**
* Set cell html
* @memberOf jQuery()
* @param {*} html
* @param {Number} rowIndex
* @param {Number} colIndex
* @param {Number} [sheetIndex] defaults to 0
*/
setCellHtml:function (html, rowIndex, colIndex, sheetIndex) {
var me = this[0],
jS = (me.jS || {}),
cell;
sheetIndex = (sheetIndex || 0);
if (
jS.getCell
&& (cell = jS.getCell(rowIndex, colIndex, sheetIndex))
) {
try {
cell.html = [html];
cell.calcLast = cell.calcDependenciesLast = 0;
jS.updateCellValue(sheetIndex, rowIndex, colIndex);
} catch (e) {}
}
return this;
},
/**
* Detect if spreadsheet is full screen
* @memberOf jQuery()
* @returns {Boolean}
*/
isSheetFullScreen:function () {
var me = this[0],
jS = (me.jS || {});
if (jS.obj) {
return jS.obj.fullScreen().is(':visible');
}
return false;
},
/**
* Get inputs serialized from spreadsheet type_sheet-index_row-index_column-index_instance-index (dropdown_0_1_1_0 = sheet 1, row 1, column A, instance 0
* @param {Boolean} [isArray] return serialized as array (true) or string (false, default false
* @memberOf jQuery()
* @returns {*}
*/
serializeCellInputs:function (isArray) {
var me = this[0],
jS = (me.jS || {}),
inputs = jS.obj.tables().find(':input');
if (isArray) {
return inputs.serializeArray();
} else {
return inputs.serialize();
}
},
/**
* prints the source of a sheet for a user to see
* @param {Boolean} [pretty] makes html a bit easier for the user to see
* @returns {String}
* @memberOf jQuery()
*/
viewSource:function (pretty) {
var source = "";
$(this).each(function () {
if (pretty) {
source += $(this).toPrettySource();
} else {
source += $(this).toCompactSource();
}
});
$.print(source);
return source;
},
/**
* prints html to 1 line
* @returns {String}
* @memberOf jQuery()
*/
toCompactSource:function () {
var node = this[0];
var result = "";
if (node.nodeType == 1) {
// ELEMENT_NODE
result += "<" + node.tagName.toLowerCase();
var n = node.attributes.length;
for (var i = 0; i < n; i++) {
var key = node.attributes[i].name,
val = node.getAttribute(key);
if (val) {
if (key == "contentEditable" && val == "inherit") {
continue;
// IE hack.
}
if (typeof(val) == "string") {
result += " " + key + '="' + val.replace(/"/g, "'") + '"';
} else if (key == "style" && val.cssText) {
result += ' style="' + val.cssText + '"';
}
}
}
if (node.tagName == "COL") {
// IE hack, which doesn't like <COL..></COL>.
result += '/>';
} else {
result += ">";
var childResult = "";
$(node.childNodes).each(function () {
childResult += $(this).toCompactSource();
});
result += childResult;
result += "</" + node.tagName.toLowerCase() + ">";
}
} else if (node.nodeType == 3) {
// TEXT_NODE
result += node.data.replace(/^\s*(.*)\s*$/g, "$1");
}
return result;
},
/**
* prints html to many lines, formatted for easy viewing
* @param {String} [prefix]
* @returns {String}
* @memberOf jQuery()
*/
toPrettySource:function (prefix) {
var node = this[0],
n,
i;
prefix = prefix || "";
var result = "";
if (node.nodeType == 1) {
// ELEMENT_NODE
result += "\n" + prefix + "<" + node.tagName.toLowerCase();
n = node.attributes.length;
for (i = 0; i < n; i++) {
var key = node.attributes[i].name,
val = node.getAttribute(key);
if (val) {
if (key == "contentEditable" && val == "inherit") {
continue; // IE hack.
}
if (typeof(val) == "string") {
result += " " + key + '="' + $.trim(val.replace(/"/g, "'")) + '"';
} else if (key == "style" && val.cssText) {
result += ' style="' + $.trim(val.cssText) + '"';
}
}
}
if (node.childNodes.length <= 0) {
if (node.tagName == "COL") {
result += "/>";
} else {
result += "></" + node.tagName.toLowerCase() + ">";
}
} else {
result += ">";
var childResult = "";
n = node.childNodes.length;
for (i = 0; i < n; i++) {
childResult += $(node.childNodes[i]).toPrettySource(prefix + " ");
}
result += childResult;
if (childResult.indexOf('\n') >= 0) {
result += "\n" + prefix;
}
result += "</" + node.tagName.toLowerCase() + ">";
}
} else if (node.nodeType == 3) {
// TEXT_NODE
result += node.data.replace(/^\s*(.*)\s*$/g, "$1");
}
return result;
},
/**
* mousewheel event handler, not a jQuery event handler
* @returns {HTMLElement}
* @memberOf jQuery()
*/
mousewheel: function(fn) {
$(this).each(function() {
this.onwheel = this.onmousewheel = this.onDOMMouseScroll = this.onMozMousePixelScroll = fn;
});
return this;
}
});
/**
* @namespace
* @type {Object}
* @memberOf jQuery
* @name jQuery.sheet
*/
$.sheet = {
/**
* Array of instances of jQuery.sheet, generally short-handed to jS
* @memberOf jQuery.sheet
*/
instance:[],
/**
* Contains the dependencies if you use $.sheet.preLoad();
* @memberOf jQuery.sheet
*/
dependencies:{
coreCss:{css:'jquery.sheet.css'},
jQueryUI:{script:'jquery-ui/ui/jquery-ui.min.js'},
jQueryUIThemeRoller:{css:'jquery-ui/theme/jquery-ui.min.css'},
globalize:{script:'plugins/globalize.js'},
formulaParser:{script:'parser/formula/formula.js'},
tsvParser:{script:'parser/tsv/tsv.js'},
nearest:{script:'plugins/jquery.nearest.min.js'}
},
/**
* Contains the optional plugins if you use $.sheet.preLoad();
* @memberOf jQuery.sheet
*/