/neatools.js

https://github.com/windviki/vBookmarks · JavaScript · 156 lines · 146 code · 5 blank · 5 comment · 5 complexity · ae6cff4ad02818074824b069a0d0e2a8 MD5 · raw file

  1. /*
  2. Neatools: a nano JavaScript framework made just for vBookmarks and nothing else.
  3. Heavily inspired by MooTools (http://mootools.net/).
  4. Works for Chrome 8 and above.
  5. */
  6. var $ = function (id) {
  7. return document.getElementById(id);
  8. };
  9. function $extend(original, extended) {
  10. for (var key in (extended || {}))
  11. original[key] = extended[key];
  12. return original;
  13. };
  14. function $each(obj, fn, bind) {
  15. for (var key in obj) {
  16. if (obj.hasOwnProperty(key))
  17. fn.call(bind, obj[key], key, obj);
  18. }
  19. }
  20. var $slice = Array.prototype.slice;
  21. $extend(String.prototype, {
  22. widont: function () {
  23. return this.replace(/\s([^\s]+)$/i, ' $1');
  24. },
  25. toInt: function (base) {
  26. return parseInt(this, base || 10);
  27. },
  28. hyphenate: function () {
  29. return this.replace(/[A-Z]/g, function (match) {
  30. return ('-' + match.charAt(0).toLowerCase());
  31. });
  32. },
  33. htmlspecialchars: function () {
  34. return this.replace(/>/g, '&gt;').replace(/</g, '&lt;').replace(/"/g, '&quot;');
  35. },
  36. escapeRegExp: function () {
  37. return this.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');
  38. }
  39. });
  40. $extend(Array.prototype, {
  41. contains: function (item, from) {
  42. return this.indexOf(item, from) != -1;
  43. },
  44. clean: function () {
  45. return this.filter(function (obj) {
  46. return (obj != undefined);
  47. });
  48. },
  49. getLast: function () {
  50. return (this.length) ? this[this.length - 1] : null;
  51. }
  52. });
  53. // Array generic
  54. ['map', 'filter', 'forEach'].forEach(function (method) {
  55. if (!Array[method])
  56. Array[method] = function () {
  57. var args = $slice.call(arguments);
  58. return Array.prototype[method].apply(args.pop(), args);
  59. };
  60. });
  61. $extend(Element.prototype, {
  62. getComputedStyle: function (property) {
  63. return window.getComputedStyle(this, null).getPropertyValue(property.hyphenate());
  64. },
  65. destroy: function () {
  66. return (this.parentNode) ? this.parentNode.removeChild(this) : this;
  67. },
  68. hasClass: function (className) {
  69. return this.classList.contains(className);
  70. },
  71. addClass: function (className) {
  72. this.classList.add(className);
  73. return this;
  74. },
  75. removeClass: function (className) {
  76. this.classList.remove(className);
  77. return this;
  78. },
  79. toggleClass: function (className) {
  80. this.classList.toggle(className);
  81. return this;
  82. },
  83. getAllNext: function () {
  84. var elements = [];
  85. var node = this;
  86. while (node = node.nextElementSibling) {
  87. elements.push(node);
  88. }
  89. return elements;
  90. },
  91. getAllPrevious: function () {
  92. var elements = [];
  93. var node = this;
  94. while (node = node.previousElementSibling) {
  95. elements.push(node);
  96. }
  97. return elements;
  98. },
  99. getSiblings: function () {
  100. return this.getAllNext().concat(this.getAllPrevious());
  101. }
  102. });
  103. (function () {
  104. var inserters = {
  105. before: function (context, element) {
  106. var parent = element.parentNode;
  107. if (parent)
  108. parent.insertBefore(context, element);
  109. },
  110. after: function (context, element) {
  111. var parent = element.parentNode;
  112. if (parent)
  113. parent.insertBefore(context, element.nextSibling);
  114. },
  115. bottom: function (context, element) {
  116. element.appendChild(context);
  117. },
  118. top: function (context, element) {
  119. element.insertBefore(context, element.firstChild);
  120. }
  121. };
  122. Element.prototype.inject = function (el, where) {
  123. inserters[where || 'bottom'](this, el);
  124. return this;
  125. };
  126. })();
  127. function getPageZoom() {
  128. //RETURN: 1.0 for 100%, and so on
  129. var zoom = 1.0;
  130. try {
  131. var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  132. svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
  133. svg.setAttribute('version', '1.1');
  134. document.body.appendChild(svg);
  135. zoom = svg.currentScale;
  136. document.body.removeChild(svg);
  137. } catch (e) {
  138. console.error("Zoom method failed: " + e.message);
  139. }
  140. return zoom;
  141. }