data_table.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. "num-html-pre": function(date_string) {
  20. date_string = $(date_string).text();
  21. return parseInt(date_string, 10);
  22. },
  23. "num-html-asc": function (a, b) {
  24. return a - b;
  25. },
  26. "num-html-desc": function (a, b) {
  27. return b - a;
  28. },
  29. // @see utils/date.js
  30. "ambari-date-pre": function (date_string) {
  31. date_string = $(date_string).text(); // strip Ember script tags
  32. var date = date_string.substring(4);
  33. var month = date.substring(1, 4);
  34. var day = date.substring(5, 7);
  35. var year = date.substring(9, 13);
  36. var hours = date.substring(14, 16);
  37. var minutes = date.substring(17, 19);
  38. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  39. month = months.indexOf(month);
  40. if (month < 10) month = '0' + month;
  41. return year + month + day + hours + minutes;
  42. },
  43. "ambari-date-asc": function (a, b) {
  44. return a - b;
  45. },
  46. "ambari-date-desc": function (a, b) {
  47. return b - a;
  48. },
  49. /**
  50. * Custom methods for correct bandwidth sorting
  51. */
  52. "ambari-bandwidth-pre": function (bandwidth_string) {
  53. bandwidth_string = (jQuery(bandwidth_string).text()) ? jQuery(bandwidth_string).text() : bandwidth_string;
  54. var convertedRowValue;
  55. if (bandwidth_string === '<1KB') {
  56. convertedRowValue = 1;
  57. } else {
  58. var rowValueScale = bandwidth_string.substr(bandwidth_string.length - 2, 2);
  59. switch (rowValueScale) {
  60. case 'KB':
  61. convertedRowValue = parseFloat(bandwidth_string)*1024;
  62. break;
  63. case 'MB':
  64. convertedRowValue = parseFloat(bandwidth_string)*1048576;
  65. break;
  66. }
  67. }
  68. return convertedRowValue;
  69. },
  70. "ambari-bandwidth-asc": function (a, b) {
  71. return a - b;
  72. },
  73. "ambari-bandwidth-desc": function (a, b) {
  74. return b - a;
  75. }
  76. });
  77. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  78. "fnFilterClear": function (oSettings) {
  79. /* Remove global filter */
  80. oSettings.oPreviousSearch.sSearch = "";
  81. /* Remove the text of the global filter in the input boxes */
  82. if (typeof oSettings.aanFeatures.f != 'undefined') {
  83. var n = oSettings.aanFeatures.f;
  84. for (var i = 0, iLen = n.length; i < iLen; i++) {
  85. $('input', n[i]).val('');
  86. }
  87. }
  88. /* Remove the search text for the column filters - NOTE - if you have input boxes for these
  89. * filters, these will need to be reset
  90. */
  91. for (var i = 0, iLen = oSettings.aoPreSearchCols.length; i < iLen; i++) {
  92. oSettings.aoPreSearchCols[i].sSearch = "";
  93. }
  94. /* Redraw */
  95. oSettings.oApi._fnReDraw(oSettings);
  96. }
  97. });
  98. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  99. "fnGetColumnData": function (oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty) {
  100. // check that we have a column id
  101. if (typeof iColumn == "undefined") return [];
  102. // by default we only wany unique data
  103. if (typeof bUnique == "undefined") bUnique = true;
  104. // by default we do want to only look at filtered data
  105. if (typeof bFiltered == "undefined") bFiltered = true;
  106. // by default we do not wany to include empty values
  107. if (typeof bIgnoreEmpty == "undefined") bIgnoreEmpty = true;
  108. // list of rows which we're going to loop through
  109. var aiRows;
  110. // use only filtered rows
  111. if (bFiltered == true) aiRows = oSettings.aiDisplay;
  112. // use all rows
  113. else aiRows = oSettings.aiDisplayMaster; // all row numbers
  114. // set up data array
  115. var asResultData = new Array();
  116. for (var i = 0, c = aiRows.length; i < c; i++) {
  117. iRow = aiRows[i];
  118. var sValue = this.fnGetData(iRow, iColumn);
  119. // ignore empty values?
  120. if (bIgnoreEmpty == true && sValue.length == 0) continue;
  121. // ignore unique values?
  122. else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
  123. // else push the value onto the result data array
  124. else asResultData.push(sValue);
  125. }
  126. return asResultData;
  127. }
  128. });
  129. jQuery.extend($.fn.dataTableExt.afnFiltering.push(
  130. function (oSettings, aData, iDataIndex) {
  131. var inputFilters = [
  132. {iColumn: '0', elementId: 'star_filter', type: 'star'},
  133. {iColumn: '2', elementId: 'cpu_filter', type: 'number'},
  134. {iColumn: '4', elementId: 'user_filter', type: 'multiple'},
  135. {iColumn: '5', elementId: 'jobs_filter', type: 'number' },
  136. {iColumn: '3', elementId: 'ram_filter', type: 'ambari-bandwidth' },
  137. {iColumn: '6', elementId: 'input_filter', type: 'ambari-bandwidth' },
  138. {iColumn: '7', elementId: 'output_filter', type: 'ambari-bandwidth' },
  139. {iColumn: '8', elementId: 'duration_filter', type: 'time' },
  140. //{iColumn: '9', elementId: 'rundate_filter', type: 'ambari-date' }
  141. ];
  142. var match = true;
  143. for (var i = 0; i < inputFilters.length; i++) {
  144. switch (inputFilters[i].type) {
  145. case 'ambari-date':
  146. if (jQuery('#' + inputFilters[i].elementId).val() !== 'Any' && match) {
  147. dateFilter(jQuery('#' + inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  148. }
  149. break;
  150. case 'number':
  151. if (jQuery('#' + inputFilters[i].elementId).val() && match) {
  152. numberFilter(jQuery('#' + inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  153. }
  154. break;
  155. case 'multiple':
  156. if (jQuery('#' + inputFilters[i].elementId).val() && match) {
  157. multipleFilter(jQuery('#' + inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  158. }
  159. break;
  160. case 'time':
  161. if (jQuery('#' + inputFilters[i].elementId).val() && match) {
  162. timeFilter(jQuery('#' + inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  163. }
  164. break;
  165. case 'ambari-bandwidth':
  166. if (jQuery('#' + inputFilters[i].elementId).val() && match) {
  167. bandwidthFilter(jQuery('#' + inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  168. }
  169. break;
  170. case 'star':
  171. if (jQuery('#' + inputFilters[i].elementId).val() && match) {
  172. starFilter(jQuery('#' + inputFilters[i].elementId).val(), aData[inputFilters[i].iColumn]);
  173. }
  174. break;
  175. }
  176. }
  177. function starFilter(d, rowValue) {
  178. match = false;
  179. if (rowValue == null) return;
  180. if (rowValue.indexOf(d) != -1) match = true;
  181. }
  182. function multipleFilter(condition, rowValue) {
  183. var options = condition.split(',');
  184. match = false;
  185. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  186. for (var i = 0; i < options.length; i++) {
  187. if (options[i] === rowValue) match = true;
  188. }
  189. }
  190. function timeFilter(rangeExp, rowValue) {
  191. var compareChar = rangeExp.charAt(0);
  192. var compareScale = rangeExp.charAt(rangeExp.length - 1);
  193. var compareValue = isNaN(parseInt(compareScale)) ? parseInt(rangeExp.substr(1, rangeExp.length - 2)) : parseInt(rangeExp.substr(1, rangeExp.length - 1));
  194. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  195. var convertedRowValue = parseInt(rowValue.substr(0, 2)) * 3600 + parseInt(rowValue.substr(3, 2)) * 60 + parseInt(rowValue.substr(6, 2));
  196. switch (compareScale) {
  197. case 'm':
  198. convertedRowValue /= 60;
  199. break;
  200. case 'h':
  201. convertedRowValue /= 3600;
  202. break;
  203. }
  204. match = false;
  205. switch (compareChar) {
  206. case '<':
  207. if (compareValue > convertedRowValue) match = true;
  208. break;
  209. case '>':
  210. if (compareValue < convertedRowValue) match = true;
  211. break;
  212. case '=':
  213. if (compareValue == convertedRowValue) match = true;
  214. break;
  215. default:
  216. match = false;
  217. }
  218. }
  219. function bandwidthFilter(rangeExp, rowValue) {
  220. rowValue = $(rowValue).text();
  221. var compareChar = rangeExp.charAt(0);
  222. var compareScale = rangeExp.charAt(rangeExp.length - 1);
  223. var compareValue = isNaN(parseFloat(compareScale)) ? parseFloat(rangeExp.substr(1, rangeExp.length - 2)) : parseFloat(rangeExp.substr(1, rangeExp.length - 1));
  224. switch (compareScale) {
  225. case 'm':
  226. compareValue *= 1048576;
  227. break;
  228. default:
  229. compareValue *= 1024;
  230. }
  231. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  232. var convertedRowValue;
  233. if (rowValue === '<1KB') {
  234. convertedRowValue = 1;
  235. } else {
  236. var rowValueScale = rowValue.substr(rowValue.length - 2, 2);
  237. switch (rowValueScale) {
  238. case 'KB':
  239. convertedRowValue = parseFloat(rowValue)*1024;
  240. break;
  241. case 'MB':
  242. convertedRowValue = parseFloat(rowValue)*1048576;
  243. break;
  244. }
  245. }
  246. match = false;
  247. switch (compareChar) {
  248. case '<':
  249. if (compareValue > convertedRowValue) match = true;
  250. break;
  251. case '>':
  252. if (compareValue < convertedRowValue) match = true;
  253. break;
  254. case '=':
  255. if (compareValue == convertedRowValue) match = true;
  256. break;
  257. default:
  258. match = false;
  259. }
  260. }
  261. function dateFilter(condition, rowValue) {
  262. refinedRowValue = $.trim(rowValue.replace(/<script[^>]*?>.*<\/script>/g, ''));
  263. var nowTime = new Date().getTime();
  264. var oneDayPast = nowTime - 86400000;
  265. var twoDaysPast = nowTime - 172800000;
  266. var sevenDaysPast = nowTime - 604800000;
  267. var fourteenDaysPast = nowTime - 1209600000;
  268. var thirtyDaysPast = nowTime - 2592000000;
  269. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  270. var lastChar = rowValue.charAt(rowValue.length - 1);
  271. rowValue = lastChar === '*' ? rowValue.substr(0, rowValue.length - 1) : rowValue;
  272. rowValue = new Date(refinedRowValue).getTime();
  273. match = false;
  274. switch (condition) {
  275. case 'Any':
  276. match = true;
  277. break;
  278. case 'Past 1 Day':
  279. if (nowTime > rowValue && rowValue > oneDayPast) match = true;
  280. break;
  281. case 'Past 2 Days':
  282. if (nowTime > rowValue && rowValue > twoDaysPast) match = true;
  283. break;
  284. case 'Past 7 Days':
  285. if (nowTime > rowValue && rowValue > sevenDaysPast) match = true;
  286. break;
  287. case 'Past 14 Days':
  288. if (nowTime > rowValue && rowValue > fourteenDaysPast) match = true;
  289. break;
  290. case 'Past 30 Days':
  291. if (nowTime > rowValue && rowValue > thirtyDaysPast) match = true;
  292. break;
  293. case 'Running Now':
  294. if (lastChar === '*') match = true;
  295. break;
  296. }
  297. }
  298. function numberFilter(rangeExp, rowValue) {
  299. var compareChar = rangeExp.charAt(0);
  300. var compareValue = parseInt(rangeExp.substr(1, rangeExp.length - 1));
  301. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  302. match = false;
  303. switch (compareChar) {
  304. case '<':
  305. if (compareValue > rowValue) match = true;
  306. break;
  307. case '>':
  308. if (compareValue < rowValue) match = true;
  309. break;
  310. case '=':
  311. if (compareValue == rowValue) match = true;
  312. default:
  313. if (rangeExp == rowValue) match = true;
  314. }
  315. }
  316. return match;
  317. }
  318. )
  319. );
  320. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  321. "fnFilterClear": function (oSettings) {
  322. /* Remove global filter */
  323. oSettings.oPreviousSearch.sSearch = "";
  324. /* Remove the text of the global filter in the input boxes */
  325. if (typeof oSettings.aanFeatures.f != 'undefined') {
  326. var n = oSettings.aanFeatures.f;
  327. for (var i = 0, iLen = n.length; i < iLen; i++) {
  328. $('input', n[i]).val('');
  329. }
  330. }
  331. /* Remove the search text for the column filters - NOTE - if you have input boxes for these
  332. * filters, these will need to be reset
  333. */
  334. for (var i = 0, iLen = oSettings.aoPreSearchCols.length; i < iLen; i++) {
  335. oSettings.aoPreSearchCols[i].sSearch = "";
  336. }
  337. /* Redraw */
  338. oSettings.oApi._fnReDraw(oSettings);
  339. }
  340. });
  341. jQuery.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
  342. // check that we have a column id
  343. if ( typeof iColumn == "undefined" ) return [];
  344. // by default we only wany unique data
  345. if ( typeof bUnique == "undefined" ) bUnique = true;
  346. // by default we do want to only look at filtered data
  347. if ( typeof bFiltered == "undefined" ) bFiltered = true;
  348. // by default we do not wany to include empty values
  349. if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;
  350. // list of rows which we're going to loop through
  351. var aiRows;
  352. // use only filtered rows
  353. if (bFiltered == true) aiRows = oSettings.aiDisplay;
  354. // use all rows
  355. else aiRows = oSettings.aiDisplayMaster; // all row numbers
  356. // set up data array
  357. var asResultData = new Array();
  358. for (var i=0,c=aiRows.length; i<c; i++) {
  359. iRow = aiRows[i];
  360. var sValue = this.fnGetData(iRow, iColumn);
  361. // ignore empty values?
  362. if (bIgnoreEmpty == true && sValue.length == 0) continue;
  363. // ignore unique values?
  364. else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
  365. // else push the value onto the result data array
  366. else asResultData.push(sValue);
  367. }
  368. return asResultData;
  369. };