utils.js 4.7 KB

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