]> git.proxmox.com Git - sencha-touch.git/blob - src/src/event/ListenerStack.js
import Sencha Touch 2.4.2 source
[sencha-touch.git] / src / src / event / ListenerStack.js
1 //@require @core
2
3 /**
4 * @private
5 */
6 Ext.define('Ext.event.ListenerStack', {
7
8 currentOrder: 'current',
9
10 length: 0,
11
12 constructor: function() {
13 this.listeners = {
14 before: [],
15 current: [],
16 after: []
17 };
18
19 this.lateBindingMap = {};
20
21 return this;
22 },
23
24 add: function(fn, scope, options, order) {
25 var lateBindingMap = this.lateBindingMap,
26 listeners = this.getAll(order),
27 i = listeners.length,
28 bindingMap, listener, id;
29
30 if (typeof fn == 'string' && scope.isIdentifiable) {
31 id = scope.getId();
32
33 bindingMap = lateBindingMap[id];
34
35 if (bindingMap) {
36 if (bindingMap[fn]) {
37 return false;
38 }
39 else {
40 bindingMap[fn] = true;
41 }
42 }
43 else {
44 lateBindingMap[id] = bindingMap = {};
45 bindingMap[fn] = true;
46 }
47 }
48 else {
49 if (i > 0) {
50 while (i--) {
51 listener = listeners[i];
52
53 if (listener.fn === fn && listener.scope === scope) {
54 listener.options = options;
55 return false;
56 }
57 }
58 }
59 }
60
61 listener = this.create(fn, scope, options, order);
62
63 if (options && options.prepend) {
64 delete options.prepend;
65 listeners.unshift(listener);
66 }
67 else {
68 listeners.push(listener);
69 }
70
71 this.length++;
72
73 return true;
74 },
75
76 getAt: function(index, order) {
77 return this.getAll(order)[index];
78 },
79
80 getAll: function(order) {
81 if (!order) {
82 order = this.currentOrder;
83 }
84
85 return this.listeners[order];
86 },
87
88 count: function(order) {
89 return this.getAll(order).length;
90 },
91
92 create: function(fn, scope, options, order) {
93 return {
94 stack: this,
95 fn: fn,
96 firingFn: false,
97 boundFn: false,
98 isLateBinding: typeof fn == 'string',
99 scope: scope,
100 options: options || {},
101 order: order
102 };
103 },
104
105 remove: function(fn, scope, order) {
106 var listeners = this.getAll(order),
107 i = listeners.length,
108 isRemoved = false,
109 lateBindingMap = this.lateBindingMap,
110 listener, id;
111
112 if (i > 0) {
113 // Start from the end index, faster than looping from the
114 // beginning for "single" listeners,
115 // which are normally LIFO
116 while (i--) {
117 listener = listeners[i];
118
119 if (listener.fn === fn && listener.scope === scope) {
120 listeners.splice(i, 1);
121 isRemoved = true;
122 this.length--;
123
124 if (typeof fn == 'string' && scope.isIdentifiable) {
125 id = scope.getId();
126
127 if (lateBindingMap[id] && lateBindingMap[id][fn]) {
128 delete lateBindingMap[id][fn];
129 }
130 }
131 break;
132 }
133 }
134 }
135
136 return isRemoved;
137 }
138 });