Johannes Zellner 9 жил өмнө
parent
commit
df40e9ba31

+ 4 - 0
frontend/css/index.css

@@ -0,0 +1,4 @@
+.vue-color__chrome {
+    width: 100% !important;
+    box-shadow: none !important;
+}

+ 2 - 2
frontend/index.html

@@ -27,7 +27,7 @@
 
                 <md-list-expand>
                     <md-list>
-                        <md-list-item class="md-inset" v-for="light in lights" @click.native="selectLight(light)">{{ light.name }}</md-list-item>
+                        <md-list-item class="md-inset" v-for="light in lights" @click.native="selectLight(light)" :key="light.id">{{ light.name }}</md-list-item>
                     </md-list>
                 </md-list-expand>
             </md-list-item>
@@ -45,7 +45,7 @@
     </md-toolbar>
 
     <br/>
-    <light-item :light="selectedLight"></light-item>
+    <light-item :light="selectedLight" v-show="selectedLight"></light-item>
 </div>
 
 <script src="./js/bundle.js"></script>

+ 48 - 25
frontend/js/main.js

@@ -9,41 +9,64 @@ var Light = require('../views/light.vue');
 Vue.use(VueMaterial);
 Vue.use(VueResource);
 
+var methods = {
+    toggleSidenav: function () {
+        this.$refs.leftSidenav.toggle();
+    },
+    sidenavOpen: function () {
+        console.log('sidenav open');
+    },
+    selectLight: function (light) {
+        this.$data.selectedLight = light;
+        this.$refs.leftSidenav.close();
+    },
+
+    // lights
+    refreshLights: function () {
+        var that = this;
+
+        this.$http.get('lights').then(function (result) {
+            console.log('result', result);
+
+            result.body.lights.forEach(function (light) {
+                var tmp = that.$data.lights.find(function (l) { return light.id === l.id; });
+                if (!tmp) return that.$data.lights.push(light);
+
+                tmp = light;
+            });
+
+            // init a light selection
+            if (!that.$data.selectedLight.id && that.$data.lights.length > 0) that.$data.selectedLight = result.body.lights[0];
+        }, function (error) {
+            console.log('error', error);
+        });
+    },
+    setLightProperties: function (light) {
+        var that = this;
+
+        this.$http.put('lights/' + light.id, light.state).then(function (result) {
+            console.log('result', result);
+            that.refreshLights();
+        }, function (error) {
+            console.log('error', error);
+        });
+    }
+};
+
 var App = new Vue({
     el: '#app',
     http: {
-        root: '/api',
+        root: (process.env.API_ENDPOINT || '') + '/api',
     },
     components: {
         lightItem: Light
     },
     data: {
         lights: [],
-        selectedLight: {}
-    },
-    methods: {
-        toggleSidenav: function () {
-            this.$refs.leftSidenav.toggle();
-        },
-        sidenavOpen: function () {
-            console.log('sidenav open');
-        },
-        selectLight: function (light) {
-            this.$data.selectedLight = light;
-            this.$refs.leftSidenav.close();
-        }
+        selectedLight: { state: {}}
     },
+    methods: methods,
     mounted: function () {
-        console.log('mounted');
-
-        var that = this;
-
-        this.$http.get('lights', { responseType: 'text' }).then(function (result) {
-            console.log('result', result);
-            that.$data.lights = result.body.lights;
-            that.$data.selectedLight = result.body.lights[3];
-        }, function (error) {
-            console.log('error', error);
-        });
+        this.refreshLights();
     }
 });

+ 35 - 18
frontend/views/light.vue

@@ -10,16 +10,26 @@
             <md-card-header>
                 <div class="md-title">{{ light.name }}</div>
                 <div class="md-subhead">{{ light.type }}</div>
+                <md-switch v-model="light.state.on" class="md-warn"></md-switch>
             </md-card-header>
 
-            <md-card-content>
-                <md-switch v-model="stateOn" class="md-warn" @change="toggleOnOff()">State</md-switch>
+            <md-card-content v-show="light.state.on">
+                <color-picker v-model="colors"></color-picker>
             </md-card-content>
 
             <md-card-actions>
-                <md-button @click.native="reset()">Reset</md-button>
-                <md-button @click.native="setColor('purple')">Purple</md-button>
-                <md-button @click.native="colorLoop()">Color Loop</md-button>
+                <md-button @click.native="reset()" :disabled="!light.state.on">Reset</md-button>
+                <md-menu>
+                    <md-button md-menu-trigger :disabled="!light.state.on">Preset Colors</md-button>
+                    <md-menu-content>
+                        <md-menu-item @selected="setColor('white')">White</md-menu-item>
+                        <md-menu-item @selected="setColor('red')">Red</md-menu-item>
+                        <md-menu-item @selected="setColor('blue')">Blue</md-menu-item>
+                        <md-menu-item @selected="setColor('green')">Green</md-menu-item>
+                        <md-menu-item @selected="setColor('purple')">Purple</md-menu-item>
+                    </md-menu-content>
+                </md-menu>
+                <md-button @click.native="colorLoop()" :disabled="!light.state.on">Color Loop</md-button>
             </md-card-actions>
         </md-card>
     </md-layout>
@@ -27,28 +37,22 @@
 
 <script>
 
+var VueColor = require('vue-color');
+
 module.exports = {
     props: ['light'],
     data: function () {
         return {
-            stateOn: false
+            colors: {}
         };
     },
     http: {
-        root: '/api',
+        root: (process.env.API_ENDPOINT || '') + '/api',
+    },
+    components: {
+        'color-picker': VueColor.Chrome
     },
     methods: {
-        setProps: function (data) {
-            this.$http.put('lights/' + this.$props.light.id, data).then(function (result) {
-                console.log('result', result);
-            }, function (error) {
-                console.log('error', error);
-            });
-        },
-        toggleOnOff: function () {
-            this.stateOn = !this.stateOn;
-            this.setProps({ state: this.stateOn });
-        },
         reset: function () {
             this.setProps({ reset: true });
         },
@@ -58,6 +62,19 @@ module.exports = {
         setColor: function (color) {
             this.setProps({ color: color });
         }
+    },
+    watch: {
+        light: {
+            handler: function (newVal, oldVal) {
+                // this is just setting a new light
+                if (newVal.id !== oldVal.id) return;
+
+                console.log('light changed, commit changes', newVal, oldVal);
+
+                this.$root.setLightProperties(this.$props.light);
+            },
+            deep: true
+        }
     }
 }
 

+ 1 - 0
package.json

@@ -18,6 +18,7 @@
   "devDependencies": {
     "envify": "^4.0.0",
     "vue": "^2.3.4",
+    "vue-color": "^2.2.0",
     "vue-material": "^0.7.1",
     "vue-resource": "^1.3.4",
     "vueify": "^9.4.1"