/
dataTables.keepConditions.js
executable file
·1584 lines (1315 loc) · 69 KB
/
dataTables.keepConditions.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
/**
* @summary KeepConditions
* @updated 11/27/15
* @description Store the status of the DataTable within the URL, making
* sharing the exact page/length/etc via copy/paste possible
* @version 1.2.0
* @file dataTables.keepConditions.js
* @author Justin Hyland (http://www.justinhyland.com)
* @contact j@linux.com
* @copyright Copyright 2015 Justin Hyland
* @url https://github.com/jhyland87/DataTables-Keep-Conditions
*
* License MIT - http://datatables.net/license/mit
*
* Store the DataTable conditions within the URL hash every time a condition is changed,
* such as the page, length, search or a column order, making it possible to copy/paste
* the URL. Once said URL is loaded, the conditions will be retrieved from the URL hash
* and implemented to the table on dt.init
*
* KeepConditions is compatable with the following settings/extensions/plugins:
* Pagination (name: page; key: p)
* Page Length (name: length; key: l)
* Table Searching (name: search; key: f)
* Column Sorting (name: order; key: o)
* Scroller Extension (name: scroller; key: s)
* http://datatables.net/extensions/scroller/
* Column Visibility (name: colvis; key: v)
* http://datatables.net/reference/button/colvis/
* Column Reorder (name: colorder; key: c)
* http://datatables.net/extensions/colreorder/
*
* @example
* // Basic Initialization (All conditions by default)
* $('#example').DataTable({
* keepConditions: true
* });
*
* @example
* // Basic Initialization (Specifically specifying enabled conditions, individually)
* $('#example').DataTable({
* keepConditions: ['order','page','length','search','colvis','colorder','scroller']
* });
*
* @example
* // Same as above, only quicker (Using condition keys in string, instead of names)
* $('#example').DataTable({
* keepConditions: 'oplfvcs'
* });
*
* @example
* // Basic Initialization with "Copy Conditions" button
* $('#example').DataTable({
* keepConditions: true,
* buttons: [
* 'copyConditions'
* ]
* });
*
* @example
* // Initialization with plugins
* $('#example').DataTable({
* ajax: "dataSrc.txt",
* deferRender: true,
* scrollY: 200,
* scrollCollapse: true,
* scroller: true,
* keepConditions: {
* conditions: ['order','length','search','scroller']
* }
* });
*
* @example
* // Same as above, but don't attach the auto-update to the events associated to
* // each condition (since it can also be ran manually via the API methods)
* $('#example').DataTable({
* ajax: "dataSrc.txt",
* deferRender: true,
* scrollY: 200,
* scrollCollapse: true,
* scroller: true,
* keepConditions: {
* conditions: 'olfs',
* attachEvents: false
* }
* });
*/
"use strict";
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var KeepConditions = (function () {
/**
* KeepConditions Constructor
*
* This can be initiated automatically when the keepConditions setting is set in
* the DT initiation settings, or by manually creating a new KeepConditions
* instance via the 'new' keyword. When manually executing new KeepConditions(),
* the user can pass either the DataTables instance, an API instance, or a CSS
* selector for the DataTables instance, the DataTables settings and API are
* extracted from any of the above.
*
* @param {object} dtSettings DataTable settings object, Required to bind KC instance to
* specific DT instance
* @var {object} _dtApi DataTables API Instance
* @var {object} _dtSettings DT Settings
* @var {object} _dtDefaults DataTables default settings
* @var {array} _enabledConditions List of enabled conditions (As in conditions being kept via
* KeepConditions)
*/
function KeepConditions(dtSettings) {
_classCallCheck(this, KeepConditions);
// Check that we were initiated on an actual DataTable (Either by selector,
// a DT instance, or an API instance of a DT)
if (!$.fn.DataTable.isDataTable(dtSettings) && !dtSettings instanceof $.fn.dataTable.Api) {
throw new Error('Failed to initialize KeepConditions plugin on non-datatable object');
}
/**
* Retrieve the DataTables API Instance
*/
if (dtSettings instanceof $.fn.dataTable.Api) this._dtApi = dtSettings;else this._dtApi = new $.fn.dataTable.Api(dtSettings);
/**
* In case this was initiated via something like a CSS selector, reset the settings
* via the API we know is legit
*/
dtSettings = this._dtApi.settings()[0];
/**
* DataTables settings object for this DT instance
*/
this._dtSettings = dtSettings;
/**
* Unique table ID of this DT Instance
*/
this._tableId = $(this._dtApi.table().node()).attr('id');
/**
* DataTables default settings
*/
this._dtDefaults = $.fn.dataTable.defaults;
/**
* Map of the condition keys to the condition names
*/
this._keysToCons = this._keyMap();
/**
* Boolean value to determine if the table should be redrawn whenever
* _drawTable( ) is called, should be set to true if any changes are made
* to the table, and the table wasn't redrawn
*/
this._shouldDraw = false;
/**
* List of enabled conditions, populated when DataTables is initiated
*/
this._enabledConditions = [];
/**
* Just the namespace to attach/detach events from for DT events. This is added to the
* DT Event namespace, so when we attach/detach functions from the 'draw.dt' event,
* it's then 'draw.dt.keepConditions' (If defaulted)
*/
this._eventNamespace = 'keepConditions';
/**
* Add this object to the DataTables setting object, so each table can have its own
* unique KeepConditions object, this is what makes it possible to have multiple
* tables using the plugin, on the same page
*/
dtSettings.oKeepConditions = this;
/**
* Initiate the main KeepConditions plugin functionality, such as parsing the initial
* URL hash value and implementing the results in the table, attaching the enabled
* conditions to be managed to the associated Datatables events, etc etc.
*/
this._init();
}
// -----------------------------------------------------------
/**
* (Hash) Query String
*
* Parse the url query-like string value of the URL hash. EG: #var1=val1&var2=val2
* will result in {var1: 'val1', var2: 'val2'};
*
* @access public
* @return object
*/
_createClass(KeepConditions, [{
key: '_init',
// -----------------------------------------------------------
/**
* Initiate (Main KeepConditions Plugin Functions)
*
* Initiate the main KeepConditions plugin functionality, such as parsing the initial
* URL hash value and implementing the results in the table, attaching the enabled
* conditions to be managed to the associated Datatables events, etc etc.
*
* @access private
* @return {void}
*/
value: function _init() {
// Enable any enabled/initiated settings/Extensions/Plugins
this._collectEnabled();
// Check if the events should be attached, they can be detached if the keepConditions
// setting is an object, with 'attachEvents' set to false
if (this._dtSettings.oInit.keepConditions === true || typeof this._dtSettings.oInit.keepConditions === 'string' || $.isArray(this._dtSettings.oInit.keepConditions) || $.isPlainObject(this._dtSettings.oInit.keepConditions) && (typeof this._dtSettings.oInit.keepConditions.attachEvents === 'undefined' || this._dtSettings.oInit.keepConditions.attachEvents === true)) this.attachEvents();
// Parse the URL hash value, have each condition object process it's associated
// hash element value, re-drawing the table accordingly
this.processHash();
}
// -----------------------------------------------------------
/**
* Collect Enabled (Conditions)
*
* Set any conditions that are enabled in the keepConditions initial settings, by adding them
* to the enabledConditions
*/
}, {
key: '_collectEnabled',
value: function _collectEnabled() {
var _this = this;
// Loop through all available conditions, checking if enabled
$.each(this.conditions(), function (sCondition, oCondition) {
// Check if condition is enabled in plugin settings, and if the table was initialized
// with said setting/extension/plugin (The latter is unique per condition, so each
// condition object has it's own method to check if table was init with condition)
if (!_this._isEnabled(sCondition) || !oCondition.isInit()) return;
// Add it to the enabled conditions list/array
_this.enableCondition(sCondition);
});
}
// -----------------------------------------------------------
/**
* Map (Condition) Keys (To Condition Names)
*
* Executed by _keysToCons to basically map each conditions key to the condition name,
* primarily used to associate the condition keys within the hash string to the condition
* name themselves.
*
* @access private
* @return {object} Returns: { f: 'search', l: 'length', ... }
*/
}, {
key: '_keyMap',
value: function _keyMap() {
return (function (conditions) {
var ret = {};
$.each(conditions, function (name, con) {
ret[con.key] = name;
});
return ret;
})(this.conditions());
}
// -----------------------------------------------------------
/**
* Is (Specific Condition) Enabled
*
* Check if a specific condition is enabled to be managed via KeepConditions
* plugin. Condition(s) can be enabled if the DT setting 'keepConditions' is
* simply 'true', which would enable all available conditions, or it can be
* a string of condition keys, or an array of condition names, or an object,
* which may contain a 'conditions' property, which can also be either an
* array of enabled conditions, or a string of condition names, or if it is
* undefined, then all available conditions are enabled.
* Note: This does not validate if the associated setting/extension/plugin
* is enabled within DataTables, but rather just set to be managed via
* KeepConditions
*
* @param {string} condition Condition name to validate
* @access private
* @return {boolean}
*/
}, {
key: '_isEnabled',
value: function _isEnabled(condition) {
var options = this._dtSettings.oInit.keepConditions;
// If were verifying a condition status by the key...
if (condition.length === 1) {
// Attempt to get the name, if it fails, throw an error..
var name = this.nameByKey(condition);
if (!condition) throw new Error('Unable to find an existing condition with the key \'' + condition + '\'');
// .. Otherwise, set the condition to the retreived name and continue
condition = name;
}
// If were verifying by the name, and the name wasn't found...
else if (this.conditions(condition) === false) {
throw new Error('Unable to find an existing condition with the name \'' + condition + '\'');
}
// Return the result based on the initial DT 'keepConditions' value, (Details on
// logic process in the above DocBlock comment)
return(
// A) If the initial 'keepConditions' is 'true', just say yes to all conditions
options === true
// If its undefined (This should only happen if KeepConditions is being
// initialized via manually executing: new KeepConditions( table )
|| typeof options === 'undefined'
// B) If the init config is a string of conditions (by keys)..
|| typeof options === 'string' && options.indexOf(this.conditions(condition).key) !== -1
// C) If the init config is an array of enabled conditions..
|| $.isArray(options) && $.inArray(condition, options) !== -1
// D) If the init configs 'conditions' property is an array of conditions..
|| $.isPlainObject(options) && $.isArray(options.conditions) && $.inArray(condition, options.conditions) !== -1
// E) If the init configs 'conditions' property is a string of conditions (by keys)..
|| $.isPlainObject(options) && typeof options.conditions === 'string' && options.conditions.indexOf(this.conditions(condition).key) !== -1
);
}
// -----------------------------------------------------------
/**
* Redraw the table
*
* This is mainly ran after all the onLoads have done for the conditions, instead of drawing the
* table after each condition is loaded, they will set _shouldDraw to true, then execute this,
* and this will check the _shouldDraw, then draw if necessary, and reset _shouldDraw.
*
* @param {boolean} force Force the draw, regardless of the value of this._shouldDraw
* @param {boolean} resetPaging Reset the paging or not (Sending view to first page)
* @access private
* @return {void}
*/
}, {
key: '_drawTable',
value: function _drawTable(force, resetPaging) {
if (this._shouldDraw === true || force === true) {
this._dtApi.draw(resetPaging === true);
this._shouldDraw = false;
}
}
// -----------------------------------------------------------
}, {
key: '_lang',
value: function _lang(key, string) {}
// -----------------------------------------------------------
/**
* Structure Hash (Conditions)
*
* Basically a non-static value of KeepConditions.structureHash(), mainly
* used via the API Methods
*
* @param {boolean} retrieve Return the hash value, as opposed
* to updating the URL hash
* @access public
* @return {void|string} Either returns the hash string, or updates hash
*/
}, {
key: 'structureHash',
value: function structureHash(retrieve) {
return KeepConditions.structureHash(this._dtSettings, retrieve);
}
// -----------------------------------------------------------
/**
* Just return DT Settings
*/
}, {
key: 'dtSettings',
value: function dtSettings() {
return this._dtSettings;
}
// -----------------------------------------------------------
/**
* Attach (Condition Update) Events
*
* Attach the KeepConditions.structureHash() method to any DT events that may require the hash to
* be updated (such as Col Reordering, Col Sort Order, Draw, etc)
*
* @access public
* @return {void}
*/
}, {
key: 'attachEvents',
value: function attachEvents() {
var _this2 = this;
var eventParams = {
dtSettings: this._dtSettings
},
enabledConditions = this.getEnabledConditions();
if (enabledConditions === false) throw new Error('No enabled conditions to attach to events');
var conditions = this.conditions(enabledConditions);
// Loop through all available conditions
$.each(conditions, function (sCondition, oCondition) {
// Attach the method that updates the hash, to the event associated with this condition
_this2._dtApi.on(oCondition.event + '.' + _this2._eventNamespace, eventParams, KeepConditions.structureHash.bind(KeepConditions));
});
}
// -----------------------------------------------------------
/**
* Detach (Condition Update) Events
*
* Detach the KeepConditions.structureHash() method to any DT events that may require the hash to
* be updated (such as Col Reordering, Col Sort Order, Draw, etc)
*
* @access public
* @return {void}
*/
}, {
key: 'detachEvents',
value: function detachEvents() {
var _this3 = this;
var eventParams = {
dtSettings: this._dtSettings
},
enabledConditions = this.getEnabledConditions();
if (enabledConditions === false) throw new Error('No enabled conditions to attach to events');
var conditions = this.conditions(enabledConditions);
// Loop through all available conditions
$.each(conditions, function (sCondition, oCondition) {
// Check if condition is enabled in plugin settings, and if the table was initialized
// with said setting/extension/plugin (The latter is unique per condition, so each
// condition object has it's own method to check if table was init with condition)
if (!_this3._isEnabled(sCondition) || !oCondition.isInit()) return;
// Attach the method that updates the hash, to the event associated with this condition
_this3._dtApi.off(oCondition.event + '.' + _this3._eventNamespace);
});
}
// -----------------------------------------------------------
/**
* Detach (Hash Update from conditions) Event
*
* The KeepConditions.structureHash() method is attached to the events specified by the 'event' property
* for each condition. This method can be used to detach that method from a specific DataTables event,
* which can be specified by giving the condition (which then the event is retrieved), or the exact
* DataTables event (ending in .dt, eg: draw.dt)
*
* @param {string|array} condition Either an array if multiple, or string of single condition(s)
* or DT event(s)
* @access public
* @return {void}
*/
}, {
key: 'detachEvent',
value: function detachEvent(condition) {
var _this4 = this;
if (typeof condition === 'undefined') {
console.warn('No condition or event specified for KeepConditions.detachEvent(), nothing is getting detached');
return;
}
// Retrieve the condition, also to make sure it exists
var oCondition = this.conditions(condition);
if (!oCondition) return false;
var event;
// Single condition or event
if (typeof condition === 'string') {
// If were given the exact event
if (condition.endsWith('.dt')) event = condition;
// If were given the condition to retrieve the event name from
else event = oCondition.event;
// Detach event callback
this._dtApi.off(event, KeepConditions.structureHash.bind(KeepConditions));
}
// Multiple events or conditions
else if ($.isArray(condition) && condition.length > 0) {
$.each(condition, function (i, c) {
// If were given the exact event
if (c.endsWith('.dt')) event = c;
// If were given the condition to retrieve the event name from, make sure it exists
else if (typeof oCondition[c] !== 'undefined') event = oCondition[c].event;
// Abort if we were given an incorrect condition
else throw new Error('Unknown condition specified: ' + c);
// Detach event callback
_this4._dtApi.off(event + '.' + _this4._eventNamespace);
});
}
// Whatever else
else {
// If we were given something that wasnt caught
console.warn('Illegal parameter type for KeepConditions.detachEvent(), should be array or string, was: ', typeof condition === 'undefined' ? 'undefined' : _typeof(condition));
}
}
// -----------------------------------------------------------
/**
* Attach (Hash Update to conditions) Event
*
* Attach the KeepConditions.structureHash() method to one or more specific event(s), specified either
* by the condition name (search, paging, etc) or the specific event itself (which should end with
* '.dt', EG: draw.dt)
*
* @param {string|array} condition Either an array if multiple, or string of single condition(s)
* or DT event(s)
* @access public
* @return {void}
* @todo Should 'this.enableCondition( sCondition );' be added? Dont think so
*/
}, {
key: 'attachEvent',
value: function attachEvent(condition) {
var _this5 = this;
if (typeof condition === 'undefined') {
console.warn('No condition or event specified for KeepConditions.attachEvent(), nothing is getting attached');
return;
}
// Data handed to the jQuery event
var eventParams = {
dtSettings: this._dtSettings
};
// Retrieve the condition, also to make sure it exists
var oCondition = this.conditions(condition);
if (!oCondition) return false;
var event;
//this._dtApi.on( oCondition.event, eventParams, KeepConditions.structureHash.bind( KeepConditions ) );
// Single condition or event
if (typeof condition === 'string') {
// If were given the exact event
if (condition.endsWith('.dt')) event = condition;
// If were given the condition to retrieve the event name from
else event = oCondition.event;
// Detach event callback
this._dtApi.on(event, eventParams, KeepConditions.structureHash.bind(KeepConditions));
}
// Multiple events or conditions
else if ($.isArray(condition) && condition.length > 0) {
$.each(condition, function (i, c) {
// If were given the exact event
if (c.endsWith('.dt')) event = c;
// If were given the condition to retrieve the event name from, make sure it exists
else if (typeof oCondition[c] !== 'undefined') event = oCondition[c].event;
// Abort if we were given an incorrect condition
else throw new Error('Unknown condition specified: ' + c);
// Detach event callback
_this5._dtApi.on(event + '.' + _this5._eventNamespace, KeepConditions.structureHash.bind(KeepConditions));
});
}
// Whatever else
else {
// If we were given something that wasn't caught
console.warn('Illegal parameter type for KeepConditions.attachEvent(), should be array or string, was: ' + (typeof condition === 'undefined' ? 'undefined' : _typeof(condition)));
}
}
// -----------------------------------------------------------
/**
* Process (Initial) URL Hash
*
* This is executed after KeepConditions has been initiated by DataTables, any conditions
* found in the URL hash will be parsed by the conditions onLoad( ) method (If the condition
* is enabled/initiated), then the table will be redrawn (if needed)
*
* @access public
* @return {void}
*/
}, {
key: 'processHash',
value: function processHash() {
var _this6 = this;
// Loop through each element in the hash, until we find an element whos key matches the table ID
$.each(KeepConditions.queryString(), function (table, cons) {
// If somehow thers more than one condition for this table, just take the first one..
if ($.isArray(cons) || $.isPlainObject(cons)) cons = cons[0];
// Skip to the next hash element if this one isn't for the current table
if (table !== _this6._tableId) return;
// Loop through each condition within the Hash, which is delimited by :
$.each(cons.split(':'), function (i, c) {
var conKey = c.charAt(0),
conVal = c.substring(1),
conName = _this6.nameByKey(conKey),
oCondition = _this6.conditions()[conName];
// Skip condition if its not enabled
if ($.inArray(conName, _this6.getEnabledConditions()) === -1) return;
if (typeof oCondition === 'undefined') {
console.warn('[keepConditions:\' ' + _this6._tableId + '] No condition object found for condition key:', conKey);
return;
}
// Have the condition object parse the hash
oCondition.onLoad(conVal);
});
// Draw the table if needed
_this6._drawTable();
});
}
// -----------------------------------------------------------
/**
* Enable Condition(s)
*
* Enable condition(s) to be managed via KeepConditions plugin - Basically just adds the condition(s) to
* this._enabledConditions. Conditions can be specified by either the full name,
* or the single character condition key
*
* @param {string|array} condition DataTables condition(s) to enable, condition(s) can be
* specified either by the full name, or the condition key
* @param {boolean} structureHash Restructure the hash after said condition has been enabled
* @access public
* @return {void}
*/
}, {
key: 'enableCondition',
value: function enableCondition(condition, structureHash) {
var _this7 = this;
var done = false;
// Process multiple conditions to enable
if ($.isArray(condition)) {
$.each(condition, function (i, c) {
// If its a key, then get the name from the key
if (c.length === 1) c = _this7.nameByKey(c);
if (_this7.conditions(c) !== false) {
_this7._enabledConditions.push(c);
done = true;
}
});
} else if (typeof condition === 'string') {
// If its a key, then get the name from the key
if (condition.length === 1) condition = this.nameByKey(condition);
if (this.conditions(condition) !== false) {
this._enabledConditions.push(condition);
done = true;
}
}
// If a condition was successfully enabled, and were requested to update the hash, do eeet!
if (structureHash === true && done === true) KeepConditions.structureHash(this._dtSettings, false);
}
// -----------------------------------------------------------
/**
* Disable Condition(s)
*
* Disable condition(s) from being managed via KeepConditions plugin - Basically just removes the
* condition(s) from this._enabledConditions. Conditions can be specified by either the full name,
* or the single character condition key
*
* @param {string|array} condition DataTables condition(s) to disable, condition(s) can be
* specified either by the full name, or the condition key
* @param {boolean} structureHash Restructure the hash after said condition has been disabled
* @access public
* @return {void}
*/
}, {
key: 'disableCondition',
value: function disableCondition(condition, structureHash) {
var _this8 = this;
var done = false;
// Process multiple conditions to disable
if ($.isArray(condition)) {
$.each(condition, function (i, c) {
// If its a key, then get the name from the key
if (c.length === 1) c = _this8.nameByKey(c);
if (_this8.conditions(c) !== false) {
_this8._enabledConditions.splice($.inArray(c, _this8._enabledConditions), 1);
done = true;
}
});
} else if (typeof condition === 'string') {
// If its a key, then get the name from the key
if (condition.length === 1) condition = this.nameByKey(condition);
if (this.conditions(condition) !== false) {
this._enabledConditions.splice($.inArray(condition, this._enabledConditions), 1);
done = true;
}
}
// If a condition was successfully disabled, and were requested to update the hash, do eeet!
if (structureHash === true && done === true) KeepConditions.structureHash(this._dtSettings, false);
}
// -----------------------------------------------------------
/**
* Get Enabled Conditions
*
* Returns a list of conditions being managed via plugin
*
* @access public
* @return {array|boolean} Array of conditions being kept, or false if none
*/
}, {
key: 'getEnabledConditions',
value: function getEnabledConditions() {
return this._enabledConditions.length > 0 ? $.unique(this._enabledConditions) : false;
}
// -----------------------------------------------------------
/**
* Condition Name by Key
*
* Return the name of a condition (search, length, colorder), given the key (f, l, c). Useful for
* when referencing conditions using the keys from the hash value
*
* @param {string} key Key of condition (Single alpha value, usually first name of
* condition, but not always)
* @access public
* @return {string} Full condition name (name of condition within this.conditions( ) obj)
*/
}, {
key: 'nameByKey',
value: function nameByKey(key) {
return this._keysToCons[key] || false;
}
// -----------------------------------------------------------
/**
* Conditions Manager
*
* Manages the object that contains the primary functionality for managing conditions,
* such as checkinf if the condition is enabled via DT, checking if the plugin/extension
* is initiated (if required), checking if hash value is set and valid, creating new hash
* value, etc etc
*
* @param {string|array|null} con Either string of single condition, or array of
* conditions, or null for all conditions
* @access public
* @return {object} Object of objects (conditions)
*/
}, {
key: 'conditions',
value: function conditions(con) {
var _this9 = this;
var _parent = this;
/**
* Main conditions object
*
* Each object within this object should be a unique condition that can be
* managed via KeepConditions. The keys need to be the name of the values
* stored within the DT initiation setting 'keepConditions', should conditions
* be specified, instead of just 'true'
*/
var conditions = {
/**
* Table searching (aka Filtering) condition
*/
search: {
// Hash Key
key: 'f',
// Event to trigger the hash update for
event: 'search.dt',
// Check if condition is setup on table
isInit: function isInit() {
return typeof _parent._dtSettings.oInit.searching === 'undefined' || _parent._dtSettings.oInit.searching !== false;
},
// Function to check if a condition exists in the hash, and to process it
onLoad: function onLoad(hashComponent) {
if (typeof hashComponent !== 'undefined' && _parent._dtApi.search() !== decodeURIComponent(hashComponent)) {
_parent._dtApi.search(decodeURIComponent(hashComponent));
_parent._shouldDraw = true;
}
},
// Check if a value for this condition is currently set for the table (and not at default)
isset: function isset() {
return _parent._dtApi.search().length !== 0;
},
// Return the new value to be stored in the hash for this conditions component
newHashVal: function newHashVal() {
return encodeURIComponent(_parent._dtApi.search());
}
},
/**
* Condition: Length
*
* @todo Check if the hash value is an existing value in the page length list
*/
length: {
// Hash Key
key: 'l',
// Event to trigger the hash update for
event: 'length.dt',
// Check if condition is setup on table
isInit: function isInit() {
return !(_parent._dtSettings.oInit.lengthChange === false || typeof _parent._dtSettings.oInit.lengthChange === 'undefined' && _parent._dtDefaults.bLengthChange === false);
},
// Function to check if a condition exists in the hash, and to process it
onLoad: function onLoad(hashComponent) {
if (typeof hashComponent !== 'undefined') {
_parent._dtApi.page.len(parseInt(hashComponent));
_parent._shouldDraw = true;
}
},
// Check if a value for this condition is currently set for the table (and not at default)
isset: function isset() {
return _parent._dtApi.page.len() && _parent._dtApi.page.len() !== (_parent._dtSettings.oInit.pageLength || _parent._dtDefaults.iDisplayLength);
},
// Return the new value to be stored in the hash for this conditions component
newHashVal: function newHashVal() {
return _parent._dtApi.page.len();
}
},
/**
* Pagination
*/
page: {
// Hash Key
key: 'p',
// Event to trigger the hash update for
event: 'page.dt',
// Check if condition is setup on table
isInit: function isInit() {
return !(_parent._dtSettings.oInit.paging === false || typeof _parent._dtSettings.oInit.paging === 'undefined' && _parent._dtDefaults.bPaginate === false);
},
// Function to check if a condition exists in the hash, and to process it
onLoad: function onLoad(hashComponent) {
if (typeof hashComponent !== 'undefined' && parseInt(hashComponent) !== 0) {
_parent._dtApi.page(parseInt(hashComponent));
_parent._shouldDraw = true;
}
},
// Check if a value for this condition is currently set for the table (and not at default)
isset: function isset() {
return _parent._dtApi.page.info() && _parent._dtApi.page.info().page !== 0;
},
// Return the new value to be stored in the hash for this conditions component
newHashVal: function newHashVal() {
return _parent._dtApi.page.info().page;
}
},
/**
* Column Visibility
*/
colvis: {
// Hash Key
key: 'v',
// Event to trigger the hash update for
event: 'column-visibility.dt',
// Colvis is always true, since it's actually just a column setting, nothing more
isInit: function isInit() {
return true;
},
// Function to check if a condition exists in the hash, and to process it
onLoad: function onLoad(hashComponent) {
if (typeof hashComponent !== 'undefined') {
var _ret = (function () {
var isVis = hashComponent.charAt(0),
columns = hashComponent.substring(1).split('.');
// If the header was messed with, just skip the col vis
if (isVis !== 'f' && isVis !== 't') {
console.warn('Unknown ColVis condition visibility value, expected t or f, found:', isVis);
return {
v: undefined
};
}
_parent._dtApi.columns().indexes().each(function (value, index) {
// Parse as visible list
if (isVis === 't') {
if ($.inArray(value.toString(), columns) === -1) _parent._dtApi.column(value).visible(false);else _parent._dtApi.column(value).visible(true);
}
// Parse as hidden list
else {
if ($.inArray(value.toString(), columns) === -1) _parent._dtApi.column(value).visible(true);else _parent._dtApi.column(value).visible(false);
}
});
_parent._shouldDraw = true;
})();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
},
// Check if a value for this condition is currently set for the table (and not at default)
isset: function isset() {
return _parent._dtApi.columns().visible().filter(function (v) {
return !v;