diff --git a/.gitignore b/.gitignore index efe387af34..975bff0f78 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ npm-debug.log .DS_Store hass_frontend/* hass_frontend_es5/* +.reify-cache # Python stuff *.py[cod] diff --git a/gulp/tasks/rollup.js b/gulp/tasks/rollup.js index 57cd455972..a36816751e 100644 --- a/gulp/tasks/rollup.js +++ b/gulp/tasks/rollup.js @@ -6,6 +6,7 @@ const rollupConfigEs6 = require('../../rollup.config-es6'); gulp.task('run_rollup_es5', () => gulp.src([ 'js/core.js', 'js/compatibility.js', + 'js/util.js', 'js/automation-editor/automation-editor.js', 'js/script-editor/script-editor.js', 'demo_data/demo_data.js', @@ -16,6 +17,7 @@ gulp.task('run_rollup_es5', () => gulp.src([ gulp.task('run_rollup', () => gulp.src([ 'js/core.js', 'js/automation-editor/automation-editor.js', + 'js/util.js', 'js/script-editor/script-editor.js', 'demo_data/demo_data.js', ]) diff --git a/js/common/util/attribute_class_names.js b/js/common/util/attribute_class_names.js new file mode 100644 index 0000000000..004e5bb0da --- /dev/null +++ b/js/common/util/attribute_class_names.js @@ -0,0 +1,6 @@ +export default function attributeClassNames(stateObj, attributes) { + if (!stateObj) return ''; + return attributes.map(function (attribute) { + return attribute in stateObj.attributes ? 'has-' + attribute : ''; + }).filter(attr => attr !== '').join(' '); +} diff --git a/js/util.js b/js/util.js new file mode 100644 index 0000000000..621f7e0019 --- /dev/null +++ b/js/util.js @@ -0,0 +1,13 @@ +/** + * Export hass util functions to window. + * + * This file is a workaround for the fact that Polymer 2 doesn't work well with + * ES6 JS imports. Once we move to Polymer 3, we should be able to simply + * import these functions where we need them. + */ + +import attributeClassNames from './common/util/attribute_class_names'; + +window.hassUtil = window.hassUtil || {}; + +window.hassUtil.attributeClassNames = attributeClassNames; diff --git a/package.json b/package.json index befb354703..eee3d19151 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "dev-watch-es5": "npm run gulp watch_ru_all_es5 gen-service-worker-es5", "lint_js": "eslint src panels js --ext js,html", "lint_html": "ls -1 src/home-assistant.html panels/**/ha-panel-*.html | xargs polymer lint --input", - "test": "npm run lint_js && npm run lint_html" + "mocha": "node_modules/.bin/mocha --opts test-mocha/mocha.opts", + "test": "npm run lint_js && npm run lint_html && npm run mocha" }, "author": "Paulus Schoutsen (http://paulusschoutsen.nl)", "license": "Apache-2.0", @@ -58,6 +59,7 @@ "home-assistant-js-websocket": "^1.1.2", "html-minifier": "^3.5.6", "merge-stream": "^1.0.1", + "mocha": "^4.0.1", "parse5": "^3.0.3", "polymer-analyzer": "^2.3.0", "polymer-build": "^2.1.0", @@ -65,6 +67,7 @@ "polymer-cli": "^1.5.6", "preact": "^8.2.6", "pump": "^1.0.2", + "reify": "^0.12.3", "require-dir": "^0.3.2", "rollup": "^0.51.3", "rollup-plugin-babel": "^3.0.2", diff --git a/src/util/hass-util.html b/src/util/hass-util.html index a1b9be3efe..3cf8d46c10 100644 --- a/src/util/hass-util.html +++ b/src/util/hass-util.html @@ -1,4 +1,5 @@ +