utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. define([
  2. 'jquery'
  3. ], function ($) {
  4. var Utils = {};
  5. Utils.Extend = function (ChildClass, SuperClass) {
  6. var __hasProp = {}.hasOwnProperty;
  7. function BaseConstructor () {
  8. this.constructor = ChildClass;
  9. }
  10. for (var key in SuperClass) {
  11. if (__hasProp.call(SuperClass, key)) {
  12. ChildClass[key] = SuperClass[key];
  13. }
  14. }
  15. BaseConstructor.prototype = SuperClass.prototype;
  16. ChildClass.prototype = new BaseConstructor();
  17. ChildClass.__super__ = SuperClass.prototype;
  18. return ChildClass;
  19. };
  20. function getMethods (theClass) {
  21. var proto = theClass.prototype;
  22. var methods = [];
  23. for (var methodName in proto) {
  24. var m = proto[methodName];
  25. if (typeof m !== 'function') {
  26. continue;
  27. }
  28. if (methodName === 'constructor') {
  29. continue;
  30. }
  31. methods.push(methodName);
  32. }
  33. return methods;
  34. }
  35. Utils.Decorate = function (SuperClass, DecoratorClass) {
  36. var decoratedMethods = getMethods(DecoratorClass);
  37. var superMethods = getMethods(SuperClass);
  38. function DecoratedClass () {
  39. var unshift = Array.prototype.unshift;
  40. var argCount = DecoratorClass.prototype.constructor.length;
  41. var calledConstructor = SuperClass.prototype.constructor;
  42. if (argCount > 0) {
  43. unshift.call(arguments, SuperClass.prototype.constructor);
  44. calledConstructor = DecoratorClass.prototype.constructor;
  45. }
  46. calledConstructor.apply(this, arguments);
  47. }
  48. DecoratorClass.displayName = SuperClass.displayName;
  49. function ctr () {
  50. this.constructor = DecoratedClass;
  51. }
  52. DecoratedClass.prototype = new ctr();
  53. for (var m = 0; m < superMethods.length; m++) {
  54. var superMethod = superMethods[m];
  55. DecoratedClass.prototype[superMethod] =
  56. SuperClass.prototype[superMethod];
  57. }
  58. var calledMethod = function (methodName) {
  59. // Stub out the original method if it's not decorating an actual method
  60. var originalMethod = function () {};
  61. if (methodName in DecoratedClass.prototype) {
  62. originalMethod = DecoratedClass.prototype[methodName];
  63. }
  64. var decoratedMethod = DecoratorClass.prototype[methodName];
  65. return function () {
  66. var unshift = Array.prototype.unshift;
  67. unshift.call(arguments, originalMethod);
  68. return decoratedMethod.apply(this, arguments);
  69. };
  70. };
  71. for (var d = 0; d < decoratedMethods.length; d++) {
  72. var decoratedMethod = decoratedMethods[d];
  73. DecoratedClass.prototype[decoratedMethod] = calledMethod(decoratedMethod);
  74. }
  75. return DecoratedClass;
  76. };
  77. var Observable = function () {
  78. this.listeners = {};
  79. };
  80. Observable.prototype.on = function (event, callback) {
  81. this.listeners = this.listeners || {};
  82. if (event in this.listeners) {
  83. this.listeners[event].push(callback);
  84. } else {
  85. this.listeners[event] = [callback];
  86. }
  87. };
  88. Observable.prototype.trigger = function (event) {
  89. var slice = Array.prototype.slice;
  90. this.listeners = this.listeners || {};
  91. if (event in this.listeners) {
  92. this.invoke(this.listeners[event], slice.call(arguments, 1));
  93. }
  94. if ('*' in this.listeners) {
  95. this.invoke(this.listeners['*'], arguments);
  96. }
  97. };
  98. Observable.prototype.invoke = function (listeners, params) {
  99. for (var i = 0, len = listeners.length; i < len; i++) {
  100. listeners[i].apply(this, params);
  101. }
  102. };
  103. Utils.Observable = Observable;
  104. Utils.generateChars = function (length) {
  105. var chars = '';
  106. for (var i = 0; i < length; i++) {
  107. var randomChar = Math.floor(Math.random() * 36);
  108. chars += randomChar.toString(36);
  109. }
  110. return chars;
  111. };
  112. Utils.bind = function (func, context) {
  113. return function () {
  114. func.apply(context, arguments);
  115. };
  116. };
  117. Utils._convertData = function (data) {
  118. for (var originalKey in data) {
  119. var keys = originalKey.split('-');
  120. var dataLevel = data;
  121. if (keys.length === 1) {
  122. continue;
  123. }
  124. for (var k = 0; k < keys.length; k++) {
  125. var key = keys[k];
  126. // Lowercase the first letter
  127. // By default, dash-separated becomes camelCase
  128. key = key.substring(0, 1).toLowerCase() + key.substring(1);
  129. if (!(key in dataLevel)) {
  130. dataLevel[key] = {};
  131. }
  132. if (k == keys.length - 1) {
  133. dataLevel[key] = data[originalKey];
  134. }
  135. dataLevel = dataLevel[key];
  136. }
  137. delete data[originalKey];
  138. }
  139. return data;
  140. };
  141. Utils.hasScroll = function (index, el) {
  142. // Adapted from the function created by @ShadowScripter
  143. // and adapted by @BillBarry on the Stack Exchange Code Review website.
  144. // The original code can be found at
  145. // http://codereview.stackexchange.com/q/13338
  146. // and was designed to be used with the Sizzle selector engine.
  147. var $el = $(el);
  148. var overflowX = el.style.overflowX;
  149. var overflowY = el.style.overflowY;
  150. //Check both x and y declarations
  151. if (overflowX === overflowY &&
  152. (overflowY === 'hidden' || overflowY === 'visible')) {
  153. return false;
  154. }
  155. if (overflowX === 'scroll' || overflowY === 'scroll') {
  156. return true;
  157. }
  158. return ($el.innerHeight() < el.scrollHeight ||
  159. $el.innerWidth() < el.scrollWidth);
  160. };
  161. return Utils;
  162. });