c.08b532a7.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import{_ as e,j as i,e as t,t as a,i as l,E as d,G as n,y as o,k as s,I as r,J as c,d as u,n as h,eF as f}from"./main-ec7846c8.js";import{a as p}from"./c.cb96fbc8.js";import{b as v}from"./c.0e3055bd.js";import"./c.8e28b461.js";import"./c.eea05cf6.js";e([h("ha-file-upload")],(function(e,i){class h extends i{constructor(...i){super(...i),e(this)}}return{F:h,d:[{kind:"field",decorators:[t({attribute:!1})],key:"hass",value:void 0},{kind:"field",decorators:[t()],key:"accept",value:void 0},{kind:"field",decorators:[t()],key:"icon",value:void 0},{kind:"field",decorators:[t()],key:"label",value:void 0},{kind:"field",decorators:[t()],key:"value",value:()=>null},{kind:"field",decorators:[t({type:Boolean})],key:"uploading",value:()=>!1},{kind:"field",decorators:[t({type:Boolean,attribute:"auto-open-file-dialog"})],key:"autoOpenFileDialog",value:()=>!1},{kind:"field",decorators:[a()],key:"_drag",value:()=>!1},{kind:"field",decorators:[l("#input")],key:"_input",value:void 0},{kind:"method",key:"firstUpdated",value:function(e){d(n(h.prototype),"firstUpdated",this).call(this,e),this.autoOpenFileDialog&&this._openFilePicker()}},{kind:"method",key:"render",value:function(){var e;return o`
  2. ${this.uploading?o`<ha-circular-progress
  3. alt="Uploading"
  4. size="large"
  5. active
  6. ></ha-circular-progress>`:o`
  7. <label
  8. for="input"
  9. class="mdc-text-field mdc-text-field--filled ${s({"mdc-text-field--focused":this._drag,"mdc-text-field--with-leading-icon":Boolean(this.icon),"mdc-text-field--with-trailing-icon":Boolean(this.value)})}"
  10. @drop=${this._handleDrop}
  11. @dragenter=${this._handleDragStart}
  12. @dragover=${this._handleDragStart}
  13. @dragleave=${this._handleDragEnd}
  14. @dragend=${this._handleDragEnd}
  15. >
  16. <span class="mdc-text-field__ripple"></span>
  17. <span
  18. class="mdc-floating-label ${this.value||this._drag?"mdc-floating-label--float-above":""}"
  19. id="label"
  20. >${this.label}</span
  21. >
  22. ${this.icon?o`<span
  23. class="mdc-text-field__icon mdc-text-field__icon--leading"
  24. tabindex="-1"
  25. >
  26. <ha-icon-button
  27. @click=${this._openFilePicker}
  28. .path=${this.icon}
  29. ></ha-icon-button>
  30. </span>`:""}
  31. <div class="value">${this.value}</div>
  32. <input
  33. id="input"
  34. type="file"
  35. class="mdc-text-field__input file"
  36. accept=${this.accept}
  37. @change=${this._handleFilePicked}
  38. aria-labelledby="label"
  39. />
  40. ${this.value?o`<span
  41. class="mdc-text-field__icon mdc-text-field__icon--trailing"
  42. tabindex="1"
  43. >
  44. <ha-icon-button
  45. slot="suffix"
  46. @click=${this._clearValue}
  47. .label=${(null===(e=this.hass)||void 0===e?void 0:e.localize("ui.common.close"))||"close"}
  48. .path=${r}
  49. ></ha-icon-button>
  50. </span>`:""}
  51. <span
  52. class="mdc-line-ripple ${this._drag?"mdc-line-ripple--active":""}"
  53. ></span>
  54. </label>
  55. `}
  56. `}},{kind:"method",key:"_openFilePicker",value:function(){var e;null===(e=this._input)||void 0===e||e.click()}},{kind:"method",key:"_handleDrop",value:function(e){var i;e.preventDefault(),e.stopPropagation(),null!==(i=e.dataTransfer)&&void 0!==i&&i.files&&c(this,"file-picked",{files:e.dataTransfer.files}),this._drag=!1}},{kind:"method",key:"_handleDragStart",value:function(e){e.preventDefault(),e.stopPropagation(),this._drag=!0}},{kind:"method",key:"_handleDragEnd",value:function(e){e.preventDefault(),e.stopPropagation(),this._drag=!1}},{kind:"method",key:"_handleFilePicked",value:function(e){c(this,"file-picked",{files:e.target.files})}},{kind:"method",key:"_clearValue",value:function(e){e.preventDefault(),this.value=null,c(this,"change")}},{kind:"get",static:!0,key:"styles",value:function(){return[v,u`
  57. :host {
  58. display: block;
  59. }
  60. .mdc-text-field--filled {
  61. height: auto;
  62. padding-top: 16px;
  63. cursor: pointer;
  64. }
  65. .mdc-text-field--filled.mdc-text-field--with-trailing-icon {
  66. padding-top: 28px;
  67. }
  68. .mdc-text-field:not(.mdc-text-field--disabled) .mdc-text-field__icon {
  69. color: var(--secondary-text-color);
  70. }
  71. .mdc-text-field--filled.mdc-text-field--with-trailing-icon
  72. .mdc-text-field__icon {
  73. align-self: flex-end;
  74. }
  75. .mdc-text-field__icon--leading {
  76. margin-bottom: 12px;
  77. inset-inline-start: initial;
  78. inset-inline-end: 0px;
  79. direction: var(--direction);
  80. }
  81. .mdc-text-field--filled .mdc-floating-label--float-above {
  82. transform: scale(0.75);
  83. top: 8px;
  84. }
  85. .mdc-floating-label {
  86. inset-inline-start: 16px !important;
  87. inset-inline-end: initial !important;
  88. direction: var(--direction);
  89. }
  90. .mdc-text-field--filled .mdc-floating-label {
  91. inset-inline-start: 48px !important;
  92. inset-inline-end: initial !important;
  93. direction: var(--direction);
  94. }
  95. .dragged:before {
  96. position: var(--layout-fit_-_position);
  97. top: var(--layout-fit_-_top);
  98. right: var(--layout-fit_-_right);
  99. bottom: var(--layout-fit_-_bottom);
  100. left: var(--layout-fit_-_left);
  101. background: currentColor;
  102. content: "";
  103. opacity: var(--dark-divider-opacity);
  104. pointer-events: none;
  105. border-radius: 4px;
  106. }
  107. .value {
  108. width: 100%;
  109. }
  110. input.file {
  111. display: none;
  112. }
  113. img {
  114. max-width: 100%;
  115. max-height: 125px;
  116. }
  117. ha-icon-button {
  118. --mdc-icon-button-size: 24px;
  119. --mdc-icon-size: 20px;
  120. }
  121. ha-circular-progress {
  122. display: block;
  123. text-align-last: center;
  124. }
  125. `]}}]}}),i);let m=e([h("ha-selector-file")],(function(e,i){class l extends i{constructor(...i){super(...i),e(this)}}return{F:l,d:[{kind:"field",decorators:[t()],key:"hass",value:void 0},{kind:"field",decorators:[t()],key:"selector",value:void 0},{kind:"field",decorators:[t()],key:"value",value:void 0},{kind:"field",decorators:[t()],key:"label",value:void 0},{kind:"field",decorators:[t()],key:"helper",value:void 0},{kind:"field",decorators:[t({type:Boolean})],key:"disabled",value:()=>!1},{kind:"field",decorators:[t({type:Boolean})],key:"required",value:()=>!0},{kind:"field",decorators:[a()],key:"_filename",value:void 0},{kind:"field",decorators:[a()],key:"_busy",value:()=>!1},{kind:"method",key:"render",value:function(){var e;return o`
  126. <ha-file-upload
  127. .hass=${this.hass}
  128. .accept=${this.selector.file.accept}
  129. .icon=${f}
  130. .label=${this.label}
  131. .required=${this.required}
  132. .disabled=${this.disabled}
  133. .helper=${this.helper}
  134. .uploading=${this._busy}
  135. .value=${this.value?(null===(e=this._filename)||void 0===e?void 0:e.name)||"Unknown file":""}
  136. @file-picked=${this._uploadFile}
  137. @change=${this._removeFile}
  138. ></ha-file-upload>
  139. `}},{kind:"method",key:"willUpdate",value:function(e){d(n(l.prototype),"willUpdate",this).call(this,e),e.has("value")&&this._filename&&this.value!==this._filename.fileId&&(this._filename=void 0)}},{kind:"method",key:"_uploadFile",value:async function(e){this._busy=!0;const i=e.detail.files[0];try{const e=await(async(e,i)=>{const t=new FormData;t.append("file",i);const a=await e.fetchWithAuth("/api/file_upload",{method:"POST",body:t});if(413===a.status)throw new Error(`Uploaded file is too large (${i.name})`);if(200!==a.status)throw new Error("Unknown error");return(await a.json()).file_id})(this.hass,i);this._filename={fileId:e,name:i.name},c(this,"value-changed",{value:e})}catch(e){p(this,{text:this.hass.localize("ui.components.selectors.file.upload_failed",{reason:e.message||e})})}finally{this._busy=!1}}},{kind:"field",key:"_removeFile",value(){return async()=>{this._busy=!0;try{await(async(e,i)=>e.callApi("DELETE","file_upload",{file_id:i}))(this.hass,this.value)}catch(e){}finally{this._busy=!1}this._filename=void 0,c(this,"value-changed",{value:""})}}}]}}),i);export{m as HaFileSelector};