data_table.js 16 KB

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