data_table.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. /* Remove global filter */
  43. oSettings.oPreviousSearch.sSearch = "";
  44. /* Remove the text of the global filter in the input boxes */
  45. if (typeof oSettings.aanFeatures.f != 'undefined') {
  46. var n = oSettings.aanFeatures.f;
  47. for (var i = 0, iLen = n.length; i < iLen; i++) {
  48. $('input', n[i]).val('');
  49. }
  50. }
  51. /* Remove the search text for the column filters - NOTE - if you have input boxes for these
  52. * filters, these will need to be reset
  53. */
  54. for (var i = 0, iLen = oSettings.aoPreSearchCols.length; i < iLen; i++) {
  55. oSettings.aoPreSearchCols[i].sSearch = "";
  56. }
  57. /* Redraw */
  58. oSettings.oApi._fnReDraw(oSettings);
  59. }
  60. });
  61. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  62. "fnGetColumnData": function (oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty) {
  63. // check that we have a column id
  64. if (typeof iColumn == "undefined") return [];
  65. // by default we only wany unique data
  66. if (typeof bUnique == "undefined") bUnique = true;
  67. // by default we do want to only look at filtered data
  68. if (typeof bFiltered == "undefined") bFiltered = true;
  69. // by default we do not wany to include empty values
  70. if (typeof bIgnoreEmpty == "undefined") bIgnoreEmpty = true;
  71. // list of rows which we're going to loop through
  72. var aiRows;
  73. // use only filtered rows
  74. if (bFiltered == true) aiRows = oSettings.aiDisplay;
  75. // use all rows
  76. else aiRows = oSettings.aiDisplayMaster; // all row numbers
  77. // set up data array
  78. var asResultData = new Array();
  79. for (var i = 0, c = aiRows.length; i < c; i++) {
  80. iRow = aiRows[i];
  81. var sValue = this.fnGetData(iRow, iColumn);
  82. // ignore empty values?
  83. if (bIgnoreEmpty == true && sValue.length == 0) continue;
  84. // ignore unique values?
  85. else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
  86. // else push the value onto the result data array
  87. else asResultData.push(sValue);
  88. }
  89. return asResultData;
  90. }
  91. });
  92. jQuery.extend($.fn.dataTableExt.afnFiltering.push(
  93. function (oSettings, aData, iDataIndex) {
  94. var inputFilters = [
  95. {iColumn: '4', elementId: 'user_filter', type: 'multiple'},
  96. {iColumn: '5', elementId: 'jobs_filter', type: 'number' },
  97. {iColumn: '6', elementId: 'input_filter', type: 'number' },
  98. {iColumn: '7', elementId: 'output_filter', type: 'number' },
  99. {iColumn: '8', elementId: 'duration_filter', type: 'time' },
  100. {iColumn: '9', elementId: 'rundate_filter', type: 'date' }
  101. ];
  102. var match = true;
  103. for (i = 0; i < inputFilters.length; i++) {
  104. switch (inputFilters[i].type) {
  105. case 'date':
  106. if (jQuery('#' + inputFilters[i].elementId).val() !== 'Any' && match) {
  107. dateFilter(jQuery('#' + inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  108. }
  109. break;
  110. case 'number':
  111. if (jQuery('#' + inputFilters[i].elementId).val() && match) {
  112. numberFilter(jQuery('#' + inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  113. }
  114. break;
  115. case 'multiple':
  116. if (jQuery('#' + inputFilters[i].elementId).val() && match) {
  117. multipleFilter(jQuery('#' + inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  118. }
  119. break;
  120. case 'time':
  121. if (jQuery('#' + inputFilters[i].elementId).val() && match) {
  122. timeFilter(jQuery('#' + inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  123. }
  124. break;
  125. }
  126. }
  127. function multipleFilter(condition, rowValue) {
  128. var options = condition.split(',');
  129. match = false;
  130. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  131. for (var i = 0; i < options.length; i++) {
  132. if (options[i] === rowValue) match = true;
  133. }
  134. }
  135. function timeFilter(rangeExp, rowValue) {
  136. var compareChar = rangeExp.charAt(0);
  137. var compareScale = rangeExp.charAt(rangeExp.length - 1);
  138. var compareValue = isNaN(parseInt(compareScale)) ? parseInt(rangeExp.substr(1, rangeExp.length - 2)) : parseInt(rangeExp.substr(1, rangeExp.length - 1));
  139. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  140. var convertedRowValue = parseInt(rowValue.substr(0, 2)) * 3600 + parseInt(rowValue.substr(3, 5)) * 60 + parseInt(rowValue.substr(6, 8));
  141. switch (compareScale) {
  142. case 'm':
  143. convertedRowValue /= 60;
  144. break;
  145. case 'h':
  146. convertedRowValue /= 3600;
  147. break;
  148. }
  149. match = false;
  150. switch (compareChar) {
  151. case '<':
  152. if (compareValue > convertedRowValue) match = true;
  153. break;
  154. case '>':
  155. if (compareValue < convertedRowValue) match = true;
  156. break;
  157. case '=':
  158. if (compareValue == convertedRowValue) match = true;
  159. break;
  160. default:
  161. match = false;
  162. }
  163. }
  164. function dateFilter(condition, rowValue) {
  165. var nowTime = new Date().getTime();
  166. var oneDayPast = nowTime - 86400000;
  167. var twoDaysPast = nowTime - 172800000;
  168. var sevenDaysPast = nowTime - 604800000;
  169. var fourteenDaysPast = nowTime - 1209600000;
  170. var thirtyDaysPast = nowTime - 2592000000;
  171. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  172. var lastChar = rowValue.charAt(rowValue.length - 1);
  173. rowValue = lastChar === '*' ? rowValue.substr(0, rowValue.length - 1) : rowValue;
  174. rowValue = new Date(rowValue).getTime();
  175. match = false;
  176. switch (condition) {
  177. case 'Any':
  178. match = true;
  179. break;
  180. case 'Past 1 Day':
  181. if (nowTime > rowValue && rowValue > oneDayPast) match = true;
  182. break;
  183. case 'Past 2 Days':
  184. if (nowTime > rowValue && rowValue > twoDaysPast) match = true;
  185. break;
  186. case 'Past 7 Days':
  187. if (nowTime > rowValue && rowValue > sevenDaysPast) match = true;
  188. break;
  189. case 'Past 14 Days':
  190. if (nowTime > rowValue && rowValue > fourteenDaysPast) match = true;
  191. break;
  192. case 'Past 30 Days':
  193. if (nowTime > rowValue && rowValue > thirtyDaysPast) match = true;
  194. break;
  195. case 'Running Now':
  196. if (lastChar === '*') match = true;
  197. break;
  198. }
  199. }
  200. function numberFilter(rangeExp, rowValue) {
  201. var compareChar = rangeExp.charAt(0);
  202. var compareValue = parseInt(rangeExp.substr(1, rangeExp.length - 1));
  203. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  204. match = false;
  205. switch (compareChar) {
  206. case '<':
  207. if (compareValue > rowValue) match = true;
  208. break;
  209. case '>':
  210. if (compareValue < rowValue) match = true;
  211. break;
  212. case '=':
  213. if (compareValue == rowValue) match = true;
  214. break;
  215. default:
  216. match = false;
  217. }
  218. }
  219. return match;
  220. }
  221. )
  222. );
  223. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  224. "fnFilterClear": function (oSettings) {
  225. /* Remove global filter */
  226. oSettings.oPreviousSearch.sSearch = "";
  227. /* Remove the text of the global filter in the input boxes */
  228. if (typeof oSettings.aanFeatures.f != 'undefined') {
  229. var n = oSettings.aanFeatures.f;
  230. for (var i = 0, iLen = n.length; i < iLen; i++) {
  231. $('input', n[i]).val('');
  232. }
  233. }
  234. /* Remove the search text for the column filters - NOTE - if you have input boxes for these
  235. * filters, these will need to be reset
  236. */
  237. for (var i = 0, iLen = oSettings.aoPreSearchCols.length; i < iLen; i++) {
  238. oSettings.aoPreSearchCols[i].sSearch = "";
  239. }
  240. /* Redraw */
  241. oSettings.oApi._fnReDraw(oSettings);
  242. }
  243. });