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.

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