Advertisement
Guest User

state-card-html & _es5

a guest
Jul 13th, 2018
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. <script>
  2. {
  3. const _NAME = 'HTML custom state card';
  4. const _URL = 'https://github.com/c727/home-assistant-html';
  5. const _VERSION = '20180601';
  6.  
  7. if (!window.CUSTOM_UI_LIST) window.CUSTOM_UI_LIST = [];
  8. window.CUSTOM_UI_LIST.push({
  9. name: _NAME,
  10. url: _URL,
  11. version: _VERSION
  12. });
  13. }
  14. </script>
  15.  
  16. <dom-module id='state-card-html'>
  17. <template>
  18. <div id='content' on-click='cardClicked'></div>
  19. </template>
  20. </dom-module>
  21.  
  22. <script>
  23. class StateCardHTML extends Polymer.Element {
  24. static get is() { return 'state-card-html'; }
  25. static get properties() {
  26. return {
  27. hass: Object,
  28. stateObj: Object,
  29. config: {
  30. type: Object,
  31. computed: 'computeConfig(stateObj)',
  32. },
  33. };
  34. }
  35. static get observers() {
  36. return [
  37. 'contentChanged(hass.states.*, config)'
  38. ]
  39. }
  40.  
  41. computeConfig(stateObj) {
  42. return stateObj && stateObj.attributes && stateObj.attributes.config;
  43. }
  44.  
  45. contentChanged(states, config) {
  46. if (config && config.html && Array.isArray(config.html)) {
  47. let content = '';
  48. config.html.forEach(item => {
  49. if ('code' in item || 'c' in item) {
  50. content += item.code || item.c;
  51. } else if ('state' in item || 's' in item) {
  52. const entity = item.state || item.s;
  53. content += this.computeState(states.value, entity);
  54. } else if ('mixed' in item || 'm' in item) {
  55. const mixed = item.mixed || item.m;
  56. content += mixed.replace(/\${(.+)}/g, this.mixedReplacer.bind(this));
  57. }
  58. });
  59. this.$.content.innerHTML = content;
  60. } else {
  61. this.$.content.innerHTML = '(config error)';
  62. }
  63. }
  64.  
  65. ready() {
  66. super.ready();
  67. if (this.config && this.config.stylesheet) {
  68. const link = document.createElement('link');
  69. link.setAttribute('rel', 'stylesheet');
  70. link.setAttribute('type', 'text/css');
  71. link.setAttribute('href', this.config.stylesheet);
  72. Polymer.dom(this.root).appendChild(link);
  73. }
  74. }
  75.  
  76. computeState(states, entity) {
  77. return states[entity] && states[entity].state ? states[entity].state : '';
  78. }
  79.  
  80. mixedReplacer(match, entity) {
  81. return this.computeState(this.hass.states, entity);
  82. }
  83.  
  84. cardClicked(ev) {
  85. if (this.config && this.config.dialog) {
  86. this.fire('hass-more-info', { entityId: this.config.dialog });
  87. }
  88. ev.stopPropagation();
  89. }
  90.  
  91. fire(type, detail = {}, options = {}) {
  92. const event = new Event(type, {
  93. bubbles: options.bubbles === undefined ? true : options.bubbles,
  94. cancelable: Boolean(options.cancelable),
  95. composed: options.composed === undefined ? true : options.composed
  96. });
  97. event.detail = detail;
  98. const node = options.node || this;
  99. node.dispatchEvent(event);
  100. return event;
  101. }
  102. }
  103. customElements.define(StateCardHTML.is, StateCardHTML);
  104. </script>
  105.  
  106. ////////////////
  107.  
  108. <script>
  109. {
  110. var _NAME = 'HTML custom state card';
  111. var _URL = 'https://github.com/c727/home-assistant-html';
  112. var _VERSION = '20180601_es5';
  113.  
  114. if (!window.CUSTOM_UI_LIST) window.CUSTOM_UI_LIST = [];
  115. window.CUSTOM_UI_LIST.push({
  116. name: _NAME,
  117. url: _URL,
  118. version: _VERSION
  119. });
  120. }
  121. </script>
  122.  
  123. <dom-module id='state-card-html'>
  124. <template>
  125. <div id='content' on-click='cardClicked'></div>
  126. </template>
  127. </dom-module>
  128.  
  129. <script>
  130. Polymer({
  131. is: 'state-card-html',
  132. properties: {
  133. hass: {
  134. type: Object,
  135. },
  136. stateObj: {
  137. type: Object,
  138. },
  139. config: {
  140. type: Object,
  141. computed: 'computeConfig(stateObj)',
  142. },
  143. },
  144. observers: [
  145. 'contentChanged(hass.states.*, config)'
  146. ],
  147.  
  148. computeConfig: function (stateObj) {
  149. return stateObj && stateObj.attributes && stateObj.attributes.config;
  150. },
  151.  
  152. contentChanged: function (states, config) {
  153. if (config && config.html && Array.isArray(config.html)) {
  154. var content = '';
  155. config.html.forEach(function (item) {
  156. if ('code' in item || 'c' in item) {
  157. content += item.code || item.c;
  158. } else if ('state' in item || 's' in item) {
  159. var entity = item.state || item.s;
  160. content += this.computeState(states.value, entity);
  161. } else if ('mixed' in item || 'm' in item) {
  162. var mixed = item.mixed || item.m;
  163. content += mixed.replace(/\${(.+)}/g, this.mixedReplacer.bind(this));
  164. }
  165. });
  166. this.$.content.innerHTML = content;
  167. } else {
  168. this.$.content.innerHTML = '(config error)';
  169. }
  170. },
  171.  
  172. ready: function () {
  173. if (this.config && this.config.stylesheet) {
  174. var link = document.createElement('link');
  175. link.setAttribute('rel', 'stylesheet');
  176. link.setAttribute('type', 'text/css');
  177. link.setAttribute('href', this.config.stylesheet);
  178. Polymer.dom(this.root).appendChild(link);
  179. }
  180. },
  181.  
  182. computeState: function (states, entity) {
  183. return states[entity] && states[entity].state ? states[entity].state : '';
  184. },
  185.  
  186. mixedReplacer: function (match, entity) {
  187. return this.computeState(this.hass.states, entity);
  188. },
  189.  
  190. cardClicked: function (ev) {
  191. if (this.config && this.config.dialog) {
  192. this.fire('hass-more-info', { entityId: this.config.dialog });
  193. }
  194. ev.stopPropagation();
  195. },
  196.  
  197. fire: function (type, detail = {}, options = {}) {
  198. var event = new Event(type, {
  199. bubbles: options.bubbles === undefined ? true : options.bubbles,
  200. cancelable: Boolean(options.cancelable),
  201. composed: options.composed === undefined ? true : options.composed
  202. });
  203. event.detail = detail;
  204. var node = options.node || this;
  205. node.dispatchEvent(event);
  206. return event;
  207. },
  208. });
  209. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement