light.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <style>
  2. .red {
  3. color: #f00;
  4. }
  5. </style>
  6. <template>
  7. <md-layout md-align="center" md-gutter="16">
  8. <md-card>
  9. <md-card-header>
  10. <div class="md-title">{{ light.name }}</div>
  11. <div class="md-subhead">{{ light.type }}</div>
  12. <md-switch v-model="light.state.on" class="md-warn"></md-switch>
  13. </md-card-header>
  14. <md-card-content v-show="light.state.on">
  15. <color-picker v-model="colors"></color-picker>
  16. </md-card-content>
  17. <md-card-actions>
  18. <md-button @click.native="reset()" :disabled="!light.state.on">Reset</md-button>
  19. <md-menu>
  20. <md-button md-menu-trigger :disabled="!light.state.on">Preset Colors</md-button>
  21. <md-menu-content>
  22. <md-menu-item @selected="setColor('white')">White</md-menu-item>
  23. <md-menu-item @selected="setColor('red')">Red</md-menu-item>
  24. <md-menu-item @selected="setColor('blue')">Blue</md-menu-item>
  25. <md-menu-item @selected="setColor('green')">Green</md-menu-item>
  26. <md-menu-item @selected="setColor('purple')">Purple</md-menu-item>
  27. </md-menu-content>
  28. </md-menu>
  29. <md-button @click.native="colorLoop()" :disabled="!light.state.on">Color Loop</md-button>
  30. </md-card-actions>
  31. </md-card>
  32. </md-layout>
  33. </template>
  34. <script>
  35. var Color = require('color');
  36. var Vue = require('vue'),
  37. VueColor = require('vue-color');
  38. module.exports = {
  39. props: ['light'],
  40. data: function () {
  41. return {
  42. colors: {
  43. hex: '#00ff00',
  44. a: 0
  45. }
  46. };
  47. },
  48. http: {
  49. root: (process.env.API_ENDPOINT || '') + '/api',
  50. },
  51. components: {
  52. 'color-picker': VueColor.Chrome
  53. },
  54. methods: {
  55. reset: function () {
  56. this.setProps({ reset: true });
  57. },
  58. colorLoop: function () {
  59. console.error('Not implemented')
  60. },
  61. setColor: function (color) {
  62. Vue.set(this.$props.light.state, 'rgb', Color(color).rgb().array());
  63. },
  64. setRgb: function (r, g, b) {
  65. Vue.set(this.$props.light.state, 'rgb', [r, g, b]);
  66. },
  67. setBrightness: function (brightness) {
  68. Vue.set(this.$props.light.state, 'bri', brightness);
  69. }
  70. },
  71. watch: {
  72. light: {
  73. handler: function (newVal, oldVal) {
  74. var that = this;
  75. // this is setting a new light
  76. if (newVal.id !== oldVal.id) {
  77. console.log(newVal.state)
  78. if (!newVal.state.rgb) return;
  79. console.log(this.$data, Color.rgb(newVal.state.rgb).hex())
  80. Vue.nextTick(function () {
  81. that.$data.colors = {
  82. hex: Color.rgb(newVal.state.rgb).hex(),
  83. a: 255. / newVal.state.bri
  84. };
  85. });
  86. return;
  87. }
  88. console.log('light changed, commit changes', newVal, oldVal);
  89. this.$root.setLightProperties(this.$props.light);
  90. },
  91. deep: true
  92. },
  93. colors: {
  94. handler: function (newVal, oldVal) {
  95. this.setRgb(newVal.rgba.r, newVal.rgba.g, newVal.rgba.b);
  96. this.setBrightness(newVal.rgba.a * 255);
  97. }
  98. }
  99. }
  100. }
  101. </script>