data_table.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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-datetime-pre": function (date_string) {
  31. date_string = $.trim(date_string.replace(/<script[^>]*?>.*<\/script>/g, ''));
  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-datetime-asc": function (a, b) {
  44. return a - b;
  45. },
  46. "ambari-datetime-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: '6', elementId: 'components_filter', type: 'multiple'},
  136. {iColumn: '5', elementId: 'jobs_filter', type: 'number' },
  137. {iColumn: '3', elementId: 'ram_filter', type: 'ambari-bandwidth' },
  138. {iColumn: '6', elementId: 'input_filter', type: 'ambari-bandwidth' },
  139. {iColumn: '7', elementId: 'output_filter', type: 'ambari-bandwidth' },
  140. {iColumn: '8', elementId: 'duration_filter', type: 'time' },
  141. {iColumn: '9', elementId: 'rundate_filter', type: 'ambari-datetime' }
  142. ];
  143. var match = true;
  144. for (var i = 0; i < inputFilters.length; i++) {
  145. var cellValue = jQuery('#' + inputFilters[i].elementId).val();
  146. if(cellValue === undefined){
  147. continue;
  148. }
  149. switch (inputFilters[i].type) {
  150. case 'ambari-datetime':
  151. if (cellValue !== 'Any' && match) {
  152. ambariDateFilter(cellValue, aData[inputFilters[i].iColumn]);
  153. }
  154. break;
  155. case 'number':
  156. if (cellValue && match) {
  157. numberFilter(cellValue, aData[inputFilters[i].iColumn]);
  158. }
  159. break;
  160. case 'multiple':
  161. if (cellValue && match) {
  162. multipleFilter(cellValue, aData[inputFilters[i].iColumn]);
  163. }
  164. break;
  165. case 'time':
  166. if (cellValue && match) {
  167. timeFilter(cellValue, aData[inputFilters[i].iColumn]);
  168. }
  169. break;
  170. case 'ambari-bandwidth':
  171. if (cellValue && match) {
  172. bandwidthFilter(cellValue, aData[inputFilters[i].iColumn]);
  173. }
  174. break;
  175. case 'star':
  176. if (cellValue && match) {
  177. starFilter(cellValue, aData[inputFilters[i].iColumn]);
  178. }
  179. break;
  180. }
  181. }
  182. function starFilter(d, rowValue) {
  183. match = false;
  184. if (rowValue == null) return;
  185. if (rowValue.indexOf(d) != -1) match = true;
  186. }
  187. function multipleFilter(condition, rowValue) {
  188. var options = condition.split(',');
  189. match = false;
  190. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  191. for (var i = 0; i < options.length; i++) {
  192. if (rowValue.indexOf(options[i]) !== -1) match = true;
  193. }
  194. }
  195. function timeFilter(rangeExp, rowValue) {
  196. var compareChar = rangeExp.charAt(0);
  197. var compareScale = rangeExp.charAt(rangeExp.length - 1);
  198. var compareValue = isNaN(parseInt(compareScale)) ? parseInt(rangeExp.substr(1, rangeExp.length - 2)) : parseInt(rangeExp.substr(1, rangeExp.length - 1));
  199. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  200. var convertedRowValue = parseInt(rowValue.substr(0, 2)) * 3600 + parseInt(rowValue.substr(3, 2)) * 60 + parseInt(rowValue.substr(6, 2));
  201. switch (compareScale) {
  202. case 'm':
  203. convertedRowValue /= 60;
  204. break;
  205. case 'h':
  206. convertedRowValue /= 3600;
  207. break;
  208. }
  209. match = false;
  210. switch (compareChar) {
  211. case '<':
  212. if (compareValue > convertedRowValue) match = true;
  213. break;
  214. case '>':
  215. if (compareValue < convertedRowValue) match = true;
  216. break;
  217. case '=':
  218. if (compareValue == convertedRowValue) match = true;
  219. break;
  220. default:
  221. match = false;
  222. }
  223. }
  224. function bandwidthFilter(rangeExp, rowValue) {
  225. rowValue = $(rowValue).text();
  226. var compareChar = rangeExp.charAt(0);
  227. var compareScale = rangeExp.charAt(rangeExp.length - 1);
  228. var compareValue = isNaN(parseFloat(compareScale)) ? parseFloat(rangeExp.substr(1, rangeExp.length - 2)) : parseFloat(rangeExp.substr(1, rangeExp.length - 1));
  229. switch (compareScale) {
  230. case 'm':
  231. compareValue *= 1048576;
  232. break;
  233. default:
  234. compareValue *= 1024;
  235. }
  236. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  237. var convertedRowValue;
  238. if (rowValue === '<1KB') {
  239. convertedRowValue = 1;
  240. } else {
  241. var rowValueScale = rowValue.substr(rowValue.length - 2, 2);
  242. switch (rowValueScale) {
  243. case 'KB':
  244. convertedRowValue = parseFloat(rowValue)*1024;
  245. break;
  246. case 'MB':
  247. convertedRowValue = parseFloat(rowValue)*1048576;
  248. break;
  249. }
  250. }
  251. match = false;
  252. switch (compareChar) {
  253. case '<':
  254. if (compareValue > convertedRowValue) match = true;
  255. break;
  256. case '>':
  257. if (compareValue < convertedRowValue) match = true;
  258. break;
  259. case '=':
  260. if (compareValue == convertedRowValue) match = true;
  261. break;
  262. default:
  263. match = false;
  264. }
  265. }
  266. function ambariDateFilter(condition, rowValue) {
  267. if (typeof rowValue !== 'undefined') {
  268. var refinedRowValue = $.trim(rowValue.replace(/<script[^>]*?>.*<\/script>/g, '').replace('&nbsp;', ''));
  269. var nowTime = new Date().getTime();
  270. var oneDayPast = nowTime - 86400000;
  271. var twoDaysPast = nowTime - 172800000;
  272. var sevenDaysPast = nowTime - 604800000;
  273. var fourteenDaysPast = nowTime - 1209600000;
  274. var thirtyDaysPast = nowTime - 2592000000;
  275. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  276. var lastChar = rowValue.charAt(rowValue.length - 1);
  277. rowValue = lastChar === '*' ? rowValue.substr(0, rowValue.length - 1) : rowValue;
  278. rowValue = new Date(refinedRowValue).getTime();
  279. match = false;
  280. switch (condition) {
  281. case 'Any':
  282. match = true;
  283. break;
  284. case 'Past 1 Day':
  285. if (nowTime > rowValue && rowValue > oneDayPast) match = true;
  286. break;
  287. case 'Past 2 Days':
  288. if (nowTime > rowValue && rowValue > twoDaysPast) match = true;
  289. break;
  290. case 'Past 7 Days':
  291. if (nowTime > rowValue && rowValue > sevenDaysPast) match = true;
  292. break;
  293. case 'Past 14 Days':
  294. if (nowTime > rowValue && rowValue > fourteenDaysPast) match = true;
  295. break;
  296. case 'Past 30 Days':
  297. if (nowTime > rowValue && rowValue > thirtyDaysPast) match = true;
  298. break;
  299. case 'Running Now':
  300. if (lastChar === '*') match = true;
  301. break;
  302. case 'Custom':
  303. var options = jQuery('#custom_rundate_filter').val();
  304. var optionsArray = options.split(',');
  305. if (optionsArray.length == 1) {
  306. equal = optionsArray[0];
  307. if (equal == rowValue) match = true;
  308. } else if (optionsArray.length == 2) {
  309. lowerBound = optionsArray[0];
  310. upperBound = optionsArray[1];
  311. if (upperBound > rowValue && rowValue > lowerBound) match = true;
  312. }
  313. break;
  314. }
  315. }
  316. }
  317. function numberFilter(rangeExp, rowValue) {
  318. var compareChar = rangeExp.charAt(0);
  319. var compareValue = parseInt(rangeExp.substr(1, rangeExp.length - 1));
  320. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  321. match = false;
  322. switch (compareChar) {
  323. case '<':
  324. if (compareValue > rowValue) match = true;
  325. break;
  326. case '>':
  327. if (compareValue < rowValue) match = true;
  328. break;
  329. case '=':
  330. if (compareValue == rowValue) match = true;
  331. default:
  332. if (rangeExp == rowValue) match = true;
  333. }
  334. }
  335. return match;
  336. }
  337. )
  338. );
  339. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  340. "fnFilterClear": function (oSettings) {
  341. /* Remove global filter */
  342. oSettings.oPreviousSearch.sSearch = "";
  343. /* Remove the text of the global filter in the input boxes */
  344. if (typeof oSettings.aanFeatures.f != 'undefined') {
  345. var n = oSettings.aanFeatures.f;
  346. for (var i = 0, iLen = n.length; i < iLen; i++) {
  347. $('input', n[i]).val('');
  348. }
  349. }
  350. /* Remove the search text for the column filters - NOTE - if you have input boxes for these
  351. * filters, these will need to be reset
  352. */
  353. for (var i = 0, iLen = oSettings.aoPreSearchCols.length; i < iLen; i++) {
  354. oSettings.aoPreSearchCols[i].sSearch = "";
  355. }
  356. /* Redraw */
  357. oSettings.oApi._fnReDraw(oSettings);
  358. }
  359. });
  360. jQuery.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
  361. // check that we have a column id
  362. if ( typeof iColumn == "undefined" ) return [];
  363. // by default we only wany unique data
  364. if ( typeof bUnique == "undefined" ) bUnique = true;
  365. // by default we do want to only look at filtered data
  366. if ( typeof bFiltered == "undefined" ) bFiltered = true;
  367. // by default we do not wany to include empty values
  368. if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;
  369. // list of rows which we're going to loop through
  370. var aiRows;
  371. // use only filtered rows
  372. if (bFiltered == true) aiRows = oSettings.aiDisplay;
  373. // use all rows
  374. else aiRows = oSettings.aiDisplayMaster; // all row numbers
  375. // set up data array
  376. var asResultData = new Array();
  377. for (var i=0,c=aiRows.length; i<c; i++) {
  378. iRow = aiRows[i];
  379. var sValue = this.fnGetData(iRow, iColumn);
  380. // ignore empty values?
  381. if (bIgnoreEmpty == true && sValue.length == 0) continue;
  382. // ignore unique values?
  383. else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
  384. // else push the value onto the result data array
  385. else asResultData.push(sValue);
  386. }
  387. return asResultData;
  388. };