-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpivot5.js
2197 lines (1763 loc) · 71 KB
/
jpivot5.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
/*
08.05.2010 17:39:58 data moved from options (so its not copied)
- calls pivot as $('#pv').jPivot(data_local,{options....});
change data as $('#pv').jPivot(data_local);
******todo - make changing data by $('#pv').jPivot('setGridParam',{})
08.05.2010 17:43:04 some optimazings for //create mapping data to pvtable routine
08.05.2010 23:11:29 some variables scope checkings
08.05.2010 23:11:48 r and c attributes for td use real table coords for collapsings
09.05.2010 14:31:18 added TD id as jpivot_rc_'+r+'_'+cn for fast collpasing
11.07.2010 0:23:20 added totals_mask usage on first pvtable and dialogs creatings
11.07.2010 0:24:17 used jQuery tableHover plugin (MODIFIED & FIXED!!!) for collapsing rows and cols, old r,c, and TD id as jpivot_rc_'+r+'_'+cn for fast collpasing removed
11.07.2010 0:30:26 added RSP and CSP value for process cols and rows spans (used for adding special class in a6.html onPrintColKey for collapsings)
*/
/* TODO - function.call(this)
* - create td attributes in object style as {colspan:,rowspan:id etc}
* - create callback function on print TD - pass coordinates of pv.data ab tabla and attributes
* - use technics bellow (refactor code)
1. //default value:
var rowSpan = c.rowSpan || 1;
2. //check for array is created:
if ( !matrix[i] )
{
matrix[i] = [];
}
3. //more readable code, use shortcuts for subarrays (arrays passed by reference)
var matrixrow = matrix[k];
for ( var l = firstAvailCol; l < firstAvailCol + colSpan; l++ )
{
matrixrow[l] = 1;
}
4. //create dummy funcs - dont want to test for on==true all the time:
if ( on )
{
$.fn.tableHoverHover = $.fn.addClass;
}
else
{
$.fn.tableHoverHover = $.fn.removeClass;
}
5. h = colIndex[cell.realIndex] || []
* - integrate jQuery tableHover plugin http://p.sohei.org for collapsings...
*/
;
( function ( $ )
{
// Private Variables and Functions
var jpv_keys_placeholder_update_list_cnt = 0;
function jpv_rowsSort2 ( a, b, $this )
{
//Compare "a" and "b" in some fashion, and return -1, 0, or 1
//Less than 0: Sort "a" to be a lower index than "b"
//Zero: "a" and "b" should be considered equal, and no sorting performed.
//Greater than 0: Sort "b" to be a lower index than "a".
// rows keep idexes of row keys
// cols keep idexes of col keys
//compare by rows order then cols order
var rows = $this.opts.rows;
var order = $this.pv.dialog_sort;
var len = rows.length;
for ( i = 0; i < len; i++ )
{
if ( a[rows[i]] < b[rows[i]] )
return 1*order[rows[i]];
if ( a[rows[i]] > b[rows[i]] )
return -1*order[rows[i]];
}
return 0;
}
function jpv_rowsSort ( a, b, $this )
{
var rowsa = a.split ( '~~~' );
var rowsb = b.split ( '~~~' );
var len = rowsa.length;
var i;
var order = $this.pv.dialog_sort;
var rows = $this.opts.rows;
for ( i = 0; i < len; i++ )
{
if ( rowsa[i] < rowsb[i] )
return 1*order[rows[i]];
if ( rowsa[i] > rowsb[i] )
return -1*order[rows[i]];
}
return 0;
}
function jpv_colsSort ( a, b, $this )
{
var colsa = a.split ( '~~~' );
var colsb = b.split ( '~~~' );
var len = colsa.length;
var i;
var order = $this.pv.dialog_sort;
var cols = $this.opts.cols;
for ( i = 0; i < len; i++ )
{
if ( colsa[i] < colsb[i] )
return 1*order[cols[i]];
if ( colsa[i] > colsb[i] )
return -1*order[cols[i]];
}
return 0;
}
function in_array ( element, array, addIfAbsent )
{
len = array.length;
for ( var i = 0; i < len; i++ )
{
if ( element == array[i] )
return i;
}
if ( addIfAbsent )
{
array.push ( element );
return ( array.length - 1 );
}
return null;
}
function jpv_create_2Darray ( len )
{
var a = [];
for ( var i = 0; i < len; i++ )
a[i] = [];
return a;
}
function jpv_onCreateManage ( $this )
{
var $this = this;
var opts = this.opts;
var pv = this.pv;
var i;
var t;
var col_drag = pv.col_keys_length ? '' : 'style="padding-bottom:10px;"'; //fix for empty colheader
col_drag = '<ul ' + opts.styles.KeysColsPlaceholder + ' ' + col_drag + ' id="pv_col_keys_list">';
for ( i = 0; i < pv.col_keys_length; i++ )
col_drag += '<li id="pv_key_header' + opts.cols[i] + '" value="' + opts.cols[i] + '" class="ui-state-highlight">'
+ '<span style="float:right;" class="ui-icon ' + ( pv.dialog_sort[opts.cols[i]] > 0 ? 'ui-icon-triangle-1-s ' : 'ui-icon-triangle-1-n' ) + ' "></span>'
+ opts.data_headers[opts.cols[i]]
+ '</li>';
col_drag += '</ul>';
//row dragables
var row_drag = '<ul id="pv_row_keys_list">';
for ( i = 0; i < pv.row_keys_length ; i++ )
row_drag += '<li id="pv_key_header' + opts.rows[i] + '" value="' + opts.rows[i] + '" class="ui-state-highlight">'
+ '<span style="float:right;" class="ui-icon ' + ( pv.dialog_sort[opts.rows[i]] > 0 ? 'ui-icon-triangle-1-s ' : 'ui-icon-triangle-1-n' ) + ' "></span>'
+ opts.data_headers[opts.rows[i]]
+ '</li>';
row_drag += '</ul>';
var aggregate_drag = '<ul id="pv_aggregate">';
for ( k = 0; k < opts.aggregate.length; k++ )
aggregate_drag += '<li value="' + opts.aggregate[k] + '" class="ui-state-default">' + opts.data_headers[opts.aggregate[k]] + '</li> ';
aggregate_drag += '</ul>';
var filter_drag = '<ul id="pv_filter">';
for ( k = 0; k < opts.filter.length; k++ )
{
t = opts.filter[k];
filter_drag += '<li value="' + t + '">'
filter_drag += opts.data_headers[t] + '<select>';
for ( i = 0; i < pv.unique_keys[t].length; i++ )
if ( pv.unique_keys[t][i] == pv.head_filter[t] )
filter_drag += '<option selected value="' + pv.unique_keys[t][i] + '">' + pv.unique_keys[t][i] + '</option>';
else
filter_drag += '<option value="' + pv.unique_keys[t][i] + '">' + pv.unique_keys[t][i] + '</option>';
filter_drag += '</select></li>';
}
filter_drag += '</ul>';
$( '#' + opts.filter_keys_placeholder )
.empty()
.append ( filter_drag )
.addClass ( opts.styles.FliterPlaceholder );
if ( $this.opts.immediate_draw )
$( '#' + opts.filter_keys_placeholder + ' select' ).bind (
"change"
, function ( e )
{
jpv_preparePv ( $this );
$this.opts.onDrawData.call ( $this );
}
);
$( '#' + opts.aggregate_keys_placeholder ).empty().append ( aggregate_drag ).addClass ( opts.styles.aggregatePlaceholder );
$( '#' + opts.col_keys_placeholder ).empty().append ( col_drag ).addClass ( opts.styles.KeysColsPlaceholder );
$( '#' + opts.row_keys_placeholder ).empty().append ( row_drag ).addClass ( opts.styles.KeysRowsPlaceholder );
$( '#pv_row_keys_list, #pv_col_keys_list, #pv_aggregate, #pv_filter' )
.addClass ( 'pv_connectWith' )
.sortable (
{
connectWith: '.pv_connectWith'
, forcePlaceholderSize: true
, forceHelperSize: true
, helper: 'clone'
, opacity: '0.6'
, placeholder: 'ui-state-default'
, tolerance: 'pointer'
//,cursorAt: 'top,left' //1.8 ui fix
, cursor: 'default'
, snap: true
, deactivate: function ( event, ui )
{
jpv_keys_placeholder_update ( $this, event, ui )
}
} ).disableSelection();
//create popup filters
jpv_keys_placeholder_popup ( $this );
}
//callback from jqueryui after sorting/dragging cols,rows etc.
function jpv_keys_placeholder_update ( $this, event, ui )
{
//this func called for each lists (we have 4) so wait until last
//this.value is value attribute added to dom-elemnt and represent key-id
if ( jpv_keys_placeholder_update_list_cnt++ > 2 )
{
$this.opts.cols = [];
$this.opts.rows = [];
$this.opts.filter = [];
$this.opts.aggregate = [];
$( 'li', '#pv_col_keys_list' ).each (
function ()
{
$this.opts.cols.push ( this.value )
}
);
$( 'li', '#pv_row_keys_list' ).each (
function ()
{
$this.opts.rows.push ( this.value )
}
) ;
$( 'li', '#pv_filter' ).each (
function ()
{
$this.opts.filter.push ( this.value)
}
) ;
$( 'li', '#pv_aggregate' ).each (
function ()
{
$this.opts.aggregate.push ( this.value )
}
) ;
jpv_keys_placeholder_update_list_cnt = 0;
$this.opts.totals_mask = $this.opts.getTotalsMask ( $this.opts.data[0].length );
if ( $this.opts.immediate_draw )
{
jpv_preparePv ( $this );
$this.opts.onDrawData.call ( $this );
}
}
}
//create jqueryui popup dialog
function jpv_keys_placeholder_popup ( $this )
{
var opts = $this.opts;
var pv = $this.pv;
var keys_index_length = pv.unique_keys.length
var data_headers = opts.data_headers;
var tstr = '';
var t1;
var t2;
for ( k = 0; k < keys_index_length; k++ )
{
var unique_keys = pv.unique_keys[k];
var unique_keys_length = pv.unique_keys[k].length;
if ( ( unique_keys == null ) || ( unique_keys_length == 0 ) )
{
$( '#pv_dlg_plh' + k ).remove(); //remove placeholder
continue;
}
//is the dialog for col and row headers
if ( pv.dialog_filter[k].length > 0 ) //we have filtered keys - show this
$( '#pv_key_header' + k ).addClass ( opts.styles.class_add_KeyHeaderFiltered );
if ( $( '#pv_dlg_plh' + k ).length == 0 ) //if dialog not created
{
//we have no dialog,create
tstr = '<div id="pv_dlg_plh' + k + '" style="display:none">';
tstr += '<div id="radio_order">asc<input type="radio" checked name="radio_order' + k + '" value="A"/>'
tstr += 'dsc<input type="radio" name="radio_order' + k + '" value="D"></div>'
tstr += '<div>total<input type="checkbox" name="total" value="1" '+(opts.totals_mask[k] ==1 ? 'checked' : '')+' ></div>';
tstr += '<div id="filter">';
for ( i = 0; i < unique_keys_length; i++ )
tstr += '<input type="checkbox" checked value="' + opts.onPrintDialogKey ( k, unique_keys[i] ) [0] + '">' + opts.onPrintDialogKey ( k, unique_keys[i] ) [0] + '<br>'
tstr += '</div></div>';
$( $this ).append ( tstr );
$( '#pv_dlg_plh' + k ).dialog
(
{
autoOpen: false
, title: opts.data_headers[k]
, dialogClass: 'pv_dialog'
, buttons:
{
Ok: function()
{
if ( $this.opts.immediate_draw )
{
//jpv_build_filters($this);
jpv_preparePv ( $this );
$this.opts.nDrawData ( $this );
}
$( this ).dialog ( 'close' );
}
, Cancel: function()
{
$( this ).dialog ( 'close' );
}
}
} );
}
//bind click event, we must rebind for each time in case header was restored from aggregate or filter state
//TODO - use on('click') aka live once on create
$( '#pv_key_header' + k ).bind
(
"click"
, {selector: '#pv_dlg_plh' + k}
, function ( e )
{
$( e.data.selector )
.dialog ( 'option', 'position', [e.pageX,e.pageY] )
.dialog ( "open" )
}
);
}
//set values if we have dialog_filter
//see getDialogFilter - here we set filters and nullify its
if ( ( typeof ( opts.dialog_filter ) === 'object' ) && ( opts.dialog_filter != null ) )
{
var filter_len = opts.dialog_filter.length;
for ( var i = 0; i < filter_len; i++ )
{
var chks = $( '#filter input:checkbox', '#pv_dlg_plh' + i );
//try to check/uncheck keys in dialogs if exists
if ( chks.length )
{
chks.attr ( 'checked', true );
for ( var j = 0; j < opts.dialog_filter[i].length; j++ )
chks.filter ( '[value=' + opts.dialog_filter[i][j] + ']' ).attr ( 'checked', false );
}
}
opts.dialog_filter = null; //clear not needed
}
}
function jpv_restoreHeaderData ( $this )
{
//restore keys fields in pv.data after jpv_nullifyHeaderData
var pv = $this.pv;
var old;
for ( var c = 0; c < pv.row_keys_length; c++ )
{
old = null;
for ( var r = pv.col_keys_length; r < pv.data_rows_count; r++ )
{
if ( pv.data[r][c] != null )
old = pv.data[r][c];
pv.data[r][c] = old;
}
}
for ( var r = 0; r < pv.col_keys_length; r++ )
{
old = null;
for ( var c = pv.row_keys_length; c < pv.data_row_length; c++ )
{
if ( pv.data[r][c] != null )
old = pv.data[r][c];
pv.data[r][c] = old;
}
}
}
function jpv_nullifyHeaderData ( $this )
{
//nullify repeated keys fields in pv.data for spans an totals
var pv = $this.pv;
var old;
for ( c = 0; c < pv.row_keys_length - 1; c++ )
{
old = pv.data[pv.col_keys_length][c];
for ( r = pv.col_keys_length + 1; r < pv.data_rows_count; r++ )
if ( ( old != pv.data[r][c] ) || ( c > 0 ) && ( pv.data[r][c-1] != null ) )
old = pv.data[r][c];
else
pv.data[r][c] = null;
}
for ( r = 0; r < pv.col_keys_length; r++ )
{
old = pv.data[r][pv.row_keys_length];
for ( c = pv.row_keys_length + 1; c < pv.data_row_length; c++ )
{
if ( ( old != pv.data[r][c] ) || ( r > 0 ) && ( pv.data[r-1][c] != null ) )
old = pv.data[r][c];
else
pv.data[r][c] = null;
}
}
}
function jpv_build_filters ( $this )
{
var drl = $this.opts.data[0].length ;
$this.opts.totals_mask = $this.opts.getTotalsMask ( drl ) || $this.opts.totals_mask;
$this.pv.head_filter = $this.opts.getHeadFilter ( drl );//=jpv_create_2Darray(data_row_length); //hold head filter values for each key
$this.pv.dialog_filter = $this.opts.getDialogFilter ( $this, drl ); //hold dialog filter values for each key
$this.pv.dialog_sort = $this.opts.getSort ( drl ); //sort direction for each data index
}
function jpv_preparePv()
{
// Persistent Context Variables
var $this = this;
var pv = this.pv;
var opts = this.opts;
var data_length = opts.data.length;
var data_row_length = opts.data[0].length;
var data_ptr = opts.data;
var rows_ptr = opts.rows;
var row_keys_length = opts.rows.length;
var cols_ptr = opts.cols;
var col_keys_length = opts.cols.length;
var filter_ptr = opts.filter;
var filter_length = opts.filter.length;
//for resoring state (not data) we need serialize
// rows_ptr,cols_ptr,filter_ptr, aggregate(not holded in Jpivot)
// head_filter,dialog_filter,dialog_sort
jpv_build_filters ( $this )
if ( opts.onBeforePrepare )
{
opts.onBeforePrepare();
}
//////////////// datt processing /////////////////
//sort
//data_ptr.sort(function(a,b){return jpv_rowsSort(a,b,$this);});
var pre_uniquie_keys = jpv_create_2Darray ( data_row_length ); //hold hash of keys to improve performance
pv.unique_keys = jpv_create_2Darray ( data_row_length ); //hold unique key values for each key for dialog filter
var rows_composite_index = []; //row key value unique composite indexes
var cols_composite_index = []; //col kye value unique composite indexes
var data_row2pv_row = []; //map raw data rows to pivot rows
var data_row2pv_col = []; //map raw data cols to pivot cols
/*
HOW IT WORKS
example data
data_rows
['rk1','rk2','cc1','cc2',v0] is row0
['rk1','rk2','cc1','cc3',v1] is row1
['rk1','rk3','cc1','cc2',v2] is row2
suppose we want draw table here rk-s in its rows and cc-s in its cols, v-s is value, so rk-s is ROWS_keys, cc-s is COLS_keys
create pivot_table which will hold prearranged data
find map "data_row to pivot_row" by its rows_kews ,we will hold this map in data_row2pv_row
for each data_row create composite_index by concatenate rows_keys with ~~~
(
row0 and row1 have equal row_composite_index ('rk1~~~rk2') and so both mapped to pivot_row 0 in pivot_table
row2 have row_composite_index ('rk1~~~rk3') and so mapped to pivot_row 1 in pivot_table
)
by columns composite index we determine position of data_row value (v-s) in pivot_row,
we will hold this map in data_row2pv_col
(
row0 has col_composite_index = 'cc1~~~cc2' and mapped to column 0 of pivot_row 0 (by data_row2pv_row)
row1 has col_composite_index = 'cc1~~~cc3' and mapped to column 1 of pivot_row 0 (by data_row2pv_row)
row2 has col_composite_index = 'cc1~~~cc2' (same as row0) so mapped to column 0 of pivot_row 1 (by data_row2pv_row)
rows indexes obtained above in row_composite_index creation
)
after this we will had this pivot_table
| cc1 | cc1 |
| cc2 | cc3 |
rk1 | rk2 | v0 | v2 |
rk1 | rk3 | v2 | |
as seen in table we have gap (no data) for rk1-rk3-cc1-cc3 keys.
For those gaps we can't sort data_rows "at once" and must separate sort col_composite_index and create remapping of data_row2pv_col
*/
//console.profile('mainLoop');
var composite_row_key_uniq = [];
var composite_row_key_uniq_len = 0;
var composite_col_key_uniq = [];
var composite_col_key_uniq_len = 0;
var composite_row_key, composite_col_key, dc, dr;
pv.grand_cols_totals = [];
pv.grand_rows_totals = [];
for ( dr = 0; dr < data_length ; dr++ )
{
var is_filtered = false;
//head filter unique keys
if ( ( opts.onCustomFilter ) && ( opts.onCustomFilter ( dr ) ) ) //onCustomFilter returns true to exclude rows and its keys from pivot
continue;
//filter by head_filter (include behavior)
for ( i = 0; i < filter_length; i++ )
{
dc = filter_ptr[i];
key = data_ptr[dr][dc];
//when we first add key to head_fiter (just drag it into hoder) we dont have values of this filter (they are creting here)
// so <select> is not drawed and populated with options yet (pv.head_filter[dc]==null) and we need take first value and use it as filter
if ( pv.head_filter[dc] == null )
pv.head_filter[dc] = key; //push first possible value to head_filter (include behavior)
pre_uniquie_keys[dc][key] = key;//add to uniq keys for using in filters
if ( pv.head_filter[dc] != key )
{
is_filtered = true; //key not allowed (filtered) by head_filter (include behavior)
}
}
if ( is_filtered )
continue; //continue loop and add all possible values to filter and skip adding row to pivot
composite_row_key = '';
//see readme.txt: pv.dialog_filter (exclude behavior)
for ( i = 0; i < row_keys_length; i++ )
{
dc = rows_ptr[i];
key = data_ptr[dr][dc];
pre_uniquie_keys[dc][key] = key;//add to uniq keys for using in filters
if ( is_filtered || ( in_array ( key, pv.dialog_filter[dc], false ) != null ) )
{
is_filtered = true; //key not allowed (filtered) in dialog, continue to add all possible values to filter an skip adding row to pivot
continue;
}
composite_row_key += key + '~~~'; //add to composite_row_key unfiltered keys
}
composite_col_key = '';
for ( i = 0; i < col_keys_length; i++ )
{
dc = cols_ptr[i];
key = data_ptr[dr][dc];
pre_uniquie_keys[dc][key] = key; //add to uniq keys for using in filters
if ( is_filtered || ( in_array ( key, pv.dialog_filter[dc], false ) != null ) )
{
is_filtered = true; //key not allowed (filtered) in dialog
continue;
}
//composite_col_key[idx++]=key;
composite_col_key += key + '~~~';
}
if ( !is_filtered )
{
/* create mapping data to pvtable,
for each row index dr we will create data_row2pv_row[dr] that contain index of row in pivot table
and data_row2pv_col[dr] that contain index of col in pivot table
see diagrams.xls page mapping
*/
if ( typeof composite_row_key_uniq[composite_row_key] === 'undefined' )
{
composite_row_key_uniq[composite_row_key] = data_row2pv_row[dr] = composite_row_key_uniq_len++;
}
else
{
data_row2pv_row[dr] = composite_row_key_uniq[composite_row_key];
}
if ( typeof composite_col_key_uniq[composite_col_key] === 'undefined' )
{
data_row2pv_col[dr] = composite_col_key_uniq[composite_col_key] = composite_col_key_uniq_len++;
}
else
{
data_row2pv_col[dr] = composite_col_key_uniq[composite_col_key];
}
}
} //end of loop by all data
//create standart array from hash;
for ( var j in composite_row_key_uniq )
rows_composite_index.push ( j );
for ( var j in composite_col_key_uniq )
cols_composite_index.push ( j );
//sort cols and create remapping
var cols_composite_index_remap = [];
cols_composite_sorted = [];
len = cols_composite_index.length;
//copy and sort array
var cols_composite_sorted = cols_composite_index.slice();
cols_composite_sorted.sort ( function ( a, b )
{
return jpv_colsSort ( a, b, $this );
} );
//find index of cols_composite_index[i]in cols_composite_sorted and map its index
for ( i = 0; i < len ; i++ )
cols_composite_index_remap[i] = in_array ( cols_composite_index[i], cols_composite_sorted, false );
//sort rows and create remapping
var rows_composite_index_remap = [];
rows_composite_sorted = [];
len = rows_composite_index.length;
//copy array
//for (i=0; i < len ; i++)
// rows_composite_sorted[i] = rows_composite_index[i];
var rows_composite_sorted = rows_composite_index.slice();
rows_composite_sorted.sort ( function ( a, b )
{
return jpv_rowsSort ( a, b, $this );
} );
//find index of cols_composite_index[i]in cols_composite_sorted and map its index
for ( i = 0; i < len ; i++ )
rows_composite_index_remap[i] = in_array ( rows_composite_index[i], rows_composite_sorted, false );
//sort unique keys for filters
for ( var i = 0, len = pre_uniquie_keys.length; i < len; i++ )
{
for ( var j in pre_uniquie_keys[i] )
{
pv.unique_keys[i].push ( j );
}
pv.unique_keys[i].sort();
}
pv.col_keys_length = col_keys_length;
pv.row_keys_length = row_keys_length;
pv.data_rows_count = rows_composite_index.length + col_keys_length; //rows count+col headers rows
pv.data_row_length = cols_composite_index.length + row_keys_length;
pv.data = [];
pv.data = jpv_create_2Darray ( pv.data_rows_count );
//init pv_data
var rn;
var cn;
for ( i = 0; i < data_length; i++ )
{
rn = rows_composite_index_remap[data_row2pv_row[i]] + col_keys_length; //data row mapped to pv.data[rn] row
if ( ( rn == null ) || ( data_row2pv_col[i] == null ) )
continue; //row filtered, so it not mapped
for ( j = 0; j < row_keys_length; j++ )
pv.data[rn][j] = data_ptr[i][rows_ptr[j]]; //fill row keys
//store data_ptr index instead of actual value
//this will be needed on totals and data display functions
cn = cols_composite_index_remap[data_row2pv_col[i]] + row_keys_length; //data row mapped to pv.data[rn][cn] col
if ( typeof pv.data[rn][cn] == 'undefined' )
pv.data[rn][cn] = []; //init value
pv.data[rn][cn].push ( i ); //add data row index to value
/* TODO some overworking in cols_keys fill */
if ( data_ptr[i][data_col] != null ) //fill cols keys
for ( c = 0; c < col_keys_length; c++ )
pv.data[c][cn] = data_ptr[i][cols_ptr[c]];
}
for ( c = 0; c < col_keys_length; c++ )
for ( var r = 0; r < row_keys_length; r++ )
pv.data[c][r] = null;
//console.dir(pv.data);
//create rows totals and spans
// contaln array wih values of pv.data cel
//save pv.data cells to totals
//save with undefined
// links and count of cells wiil be needed by statistics functions ( MIN for exmple)
jpv_nullifyHeaderData ( $this )
var cur_key;
//format of totals = [key][key_pos][data_array]
//where key =0;key < row_keys_length-1 - is a number of row_key - 0 - is most left in table
//keypos - cur number of total row (one for each distinct key value)
//format of data_rows_totals_pos - hold postiton of pv.data cll in totals_row
// pv.data_rows_totals_pos[rw][i][key] = key_pos
pv.rows_totals = jpv_create_2Darray ( row_keys_length );
pv.data_rows_totals_pos = [];
for ( r = col_keys_length; r < pv.data_rows_count; r++ )
{
pv.data_rows_totals_pos[r] = [];
for ( var i = row_keys_length; i < pv.data_row_length; i++ )
{
pv.data_rows_totals_pos[r][i] = [];
}
}
function fill_cnt ( rw )
{
for ( var i = row_keys_length; i < pv.data_row_length; i++ )
{
pv.rows_totals[c][cur_key][i].push( pv.data[rw][i] );
pv.data_rows_totals_pos[rw][i][c] = cur_key;
}
}
if ( row_keys_length > 1 )
{
for ( c = 0; c < row_keys_length - 1; c++ )
{
cur_key = 0;
pv.rows_totals[c][cur_key] = jpv_create_2Darray ( pv.data_row_length );//hold ptr to array with totals
pv.rows_totals[c][cur_key] [c] = pv.data[col_keys_length][c];
fill_cnt ( col_keys_length )
for ( r = col_keys_length + 1; r < pv.data_rows_count; r++ )
{
if ( pv.data[r][c] != null )
{
pv.rows_totals[c][++cur_key] = jpv_create_2Darray ( pv.data_row_length );
pv.rows_totals[c][cur_key] [c] = pv.data[r][c];
}
fill_cnt ( r );
}
}
}
pv.cols_totals = jpv_create_2Darray ( col_keys_length );
pv.data_cols_totals_pos = [];
for ( var i = col_keys_length; i < pv.data_rows_count; i++ )
{
pv.data_cols_totals_pos[i] = []
for ( c = row_keys_length; c < pv.data_row_length; c++ )
{
pv.data_cols_totals_pos[i][c] = []
}
}
function fill_cnt_col ( col )
{
for ( var i = col_keys_length; i < pv.data_rows_count; i++ )
{
pv.cols_totals[r][cur_key][i][pv.cols_totals[r][cur_key][i].length + 1] = pv.data[i][col];
pv.data_cols_totals_pos[i][col][r] = cur_key;
}
}
if ( col_keys_length > 1 )
{
for ( r = 0; r < col_keys_length - 1; r++ )
{
cur_key = 0;
pv.cols_totals[r][cur_key] = jpv_create_2Darray ( pv.data_rows_count );//hold ptr to array with totals
pv.cols_totals[r][cur_key][r] = pv.data[r][row_keys_length];
fill_cnt_col ( row_keys_length )
for ( c = row_keys_length + 1; c < pv.data_row_length; c++ )
{
if ( pv.data[r][c] != null )
{
pv.cols_totals[r][++cur_key] = jpv_create_2Darray ( pv.data_rows_count )
pv.cols_totals[r][cur_key] [r] = pv.data[r][c];
}
fill_cnt_col ( c );
}
}
}
//Grand totals
pv.grand_cols_totals = jpv_create_2Darray ( pv.data_rows_count );
pv.grand_rows_totals = jpv_create_2Darray ( pv.data_row_length );
for ( c = row_keys_length; c < pv.data_row_length; c++ )
for ( r = col_keys_length; r < pv.data_rows_count; r++ )
{
//if (pv.data[r][c]!= null)
pv.grand_cols_totals[r].push ( pv.data[r][c] );
pv.grand_rows_totals[c].push ( pv.data[r][c] );
}
/// aggregators setup
if ( typeof debug === 'undefined' )
{
debug = 0;
}
if ( debug && ( debug !== 3 ) )
{
opts.aggregate_value.aggregator = jpv_debug_value;
opts.aggregate_total_col.aggregator = jpv_debug_total;
opts.aggregate_total_row.aggregator = jpv_debug_total;
}
else
{
switch ( opts.aggregate_value.aggregator )
{
case null: //default
case 'SUM':
opts.aggregate_value.aggregator = jpv_aggregateSUM_val;
break;
case 'AVG':
opts.aggregate_value.aggregator = jpv_aggregateAVG_val;
break;
case 'PERC':
opts.aggregate_value.aggregator = jpv_aggregatePERC_val;
break;
}
switch ( opts.aggregate_total_col.aggregator )
{
case null://default
case 'SUM':
opts.aggregate_total_col.aggregator = jpv_aggregateSUM_total;
break;
case 'AVG':
opts.aggregate_total_col.aggregator = jpv_aggregateAVG_total;
break;
}
switch ( opts.aggregate_total_row.aggregator )
{
case null://default
case 'SUM':
opts.aggregate_total_row.aggregator = jpv_aggregateSUM_total;
break;
case 'AVG':
opts.aggregate_total_row.aggregator = jpv_aggregateAVG_total;
break;
}
}
//set default precision for values calculations
if ( ( typeof( opts.aggregate_value.precision ) === 'undefined' ) || ( opts.aggregate_value.precision == null ) )
opts.aggregate_value.precision = 2;
opts.aggregate_value.precision_k = Math.pow( 10, opts.aggregate_value.precision );
if ( ( typeof( opts.aggregate_total_col.precision ) === 'undefined' ) || ( opts.aggregate_total_col.precision == null ) )
opts.aggregate_total_col.precision = 2;
opts.aggregate_total_col.precision_k = Math.pow( 10, opts.aggregate_total_col.precision );
//opts.pivot_data = pv;
if ( ( typeof( opts.aggregate_total_row.precision ) === 'undefined' ) || ( opts.aggregate_total_row.precision == null ) )
opts.aggregate_total_row.precision = 2;
opts.aggregate_total_row.precision_k = Math.pow( 10, opts.aggregate_total_row.precision );
//opts.pivot_data = pv;
if ( opts.precalculateTotals )
{
jpv_precalculateTotals ( opts );
}
if ( opts.onAfterPrepare )
{
opts.onAfterPrepare();
}
}
function jpv_pivotDrawData()
{
var $this = this;
if ( $this.opts.onBeforeDraw )
$this.opts.onBeforeDraw.call(this);
jpv_nullifyHeaderData ( $this );
var table_data = [];
var opts = $this.opts;
var pv = $this.pv;
var filter_length = opts.filter.length;
var row_keys_length = pv.row_keys_length;
row_keys_length = ( !row_keys_length ) ? 1 : row_keys_length; //fix when no row keys, reserve place for key header placeholder
var col_keys_length = pv.col_keys_length;
//temp vars
var r;
var c;
//create array for table_data
var td_data_rows_start = 1 + col_keys_length;//actual data start 1(row headers here and delimiter) +[col keys count]
var td_rows_count = pv.data_rows_count + 1;//+ [number of rows +1 for horizontal delimiter];