utils.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 - 80);
  22. }
  23. function appendAndScroll(element, logs) {
  24. var newLines = logs.split("\n").slice(element.text().split("\n").length);
  25. if (newLines.length > 0) {
  26. element.text(element.text() + newLines.join("\n") + "\n");
  27. }
  28. if (element.data("logsAtEnd")) {
  29. element.scrollTop(element[0].scrollHeight - element.height());
  30. }
  31. }
  32. function resizeLogs(element) {
  33. element.css("overflow", "auto").height($(window).height() - element.offset().top - 80);
  34. }
  35. var _resizeTimeout = -1;
  36. var _winWidth = $(window).width();
  37. var _winHeight = $(window).height();
  38. function enableResizeLogs() {
  39. $(window).on("resize", function () {
  40. window.clearTimeout(_resizeTimeout);
  41. _resizeTimeout = window.setTimeout(function () {
  42. // prevents endless loop in IE8
  43. if (_winWidth != $(window).width() || _winHeight != $(window).height()) {
  44. $(document).trigger("resized");
  45. _winWidth = $(window).width();
  46. _winHeight = $(window).height();
  47. }
  48. }, 200);
  49. });
  50. }
  51. function getQueryStringParameter(name) {
  52. name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  53. var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
  54. results = regex.exec(location.search);
  55. return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  56. }
  57. function getStatusClass(status, prefix) {
  58. if (prefix == null) {
  59. prefix = "label-";
  60. }
  61. var klass = "";
  62. if (['SUCCEEDED', 'OK', 'DONE'].indexOf(status) > -1) {
  63. klass = prefix + "success";
  64. }
  65. else if (['RUNNING', 'ACCEPTED', 'READY',
  66. 'PREP', 'WAITING', 'SUSPENDED',
  67. 'PREPSUSPENDED', 'PREPPAUSED', 'PAUSED',
  68. 'SUBMITTED', 'SUSPENDEDWITHERROR', 'PAUSEDWITHERROR'].indexOf(status) > -1) {
  69. klass = prefix + "warning";
  70. }
  71. else {
  72. klass = prefix + "important";
  73. if (prefix == "bar-") {
  74. klass = prefix + "danger";
  75. }
  76. }
  77. return klass;
  78. }
  79. function emptyStringIfNull(obj) {
  80. if (obj != null && obj != undefined) {
  81. return obj;
  82. }
  83. return "";
  84. }
  85. jQuery.fn.dataTableExt.oSort['title-numeric-asc'] = function (a, b) {
  86. var x = a.match(/title="*(-?[0-9\.]+)/)[1];
  87. var y = b.match(/title="*(-?[0-9\.]+)/)[1];
  88. x = parseFloat(x);
  89. y = parseFloat(y);
  90. return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  91. };
  92. jQuery.fn.dataTableExt.oSort['title-numeric-desc'] = function (a, b) {
  93. var x = a.match(/title="*(-?[0-9\.]+)/)[1];
  94. var y = b.match(/title="*(-?[0-9\.]+)/)[1];
  95. x = parseFloat(x);
  96. y = parseFloat(y);
  97. return ((x < y) ? 1 : ((x > y) ? -1 : 0));
  98. };
  99. function hellipsify() {
  100. var MAX_LENGTH = 20;
  101. $(".hellipsify").each(function () {
  102. var _this = $(this);
  103. if (_this.text().length > MAX_LENGTH) {
  104. var _oText = _this.text();
  105. var _clone = $("<div>");
  106. _clone.html(_this.html());
  107. _clone.css("position", "absolute").css("background-color", "#F5F5F5");
  108. _clone.css("top", (_this.is("a") ? _this.position().top + 3 : _this.position().top)).css("left", _this.position().left)
  109. _clone.css("z-index", "9999").css("display", "none");
  110. _clone.appendTo($("body"));
  111. _clone.mouseout(function () {
  112. $(this).hide();
  113. })
  114. $(this).html(_oText.substr(0, MAX_LENGTH - 1) + "&hellip;&nbsp;");
  115. var _eye = $("<button class='nochrome btn-small'></button>");
  116. _eye.html("<i class='icon-eye-open'></i>");
  117. _eye.css("cursor", "pointer");
  118. _eye.on("click", function (e) {
  119. e.stopImmediatePropagation();
  120. e.preventDefault();
  121. _clone.show();
  122. _this.tooltip("hide");
  123. });
  124. _eye.appendTo(_this);
  125. _this.data("original-text", _oText);
  126. _this.tooltip({
  127. title: _oText,
  128. placement: "right"
  129. });
  130. }
  131. });
  132. }