dependencyLib.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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[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. return new Event(elem);
  70. }
  71. //static
  72. DependencyLib.isFunction = function(obj) {
  73. return type(obj) === "function";
  74. };
  75. DependencyLib.noop = function() {};
  76. DependencyLib.parseJSON = function(data) {
  77. return JSON.parse(data + "");
  78. };
  79. DependencyLib.isArray = Array.isArray;
  80. DependencyLib.inArray = function(elem, arr, i) {
  81. return arr == null ? -1 : indexOf(arr, elem, i);
  82. };
  83. DependencyLib.valHooks = undefined;
  84. DependencyLib.extend = $.extend;
  85. DependencyLib.each = $.each;
  86. DependencyLib.map = $.map;
  87. DependencyLib.Event = $.Event; //needs to be replaced
  88. DependencyLib._data = $._data; //needs to be replaced
  89. DependencyLib.data = $.data; //needs to be replaced
  90. window.dependencyLib = DependencyLib;
  91. return DependencyLib;
  92. }));