utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. function initLogsElement(element) {
  17. element.data("logsAtEnd", true);
  18. element.scroll(function () {
  19. element.data("logsAtEnd", ($(this).scrollTop() + $(this).height() + 20 >= $(this)[0].scrollHeight));
  20. });
  21. element.css("overflow", "auto").height($(window).height() - element.offset().top - 50);
  22. }
  23. function appendAndScroll(element, logs) {
  24. var newLines = logs.split("\n").slice(element.text().split("\n").length);
  25. element.text(element.text() + newLines.join("\n"));
  26. if (element.data("logsAtEnd")) {
  27. element.scrollTop(element[0].scrollHeight - element.height());
  28. }
  29. }
  30. function resizeLogs(element) {
  31. element.css("overflow", "auto").height($(window).height() - element.offset().top - 50);
  32. }
  33. var _resizeTimeout = -1;
  34. var _winWidth = $(window).width();
  35. var _winHeight = $(window).height();
  36. function enableResizeLogs() {
  37. $(window).on("resize", function () {
  38. window.clearTimeout(_resizeTimeout);
  39. _resizeTimeout = window.setTimeout(function () {
  40. // prevents endless loop in IE8
  41. if (_winWidth != $(window).width() || _winHeight != $(window).height()) {
  42. $(document).trigger("resized");
  43. _winWidth = $(window).width();
  44. _winHeight = $(window).height();
  45. }
  46. }, 200);
  47. });
  48. }
  49. function getQueryStringParameter(name) {
  50. name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  51. var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
  52. results = regex.exec(location.search);
  53. return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  54. }
  55. function getStatusClass(status, prefix) {
  56. if (!Array.prototype.indexOf) {
  57. Array.prototype.indexOf = function (needle) {
  58. for (var i = 0; i < this.length; i++) {
  59. if (this[i] === needle) {
  60. return i;
  61. }
  62. }
  63. return -1;
  64. };
  65. }
  66. if (prefix == null) {
  67. prefix = "label-";
  68. }
  69. var klass = "";
  70. if (['SUCCEEDED', 'OK', 'DONE'].indexOf(status) > -1) {
  71. klass = prefix + "success";
  72. }
  73. else if (['RUNNING', 'READY', 'PREP', 'WAITING', 'SUSPENDED', 'PREPSUSPENDED', 'PREPPAUSED', 'PAUSED',
  74. 'SUBMITTED',
  75. 'SUSPENDEDWITHERROR',
  76. 'PAUSEDWITHERROR'].indexOf(status) > -1) {
  77. klass = prefix + "warning";
  78. }
  79. else {
  80. klass = prefix + "important";
  81. if (prefix == "bar-") {
  82. klass = prefix + "danger";
  83. }
  84. }
  85. return klass;
  86. }
  87. function emptyStringIfNull(obj) {
  88. if (obj != null && obj != undefined) {
  89. return obj;
  90. }
  91. return "";
  92. }
  93. jQuery.fn.dataTableExt.oSort['title-numeric-asc'] = function (a, b) {
  94. var x = a.match(/title="*(-?[0-9\.]+)/)[1];
  95. var y = b.match(/title="*(-?[0-9\.]+)/)[1];
  96. x = parseFloat(x);
  97. y = parseFloat(y);
  98. return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  99. };
  100. jQuery.fn.dataTableExt.oSort['title-numeric-desc'] = function (a, b) {
  101. var x = a.match(/title="*(-?[0-9\.]+)/)[1];
  102. var y = b.match(/title="*(-?[0-9\.]+)/)[1];
  103. x = parseFloat(x);
  104. y = parseFloat(y);
  105. return ((x < y) ? 1 : ((x > y) ? -1 : 0));
  106. };