|
31 | 31 | import extend from '../utils/extend.js' |
32 | 32 |
|
33 | 33 | class AbstractDomElement { |
34 | | - constructor(element, options) { |
35 | | - let oldInstance |
36 | | - |
37 | | - // provide an explicit spaceName to prevent conflict after minification |
38 | | - // MaClass.nameSpace = 'MaClass' |
39 | | - this.constructor.nameSpace = this.constructor.nameSpace || this.constructor.name |
40 | | - const nameSpace = this.constructor.nameSpace |
41 | | - |
42 | | - // if no spacename beapi, create it - avoid futur test |
43 | | - if (!element.beapi) { |
44 | | - element.beapi = {} |
45 | | - } |
46 | | - |
47 | | - oldInstance = element.beapi[nameSpace] |
48 | | - |
49 | | - if (oldInstance) { |
50 | | - console.warn( |
51 | | - '[AbstractDomElement] more than 1 class is initialised with the same name space on :', |
52 | | - element, |
53 | | - oldInstance |
54 | | - ) |
55 | | - oldInstance._isNewInstance = false |
56 | | - return oldInstance |
57 | | - } |
58 | | - |
59 | | - this._element = element |
60 | | - this._settings = extend(true, {}, this.constructor.defaults, options) |
61 | | - this._element.beapi[nameSpace] = this |
62 | | - this._isNewInstance = true |
63 | | - } |
64 | | - |
65 | | - isNewInstance() { |
66 | | - return this._isNewInstance |
67 | | - } |
68 | | - |
69 | | - destroy() { |
70 | | - this._element.beapi[this.constructor.nameSpace] = undefined |
71 | | - return this |
72 | | - } |
73 | | - |
74 | | - static init(element, options) { |
75 | | - foreach(element, (el) => { |
76 | | - new this(el, options) |
77 | | - }) |
78 | | - |
79 | | - return this |
80 | | - } |
81 | | - |
82 | | - static hasInstance(element) { |
83 | | - const el = getDomElement(element) |
84 | | - return el && el.beapi && !!el.beapi[this.nameSpace] |
85 | | - } |
86 | | - |
87 | | - static getInstance(element) { |
88 | | - const el = getDomElement(element) |
89 | | - return el && el.beapi ? el.beapi[this.nameSpace] : undefined |
90 | | - } |
91 | | - |
92 | | - static destroy(element) { |
93 | | - this.foreach(element, (el) => { |
94 | | - if (el.beapi && el.beapi[this.nameSpace]) { |
95 | | - el.beapi[this.nameSpace].destroy() |
96 | | - } |
97 | | - }) |
98 | | - |
99 | | - return this |
100 | | - } |
101 | | - |
102 | | - static foreach(element, callback) { |
103 | | - foreach(element, (el) => { |
104 | | - if (el.beapi && el.beapi[this.nameSpace]) { |
105 | | - callback(el) |
106 | | - } |
107 | | - }) |
108 | | - |
109 | | - return this |
110 | | - } |
111 | | - |
112 | | - static initFromPreset() { |
113 | | - const preset = this.preset |
114 | | - let selector |
115 | | - |
116 | | - for (selector in preset) { |
117 | | - this.init(selector, preset[selector]) |
118 | | - } |
119 | | - |
120 | | - return this |
121 | | - } |
122 | | - |
123 | | - static destroyFromPreset() { |
124 | | - const preset = this.preset |
125 | | - let selector |
126 | | - |
127 | | - for (selector in preset) { |
128 | | - this.destroy(selector) |
129 | | - } |
130 | | - |
131 | | - return this |
132 | | - } |
| 34 | + constructor(element, options) { |
| 35 | + let oldInstance |
| 36 | + |
| 37 | + // provide an explicit spaceName to prevent conflict after minification |
| 38 | + // MaClass.nameSpace = 'MaClass' |
| 39 | + this.constructor.nameSpace = this.constructor.nameSpace || this.constructor.name |
| 40 | + const { nameSpace } = this.constructor |
| 41 | + |
| 42 | + // if no spacename beapi, create it - avoid futur test |
| 43 | + if (!element.beapi) { |
| 44 | + element.beapi = {} |
| 45 | + } |
| 46 | + |
| 47 | + oldInstance = element.beapi[nameSpace] |
| 48 | + |
| 49 | + if (oldInstance) { |
| 50 | + console.warn( |
| 51 | + '[AbstractDomElement] more than 1 class is initialised with the same name space on :', |
| 52 | + element, |
| 53 | + oldInstance |
| 54 | + ) |
| 55 | + oldInstance._isNewInstance = false |
| 56 | + return oldInstance |
| 57 | + } |
| 58 | + |
| 59 | + this._element = element |
| 60 | + this._settings = extend(true, {}, this.constructor.defaults, options) |
| 61 | + this._element.beapi[nameSpace] = this |
| 62 | + this._isNewInstance = true |
| 63 | + } |
| 64 | + |
| 65 | + isNewInstance() { |
| 66 | + return this._isNewInstance |
| 67 | + } |
| 68 | + |
| 69 | + destroy() { |
| 70 | + this._element.beapi[this.constructor.nameSpace] = undefined |
| 71 | + return this |
| 72 | + } |
| 73 | + |
| 74 | + static init(element, options) { |
| 75 | + foreach(element, (el) => { |
| 76 | + new this(el, options) |
| 77 | + }) |
| 78 | + |
| 79 | + return this |
| 80 | + } |
| 81 | + |
| 82 | + static hasInstance(element) { |
| 83 | + const el = getDomElement(element) |
| 84 | + return el && el.beapi && !!el.beapi[this.nameSpace] |
| 85 | + } |
| 86 | + |
| 87 | + static getInstance(element) { |
| 88 | + const el = getDomElement(element) |
| 89 | + return el && el.beapi ? el.beapi[this.nameSpace] : undefined |
| 90 | + } |
| 91 | + |
| 92 | + static destroy(element) { |
| 93 | + this.foreach(element, (el) => { |
| 94 | + if (el.beapi && el.beapi[this.nameSpace]) { |
| 95 | + el.beapi[this.nameSpace].destroy() |
| 96 | + } |
| 97 | + }) |
| 98 | + |
| 99 | + return this |
| 100 | + } |
| 101 | + |
| 102 | + static foreach(element, callback) { |
| 103 | + foreach(element, (el) => { |
| 104 | + if (el.beapi && el.beapi[this.nameSpace]) { |
| 105 | + callback(el) |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + return this |
| 110 | + } |
| 111 | + |
| 112 | + static initFromPreset() { |
| 113 | + const { preset } = this |
| 114 | + let selector |
| 115 | + |
| 116 | + for (selector in preset) { |
| 117 | + this.init(selector, preset[selector]) |
| 118 | + } |
| 119 | + |
| 120 | + return this |
| 121 | + } |
| 122 | + |
| 123 | + static destroyFromPreset() { |
| 124 | + const { preset } = this |
| 125 | + let selector |
| 126 | + |
| 127 | + for (selector in preset) { |
| 128 | + this.destroy(selector) |
| 129 | + } |
| 130 | + |
| 131 | + return this |
| 132 | + } |
133 | 133 | } |
134 | 134 |
|
135 | 135 | // ---- |
136 | 136 | // utils |
137 | 137 | // ---- |
138 | 138 | function foreach(element, callback) { |
139 | | - const el = getDomElements(element) |
140 | | - let i |
141 | | - |
142 | | - for (i = 0; i < el.length; i++) { |
143 | | - if (callback(el[i]) === false) { |
144 | | - break |
145 | | - } |
146 | | - } |
| 139 | + const el = getDomElements(element) |
| 140 | + let i |
| 141 | + |
| 142 | + for (i = 0; i < el.length; i++) { |
| 143 | + if (callback(el[i]) === false) { |
| 144 | + break |
| 145 | + } |
| 146 | + } |
147 | 147 | } |
148 | 148 |
|
149 | 149 | function getDomElements(element) { |
150 | | - return typeof element === 'string' ? document.querySelectorAll(element) : element.length >= 0 ? element : [element] |
| 150 | + return typeof element === 'string' ? document.querySelectorAll(element) : element.length >= 0 ? element : [element] |
151 | 151 | } |
152 | 152 |
|
153 | 153 | function getDomElement(element) { |
154 | | - return getDomElements(element)[0] |
| 154 | + return getDomElements(element)[0] |
155 | 155 | } |
156 | 156 |
|
157 | 157 | // ---- |
|
0 commit comments