From 4ae7ce24d8e0a8f5107dde5c8e76af23dd751415 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Sat, 26 Nov 2016 10:39:31 -0600 Subject: [PATCH] Core: Add option validator. Fixes #1319 --- js/jquery.tablesorter.js | 37 +++++++++++++++++++++++- js/parsers/parser-date-two-digit-year.js | 3 ++ js/parsers/parser-input-select.js | 4 +++ js/widgets/widget-build-table.js | 3 ++ js/widgets/widget-storage.js | 11 +++++++ 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/js/jquery.tablesorter.js b/js/jquery.tablesorter.js index c9cbac12..87d1ed5b 100644 --- a/js/jquery.tablesorter.js +++ b/js/jquery.tablesorter.js @@ -116,7 +116,11 @@ headerList: [], empties: {}, strings: {}, - parsers: [] + parsers: [], + + // *** parser options for validator; values must be falsy! + globalize: 0, + imgAttr: 0 // removed: widgetZebra: { css: ['even', 'odd'] } @@ -290,6 +294,7 @@ ts.setupParsers( c ); // start total row count at zero c.totalRows = 0; + ts.validateOptions( c ); // build the cache for the tbody cells // delayInit will delay building the cache until the user starts a sort if ( !c.delayInit ) { ts.buildCache( c ); } @@ -1911,6 +1916,8 @@ widget = ts.getWidgetById( c.widgets[ indx ] ); if ( widget && widget.options ) { c.widgetOptions = $.extend( true, {}, widget.options, c.widgetOptions ); + // add widgetOptions to defaults for option validator + $.extend( true, ts.defaults.widgetOptions, widget.options ); } } } @@ -2396,6 +2403,34 @@ return str; }, + validateOptions : function( c ) { + var setting, setting2, typ, timer, + // ignore options containing an array + ignore = 'sortForce sortList sortAppend widgets'.split( ' ' ), + orig = c.originalSettings; + if ( orig ) { + if ( c.debug ) { + timer = new Date(); + } + for ( setting in orig ) { + typ = typeof ts.defaults[setting]; + if ( typ === 'undefined' ) { + console.warn( 'Tablesorter Warning! "table.config.' + setting + '" option not recognized' ); + } else if ( typ === 'object' ) { + for ( setting2 in orig[setting] ) { + typ = typeof ts.defaults[setting][setting2]; + if ( $.inArray( setting, ignore ) < 0 && typ === 'undefined' ) { + console.warn( 'Tablesorter Warning! "table.config.' + setting + '.' + setting2 + '" option not recognized' ); + } + } + } + } + if ( c.debug ) { + console.log( 'validate options time:' + ts.benchmark( timer ) ); + } + } + }, + // restore headers restoreHeaders : function( table ) { var index, $cell, diff --git a/js/parsers/parser-date-two-digit-year.js b/js/parsers/parser-date-two-digit-year.js index 08b8241d..3f0b4b83 100644 --- a/js/parsers/parser-date-two-digit-year.js +++ b/js/parsers/parser-date-two-digit-year.js @@ -13,6 +13,9 @@ ts = $.tablesorter, now = new Date().getFullYear(); + // add dateRange to defaults for validator; value must be falsy + ts.defaults.dataRange = ''; + if ( !ts.dates ) { ts.dates = {}; } ts.dates.regxxxxyy = /(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{2})/; ts.dates.regyyxxxx = /(\d{2})[\/\s](\d{1,2})[\/\s](\d{1,2})/; diff --git a/js/parsers/parser-input-select.js b/js/parsers/parser-input-select.js index dfcbdee1..d032b549 100644 --- a/js/parsers/parser-input-select.js +++ b/js/parsers/parser-input-select.js @@ -110,6 +110,10 @@ type : 'text' }); + // update defaults for validator; values must be falsy + ts.defaults.checkboxClass = ''; + ts.defaults.checkboxVisible = ''; + // update select and all input types in the tablesorter cache when the change event fires. // This method only works with jQuery 1.7+ // you can change it to use delegate (v1.4.3+) or live (v1.3+) as desired diff --git a/js/widgets/widget-build-table.js b/js/widgets/widget-build-table.js index cf087529..4c3c3538 100644 --- a/js/widgets/widget-build-table.js +++ b/js/widgets/widget-build-table.js @@ -79,6 +79,9 @@ } }; + // add data to defaults for validator; value must be falsy! + ts.defaults.data = ''; + bt.defaults = { // *** build widget core *** build_type : '', // array, csv, object, json, html diff --git a/js/widgets/widget-storage.js b/js/widgets/widget-storage.js index 948356aa..351baf83 100644 --- a/js/widgets/widget-storage.js +++ b/js/widgets/widget-storage.js @@ -43,6 +43,17 @@ url = options && options.url || $table.attr(options && options.page || wo && wo.storage_page || 'data-table-page') || wo && wo.storage_fixedUrl || c && c.fixedUrl || window.location.pathname; + // update defaults for validator; these values must be falsy! + $.extend(true, ts.defaults, { + fixedUrl: '', + widgetOptions: { + storage_fixedUrl: '', + storage_group: '', + storage_page: '', + storage_tableId: '', + storage_useSessionStorage: '' + } + }); // https://gist.github.com/paulirish/5558557 if (storageType in window) { try {