PageRenderTime 40ms CodeModel.GetById 20ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/public/js/tablesorter/parsers/parser-date-weekday.js

https://gitlab.com/webster5361/UserFrosting
JavaScript | 96 lines | 73 code | 7 blank | 16 comment | 14 complexity | 3ccf64f3ca15634fe36d94c54203d095 MD5 | raw file
 1/*! Parser: weekday - updated 11/2/2015 (v2.24.1) */
 2/* Demo: http://jsfiddle.net/Mottie/abkNM/4169/ */
 3/*jshint jquery:true */
 4;(function($){
 5	'use strict';
 6
 7	var ts = $.tablesorter;
 8	ts.dates = $.extend( true, {}, {
 9		// See http://mottie.github.io/tablesorter/docs/example-widget-grouping.html
10		// for details on how to use CLDR data for a locale to add data for this parser
11		// CLDR returns { sun: "Sun", mon: "Mon", tue: "Tue", wed: "Wed", thu: "Thu", ... }
12		weekdays : {
13			'en' :  {
14				'sun' : 'Sun',
15				'mon' : 'Mon',
16				'tue' : 'Tue',
17				'wed' : 'Wed',
18				'thu' : 'Thu',
19				'fri' : 'Fri',
20				'sat' : 'Sat'
21			}
22		},
23
24		// set table.config.weekStarts to change weekday start date for your locale
25		// cross-reference of a date on which the week starts on a...
26		// https://github.com/unicode-cldr/cldr-core/blob/master/supplemental/weekData.json
27		weekStartList : {
28			'sun' : '1995', // Sun 1/1/1995
29			'mon' : '1996', // Mon 1/1/1996
30			'fri' : '1999', // Friday 1/1/1999
31			'sat' : '2000'  // Sat 1/1/2000
32		},
33
34		// do not modify this array; it is used for cross referencing
35		weekdaysXref : [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ]
36
37	}, ts.dates );
38
39	ts.addParser({
40		id: 'weekday',
41		is: function() {
42			return false;
43		},
44		format: function( str, table, cell, cellIndex ) {
45			if ( str ) {
46				var d, day, num,
47					c = table.config,
48					// add options to 'config.globalize' for all columns --> globalize : { lang: 'en' }
49					// or per column by using the column index --> globalize : { 0 : { lang: 'fr' } }
50					options = c.globalize && ( c.globalize[ cellIndex ] || c.globalize ) || {},
51					days = ts.dates.weekdays[ options.lang || 'en' ],
52					xref = ts.dates.weekdaysXref;
53				if ( c.ignoreCase ) {
54					str = str.toLowerCase();
55				}
56				for ( day in days ) {
57					if ( typeof day === 'string' ) {
58						d = days[ day ];
59						if ( c.ignoreCase ) {
60							d = d.toLowerCase();
61						}
62						if ( str.match( d ) ) {
63							num = $.inArray( day, xref );
64							return num > -1 ? num : str;
65						}
66					}
67				}
68			}
69			return str;
70		},
71		type: 'numeric'
72	});
73
74	// useful when a group widget date column is set to "group-date-week"
75	// and you want to only sort on the day of the week ignore the actual date, month and year
76	ts.addParser({
77		id: 'weekday-index',
78		is: function() {
79			return false;
80		},
81		format: function( str, table ) {
82			if ( str ) {
83				var c = table.config,
84					date = new Date( str );
85				if ( date instanceof Date && isFinite( date ) ) {
86					// use a specific date that started with that weekday so sorting is only going to be
87					// based on the day of the week and not the date, month or year
88					return new Date( '1/' + ( date.getDay() + 1 ) + '/' + ts.dates.weekStartList[ c.weekStarts || 'sun' ] );
89				}
90			}
91			return str;
92		},
93		type: 'numeric'
94	});
95
96})(jQuery);