device-name.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <link rel="import" href="../../bower_components/paper-input/paper-input.html">
  2. <link rel="import" href="../../bower_components/iron-localstorage/iron-localstorage.html">
  3. <link rel="import" href="device-name-dialog.html">
  4. <dom-module id="device-name">
  5. <template>
  6. <style>
  7. :host {
  8. display: inline-block;
  9. cursor: pointer;
  10. }
  11. .name-label {
  12. @apply(--paper-font-subhead);
  13. text-align: center;
  14. cursor: pointer;
  15. width: 160px;
  16. line-height: 12px !important;
  17. }
  18. :root {
  19. /* Label and underline color when the input is not focused */
  20. --paper-input-container-color: #333;
  21. /* Label and underline color when the input is focused */
  22. --paper-input-container-focus-color: #4285f4;
  23. /* Label and underline color when the input is invalid */
  24. --paper-input-container-invalid-color: red;
  25. /* Input foreground color */
  26. --paper-input-container-input-color: #333;
  27. }
  28. @media all and (max-height: 370px) {
  29. :host {}
  30. }
  31. paper-dialog {
  32. width: 300px;
  33. text-align: left;
  34. }
  35. </style>
  36. <div class="name-label" hidden$="{{name}}">My Device</div>
  37. <div class="name-label" hidden$="{{!name}}">{{name}}</div>
  38. <iron-localstorage name="device-name" value="{{name}}" iron-localstorage-load="_nameChanged"></iron-localstorage>
  39. </template>
  40. <script>
  41. 'use strict';
  42. Polymer({
  43. is: 'device-name',
  44. properties: {
  45. name: {
  46. observer: '_nameChanged'
  47. }
  48. },
  49. open: function() {
  50. this.deviceNameDialog.open();
  51. },
  52. _nameChanged: function(name) {
  53. if (!name) {
  54. return;
  55. }
  56. this.cancelAsync(this.timer);
  57. this.timer = this.async(function() {
  58. if (!app.conn.notifyServer) {
  59. this._nameChanged(name);
  60. return;
  61. }
  62. try {
  63. this._sendNameToServer(name);
  64. } catch (e) {
  65. this._nameChanged(name);
  66. }
  67. }, 300);
  68. },
  69. _sendNameToServer: function(name) {
  70. app.conn.notifyServer({
  71. serverMsg: 'device-name',
  72. name: name
  73. });
  74. },
  75. _initialize: function() {
  76. console.log('initialize name');
  77. },
  78. get deviceNameDialog() {
  79. var deviceNameDialog = document.querySelector('device-name-dialog');
  80. if (!deviceNameDialog) {
  81. deviceNameDialog = Polymer.Base.create('device-name-dialog');
  82. deviceNameDialog.addEventListener('save-device-name', function(e) {
  83. this.name = e.detail;
  84. console.log(this.name);
  85. }.bind(this));
  86. document.body.appendChild(deviceNameDialog);
  87. };
  88. deviceNameDialog.deviceName = this.name;
  89. return deviceNameDialog;
  90. }
  91. });
  92. </script>
  93. </dom-module>