-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce Lit Element #1738
Introduce Lit Element #1738
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,67 @@ | ||
import { html } from '@polymer/polymer/lib/utils/html-tag.js'; | ||
import { PolymerElement } from '@polymer/polymer/polymer-element.js'; | ||
|
||
import { LitElement, html } from '@polymer/lit-element'; | ||
|
||
import './ha-progress-button.js'; | ||
import EventsMixin from '../../mixins/events-mixin.js'; | ||
import fireEvent from '../../common/dom/fire_event.js'; | ||
|
||
/* | ||
* @appliesMixin EventsMixin | ||
*/ | ||
class HaCallApiButton extends EventsMixin(PolymerElement) { | ||
static get template() { | ||
class HaCallApiButton extends LitElement { | ||
render() { | ||
return html` | ||
<ha-progress-button id="progress" progress="[[progress]]" on-click="buttonTapped" disabled="[[disabled]]"><slot></slot></ha-progress-button> | ||
`; | ||
<ha-progress-button | ||
.progress="${this.progress}" | ||
@click="${this._buttonTapped}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Listen to events on this component (in JS: |
||
?disabled="${this.disabled}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Toggle a boolean based attribute (in JS: el.toggleAttribute('disabled', this.disabled)`) |
||
><slot></slot></ha-progress-button> | ||
`; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
this.method = 'POST'; | ||
this.data = {}; | ||
this.disabled = false; | ||
this.progress = false; | ||
// Can be removed once this is merged: | ||
// https://github.com/Polymer/lit-element/pull/244 | ||
this._buttonTapped = this._buttonTapped.bind(this); | ||
} | ||
|
||
static get properties() { | ||
return { | ||
hass: Object, | ||
|
||
progress: { | ||
type: Boolean, | ||
value: false, | ||
}, | ||
|
||
hass: { }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why empty object here? Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Using a function will be used to serialize values when set as attributes. We don't want it to be serialized. See |
||
progress: Boolean, | ||
path: String, | ||
|
||
method: { | ||
type: String, | ||
value: 'POST', | ||
}, | ||
|
||
data: { | ||
type: Object, | ||
value: {}, | ||
}, | ||
|
||
disabled: { | ||
type: Boolean, | ||
value: false, | ||
}, | ||
method: String, | ||
data: { }, | ||
disabled: Boolean | ||
}; | ||
} | ||
|
||
buttonTapped() { | ||
get progressButton() { | ||
return this.renderRoot.querySelector('ha-progress-button'); | ||
} | ||
|
||
async _buttonTapped() { | ||
this.progress = true; | ||
const eventData = { | ||
method: this.method, | ||
path: this.path, | ||
data: this.data, | ||
data: this.data | ||
}; | ||
|
||
this.hass.callApi(this.method, this.path, this.data) | ||
.then((resp) => { | ||
this.progress = false; | ||
this.$.progress.actionSuccess(); | ||
eventData.success = true; | ||
eventData.response = resp; | ||
}, (resp) => { | ||
this.progress = false; | ||
this.$.progress.actionError(); | ||
eventData.success = false; | ||
eventData.response = resp; | ||
}).then(() => { | ||
this.fire('hass-api-called', eventData); | ||
}); | ||
try { | ||
const resp = await this.hass.callApi(this.method, this.path, this.data); | ||
this.progress = false; | ||
this.progressButton.actionSuccess(); | ||
eventData.success = true; | ||
eventData.response = resp; | ||
} catch (err) { | ||
this.progress = false; | ||
this.progressButton.actionError(); | ||
eventData.success = false; | ||
eventData.response = err; | ||
} | ||
|
||
fireEvent(this, 'hass-api-called', eventData); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sets a property on the DOM element (in JS:
el.progress = this.progress
)