|
|
@ -1,5 +1,6 @@ |
|
|
import { result, forEach, keys } from 'underscore'; |
|
|
import { result, forEach, keys } from "underscore"; |
|
|
import { Model } from '../../common'; |
|
|
import { Model } from "../../common"; |
|
|
|
|
|
import EditorModel from "../../editor/model/Editor"; |
|
|
|
|
|
|
|
|
const TYPE_CLASS = 1; |
|
|
const TYPE_CLASS = 1; |
|
|
const TYPE_ID = 2; |
|
|
const TYPE_ID = 2; |
|
|
@ -16,8 +17,8 @@ const TYPE_ID = 2; |
|
|
export default class Selector extends Model { |
|
|
export default class Selector extends Model { |
|
|
defaults() { |
|
|
defaults() { |
|
|
return { |
|
|
return { |
|
|
name: '', |
|
|
name: "", |
|
|
label: '', |
|
|
label: "", |
|
|
type: TYPE_CLASS, |
|
|
type: TYPE_CLASS, |
|
|
active: true, |
|
|
active: true, |
|
|
private: false, |
|
|
private: false, |
|
|
@ -26,43 +27,52 @@ export default class Selector extends Model { |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
initialize(props, opts = {}) { |
|
|
// Type selectors: https://developer.mozilla.org/it/docs/Web/CSS/CSS_Selectors
|
|
|
|
|
|
static readonly TYPE_CLASS = TYPE_CLASS; |
|
|
|
|
|
static readonly TYPE_ID = TYPE_ID; |
|
|
|
|
|
|
|
|
|
|
|
em: EditorModel; |
|
|
|
|
|
|
|
|
|
|
|
constructor(props: any, opts: any = {}) { |
|
|
|
|
|
super(props, opts); |
|
|
const { config = {} } = opts; |
|
|
const { config = {} } = opts; |
|
|
const name = this.get('name'); |
|
|
const name = this.get("name"); |
|
|
const label = this.get('label'); |
|
|
const label = this.get("label"); |
|
|
|
|
|
|
|
|
if (!name) { |
|
|
if (!name) { |
|
|
this.set('name', label); |
|
|
this.set("name", label); |
|
|
} else if (!label) { |
|
|
} else if (!label) { |
|
|
this.set('label', name); |
|
|
this.set("label", name); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const namePreEsc = this.get('name'); |
|
|
const namePreEsc = this.get("name"); |
|
|
const { escapeName } = config; |
|
|
const { escapeName } = config; |
|
|
const nameEsc = escapeName ? escapeName(namePreEsc) : Selector.escapeName(namePreEsc); |
|
|
const nameEsc = escapeName |
|
|
this.set('name', nameEsc); |
|
|
? escapeName(namePreEsc) |
|
|
this.em = config.em; |
|
|
: Selector.escapeName(namePreEsc); |
|
|
|
|
|
this.set("name", nameEsc); |
|
|
|
|
|
this.em = opts.em; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
isId() { |
|
|
isId() { |
|
|
return this.get('type') === TYPE_ID; |
|
|
return this.get("type") === TYPE_ID; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
isClass() { |
|
|
isClass() { |
|
|
return this.get('type') === TYPE_CLASS; |
|
|
return this.get("type") === TYPE_CLASS; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
getFullName(opts = {}) { |
|
|
getFullName(opts: any = {}) { |
|
|
const { escape } = opts; |
|
|
const { escape } = opts; |
|
|
const name = this.get('name'); |
|
|
const name = this.get("name"); |
|
|
let pfx = ''; |
|
|
let pfx = ""; |
|
|
|
|
|
|
|
|
switch (this.get('type')) { |
|
|
switch (this.get("type")) { |
|
|
case TYPE_CLASS: |
|
|
case TYPE_CLASS: |
|
|
pfx = '.'; |
|
|
pfx = "."; |
|
|
break; |
|
|
break; |
|
|
case TYPE_ID: |
|
|
case TYPE_ID: |
|
|
pfx = '#'; |
|
|
pfx = "#"; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -90,7 +100,7 @@ export default class Selector extends Model { |
|
|
* // -> `My selector`
|
|
|
* // -> `My selector`
|
|
|
*/ |
|
|
*/ |
|
|
getLabel() { |
|
|
getLabel() { |
|
|
return this.get('label'); |
|
|
return this.get("label"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -102,30 +112,30 @@ export default class Selector extends Model { |
|
|
* console.log(selector.getLabel()); |
|
|
* console.log(selector.getLabel()); |
|
|
* // -> `New Label`
|
|
|
* // -> `New Label`
|
|
|
*/ |
|
|
*/ |
|
|
setLabel(label) { |
|
|
setLabel(label: string) { |
|
|
return this.set('label', label); |
|
|
return this.set("label", label); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Get selector active state. |
|
|
* Get selector active state. |
|
|
* @returns {Boolean} |
|
|
* @returns {Boolean} |
|
|
*/ |
|
|
*/ |
|
|
getActive() { |
|
|
getActive(): boolean { |
|
|
return this.get('active'); |
|
|
return this.get("active"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Update selector active state. |
|
|
* Update selector active state. |
|
|
* @param {Boolean} value New active state |
|
|
* @param {Boolean} value New active state |
|
|
*/ |
|
|
*/ |
|
|
setActive(value) { |
|
|
setActive(value: boolean) { |
|
|
return this.set('active', value); |
|
|
return this.set("active", value); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
toJSON(opts = {}) { |
|
|
toJSON(opts = {}) { |
|
|
const { em } = this; |
|
|
const { em } = this; |
|
|
let obj = Model.prototype.toJSON.call(this, [opts]); |
|
|
let obj = Model.prototype.toJSON.call(this, [opts]); |
|
|
const defaults = result(this, 'defaults'); |
|
|
const defaults = result(this, "defaults"); |
|
|
|
|
|
|
|
|
if (em && em.getConfig().avoidDefaults) { |
|
|
if (em && em.getConfig().avoidDefaults) { |
|
|
forEach(defaults, (value, key) => { |
|
|
forEach(defaults, (value, key) => { |
|
|
@ -151,20 +161,16 @@ export default class Selector extends Model { |
|
|
|
|
|
|
|
|
return obj; |
|
|
return obj; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Selector.prototype.idAttribute = 'name'; |
|
|
/** |
|
|
|
|
|
* Escape string |
|
|
// Type selectors: https://developer.mozilla.org/it/docs/Web/CSS/CSS_Selectors
|
|
|
* @param {string} name |
|
|
Selector.TYPE_CLASS = TYPE_CLASS; |
|
|
* @return {string} |
|
|
Selector.TYPE_ID = TYPE_ID; |
|
|
* @public |
|
|
|
|
|
*/ |
|
|
|
|
|
static escapeName(name: string) { |
|
|
|
|
|
return `${name}`.trim().replace(/([^a-z0-9\w-\:]+)/gi, "-"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
Selector.prototype.idAttribute = "name"; |
|
|
* Escape string |
|
|
|
|
|
* @param {string} name |
|
|
|
|
|
* @return {string} |
|
|
|
|
|
* @private |
|
|
|
|
|
*/ |
|
|
|
|
|
Selector.escapeName = name => { |
|
|
|
|
|
return `${name}`.trim().replace(/([^a-z0-9\w-\:]+)/gi, '-'); |
|
|
|
|
|
}; |
|
|
|