1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,173 +0,0 @@ |
1 |
-import Utils from './utils'; |
|
2 |
- |
|
3 |
-class SwiperClass { |
|
4 |
- constructor(params = {}) { |
|
5 |
- const self = this; |
|
6 |
- self.params = params; |
|
7 |
- |
|
8 |
- // Events |
|
9 |
- self.eventsListeners = {}; |
|
10 |
- |
|
11 |
- if (self.params && self.params.on) { |
|
12 |
- Object.keys(self.params.on).forEach((eventName) => { |
|
13 |
- self.on(eventName, self.params.on[eventName]); |
|
14 |
- }); |
|
15 |
- } |
|
16 |
- } |
|
17 |
- |
|
18 |
- on(events, handler, priority) { |
|
19 |
- const self = this; |
|
20 |
- if (typeof handler !== 'function') return self; |
|
21 |
- const method = priority ? 'unshift' : 'push'; |
|
22 |
- events.split(' ').forEach((event) => { |
|
23 |
- if (!self.eventsListeners[event]) self.eventsListeners[event] = []; |
|
24 |
- self.eventsListeners[event][method](handler); |
|
25 |
- }); |
|
26 |
- return self; |
|
27 |
- } |
|
28 |
- |
|
29 |
- once(events, handler, priority) { |
|
30 |
- const self = this; |
|
31 |
- if (typeof handler !== 'function') return self; |
|
32 |
- function onceHandler(...args) { |
|
33 |
- self.off(events, onceHandler); |
|
34 |
- if (onceHandler.f7proxy) { |
|
35 |
- delete onceHandler.f7proxy; |
|
36 |
- } |
|
37 |
- handler.apply(self, args); |
|
38 |
- } |
|
39 |
- onceHandler.f7proxy = handler; |
|
40 |
- return self.on(events, onceHandler, priority); |
|
41 |
- } |
|
42 |
- |
|
43 |
- off(events, handler) { |
|
44 |
- const self = this; |
|
45 |
- if (!self.eventsListeners) return self; |
|
46 |
- events.split(' ').forEach((event) => { |
|
47 |
- if (typeof handler === 'undefined') { |
|
48 |
- self.eventsListeners[event] = []; |
|
49 |
- } else if (self.eventsListeners[event] && self.eventsListeners[event].length) { |
|
50 |
- self.eventsListeners[event].forEach((eventHandler, index) => { |
|
51 |
- if (eventHandler === handler || (eventHandler.f7proxy && eventHandler.f7proxy === handler)) { |
|
52 |
- self.eventsListeners[event].splice(index, 1); |
|
53 |
- } |
|
54 |
- }); |
|
55 |
- } |
|
56 |
- }); |
|
57 |
- return self; |
|
58 |
- } |
|
59 |
- |
|
60 |
- emit(...args) { |
|
61 |
- const self = this; |
|
62 |
- if (!self.eventsListeners) return self; |
|
63 |
- let events; |
|
64 |
- let data; |
|
65 |
- let context; |
|
66 |
- if (typeof args[0] === 'string' || Array.isArray(args[0])) { |
|
67 |
- events = args[0]; |
|
68 |
- data = args.slice(1, args.length); |
|
69 |
- context = self; |
|
70 |
- } else { |
|
71 |
- events = args[0].events; |
|
72 |
- data = args[0].data; |
|
73 |
- context = args[0].context || self; |
|
74 |
- } |
|
75 |
- const eventsArray = Array.isArray(events) ? events : events.split(' '); |
|
76 |
- eventsArray.forEach((event) => { |
|
77 |
- if (self.eventsListeners && self.eventsListeners[event]) { |
|
78 |
- const handlers = []; |
|
79 |
- self.eventsListeners[event].forEach((eventHandler) => { |
|
80 |
- handlers.push(eventHandler); |
|
81 |
- }); |
|
82 |
- handlers.forEach((eventHandler) => { |
|
83 |
- eventHandler.apply(context, data); |
|
84 |
- }); |
|
85 |
- } |
|
86 |
- }); |
|
87 |
- return self; |
|
88 |
- } |
|
89 |
- |
|
90 |
- useModulesParams(instanceParams) { |
|
91 |
- const instance = this; |
|
92 |
- if (!instance.modules) return; |
|
93 |
- Object.keys(instance.modules).forEach((moduleName) => { |
|
94 |
- const module = instance.modules[moduleName]; |
|
95 |
- // Extend params |
|
96 |
- if (module.params) { |
|
97 |
- Utils.extend(instanceParams, module.params); |
|
98 |
- } |
|
99 |
- }); |
|
100 |
- } |
|
101 |
- |
|
102 |
- useModules(modulesParams = {}) { |
|
103 |
- const instance = this; |
|
104 |
- if (!instance.modules) return; |
|
105 |
- Object.keys(instance.modules).forEach((moduleName) => { |
|
106 |
- const module = instance.modules[moduleName]; |
|
107 |
- const moduleParams = modulesParams[moduleName] || {}; |
|
108 |
- // Extend instance methods and props |
|
109 |
- if (module.instance) { |
|
110 |
- Object.keys(module.instance).forEach((modulePropName) => { |
|
111 |
- const moduleProp = module.instance[modulePropName]; |
|
112 |
- if (typeof moduleProp === 'function') { |
|
113 |
- instance[modulePropName] = moduleProp.bind(instance); |
|
114 |
- } else { |
|
115 |
- instance[modulePropName] = moduleProp; |
|
116 |
- } |
|
117 |
- }); |
|
118 |
- } |
|
119 |
- // Add event listeners |
|
120 |
- if (module.on && instance.on) { |
|
121 |
- Object.keys(module.on).forEach((moduleEventName) => { |
|
122 |
- instance.on(moduleEventName, module.on[moduleEventName]); |
|
123 |
- }); |
|
124 |
- } |
|
125 |
- |
|
126 |
- // Module create callback |
|
127 |
- if (module.create) { |
|
128 |
- module.create.bind(instance)(moduleParams); |
|
129 |
- } |
|
130 |
- }); |
|
131 |
- } |
|
132 |
- |
|
133 |
- static set components(components) { |
|
134 |
- const Class = this; |
|
135 |
- if (!Class.use) return; |
|
136 |
- Class.use(components); |
|
137 |
- } |
|
138 |
- |
|
139 |
- static installModule(module, ...params) { |
|
140 |
- const Class = this; |
|
141 |
- if (!Class.prototype.modules) Class.prototype.modules = {}; |
|
142 |
- const name = module.name || (`${Object.keys(Class.prototype.modules).length}_${Utils.now()}`); |
|
143 |
- Class.prototype.modules[name] = module; |
|
144 |
- // Prototype |
|
145 |
- if (module.proto) { |
|
146 |
- Object.keys(module.proto).forEach((key) => { |
|
147 |
- Class.prototype[key] = module.proto[key]; |
|
148 |
- }); |
|
149 |
- } |
|
150 |
- // Class |
|
151 |
- if (module.static) { |
|
152 |
- Object.keys(module.static).forEach((key) => { |
|
153 |
- Class[key] = module.static[key]; |
|
154 |
- }); |
|
155 |
- } |
|
156 |
- // Callback |
|
157 |
- if (module.install) { |
|
158 |
- module.install.apply(Class, params); |
|
159 |
- } |
|
160 |
- return Class; |
|
161 |
- } |
|
162 |
- |
|
163 |
- static use(module, ...params) { |
|
164 |
- const Class = this; |
|
165 |
- if (Array.isArray(module)) { |
|
166 |
- module.forEach((m) => Class.installModule(m)); |
|
167 |
- return Class; |
|
168 |
- } |
|
169 |
- return Class.installModule(module, ...params); |
|
170 |
- } |
|
171 |
-} |
|
172 |
- |
|
173 |
-export default SwiperClass; |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,173 @@ |
1 |
+import Utils from './utils'; |
|
2 |
+ |
|
3 |
+class SwiperClass { |
|
4 |
+ constructor(params = {}) { |
|
5 |
+ const self = this; |
|
6 |
+ self.params = params; |
|
7 |
+ |
|
8 |
+ // Events |
|
9 |
+ self.eventsListeners = {}; |
|
10 |
+ |
|
11 |
+ if (self.params && self.params.on) { |
|
12 |
+ Object.keys(self.params.on).forEach((eventName) => { |
|
13 |
+ self.on(eventName, self.params.on[eventName]); |
|
14 |
+ }); |
|
15 |
+ } |
|
16 |
+ } |
|
17 |
+ |
|
18 |
+ on(events, handler, priority) { |
|
19 |
+ const self = this; |
|
20 |
+ if (typeof handler !== 'function') return self; |
|
21 |
+ const method = priority ? 'unshift' : 'push'; |
|
22 |
+ events.split(' ').forEach((event) => { |
|
23 |
+ if (!self.eventsListeners[event]) self.eventsListeners[event] = []; |
|
24 |
+ self.eventsListeners[event][method](handler); |
|
25 |
+ }); |
|
26 |
+ return self; |
|
27 |
+ } |
|
28 |
+ |
|
29 |
+ once(events, handler, priority) { |
|
30 |
+ const self = this; |
|
31 |
+ if (typeof handler !== 'function') return self; |
|
32 |
+ function onceHandler(...args) { |
|
33 |
+ self.off(events, onceHandler); |
|
34 |
+ if (onceHandler.f7proxy) { |
|
35 |
+ delete onceHandler.f7proxy; |
|
36 |
+ } |
|
37 |
+ handler.apply(self, args); |
|
38 |
+ } |
|
39 |
+ onceHandler.f7proxy = handler; |
|
40 |
+ return self.on(events, onceHandler, priority); |
|
41 |
+ } |
|
42 |
+ |
|
43 |
+ off(events, handler) { |
|
44 |
+ const self = this; |
|
45 |
+ if (!self.eventsListeners) return self; |
|
46 |
+ events.split(' ').forEach((event) => { |
|
47 |
+ if (typeof handler === 'undefined') { |
|
48 |
+ self.eventsListeners[event] = []; |
|
49 |
+ } else if (self.eventsListeners[event] && self.eventsListeners[event].length) { |
|
50 |
+ self.eventsListeners[event].forEach((eventHandler, index) => { |
|
51 |
+ if (eventHandler === handler || (eventHandler.f7proxy && eventHandler.f7proxy === handler)) { |
|
52 |
+ self.eventsListeners[event].splice(index, 1); |
|
53 |
+ } |
|
54 |
+ }); |
|
55 |
+ } |
|
56 |
+ }); |
|
57 |
+ return self; |
|
58 |
+ } |
|
59 |
+ |
|
60 |
+ emit(...args) { |
|
61 |
+ const self = this; |
|
62 |
+ if (!self.eventsListeners) return self; |
|
63 |
+ let events; |
|
64 |
+ let data; |
|
65 |
+ let context; |
|
66 |
+ if (typeof args[0] === 'string' || Array.isArray(args[0])) { |
|
67 |
+ events = args[0]; |
|
68 |
+ data = args.slice(1, args.length); |
|
69 |
+ context = self; |
|
70 |
+ } else { |
|
71 |
+ events = args[0].events; |
|
72 |
+ data = args[0].data; |
|
73 |
+ context = args[0].context || self; |
|
74 |
+ } |
|
75 |
+ const eventsArray = Array.isArray(events) ? events : events.split(' '); |
|
76 |
+ eventsArray.forEach((event) => { |
|
77 |
+ if (self.eventsListeners && self.eventsListeners[event]) { |
|
78 |
+ const handlers = []; |
|
79 |
+ self.eventsListeners[event].forEach((eventHandler) => { |
|
80 |
+ handlers.push(eventHandler); |
|
81 |
+ }); |
|
82 |
+ handlers.forEach((eventHandler) => { |
|
83 |
+ eventHandler.apply(context, data); |
|
84 |
+ }); |
|
85 |
+ } |
|
86 |
+ }); |
|
87 |
+ return self; |
|
88 |
+ } |
|
89 |
+ |
|
90 |
+ useModulesParams(instanceParams) { |
|
91 |
+ const instance = this; |
|
92 |
+ if (!instance.modules) return; |
|
93 |
+ Object.keys(instance.modules).forEach((moduleName) => { |
|
94 |
+ const module = instance.modules[moduleName]; |
|
95 |
+ // Extend params |
|
96 |
+ if (module.params) { |
|
97 |
+ Utils.extend(instanceParams, module.params); |
|
98 |
+ } |
|
99 |
+ }); |
|
100 |
+ } |
|
101 |
+ |
|
102 |
+ useModules(modulesParams = {}) { |
|
103 |
+ const instance = this; |
|
104 |
+ if (!instance.modules) return; |
|
105 |
+ Object.keys(instance.modules).forEach((moduleName) => { |
|
106 |
+ const module = instance.modules[moduleName]; |
|
107 |
+ const moduleParams = modulesParams[moduleName] || {}; |
|
108 |
+ // Extend instance methods and props |
|
109 |
+ if (module.instance) { |
|
110 |
+ Object.keys(module.instance).forEach((modulePropName) => { |
|
111 |
+ const moduleProp = module.instance[modulePropName]; |
|
112 |
+ if (typeof moduleProp === 'function') { |
|
113 |
+ instance[modulePropName] = moduleProp.bind(instance); |
|
114 |
+ } else { |
|
115 |
+ instance[modulePropName] = moduleProp; |
|
116 |
+ } |
|
117 |
+ }); |
|
118 |
+ } |
|
119 |
+ // Add event listeners |
|
120 |
+ if (module.on && instance.on) { |
|
121 |
+ Object.keys(module.on).forEach((moduleEventName) => { |
|
122 |
+ instance.on(moduleEventName, module.on[moduleEventName]); |
|
123 |
+ }); |
|
124 |
+ } |
|
125 |
+ |
|
126 |
+ // Module create callback |
|
127 |
+ if (module.create) { |
|
128 |
+ module.create.bind(instance)(moduleParams); |
|
129 |
+ } |
|
130 |
+ }); |
|
131 |
+ } |
|
132 |
+ |
|
133 |
+ static set components(components) { |
|
134 |
+ const Class = this; |
|
135 |
+ if (!Class.use) return; |
|
136 |
+ Class.use(components); |
|
137 |
+ } |
|
138 |
+ |
|
139 |
+ static installModule(module, ...params) { |
|
140 |
+ const Class = this; |
|
141 |
+ if (!Class.prototype.modules) Class.prototype.modules = {}; |
|
142 |
+ const name = module.name || (`${Object.keys(Class.prototype.modules).length}_${Utils.now()}`); |
|
143 |
+ Class.prototype.modules[name] = module; |
|
144 |
+ // Prototype |
|
145 |
+ if (module.proto) { |
|
146 |
+ Object.keys(module.proto).forEach((key) => { |
|
147 |
+ Class.prototype[key] = module.proto[key]; |
|
148 |
+ }); |
|
149 |
+ } |
|
150 |
+ // Class |
|
151 |
+ if (module.static) { |
|
152 |
+ Object.keys(module.static).forEach((key) => { |
|
153 |
+ Class[key] = module.static[key]; |
|
154 |
+ }); |
|
155 |
+ } |
|
156 |
+ // Callback |
|
157 |
+ if (module.install) { |
|
158 |
+ module.install.apply(Class, params); |
|
159 |
+ } |
|
160 |
+ return Class; |
|
161 |
+ } |
|
162 |
+ |
|
163 |
+ static use(module, ...params) { |
|
164 |
+ const Class = this; |
|
165 |
+ if (Array.isArray(module)) { |
|
166 |
+ module.forEach((m) => Class.installModule(m)); |
|
167 |
+ return Class; |
|
168 |
+ } |
|
169 |
+ return Class.installModule(module, ...params); |
|
170 |
+ } |
|
171 |
+} |
|
172 |
+ |
|
173 |
+export default SwiperClass; |