data_table.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 parseFloat(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. var convertedRowValue;
  54. if (Boolean(bandwidth_string.match(/<.*>/g))) {
  55. bandwidth_string = jQuery(bandwidth_string).text();
  56. }
  57. if (bandwidth_string === '<1KB' || bandwidth_string === '&lt;1KB') {
  58. convertedRowValue = 1;
  59. } else {
  60. var rowValueScale = bandwidth_string.substr(bandwidth_string.length - 2, 2);
  61. switch (rowValueScale) {
  62. case 'KB':
  63. convertedRowValue = parseFloat(bandwidth_string)*1024;
  64. break;
  65. case 'MB':
  66. convertedRowValue = parseFloat(bandwidth_string)*1048576;
  67. break;
  68. }
  69. }
  70. return convertedRowValue;
  71. },
  72. "ambari-bandwidth-asc": function (a, b) {
  73. return a - b;
  74. },
  75. "ambari-bandwidth-desc": function (a, b) {
  76. return b - a;
  77. }
  78. });
  79. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  80. "fnFilterClear": function (oSettings) {
  81. /* Remove global filter */
  82. oSettings.oPreviousSearch.sSearch = "";
  83. /* Remove the text of the global filter in the input boxes */
  84. if (typeof oSettings.aanFeatures.f != 'undefined') {
  85. var n = oSettings.aanFeatures.f;
  86. for (var i = 0, iLen = n.length; i < iLen; i++) {
  87. $('input', n[i]).val('');
  88. }
  89. }
  90. /* Remove the search text for the column filters - NOTE - if you have input boxes for these
  91. * filters, these will need to be reset
  92. */
  93. for (var i = 0, iLen = oSettings.aoPreSearchCols.length; i < iLen; i++) {
  94. oSettings.aoPreSearchCols[i].sSearch = "";
  95. }
  96. /* Redraw */
  97. oSettings.oApi._fnReDraw(oSettings);
  98. }
  99. });
  100. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  101. "fnGetColumnData": function (oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty) {
  102. // check that we have a column id
  103. if (typeof iColumn == "undefined") return [];
  104. // by default we only wany unique data
  105. if (typeof bUnique == "undefined") bUnique = true;
  106. // by default we do want to only look at filtered data
  107. if (typeof bFiltered == "undefined") bFiltered = true;
  108. // by default we do not wany to include empty values
  109. if (typeof bIgnoreEmpty == "undefined") bIgnoreEmpty = true;
  110. // list of rows which we're going to loop through
  111. var aiRows;
  112. // use only filtered rows
  113. if (bFiltered == true) aiRows = oSettings.aiDisplay;
  114. // use all rows
  115. else aiRows = oSettings.aiDisplayMaster; // all row numbers
  116. // set up data array
  117. var asResultData = new Array();
  118. for (var i = 0, c = aiRows.length; i < c; i++) {
  119. iRow = aiRows[i];
  120. var sValue = this.fnGetData(iRow, iColumn);
  121. // ignore empty values?
  122. if (bIgnoreEmpty == true && sValue.length == 0) continue;
  123. // ignore unique values?
  124. else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
  125. // else push the value onto the result data array
  126. else asResultData.push(sValue);
  127. }
  128. return asResultData;
  129. }
  130. });
  131. jQuery.extend($.fn.dataTableExt.afnFiltering.push(
  132. function (oSettings, aData, iDataIndex) {
  133. var inputFilters = [
  134. {iColumn: '0', elementId: 'star_filter', type: 'star'},
  135. {iColumn: '2', elementId: 'cpu_filter', type: 'number'},
  136. {iColumn: '5', elementId: 'load_avg_filter', type: 'number'},
  137. {iColumn: '4', elementId: 'user_filter', type: 'multiple'},
  138. {iColumn: '6', elementId: 'components_filter', type: 'multiple'},
  139. {iColumn: '5', elementId: 'jobs_filter', type: 'number' },
  140. {iColumn: '3', elementId: 'ram_filter', type: 'ambari-bandwidth' },
  141. {iColumn: '6', elementId: 'input_filter', type: 'ambari-bandwidth' },
  142. {iColumn: '7', elementId: 'output_filter', type: 'ambari-bandwidth' },
  143. {iColumn: '8', elementId: 'duration_filter', type: 'time' },
  144. {iColumn: '9', elementId: 'rundate_filter', type: 'ambari-datetime' }
  145. ];
  146. var match = true;
  147. for (var i = 0; i < inputFilters.length; i++) {
  148. var cellValue = jQuery('#' + inputFilters[i].elementId).val();
  149. if(cellValue === undefined){
  150. continue;
  151. }
  152. switch (inputFilters[i].type) {
  153. case 'ambari-datetime':
  154. if (cellValue !== 'Any' && match) {
  155. ambariDateFilter(cellValue, aData[inputFilters[i].iColumn]);
  156. }
  157. break;
  158. case 'number':
  159. if (cellValue && match) {
  160. numberFilter(cellValue, aData[inputFilters[i].iColumn]);
  161. }
  162. break;
  163. case 'multiple':
  164. if (cellValue && match) {
  165. multipleFilter(cellValue, aData[inputFilters[i].iColumn]);
  166. }
  167. break;
  168. case 'time':
  169. if (cellValue && match) {
  170. timeFilter(cellValue, aData[inputFilters[i].iColumn]);
  171. }
  172. break;
  173. case 'ambari-bandwidth':
  174. if (cellValue && match) {
  175. bandwidthFilter(cellValue, aData[inputFilters[i].iColumn]);
  176. }
  177. break;
  178. case 'star':
  179. if (cellValue && match) {
  180. starFilter(cellValue, aData[inputFilters[i].iColumn]);
  181. }
  182. break;
  183. }
  184. }
  185. function starFilter(d, rowValue) {
  186. match = false;
  187. if (rowValue == null) return;
  188. if (rowValue.indexOf(d) != -1) match = true;
  189. }
  190. function multipleFilter(condition, rowValue) {
  191. var options = condition.split(',');
  192. match = false;
  193. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  194. for (var i = 0; i < options.length; i++) {
  195. if (rowValue.indexOf(options[i]) !== -1) match = true;
  196. }
  197. }
  198. function timeFilter(rangeExp, rowValue) {
  199. var compareChar = rangeExp.charAt(0);
  200. var compareScale = rangeExp.charAt(rangeExp.length - 1);
  201. var compareValue = isNaN(parseInt(compareScale)) ? parseInt(rangeExp.substr(1, rangeExp.length - 2)) : parseInt(rangeExp.substr(1, rangeExp.length - 1));
  202. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  203. var convertedRowValue = parseInt(rowValue.substr(0, 2)) * 3600 + parseInt(rowValue.substr(3, 2)) * 60 + parseInt(rowValue.substr(6, 2));
  204. switch (compareScale) {
  205. case 'm':
  206. convertedRowValue /= 60;
  207. break;
  208. case 'h':
  209. convertedRowValue /= 3600;
  210. break;
  211. }
  212. match = false;
  213. switch (compareChar) {
  214. case '<':
  215. if (compareValue > convertedRowValue) match = true;
  216. break;
  217. case '>':
  218. if (compareValue < convertedRowValue) match = true;
  219. break;
  220. case '=':
  221. if (compareValue == convertedRowValue) match = true;
  222. break;
  223. default:
  224. match = false;
  225. }
  226. }
  227. function bandwidthFilter(rangeExp, rowValue) {
  228. //rowValue = $(rowValue).text();
  229. var compareChar = rangeExp.charAt(0);
  230. var compareScale = rangeExp.charAt(rangeExp.length - 1);
  231. var compareValue = isNaN(parseFloat(compareScale)) ? parseFloat(rangeExp.substr(1, rangeExp.length - 2)) : parseFloat(rangeExp.substr(1, rangeExp.length - 1));
  232. switch (compareScale) {
  233. case 'm':
  234. compareValue *= 1048576;
  235. break;
  236. default:
  237. compareValue *= 1024;
  238. }
  239. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  240. var convertedRowValue;
  241. if (rowValue === '<1KB') {
  242. convertedRowValue = 1;
  243. } else {
  244. var rowValueScale = rowValue.substr(rowValue.length - 2, 2);
  245. switch (rowValueScale) {
  246. case 'KB':
  247. convertedRowValue = parseFloat(rowValue)*1024;
  248. break;
  249. case 'MB':
  250. convertedRowValue = parseFloat(rowValue)*1048576;
  251. break;
  252. }
  253. }
  254. match = false;
  255. switch (compareChar) {
  256. case '<':
  257. if (compareValue > convertedRowValue) match = true;
  258. break;
  259. case '>':
  260. if (compareValue < convertedRowValue) match = true;
  261. break;
  262. case '=':
  263. if (compareValue == convertedRowValue) match = true;
  264. break;
  265. default:
  266. match = false;
  267. }
  268. }
  269. function ambariDateFilter(condition, rowValue) {
  270. if (typeof rowValue !== 'undefined') {
  271. var refinedRowValue = $.trim(rowValue.replace(/<script[^>]*?>.*<\/script>/g, '').replace('&nbsp;', ''));
  272. var nowTime = new Date().getTime();
  273. var oneDayPast = nowTime - 86400000;
  274. var twoDaysPast = nowTime - 172800000;
  275. var sevenDaysPast = nowTime - 604800000;
  276. var fourteenDaysPast = nowTime - 1209600000;
  277. var thirtyDaysPast = nowTime - 2592000000;
  278. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  279. var lastChar = rowValue.charAt(rowValue.length - 1);
  280. rowValue = lastChar === '*' ? rowValue.substr(0, rowValue.length - 1) : rowValue;
  281. rowValue = new Date(refinedRowValue).getTime();
  282. match = false;
  283. switch (condition) {
  284. case 'Any':
  285. match = true;
  286. break;
  287. case 'Past 1 Day':
  288. if (nowTime > rowValue && rowValue > oneDayPast) match = true;
  289. break;
  290. case 'Past 2 Days':
  291. if (nowTime > rowValue && rowValue > twoDaysPast) match = true;
  292. break;
  293. case 'Past 7 Days':
  294. if (nowTime > rowValue && rowValue > sevenDaysPast) match = true;
  295. break;
  296. case 'Past 14 Days':
  297. if (nowTime > rowValue && rowValue > fourteenDaysPast) match = true;
  298. break;
  299. case 'Past 30 Days':
  300. if (nowTime > rowValue && rowValue > thirtyDaysPast) match = true;
  301. break;
  302. case 'Running Now':
  303. if (lastChar === '*') match = true;
  304. break;
  305. case 'Custom':
  306. var options = jQuery('#custom_rundate_filter').val();
  307. var optionsArray = options.split(',');
  308. if (optionsArray.length == 1) {
  309. equal = optionsArray[0];
  310. if (equal == rowValue) match = true;
  311. } else if (optionsArray.length == 2) {
  312. lowerBound = optionsArray[0];
  313. upperBound = optionsArray[1];
  314. if (upperBound > rowValue && rowValue > lowerBound) match = true;
  315. }
  316. break;
  317. }
  318. }
  319. }
  320. function numberFilter(rangeExp, rowValue) {
  321. var compareChar = rangeExp.charAt(0);
  322. var compareValue;
  323. if (rangeExp.length == 1) {
  324. if (isNaN(parseInt(compareChar))) {
  325. // User types only '=' or '>' or '<', so don't filter column values
  326. match = true;
  327. return match;
  328. }
  329. else {
  330. compareValue = parseFloat(parseFloat(rangeExp).toFixed(2));
  331. }
  332. }
  333. else {
  334. if (isNaN(parseInt(compareChar))) {
  335. compareValue = parseFloat(parseFloat(rangeExp.substr(1, rangeExp.length)).toFixed(2));
  336. }
  337. else {
  338. compareValue = parseFloat(parseFloat(rangeExp.substr(0, rangeExp.length)).toFixed(2));
  339. }
  340. }
  341. rowValue = parseFloat((jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue);
  342. match = false;
  343. switch (compareChar) {
  344. case '<':
  345. if (compareValue > rowValue) match = true;
  346. break;
  347. case '>':
  348. if (compareValue < rowValue) match = true;
  349. break;
  350. case '=':
  351. if (compareValue == rowValue) match = true;
  352. default:
  353. if (rangeExp == rowValue) match = true;
  354. }
  355. }
  356. return match;
  357. }
  358. )
  359. );
  360. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  361. "fnFilterClear": function (oSettings) {
  362. /* Remove global filter */
  363. oSettings.oPreviousSearch.sSearch = "";
  364. /* Remove the text of the global filter in the input boxes */
  365. if (typeof oSettings.aanFeatures.f != 'undefined') {
  366. var n = oSettings.aanFeatures.f;
  367. for (var i = 0, iLen = n.length; i < iLen; i++) {
  368. $('input', n[i]).val('');
  369. }
  370. }
  371. /* Remove the search text for the column filters - NOTE - if you have input boxes for these
  372. * filters, these will need to be reset
  373. */
  374. for (var i = 0, iLen = oSettings.aoPreSearchCols.length; i < iLen; i++) {
  375. oSettings.aoPreSearchCols[i].sSearch = "";
  376. }
  377. /* Redraw */
  378. oSettings.oApi._fnReDraw(oSettings);
  379. }
  380. });
  381. jQuery.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
  382. // check that we have a column id
  383. if ( typeof iColumn == "undefined" ) return [];
  384. // by default we only wany unique data
  385. if ( typeof bUnique == "undefined" ) bUnique = true;
  386. // by default we do want to only look at filtered data
  387. if ( typeof bFiltered == "undefined" ) bFiltered = true;
  388. // by default we do not wany to include empty values
  389. if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;
  390. // list of rows which we're going to loop through
  391. var aiRows;
  392. // use only filtered rows
  393. if (bFiltered == true) aiRows = oSettings.aiDisplay;
  394. // use all rows
  395. else aiRows = oSettings.aiDisplayMaster; // all row numbers
  396. // set up data array
  397. var asResultData = new Array();
  398. for (var i=0,c=aiRows.length; i<c; i++) {
  399. iRow = aiRows[i];
  400. var sValue = this.fnGetData(iRow, iColumn);
  401. // ignore empty values?
  402. if (bIgnoreEmpty == true && sValue.length == 0) continue;
  403. // ignore unique values?
  404. else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
  405. // else push the value onto the result data array
  406. else asResultData.push(sValue);
  407. }
  408. return asResultData;
  409. };
  410. jQuery.fn.dataTableExt.ofnSearch['num-html'] = function ( sData ) {
  411. return sData.replace(/[\r\n]/g, " ").replace(/<.*?>/g, "");
  412. }
  413. jQuery.fn.dataTableExt.ofnSearch['ambari-bandwidth'] = function ( sData ) {
  414. return sData.replace(/[\r\n]/g, " ").replace(/<.*?>/g, "");
  415. }