utils.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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("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. $(window).on("resize", function () {
  37. window.clearTimeout(resizeTimeout);
  38. resizeTimeout = window.setTimeout(function () {
  39. // prevents endless loop in IE8
  40. if (winWidth != $(window).width() || winHeight != $(window).height()) {
  41. $(document).trigger("resized");
  42. winWidth = $(window).width();
  43. winHeight = $(window).height();
  44. }
  45. }, 200);
  46. });