hue.utils.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // Array polyfills for older browsers
  17. if (!('clean' in Array.prototype)) {
  18. Array.prototype.clean = function (deleteValue) {
  19. for (var i = 0; i < this.length; i++) {
  20. if (this[i] == deleteValue) {
  21. this.splice(i, 1);
  22. i--;
  23. }
  24. }
  25. return this;
  26. };
  27. }
  28. if (!('move' in Array.prototype)) {
  29. Array.prototype.move = function (old_index, new_index) {
  30. if (new_index >= this.length) {
  31. var k = new_index - this.length;
  32. while ((k--) + 1) {
  33. this.push(undefined);
  34. }
  35. }
  36. this.splice(new_index, 0, this.splice(old_index, 1)[0]);
  37. return this;
  38. };
  39. }
  40. if (!('indexOf' in Array.prototype)) {
  41. Array.prototype.indexOf = function (needle) {
  42. for (var i = 0; i < this.length; i++) {
  43. if (this[i] === needle) {
  44. return i;
  45. }
  46. }
  47. return -1;
  48. };
  49. }
  50. // adding missing .filter for IE8
  51. if (!('filter' in Array.prototype)) {
  52. Array.prototype.filter = function (filter, that /*opt*/) {
  53. var other = [], v;
  54. for (var i = 0, n = this.length; i < n; i++)
  55. if (i in this && filter.call(that, v = this[i], i, this))
  56. other.push(v);
  57. return other;
  58. };
  59. }
  60. Array.prototype.diff = function (a) {
  61. return this.filter(function (i) {
  62. return a.indexOf(i) < 0;
  63. });
  64. };
  65. /*
  66. * Add utility methods to the HUE object
  67. */
  68. (function (hue) {
  69. 'use strict';
  70. /*
  71. * Convert text to URLs
  72. * Selector arg can be jQuery or document.querySelectorAll()
  73. */
  74. hue.text2Url = function (selectors) {
  75. var i = 0,
  76. len = selectors.length;
  77. for (i; i < len; i++) {
  78. var arr = [],
  79. selector = selectors[i],
  80. val = selector.innerHTML.replace(/&nbsp;/g, ' ').split(' ');
  81. val.forEach(function(word) {
  82. var matched = null,
  83. re = /(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/gi;
  84. if (re.test(word)) {
  85. matched = word.match(re);
  86. word = word.replace(matched, '<a href="' + matched + '">' + matched + '</a>')
  87. arr.push(word);
  88. } else {
  89. arr.push(word);
  90. }
  91. });
  92. selector.innerHTML = arr.join(' ');
  93. }
  94. return this;
  95. };
  96. /*
  97. * Create a in-memory div, set it's inner text(which jQuery automatically encodes)
  98. * then grab the encoded contents back out.
  99. */
  100. hue.htmlEncode = function (value){
  101. return $('<div/>').text(value).html();
  102. };
  103. }(hue = window.hue || {}));
  104. if (!Object.keys) {
  105. Object.keys = (function () {
  106. 'use strict';
  107. var hasOwnProperty = Object.prototype.hasOwnProperty,
  108. hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
  109. dontEnums = [
  110. 'toString',
  111. 'toLocaleString',
  112. 'valueOf',
  113. 'hasOwnProperty',
  114. 'isPrototypeOf',
  115. 'propertyIsEnumerable',
  116. 'constructor'
  117. ],
  118. dontEnumsLength = dontEnums.length;
  119. return function (obj) {
  120. if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
  121. throw new TypeError('Object.keys called on non-object');
  122. }
  123. var result = [], prop, i;
  124. for (prop in obj) {
  125. if (hasOwnProperty.call(obj, prop)) {
  126. result.push(prop);
  127. }
  128. }
  129. if (hasDontEnumBug) {
  130. for (i = 0; i < dontEnumsLength; i++) {
  131. if (hasOwnProperty.call(obj, dontEnums[i])) {
  132. result.push(dontEnums[i]);
  133. }
  134. }
  135. }
  136. return result;
  137. };
  138. }());
  139. }