hue.utils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }(hue = window.hue || {}));
  97. if (!Object.keys) {
  98. Object.keys = (function () {
  99. 'use strict';
  100. var hasOwnProperty = Object.prototype.hasOwnProperty,
  101. hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
  102. dontEnums = [
  103. 'toString',
  104. 'toLocaleString',
  105. 'valueOf',
  106. 'hasOwnProperty',
  107. 'isPrototypeOf',
  108. 'propertyIsEnumerable',
  109. 'constructor'
  110. ],
  111. dontEnumsLength = dontEnums.length;
  112. return function (obj) {
  113. if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
  114. throw new TypeError('Object.keys called on non-object');
  115. }
  116. var result = [], prop, i;
  117. for (prop in obj) {
  118. if (hasOwnProperty.call(obj, prop)) {
  119. result.push(prop);
  120. }
  121. }
  122. if (hasDontEnumBug) {
  123. for (i = 0; i < dontEnumsLength; i++) {
  124. if (hasOwnProperty.call(obj, dontEnums[i])) {
  125. result.push(dontEnums[i]);
  126. }
  127. }
  128. }
  129. return result;
  130. };
  131. }());
  132. }