|
|
@ -1,10 +1,10 @@ |
|
|
import { isUndefined, isArray } from 'underscore'; |
|
|
import { isUndefined, isArray, isEmpty, has, clone } from 'underscore'; |
|
|
import Styleable from 'domain_abstract/model/Styleable'; |
|
|
import Styleable from 'domain_abstract/model/Styleable'; |
|
|
|
|
|
|
|
|
var Backbone = require('backbone'); |
|
|
const Backbone = require('backbone'); |
|
|
var Components = require('./Components'); |
|
|
const Components = require('./Components'); |
|
|
var Selectors = require('selector_manager/model/Selectors'); |
|
|
const Selectors = require('selector_manager/model/Selectors'); |
|
|
var Traits = require('trait_manager/model/Traits'); |
|
|
const Traits = require('trait_manager/model/Traits'); |
|
|
|
|
|
|
|
|
const escapeRegExp = (str) => { |
|
|
const escapeRegExp = (str) => { |
|
|
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'); |
|
|
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'); |
|
|
@ -121,7 +121,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialize(props = {}, opt = {}) { |
|
|
initialize(props = {}, opt = {}) { |
|
|
const em = opt.sm || opt.em || {}; |
|
|
const em = opt.sm || opt.em || ''; |
|
|
|
|
|
|
|
|
// Propagate properties from parent if indicated
|
|
|
// Propagate properties from parent if indicated
|
|
|
const parent = this.parent(); |
|
|
const parent = this.parent(); |
|
|
@ -150,7 +150,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({ |
|
|
this.opt = opt; |
|
|
this.opt = opt; |
|
|
this.sm = em; |
|
|
this.sm = em; |
|
|
this.em = em; |
|
|
this.em = em; |
|
|
this.config = props; |
|
|
this.config = opt.config || {}; |
|
|
this.set('attributes', this.get('attributes') || {}); |
|
|
this.set('attributes', this.get('attributes') || {}); |
|
|
this.listenTo(this, 'change:script', this.scriptUpdated); |
|
|
this.listenTo(this, 'change:script', this.scriptUpdated); |
|
|
this.listenTo(this, 'change:traits', this.traitsUpdated); |
|
|
this.listenTo(this, 'change:traits', this.traitsUpdated); |
|
|
@ -158,18 +158,6 @@ module.exports = Backbone.Model.extend(Styleable).extend({ |
|
|
this.initClasses(); |
|
|
this.initClasses(); |
|
|
this.initComponents(); |
|
|
this.initComponents(); |
|
|
this.initToolbar(); |
|
|
this.initToolbar(); |
|
|
|
|
|
|
|
|
// Normalize few properties from strings to arrays
|
|
|
|
|
|
var toNormalize = ['stylable']; |
|
|
|
|
|
toNormalize.forEach(function(name) { |
|
|
|
|
|
var value = this.get(name); |
|
|
|
|
|
|
|
|
|
|
|
if (typeof value == 'string') { |
|
|
|
|
|
var newValue = value.split(',').map(prop => prop.trim()); |
|
|
|
|
|
this.set(name, newValue); |
|
|
|
|
|
} |
|
|
|
|
|
}, this); |
|
|
|
|
|
|
|
|
|
|
|
this.set('status', ''); |
|
|
this.set('status', ''); |
|
|
this.init(); |
|
|
this.init(); |
|
|
}, |
|
|
}, |
|
|
@ -198,12 +186,58 @@ module.exports = Backbone.Model.extend(Styleable).extend({ |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getStyle() { |
|
|
|
|
|
const em = this.em; |
|
|
|
|
|
|
|
|
|
|
|
if (em && em.getConfig('avoidInlineStyle')) { |
|
|
|
|
|
const state = this.get('state'); |
|
|
|
|
|
const cc = em.get('CssComposer'); |
|
|
|
|
|
const rule = cc.getIdRule(this.getId(), { state }); |
|
|
|
|
|
this.rule = rule; |
|
|
|
|
|
|
|
|
|
|
|
if (rule) { |
|
|
|
|
|
return rule.getStyle(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return Styleable.getStyle.call(this); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setStyle(prop = {}, opts = {}) { |
|
|
|
|
|
const em = this.em; |
|
|
|
|
|
|
|
|
|
|
|
if (em && em.getConfig('avoidInlineStyle')) { |
|
|
|
|
|
prop = Styleable.setStyle.call(this, prop, {silent: 1, avoidStore: 1}); |
|
|
|
|
|
const state = this.get('state'); |
|
|
|
|
|
const cc = em.get('CssComposer'); |
|
|
|
|
|
this.rule = cc.setIdRule(this.getId(), prop, { ...opts, state }); |
|
|
|
|
|
} else { |
|
|
|
|
|
prop = Styleable.setStyle.apply(this, arguments); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return prop; |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Return attributes |
|
|
* Return attributes |
|
|
* @return {Object} |
|
|
* @return {Object} |
|
|
*/ |
|
|
*/ |
|
|
getAttributes() { |
|
|
getAttributes() { |
|
|
return this.get('attributes'); |
|
|
const classes = []; |
|
|
|
|
|
const attributes = this.get('attributes') || {}; |
|
|
|
|
|
|
|
|
|
|
|
// Add classes
|
|
|
|
|
|
this.get('classes').each(cls => classes.push(cls.get('name'))); |
|
|
|
|
|
classes.length && (attributes.class = classes.join(' ')); |
|
|
|
|
|
|
|
|
|
|
|
// If style is not empty I need an ID attached to the component
|
|
|
|
|
|
if (!isEmpty(this.getStyle()) && !has(attributes, 'id')) { |
|
|
|
|
|
attributes.id = this.getId(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return attributes; |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -240,7 +274,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initClasses() { |
|
|
initClasses() { |
|
|
const classes = this.normalizeClasses(this.get('classes') || this.config.classes || []); |
|
|
const classes = this.normalizeClasses(this.get('classes') || []); |
|
|
this.set('classes', new Selectors(classes)); |
|
|
this.set('classes', new Selectors(classes)); |
|
|
return this; |
|
|
return this; |
|
|
}, |
|
|
}, |
|
|
@ -329,7 +363,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({ |
|
|
*/ |
|
|
*/ |
|
|
traitsUpdated() { |
|
|
traitsUpdated() { |
|
|
let found = 0; |
|
|
let found = 0; |
|
|
const attrs = Object.assign({}, this.get('attributes')); |
|
|
const attrs = { ...this.get('attributes') }; |
|
|
const traits = this.get('traits'); |
|
|
const traits = this.get('traits'); |
|
|
|
|
|
|
|
|
if (!(traits instanceof Traits)) { |
|
|
if (!(traits instanceof Traits)) { |
|
|
@ -442,21 +476,21 @@ module.exports = Backbone.Model.extend(Styleable).extend({ |
|
|
* @private |
|
|
* @private |
|
|
*/ |
|
|
*/ |
|
|
clone(reset) { |
|
|
clone(reset) { |
|
|
var attr = _.clone(this.attributes), |
|
|
const em = this.em; |
|
|
comp = this.get('components'), |
|
|
const style = this.getStyle(); |
|
|
traits = this.get('traits'), |
|
|
const attr = clone(this.attributes); |
|
|
cls = this.get('classes'); |
|
|
delete attr.attributes.id; |
|
|
attr.components = []; |
|
|
attr.components = []; |
|
|
attr.classes = []; |
|
|
attr.classes = []; |
|
|
attr.traits = []; |
|
|
attr.traits = []; |
|
|
|
|
|
|
|
|
comp.each((md, i) => { |
|
|
this.get('components').each((md, i) => { |
|
|
attr.components[i] = md.clone(1); |
|
|
attr.components[i] = md.clone(1); |
|
|
}); |
|
|
}); |
|
|
traits.each((md, i) => { |
|
|
this.get('traits').each((md, i) => { |
|
|
attr.traits[i] = md.clone(); |
|
|
attr.traits[i] = md.clone(); |
|
|
}); |
|
|
}); |
|
|
cls.each((md, i) => { |
|
|
this.get('classes').each((md, i) => { |
|
|
attr.classes[i] = md.get('name'); |
|
|
attr.classes[i] = md.get('name'); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
@ -467,6 +501,10 @@ module.exports = Backbone.Model.extend(Styleable).extend({ |
|
|
this.opt.collection = null; |
|
|
this.opt.collection = null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (em && em.getConfig('avoidInlineStyle') && !isEmpty(style)) { |
|
|
|
|
|
attr.style = style; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return new this.constructor(attr, this.opt); |
|
|
return new this.constructor(attr, this.opt); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
@ -502,43 +540,25 @@ module.exports = Backbone.Model.extend(Styleable).extend({ |
|
|
* @private |
|
|
* @private |
|
|
*/ |
|
|
*/ |
|
|
toHTML(opts) { |
|
|
toHTML(opts) { |
|
|
var code = ''; |
|
|
const model = this; |
|
|
var m = this; |
|
|
const attrs = []; |
|
|
var tag = m.get('tagName'); |
|
|
const classes = []; |
|
|
var idFound = 0; |
|
|
const tag = model.get('tagName'); |
|
|
var sTag = m.get('void'); |
|
|
const sTag = model.get('void'); |
|
|
var attrId = ''; |
|
|
const attributes = this.getAttrToHTML(); |
|
|
var strAttr = ''; |
|
|
|
|
|
var attr = this.getAttrToHTML(); |
|
|
for (let attr in attributes) { |
|
|
|
|
|
const value = attributes[attr]; |
|
|
for (var prop in attr) { |
|
|
|
|
|
if (prop == 'id') { |
|
|
if (!isUndefined(value) && value !== '') { |
|
|
idFound = 1; |
|
|
attrs.push(`${attr}="${value}"`); |
|
|
} |
|
|
} |
|
|
var val = attr[prop]; |
|
|
|
|
|
strAttr += typeof val !== undefined && val !== '' ? |
|
|
|
|
|
' ' + prop + '="' + val + '"' : ''; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Build the string of classes
|
|
|
let attrString = attrs.length ? ` ${attrs.join(' ')}` : ''; |
|
|
var strCls = ''; |
|
|
let code = `<${tag}${attrString}${sTag ? '/' : ''}>${model.get('content')}`; |
|
|
m.get('classes').each(m => { |
|
|
model.get('components').each(comp => code += comp.toHTML()); |
|
|
strCls += ' ' + m.get('name'); |
|
|
!sTag && (code += `</${tag}>`); |
|
|
}); |
|
|
|
|
|
strCls = strCls !== '' ? ' class="' + strCls.trim() + '"' : ''; |
|
|
|
|
|
|
|
|
|
|
|
// If style is not empty I need an ID attached to the component
|
|
|
|
|
|
if(!_.isEmpty(m.get('style')) && !idFound) |
|
|
|
|
|
attrId = ' id="' + m.getId() + '" '; |
|
|
|
|
|
|
|
|
|
|
|
code += '<' + tag + strCls + attrId + strAttr + (sTag ? '/' : '') + '>' + m.get('content'); |
|
|
|
|
|
|
|
|
|
|
|
m.get('components').each(m => { |
|
|
|
|
|
code += m.toHTML(); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if(!sTag) |
|
|
|
|
|
code += '</'+tag+'>'; |
|
|
|
|
|
|
|
|
|
|
|
return code; |
|
|
return code; |
|
|
}, |
|
|
}, |
|
|
@ -550,7 +570,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({ |
|
|
* @private |
|
|
* @private |
|
|
*/ |
|
|
*/ |
|
|
getAttrToHTML() { |
|
|
getAttrToHTML() { |
|
|
var attr = this.get('attributes') || {}; |
|
|
var attr = this.getAttributes(); |
|
|
delete attr.style; |
|
|
delete attr.style; |
|
|
return attr; |
|
|
return attr; |
|
|
}, |
|
|
}, |
|
|
|