dependencyLib.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. (function(factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define(["jquery"], factory);
  4. } else if (typeof exports === "object") {
  5. module.exports = factory(require("jquery"));
  6. } else {
  7. factory(jQuery);
  8. }
  9. }
  10. (function($) {
  11. //helper functions
  12. // Use a stripped-down indexOf as it's faster than native
  13. // http://jsperf.com/thor-indexof-vs-for/5
  14. function indexOf(list, elem) {
  15. var i = 0,
  16. len = list.length;
  17. for (; i < len; i++) {
  18. if (list[i] === elem) {
  19. return i;
  20. }
  21. }
  22. return -1;
  23. }
  24. var class2type = {},
  25. classTypes = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
  26. for (var nameNdx = 0; nameNdx < classTypes.length; nameNdx++) {
  27. class2type["[object " + classTypes[nameNdx] + "]"] = classTypes[nameNdx].toLowerCase();
  28. }
  29. function type(obj) {
  30. if (obj == null) {
  31. return obj + "";
  32. }
  33. // Support: Android<4.0, iOS<6 (functionish RegExp)
  34. return typeof obj === "object" || typeof obj === "function" ?
  35. class2type[class2type.toString.call(obj)] || "object" :
  36. typeof obj;
  37. }
  38. //micro event lib
  39. function Event(elem) {
  40. this[0] = elem;
  41. }
  42. Event.prototype = {
  43. on: function() {
  44. $.fn.on.apply($(this[0]), arguments);
  45. return this;
  46. },
  47. off: function() {
  48. $.fn.off.apply($(this[0]), arguments);
  49. return this;
  50. },
  51. trigger: function() {
  52. $.fn.trigger.apply($(this[0]), arguments);
  53. return this;
  54. },
  55. triggerHandler: function() {
  56. $.fn.triggerHandler.apply($(this[0]), arguments);
  57. return this;
  58. }
  59. };
  60. function getDomEvents() {
  61. var domEvents = [];
  62. for (var i in document) {
  63. if (i.substring(0, 2) === "on" && (document[i] === null || typeof document[i] === 'function'))
  64. domEvents.push(i);
  65. }
  66. return domEvents;
  67. };
  68. function DependencyLib(elem) {
  69. this[0] = elem;
  70. if (!(this instanceof DependencyLib)) {
  71. return new DependencyLib(elem);
  72. }
  73. }
  74. DependencyLib.prototype = Event.prototype;
  75. //static
  76. DependencyLib.isFunction = function(obj) {
  77. return type(obj) === "function";
  78. };
  79. DependencyLib.noop = function() {};
  80. DependencyLib.parseJSON = function(data) {
  81. return JSON.parse(data + "");
  82. };
  83. DependencyLib.isArray = Array.isArray;
  84. DependencyLib.inArray = function(elem, arr, i) {
  85. return arr == null ? -1 : indexOf(arr, elem, i);
  86. };
  87. DependencyLib.valHooks = $.valHooks;
  88. DependencyLib.isWindow = function(obj) {
  89. return obj != null && obj === obj.window;
  90. };
  91. DependencyLib.isPlainObject = function(obj) {
  92. // Not plain objects:
  93. // - Any object or value whose internal [[Class]] property is not "[object Object]"
  94. // - DOM nodes
  95. // - window
  96. if (type(obj) !== "object" || obj.nodeType || DependencyLib.isWindow(obj)) {
  97. return false;
  98. }
  99. if (obj.constructor && !class2type.hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf")) {
  100. return false;
  101. }
  102. // If the function hasn't returned already, we're confident that
  103. // |obj| is a plain object, created by {} or constructed with new Object
  104. return true;
  105. };
  106. DependencyLib.extend = function() {
  107. var options, name, src, copy, copyIsArray, clone,
  108. target = arguments[0] || {},
  109. i = 1,
  110. length = arguments.length,
  111. deep = false;
  112. // Handle a deep copy situation
  113. if (typeof target === "boolean") {
  114. deep = target;
  115. // Skip the boolean and the target
  116. target = arguments[i] || {};
  117. i++;
  118. }
  119. // Handle case when target is a string or something (possible in deep copy)
  120. if (typeof target !== "object" && !DependencyLib.isFunction(target)) {
  121. target = {};
  122. }
  123. // Extend jQuery itself if only one argument is passed
  124. if (i === length) {
  125. target = this;
  126. i--;
  127. }
  128. for (; i < length; i++) {
  129. // Only deal with non-null/undefined values
  130. if ((options = arguments[i]) != null) {
  131. // Extend the base object
  132. for (name in options) {
  133. src = target[name];
  134. copy = options[name];
  135. // Prevent never-ending loop
  136. if (target === copy) {
  137. continue;
  138. }
  139. // Recurse if we're merging plain objects or arrays
  140. if (deep && copy && (DependencyLib.isPlainObject(copy) || (copyIsArray = DependencyLib.isArray(copy)))) {
  141. if (copyIsArray) {
  142. copyIsArray = false;
  143. clone = src && DependencyLib.isArray(src) ? src : [];
  144. } else {
  145. clone = src && DependencyLib.isPlainObject(src) ? src : {};
  146. }
  147. // Never move original objects, clone them
  148. target[name] = DependencyLib.extend(deep, clone, copy);
  149. // Don't bring in undefined values
  150. } else if (copy !== undefined) {
  151. target[name] = copy;
  152. }
  153. }
  154. }
  155. }
  156. // Return the modified object
  157. return target;
  158. };
  159. DependencyLib.each = $.each;
  160. DependencyLib.map = $.map;
  161. DependencyLib.Event = $.Event; //needs to be replaced
  162. DependencyLib.data = $.data; //needs to be replaced
  163. window.dependencyLib = DependencyLib;
  164. return DependencyLib;
  165. }));