data_table.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. jQuery.extend(jQuery.fn.dataTableExt.oSort, {
  19. // @see utils/date.js
  20. "ambari-date-pre":function (date_string) {
  21. date_string = $(date_string).text(); // strip Ember script tags
  22. var date = date_string.substring(4);
  23. var month = date.substring(1, 4);
  24. var day = date.substring(5, 7);
  25. var year = date.substring(9, 13);
  26. var hours = date.substring(14, 16);
  27. var minutes = date.substring(17, 19);
  28. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  29. month = months.indexOf(month);
  30. if (month < 10) month = '0' + month;
  31. return year + month + day + hours + minutes;
  32. },
  33. "ambari-date-asc":function (a, b) {
  34. return a - b;
  35. },
  36. "ambari-date-desc":function (a, b) {
  37. return b - a;
  38. }
  39. });
  40. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  41. "fnFilterClear":function ( oSettings )
  42. {
  43. /* Remove global filter */
  44. oSettings.oPreviousSearch.sSearch = "";
  45. /* Remove the text of the global filter in the input boxes */
  46. if ( typeof oSettings.aanFeatures.f != 'undefined' )
  47. {
  48. var n = oSettings.aanFeatures.f;
  49. for ( var i=0, iLen=n.length ; i<iLen ; i++ )
  50. {
  51. $('input', n[i]).val( '' );
  52. }
  53. }
  54. /* Remove the search text for the column filters - NOTE - if you have input boxes for these
  55. * filters, these will need to be reset
  56. */
  57. for ( var i=0, iLen=oSettings.aoPreSearchCols.length ; i<iLen ; i++ )
  58. {
  59. oSettings.aoPreSearchCols[i].sSearch = "";
  60. }
  61. /* Redraw */
  62. oSettings.oApi._fnReDraw( oSettings );
  63. }
  64. });
  65. jQuery.extend($.fn.dataTableExt.afnFiltering.push(
  66. function( oSettings, aData, iDataIndex ) {
  67. var inputFilters = [
  68. {iColumn:'4', elementId: 'jobs_filter', type:'number'},
  69. {iColumn:'5', elementId: 'input_filter', type:'number' },
  70. {iColumn:'6', elementId: 'output_filter', type:'number' },
  71. {iColumn:'7', elementId: 'duration_filter', type:'number' },
  72. {iColumn:'8', elementId: 'rundate_filter', type:'date' }
  73. ];
  74. var match = true;
  75. for(i = 0; i < inputFilters.length; i++){
  76. if(inputFilters[i].type === 'date'){
  77. if(jQuery('#'+inputFilters[i].elementId).val() !== 'Any' && match) {
  78. dateFilter(jQuery('#'+inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  79. }
  80. }
  81. if(inputFilters[i].type === 'number'){
  82. if(jQuery('#'+inputFilters[i].elementId).val() && match){
  83. numberFilter(jQuery('#'+inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  84. }
  85. }
  86. }
  87. function dateFilter(condition, rowValue) {
  88. var nowTime = new Date().getTime();
  89. var oneDayPast = nowTime - 86400000;
  90. var twoDaysPast = nowTime - 172800000;
  91. var sevenDaysPast = nowTime - 604800000;
  92. var fourteenDaysPast = nowTime - 1209600000;
  93. var thirtyDaysPast = nowTime - 2592000000;
  94. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  95. rowValue = new Date(rowValue).getTime();
  96. match = false;
  97. switch (condition) {
  98. case 'Past 1 Day':
  99. if (nowTime > rowValue && rowValue > oneDayPast) match = true;
  100. break;
  101. case 'Past 2 Days':
  102. if (nowTime > rowValue && rowValue > twoDaysPast) match = true;
  103. break;
  104. case 'Past 7 Days':
  105. if (nowTime > rowValue && rowValue > sevenDaysPast) match = true;
  106. break;
  107. case 'Past 14 Days':
  108. if (nowTime > rowValue && rowValue > fourteenDaysPast) match = true;
  109. break;
  110. case 'Past 30 Days':
  111. if (nowTime > rowValue && rowValue > thirtyDaysPast) match = true;
  112. break;
  113. }
  114. }
  115. function numberFilter(rangeExp, rowValue){
  116. var compareChar = rangeExp.charAt(0);
  117. var compareValue = parseInt(rangeExp.substr(1, rangeExp.length - 1));
  118. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  119. match = false;
  120. switch (compareChar) {
  121. case '<':
  122. if(compareValue > rowValue) match = true;
  123. break;
  124. case '>':
  125. if(compareValue < rowValue) match = true;
  126. break;
  127. case '=':
  128. if(compareValue == rowValue) match = true;
  129. break;
  130. default:
  131. match = false;
  132. }
  133. }
  134. return match;
  135. }
  136. )
  137. );