Browse Source

Improve `toHTML` and `getAttributes` in Component

pull/540/head
Artur Arseniev 9 years ago
parent
commit
4152c32e19
  1. 68
      src/dom_components/model/Component.js
  2. 2
      test/specs/code_manager/model/CodeModels.js
  3. 1
      test/specs/dom_components/model/Component.js

68
src/dom_components/model/Component.js

@ -1,4 +1,4 @@
import { isUndefined, isArray } from 'underscore'; import { isUndefined, isArray, isEmpty, has } from 'underscore';
import Styleable from 'domain_abstract/model/Styleable'; import Styleable from 'domain_abstract/model/Styleable';
const Backbone = require('backbone'); const Backbone = require('backbone');
@ -219,7 +219,19 @@ module.exports = Backbone.Model.extend(Styleable).extend({
* @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.get('style')) && !has(attributes, 'id')) {
attributes.id = this.getId();
}
return attributes;
}, },
@ -518,43 +530,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;
}, },
@ -566,7 +560,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;
}, },

2
test/specs/code_manager/model/CodeModels.js

@ -54,7 +54,7 @@ module.exports = {
['class1', 'class2'].forEach(item => { ['class1', 'class2'].forEach(item => {
m1.get('classes').add({name: item}); m1.get('classes').add({name: item});
}); });
expect(obj.build(comp)).toEqual('<article class="class1 class2" data-test1="value1" data-test2="value2"></article>'); expect(obj.build(comp)).toEqual('<article data-test1="value1" data-test2="value2" class="class1 class2"></article>');
}); });
}); });

1
test/specs/dom_components/model/Component.js

@ -169,6 +169,7 @@ module.exports = {
}); });
expect(obj.getAttributes()).toEqual({ expect(obj.getAttributes()).toEqual({
id: 'test', id: 'test',
class: 'class1 class2',
'data-test': 'value', 'data-test': 'value',
}); });
expect(obj.get('classes').length).toEqual(2); expect(obj.get('classes').length).toEqual(2);

Loading…
Cancel
Save