callback.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Zepto.js
  2. // (c) 2010-2016 Thomas Fuchs
  3. // Zepto.js may be freely distributed under the MIT license.
  4. ;(function($){
  5. // Create a collection of callbacks to be fired in a sequence, with configurable behaviour
  6. // Option flags:
  7. // - once: Callbacks fired at most one time.
  8. // - memory: Remember the most recent context and arguments
  9. // - stopOnFalse: Cease iterating over callback list
  10. // - unique: Permit adding at most one instance of the same callback
  11. $.Callbacks = function(options) {
  12. options = $.extend({}, options)
  13. var memory, // Last fire value (for non-forgettable lists)
  14. fired, // Flag to know if list was already fired
  15. firing, // Flag to know if list is currently firing
  16. firingStart, // First callback to fire (used internally by add and fireWith)
  17. firingLength, // End of the loop when firing
  18. firingIndex, // Index of currently firing callback (modified by remove if needed)
  19. list = [], // Actual callback list
  20. stack = !options.once && [], // Stack of fire calls for repeatable lists
  21. fire = function(data) {
  22. memory = options.memory && data
  23. fired = true
  24. firingIndex = firingStart || 0
  25. firingStart = 0
  26. firingLength = list.length
  27. firing = true
  28. for ( ; list && firingIndex < firingLength ; ++firingIndex ) {
  29. if (list[firingIndex].apply(data[0], data[1]) === false && options.stopOnFalse) {
  30. memory = false
  31. break
  32. }
  33. }
  34. firing = false
  35. if (list) {
  36. if (stack) stack.length && fire(stack.shift())
  37. else if (memory) list.length = 0
  38. else Callbacks.disable()
  39. }
  40. },
  41. Callbacks = {
  42. add: function() {
  43. if (list) {
  44. var start = list.length,
  45. add = function(args) {
  46. $.each(args, function(_, arg){
  47. if (typeof arg === "function") {
  48. if (!options.unique || !Callbacks.has(arg)) list.push(arg)
  49. }
  50. else if (arg && arg.length && typeof arg !== 'string') add(arg)
  51. })
  52. }
  53. add(arguments)
  54. if (firing) firingLength = list.length
  55. else if (memory) {
  56. firingStart = start
  57. fire(memory)
  58. }
  59. }
  60. return this
  61. },
  62. remove: function() {
  63. if (list) {
  64. $.each(arguments, function(_, arg){
  65. var index
  66. while ((index = $.inArray(arg, list, index)) > -1) {
  67. list.splice(index, 1)
  68. // Handle firing indexes
  69. if (firing) {
  70. if (index <= firingLength) --firingLength
  71. if (index <= firingIndex) --firingIndex
  72. }
  73. }
  74. })
  75. }
  76. return this
  77. },
  78. has: function(fn) {
  79. return !!(list && (fn ? $.inArray(fn, list) > -1 : list.length))
  80. },
  81. empty: function() {
  82. firingLength = list.length = 0
  83. return this
  84. },
  85. disable: function() {
  86. list = stack = memory = undefined
  87. return this
  88. },
  89. disabled: function() {
  90. return !list
  91. },
  92. lock: function() {
  93. stack = undefined
  94. if (!memory) Callbacks.disable()
  95. return this
  96. },
  97. locked: function() {
  98. return !stack
  99. },
  100. fireWith: function(context, args) {
  101. if (list && (!fired || stack)) {
  102. args = args || []
  103. args = [context, args.slice ? args.slice() : args]
  104. if (firing) stack.push(args)
  105. else fire(args)
  106. }
  107. return this
  108. },
  109. fire: function() {
  110. return Callbacks.fireWith(this, arguments)
  111. },
  112. fired: function() {
  113. return !!fired
  114. }
  115. }
  116. return Callbacks
  117. }
  118. })(Zepto)