diff --git a/src/browser/js/app/parser.js b/src/browser/js/app/parser.js index d589b911..2801ec85 100644 --- a/src/browser/js/app/parser.js +++ b/src/browser/js/app/parser.js @@ -1,7 +1,5 @@ var widgetManager = require('./managers/widgets'), - stateManager = require('./managers/state'), - {iconify} = require('./ui/utils'), - scope = require('scope-css') + stateManager = require('./managers/state') var Parser = class Parser { @@ -59,92 +57,25 @@ var Parser = class Parser { // Generate default address props.address = props.address == 'auto' ? '/' + props.id : props.address - // create container - var widgetContainer = DOM.create(` -
-
-
- `) // create widget - var widgetInner = new this.widgets[props.type]({props:props, container:widgetContainer, parent:parentWidget, parentNode:parentNode}) + var widget = new this.widgets[props.type]({props:props, container:true, parent:parentWidget, parentNode:parentNode}) - widgetContainer.appendChild(widgetInner.widget) + widgetManager.addWidget(widget) - widgetManager.addWidget(widgetInner) - - widgetContainer.setAttribute('data-widget', widgetInner.hash) - - widgetInner.created() - - // dimensions / coordinates can't be < 0 - for (let t of ['width', 'height', 'top', 'left']) { - if (props.hasOwnProperty(t)) - props[t] = `${props[t]}`.indexOf('-')!=-1?0:props[t] - } - - // convert dimensions / coordinates to rem - var geometry = {} - for (var d of ['width', 'height', 'left', 'top']){ - let val = widgetInner.getProp(d) - if (val !== undefined) - geometry[d] = `${val}`.indexOf('-') != -1 ? 0 : - parseFloat(val) == val ? - parseFloat(val)+'rem' : val - } - - // dimensions / coordinates css - var styleW = widgetInner.getProp('width') && widgetInner.getProp('width') != 'auto' ? `width: ${geometry.width}; min-width:${geometry.width};` : '', - styleH = widgetInner.getProp('height') && widgetInner.getProp('height') != 'auto' ? `height: ${geometry.height}; min-height:${geometry.height};` : '', - styleL = widgetInner.getProp('left') && widgetInner.getProp('left') != 'auto'|| widgetInner.getProp('left') == 0 ?`left: ${geometry.left};` : '', - styleT = widgetInner.getProp('top') && widgetInner.getProp('top') != 'auto'|| widgetInner.getProp('top') == 0 ? `top: ${geometry.top};` : '' - - if (styleL.length || styleT.length) widgetContainer.classList.add('absolute-position') - - // Hide label if false - if (widgetInner.getProp('label')===false) { - widgetContainer.classList.add('nolabel') - } else { - // Generate label, iconify if starting with "icon:" - var label = widgetInner.getProp('label') == 'auto'? - widgetInner.getProp('id'): - iconify(widgetInner.getProp('label')) - - DOM.get(widgetContainer, '> .label')[0].innerHTML = label - } - - // apply styles - var css = widgetInner.getProp('css') - - widgetContainer.setAttribute('style', styleW + styleH + styleL + styleT + css.replace(/;{[^\}]}/g),'') - - // apply scoped css - var prefix = '#' + widgetInner.hash, - scopedCss = scope(css, prefix) - - if (scopedCss.indexOf(prefix) > -1) { - - if (scopedCss.indexOf('@keyframes') > -1) scopedCss = scopedCss.replace(new RegExp(prefix + '\\s+([0-9]+%|to|from)', 'g'), ' $1') - if (scopedCss.indexOf('&') > -1) scopedCss = scopedCss.replace(new RegExp(prefix + '\\s&', 'g'), prefix) - - widgetContainer.setAttribute('id', widgetInner.hash) - widgetContainer.insertBefore(DOM.create(``), widgetInner.widget) - } - - // Set custom css color variable - if (widgetInner.getProp('color') && widgetInner.getProp('color')!='auto') widgetContainer.style.setProperty('--color-custom',widgetInner.getProp('color')) + widget.created() // set widget's initial state - if (widgetInner.getProp('value') !== '') { - stateManager.pushValueNewProp(widgetInner.getProp('id'), widgetInner.getProp('value')) + if (widget.getProp('value') !== '') { + stateManager.pushValueNewProp(widget.getProp('id'), widget.getProp('value')) } // Append the widget to its parent - parentNode.appendChild(widgetContainer) + parentNode.appendChild(widget.container) } // Editor needs to get the container object - if (data && data.length==1) return widgetInner + if (data && data.length==1) return widget } diff --git a/src/browser/js/app/widgets/common/widget.js b/src/browser/js/app/widgets/common/widget.js index 308777bb..cfa5bf70 100644 --- a/src/browser/js/app/widgets/common/widget.js +++ b/src/browser/js/app/widgets/common/widget.js @@ -3,13 +3,16 @@ var EventEmitter = require('../../events/event-emitter'), shortid = require('shortid'), widgetManager = require('../../managers/widgets'), {math} = require('../utils'), - updateWidget = function(){ updateWidget = require('../../editor/data-workers').updateWidget; updateWidget(...arguments)} + scopeCss = require('scope-css'), + {iconify} = require('../../ui/utils'), + updateWidget = ()=>{} var fallbackContainer DOM.ready(()=>{ fallbackContainer = DOM.create('
') + updateWidget = require('../../editor/data-workers').updateWidget }) class Widget extends EventEmitter { @@ -24,7 +27,6 @@ class Widget extends EventEmitter { super() - this.container = options.container || fallbackContainer this.widget = DOM.create(options.html) this.props = options.props this.errors = {} @@ -34,11 +36,6 @@ class Widget extends EventEmitter { this.hash = shortid.generate() this.childrenHashes = [] - if (options.container) { - this.container.setAttribute('data-widget', this.hash) - this.container._widget_instance = this - } - // Turn preArgs into array if (this.props.preArgs !== undefined && !Array.isArray(this.resolveProp('preArgs', undefined, false))) { this.props.preArgs = [this.props.preArgs] @@ -56,7 +53,6 @@ class Widget extends EventEmitter { delete this.props[this.parent.getProp('horizontal') ? 'height' : 'width'] } - // @{props} links lists this.linkedProps = {} this.linkedPropsValue = {} @@ -125,6 +121,21 @@ class Widget extends EventEmitter { }) + if (options.container) { + + this.container = DOM.create(` +
+ `) + this.label = DOM.create(`
`) + this.container.appendChild(this.label) + this.container.appendChild(this.widget) + this.container.setAttribute('data-widget', this.hash) + this.container._widget_instance = this + this.setContainerStyles() + } else { + this.container = fallbackContainer + } + } created() { @@ -395,8 +406,23 @@ class Widget extends EventEmitter { this.setValue(this.getProp('value'), {sync: true, send: options && options.send}) return + case 'top': + case 'left': + case 'height': + case 'width': + this.setContainerStyles(['geometry']) + return + + case 'label': + this.setContainerStyles(['label']) + return + + case 'css': + this.setContainerStyles(['css', 'color']) + return + case 'color': - this.container.style.setProperty('--color-custom', this.getProp('color') != 'auto' ? this.getProp('color') : '') + this.setContainerStyles(['color']) return case 'precision': @@ -418,6 +444,80 @@ class Widget extends EventEmitter { } + setContainerStyles(styles = ['geometry', 'label', 'css', 'color']) { + + if (styles.includes('geometry')) { + + // geometry + for (let d of ['width', 'height', 'top', 'left']) { + let val = this.getProp(d), + geometry + + if (val !== undefined) { + if (parseFloat(val) < 0) val = 0 + geometry = parseFloat(val) == val ? parseFloat(val)+'rem' : val + } + + if (geometry && geometry != 'auto') { + this.container.style[d] = geometry + if (d == 'width') this.container.style.minWidth = geometry + if (d == 'height') this.container.style.minHeight = geometry + if (d == 'top' || d == 'left') this.container.classList.add('absolute-position') + } + } + + } + + if (styles.includes('label')) { + + // label + if (this.getProp('label') === false) { + this.container.classList.add('nolabel') + } else { + var label = this.getProp('label') == 'auto'? + this.getProp('id'): + iconify(this.getProp('label')) + + this.label.innerHTML = label + } + + } + + if (styles.includes('css')) { + + // css + var prefix = '#' + this.hash, + scopedCss = scopeCss(this.getProp('css'), prefix), + unScopedCss = prefix + '{' + this.getProp('css').replace(/(^|\n)([^\{]*)\{([^\}]*)\}/g, '') + '}' + + if (scopedCss.indexOf(prefix) > -1) { + + if (scopedCss.indexOf('@keyframes') > -1) scopedCss = scopedCss.replace(new RegExp(prefix + '\\s+([0-9]+%|to|from)', 'g'), ' $1') + if (scopedCss.indexOf('&') > -1) scopedCss = scopedCss.replace(new RegExp(prefix + '\\s&', 'g'), prefix) + + this.container.setAttribute('id', this.hash) + + var style = DOM.create(``), + oldStyle = DOM.get(this.container, '> style')[0] + if (oldStyle) { + this.container.replaceChild(style, oldStyle) + } else { + this.container.insertBefore(style, this.widget) + } + } + + } + + if (styles.includes('color')) { + + // color + if (this.getProp('color') && this.getProp('color')!='auto') this.container.style.setProperty('--color-custom',this.getProp('color')) + + } + + + } + reCreateWidget(){ updateWidget(this, {remote: true}) @@ -433,6 +533,12 @@ class Widget extends EventEmitter { } Widget.dynamicProps = [ + 'top', + 'left', + 'height', + 'width', + 'label', + 'css', 'value', 'color', 'precision',