-
Notifications
You must be signed in to change notification settings - Fork 4
/
register.js
115 lines (101 loc) · 3.26 KB
/
register.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { isFunction, isString, isArray } from '@pluginjs/is'
import GlobalComponent from '@pluginjs/global-component'
import Pj from '@pluginjs/factory'
import { deepMerge, getUID } from '@pluginjs/utils'
import { getData, setData, removeData } from '@pluginjs/dom'
export default function register(name, obj = {}) {
return function(plugin) {
const { methods = [], dependencies = {}, ...others } = obj
Pj.register(
name,
Object.assign(plugin, {
methods: plugin.methods ? deepMerge(plugin.methods, methods) : methods,
dependencies: plugin.dependencies
? deepMerge(plugin.dependencies, dependencies)
: dependencies,
...others
})
)
let instances = []
plugin.prototype.plugin = name
plugin.getInstances = () => {
return instances
}
plugin.addInstance = instance => {
if (!(plugin.prototype instanceof GlobalComponent)) {
setData(name, instance, instance.element)
}
instance.instanceId = getUID(instance.plugin)
instances.push(instance)
}
plugin.removeInstance = instance => {
if (!(plugin.prototype instanceof GlobalComponent)) {
removeData(name, instance.element)
}
instances = instances.filter(i => i !== instance)
}
plugin.findInstanceByElement = el =>
instances.find(plugin => plugin.element === el)
if (plugin.prototype.resize && typeof plugin.resize === 'undefined') {
plugin.resize = function(documentWidth, documentHeight) {
for (let i = 0; i < instances.length; i++) {
instances[i].resize(documentWidth, documentHeight)
}
}
}
if (isFunction(plugin.resize)) {
Pj.emitter.on('resize', (e, documentWidth, documentHeight) => {
plugin.resize(documentWidth, documentHeight)
})
}
if (plugin.prototype instanceof GlobalComponent) {
Pj[name] = plugin
} else {
const elementParse = selector => {
if (typeof selector === 'string') {
return Array.from(document.querySelectorAll(selector))
}
if (
selector instanceof NodeList ||
selector instanceof HTMLCollection
) {
return Array.from(selector)
}
if (selector instanceof Node) {
return Array.of(selector)
}
if (isArray(selector)) {
return selector
}
return []
}
Pj[name] = (selector, options, ...args) => {
const elements = elementParse(selector)
if (!elements.length) {
throw new Error('Element is not exists.')
}
let results = []
if (isString(options)) {
if (!plugin.methods.includes(options)) {
throw new Error(`Method "${options}" is not exists on "${name}".`)
}
results = elements.map(el => {
const instance = getData(name, el)
if (instance instanceof plugin) {
return instance[options](...args)
}
return null
})
} else {
results = elements.map(el => plugin.of(el, options))
}
if (results.length === 1) {
return results[0]
}
return results
}
Object.setPrototypeOf(Pj[name], plugin)
}
return plugin
}
}