You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

onfire.js 2.4KB

1 vuosi sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ! function(root, factory) {
  2. if (typeof module === "object" && module.exports) module.exports = factory();
  3. else root.onfire = factory()
  4. }(typeof window !== "undefined" ? window : this, function() {
  5. var __onfireEvents = {},
  6. __cnt = 0,
  7. string_str = "string",
  8. function_str = "function",
  9. hasOwnKey = Function.call.bind(Object.hasOwnProperty),
  10. slice = Function.call.bind(Array.prototype.slice);
  11. function _bind(eventName, callback, is_one, context) {
  12. if (typeof eventName !== string_str || typeof callback !== function_str) {
  13. throw new Error("args: " + string_str + ", " + function_str + "")
  14. }
  15. if (!hasOwnKey(__onfireEvents, eventName)) {
  16. __onfireEvents[eventName] = {}
  17. }
  18. __onfireEvents[eventName][++__cnt] = [callback, is_one, context];
  19. return [eventName, __cnt]
  20. }
  21. function _each(obj, callback) {
  22. for (var key in obj) {
  23. if (hasOwnKey(obj, key)) callback(key, obj[key])
  24. }
  25. }
  26. function on(eventName, callback, context) {
  27. return _bind(eventName, callback, 0, context)
  28. }
  29. function one(eventName, callback, context) {
  30. return _bind(eventName, callback, 1, context)
  31. }
  32. function _fire_func(eventName, args) {
  33. if (hasOwnKey(__onfireEvents, eventName)) {
  34. _each(__onfireEvents[eventName], function(key, item) {
  35. item[0].apply(item[2], args);
  36. if (item[1]) delete __onfireEvents[eventName][key]
  37. })
  38. }
  39. }
  40. function fire(eventName) {
  41. var args = slice(arguments, 1);
  42. setTimeout(function() {
  43. _fire_func(eventName, args)
  44. })
  45. }
  46. function fireSync(eventName) {
  47. _fire_func(eventName, slice(arguments, 1))
  48. }
  49. function un(event) {
  50. var eventName, key, r = false,
  51. type = typeof event;
  52. if (type === string_str) {
  53. if (hasOwnKey(__onfireEvents, event)) {
  54. delete __onfireEvents[event];
  55. return true
  56. }
  57. return false
  58. } else if (type === "object") {
  59. eventName = event[0];
  60. key = event[1];
  61. if (hasOwnKey(__onfireEvents, eventName) && hasOwnKey(__onfireEvents[eventName], key)) {
  62. delete __onfireEvents[eventName][key];
  63. return true
  64. }
  65. return false
  66. } else if (type === function_str) {
  67. _each(__onfireEvents, function(key_1, item_1) {
  68. _each(item_1, function(key_2, item_2) {
  69. if (item_2[0] === event) {
  70. delete __onfireEvents[key_1][key_2];
  71. r = true
  72. }
  73. })
  74. });
  75. return r
  76. }
  77. return true
  78. }
  79. function clear() {
  80. __onfireEvents = {}
  81. }
  82. return {
  83. on: on,
  84. one: one,
  85. un: un,
  86. fire: fire,
  87. fireSync: fireSync,
  88. clear: clear
  89. }
  90. });