PageSourceSearch

https://georgiactsa.org/_includes/js/absolute.js

js georgiactsa.org collected 2026-10-01 13:06:33 UTC 5,077 bytes, 184 lines download raw bytes

1/**
2 * When sorting a DataTable you might find that you want to keep a specific
3 * item at the top or bottom of the table. For example when sorting a column
4 * of numbers, if a value is `null` or `N/A` you might want to keep it at the
5 * bottom of the table, regardless of if ascending or descending sorting is
6 * applied. This plug-in provides that ability.
7 *
8 * You must call the `$.fn.dataTable.absoluteOrder` with information about the
9 * value(s) you wish to make absolute in the sorting order and store the
10 * returned value, assigning it to the `columns.type` option for the column
11 * you wish this sorting to be applied to.
12 *
13 * For number based columns a `$.fn.dataTable.absoluteOrderNumber` function is
14 * also available.
15 *
16 * @name Absolute sorting
17 * @summary Keep one or more items at the top and/or bottom of a table when sorting
18 * @author [Allan Jardine](//datatables.net)
19 * @depends DataTables 1.10+
20 *
21 * @example
22 *    var namesType = $.fn.dataTable.absoluteOrder( [
23 *      { value: '', position: 'top' }
24 *    ] );
25 *    
26 *    var numbersType = $.fn.dataTable.absoluteOrderNumber( [
27 *      { value: 'N/A', position: 'bottom' }
28 *    ] );
29 *    
30 *    $('#example').DataTable( {
31 *      columnDefs: [
32 *        { type: namesType, targets: 0 },
33 *        { type: numbersType, targets: 1 }
34 *      ]
35 *    } );
36 */
37
38(function( factory ){
39	if ( typeof define === 'function' && define.amd ) {
40		// AMD
41		define( ['jquery', 'datatables.net'], function ( $ ) {
42			return factory( $, window, document );
43		} );
44	}
45	else if ( typeof exports === 'object' ) {
46		// CommonJS
47		module.exports = function (root, $) {
48			if ( ! root ) {
49				root = window;
50			}
51
52			if ( ! $ || ! $.fn.dataTable ) {
53				$ = require('datatables.net')(root, $).$;
54			}
55
56			return factory( $, root, root.document );
57		};
58	}
59	else {
60		// Browser
61		factory( jQuery, window, document );
62	}
63}(function( $, window, document, undefined ) {
64'use strict';
65
66// Unique value allowing multiple absolute ordering use cases on a single page.
67var _unique = 0;
68
69// Function to encapsulate code that is common to both the string and number
70// ordering plug-ins.
71var _setup = function ( values ) {
72	if ( ! $.isArray( values ) ) {
73		values = [ values ];
74	}
75
76	var o = {
77		name: 'absoluteOrder'+(_unique++),
78		alwaysTop: {},
79		alwaysBottom: {}
80	};
81
82	// In order to provide performance, the symbols that are to be looked for
83	// are stored as parameter keys in an object, allowing O(1) lookup, rather
84	// than O(n) if it were in an array.
85	for ( var i=0, ien=values.length ; i<ien ; i++ ) {
86		var conf = values[i];
87
88		if ( typeof conf === 'string' ) {
89			o.alwaysTop[ conf ] = true;
90		}
91		else if ( conf.position === undefined || conf.position === 'top' ) {
92			o.alwaysTop[ conf.value ] = true;
93		}
94		else {
95			o.alwaysBottom[ conf.value ] = true;
96		}
97	}
98
99	// Ascending ordering method
100	o.asc = function ( a, b, isNumber ) {
101		if ( o.alwaysTop[ a ] && o.alwaysTop[ b ] ) {
102			return 0;
103		}
104		else if ( o.alwaysBottom[ a ] && o.alwaysBottom[ b ] ) {
105			return 0;
106		}
107		else if ( o.alwaysTop[ a ] || o.alwaysBottom[ b ] ) {
108			return -1;
109		}
110		else if ( o.alwaysBottom[ a ] || o.alwaysTop[ b ] ) {
111			return 1;
112		}
113
114		if ( isNumber ) {
115			// Cast as a number if required
116			if ( typeof a === 'string' ) {
117				a = a.replace(/[^\d\-\.]/g, '') * 1;
118			}
119			if ( typeof b === 'string' ) {
120				b = b.replace(/[^\d\-\.]/g, '') * 1;
121			}
122		}
123
124		return ((a < b) ? -1 : ((a > b) ? 1 : 0));
125	};
126
127	// Descending ordering method
128	o.desc = function ( a, b, isNumber ) {
129		if ( o.alwaysTop[ a ] && o.alwaysTop[ b ] ) {
130			return 0;
131		}
132		else if ( o.alwaysBottom[ a ] && o.alwaysBottom[ b ] ) {
133			return 0;
134		}
135		else if ( o.alwaysTop[ a ] || o.alwaysBottom[ b ] ) {
136			return -1;
137		}
138		else if ( o.alwaysBottom[ a ] || o.alwaysTop[ b ] ) {
139			return 1;
140		}
141
142		if ( isNumber ) {
143			if ( typeof a === 'string' ) {
144				a = a.replace(/[^\d\-\.]/g, '') * 1;
145			}
146			if ( typeof b === 'string' ) {
147				b = b.replace(/[^\d\-\.]/g, '') * 1;
148			}
149		}
150
151		return ((a < b) ? 1 : ((a > b) ? -1 : 0));
152	};
153
154	return o;
155};
156
157// String based ordering
158$.fn.dataTable.absoluteOrder = function ( values ) {
159	var conf = _setup( values );
160
161	$.fn.dataTable.ext.type.order[ conf.name+'-asc' ] = conf.asc;
162	$.fn.dataTable.ext.type.order[ conf.name+'-desc' ] = conf.desc;
163
164	// Return the name of the sorting plug-in that was created so it can be used
165	// with the `columns.type` parameter. There is no auto-detection here.
166	return conf.name;
167};
168
169// Number based ordering - strips out everything but the number information
170$.fn.dataTable.absoluteOrderNumber = function ( values ) {
171	var conf = _setup( values );
172
173	$.fn.dataTable.ext.type.order[ conf.name+'-asc' ] = function ( a, b ) {
174		return conf.asc( a, b, true );
175	};
176	$.fn.dataTable.ext.type.order[ conf.name+'-desc' ] = function ( a, b ) {
177		return conf.desc( a, b, true );
178	};
179
180	return conf.name;
181};
182
183
184}));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.