Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"sourceType": "module"
},
"rules": {
"indent": ["error", "tab"],
"no-console": "off",
"strict": ["error", "global"],
"curly": "warn",
Expand All @@ -26,12 +27,12 @@
"singleQuote": true,
"printWidth": 120,
"semi": false,
"useTabs": false
"useTabs": true
}
]
},
"globals": {
"jQuery": true,
"wp": true
}
}
}
4 changes: 2 additions & 2 deletions .stylelintrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"scss/at-if-closing-brace-space-after": null,
"no-invalid-position-at-import-rule": null,
"custom-property-pattern": null,
"indentation": 4,
"indentation": "tab",
"number-leading-zero": "never",
"string-quotes": "double",
"length-zero-no-unit": [
Expand All @@ -43,4 +43,4 @@
}
}
]
}
}
218 changes: 109 additions & 109 deletions src/js/classes/AbstractDomElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,127 +31,127 @@
import extend from '../utils/extend.js'

class AbstractDomElement {
constructor(element, options) {
let oldInstance

// provide an explicit spaceName to prevent conflict after minification
// MaClass.nameSpace = 'MaClass'
this.constructor.nameSpace = this.constructor.nameSpace || this.constructor.name
const nameSpace = this.constructor.nameSpace

// if no spacename beapi, create it - avoid futur test
if (!element.beapi) {
element.beapi = {}
}

oldInstance = element.beapi[nameSpace]

if (oldInstance) {
console.warn(
'[AbstractDomElement] more than 1 class is initialised with the same name space on :',
element,
oldInstance
)
oldInstance._isNewInstance = false
return oldInstance
}

this._element = element
this._settings = extend(true, {}, this.constructor.defaults, options)
this._element.beapi[nameSpace] = this
this._isNewInstance = true
}

isNewInstance() {
return this._isNewInstance
}

destroy() {
this._element.beapi[this.constructor.nameSpace] = undefined
return this
}

static init(element, options) {
foreach(element, (el) => {
new this(el, options)
})

return this
}

static hasInstance(element) {
const el = getDomElement(element)
return el && el.beapi && !!el.beapi[this.nameSpace]
}

static getInstance(element) {
const el = getDomElement(element)
return el && el.beapi ? el.beapi[this.nameSpace] : undefined
}

static destroy(element) {
this.foreach(element, (el) => {
if (el.beapi && el.beapi[this.nameSpace]) {
el.beapi[this.nameSpace].destroy()
}
})

return this
}

static foreach(element, callback) {
foreach(element, (el) => {
if (el.beapi && el.beapi[this.nameSpace]) {
callback(el)
}
})

return this
}

static initFromPreset() {
const preset = this.preset
let selector

for (selector in preset) {
this.init(selector, preset[selector])
}

return this
}

static destroyFromPreset() {
const preset = this.preset
let selector

for (selector in preset) {
this.destroy(selector)
}

return this
}
constructor(element, options) {
let oldInstance

// provide an explicit spaceName to prevent conflict after minification
// MaClass.nameSpace = 'MaClass'
this.constructor.nameSpace = this.constructor.nameSpace || this.constructor.name
const nameSpace = this.constructor.nameSpace

// if no spacename beapi, create it - avoid futur test
if (!element.beapi) {
element.beapi = {}
}

oldInstance = element.beapi[nameSpace]

if (oldInstance) {
console.warn(
'[AbstractDomElement] more than 1 class is initialised with the same name space on :',
element,
oldInstance
)
oldInstance._isNewInstance = false
return oldInstance
}

this._element = element
this._settings = extend(true, {}, this.constructor.defaults, options)
this._element.beapi[nameSpace] = this
this._isNewInstance = true
}

isNewInstance() {
return this._isNewInstance
}

destroy() {
this._element.beapi[this.constructor.nameSpace] = undefined
return this
}

static init(element, options) {
foreach(element, (el) => {
new this(el, options)
})

return this
}

static hasInstance(element) {
const el = getDomElement(element)
return el && el.beapi && !!el.beapi[this.nameSpace]
}

static getInstance(element) {
const el = getDomElement(element)
return el && el.beapi ? el.beapi[this.nameSpace] : undefined
}

static destroy(element) {
this.foreach(element, (el) => {
if (el.beapi && el.beapi[this.nameSpace]) {
el.beapi[this.nameSpace].destroy()
}
})

return this
}

static foreach(element, callback) {
foreach(element, (el) => {
if (el.beapi && el.beapi[this.nameSpace]) {
callback(el)
}
})

return this
}

static initFromPreset() {
const preset = this.preset
let selector

for (selector in preset) {
this.init(selector, preset[selector])
}

return this
}

static destroyFromPreset() {
const preset = this.preset
let selector

for (selector in preset) {
this.destroy(selector)
}

return this
}
}

// ----
// utils
// ----
function foreach(element, callback) {
const el = getDomElements(element)
let i

for (i = 0; i < el.length; i++) {
if (callback(el[i]) === false) {
break
}
}
const el = getDomElements(element)
let i

for (i = 0; i < el.length; i++) {
if (callback(el[i]) === false) {
break
}
}
}

function getDomElements(element) {
return typeof element === 'string' ? document.querySelectorAll(element) : element.length >= 0 ? element : [element]
return typeof element === 'string' ? document.querySelectorAll(element) : element.length >= 0 ? element : [element]
}

function getDomElement(element) {
return getDomElements(element)[0]
return getDomElements(element)[0]
}

// ----
Expand Down
Loading
Loading