hue.utils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /*
  61. * Add utility methods to the HUE object
  62. */
  63. (function (hue) {
  64. 'use strict';
  65. /*
  66. * Convert text to URLs
  67. * Selector arg can be jQuery or document.querySelectorAll()
  68. */
  69. hue.text2Url = function (selectors) {
  70. var i = 0,
  71. len = selectors.length;
  72. for (i; i < len; i++) {
  73. var arr = [],
  74. selector = selectors[i],
  75. val = selector.innerHTML.replace(/&nbsp;/g, ' ').split(' ');
  76. val.forEach(function(word) {
  77. var matched = null,
  78. re = /(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/gi;
  79. if (re.test(word)) {
  80. matched = word.match(re);
  81. word = word.replace(matched, '<a href="' + matched + '">' + matched + '</a>')
  82. arr.push(word);
  83. } else {
  84. arr.push(word);
  85. }
  86. });
  87. selector.innerHTML = arr.join(' ');
  88. }
  89. return this;
  90. };
  91. }(hue = window.hue || {}));