From 4152c32e193c394ec599eba55c45e339cbd4fca1 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sun, 12 Nov 2017 20:32:03 +0100 Subject: [PATCH] Improve `toHTML` and `getAttributes` in Component --- src/dom_components/model/Component.js | 68 +++++++++----------- test/specs/code_manager/model/CodeModels.js | 2 +- test/specs/dom_components/model/Component.js | 1 + 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/dom_components/model/Component.js b/src/dom_components/model/Component.js index ec33be835..dea228859 100644 --- a/src/dom_components/model/Component.js +++ b/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'; const Backbone = require('backbone'); @@ -219,7 +219,19 @@ module.exports = Backbone.Model.extend(Styleable).extend({ * @return {Object} */ 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 */ toHTML(opts) { - var code = ''; - var m = this; - var tag = m.get('tagName'); - var idFound = 0; - var sTag = m.get('void'); - var attrId = ''; - var strAttr = ''; - var attr = this.getAttrToHTML(); - - for (var prop in attr) { - if (prop == 'id') { - idFound = 1; + const model = this; + const attrs = []; + const classes = []; + const tag = model.get('tagName'); + const sTag = model.get('void'); + const attributes = this.getAttrToHTML(); + + for (let attr in attributes) { + const value = attributes[attr]; + + if (!isUndefined(value) && value !== '') { + attrs.push(`${attr}="${value}"`); } - var val = attr[prop]; - strAttr += typeof val !== undefined && val !== '' ? - ' ' + prop + '="' + val + '"' : ''; } - // Build the string of classes - var strCls = ''; - m.get('classes').each(m => { - strCls += ' ' + m.get('name'); - }); - 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 += ''; + let attrString = attrs.length ? ` ${attrs.join(' ')}` : ''; + let code = `<${tag}${attrString}${sTag ? '/' : ''}>${model.get('content')}`; + model.get('components').each(comp => code += comp.toHTML()); + !sTag && (code += ``); return code; }, @@ -566,7 +560,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({ * @private */ getAttrToHTML() { - var attr = this.get('attributes') || {}; + var attr = this.getAttributes(); delete attr.style; return attr; }, diff --git a/test/specs/code_manager/model/CodeModels.js b/test/specs/code_manager/model/CodeModels.js index 6c580d047..ebddfd62a 100644 --- a/test/specs/code_manager/model/CodeModels.js +++ b/test/specs/code_manager/model/CodeModels.js @@ -54,7 +54,7 @@ module.exports = { ['class1', 'class2'].forEach(item => { m1.get('classes').add({name: item}); }); - expect(obj.build(comp)).toEqual('
'); + expect(obj.build(comp)).toEqual('
'); }); }); diff --git a/test/specs/dom_components/model/Component.js b/test/specs/dom_components/model/Component.js index c0c57a787..5cda7b3ea 100644 --- a/test/specs/dom_components/model/Component.js +++ b/test/specs/dom_components/model/Component.js @@ -169,6 +169,7 @@ module.exports = { }); expect(obj.getAttributes()).toEqual({ id: 'test', + class: 'class1 class2', 'data-test': 'value', }); expect(obj.get('classes').length).toEqual(2);