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