mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-09 19:09:48 +00:00
Add experimental sections view (#19846)
This commit is contained in:
@@ -14,9 +14,16 @@ declare global {
|
||||
oldPath?: ItemPath;
|
||||
newPath?: ItemPath;
|
||||
};
|
||||
"drag-start": undefined;
|
||||
"drag-end": undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export type HaSortableOptions = Omit<
|
||||
SortableInstance.SortableOptions,
|
||||
"onStart" | "onChoose" | "onEnd"
|
||||
>;
|
||||
|
||||
@customElement("ha-sortable")
|
||||
export class HaSortable extends LitElement {
|
||||
private _sortable?: SortableInstance;
|
||||
@@ -36,14 +43,17 @@ export class HaSortable extends LitElement {
|
||||
@property({ type: String, attribute: "handle-selector" })
|
||||
public handleSelector?: string;
|
||||
|
||||
@property({ type: String, attribute: "group" })
|
||||
public group?: string;
|
||||
|
||||
@property({ type: Number, attribute: "swap-threshold" })
|
||||
public swapThreshold?: number;
|
||||
@property({ type: String })
|
||||
public group?: string | SortableInstance.GroupOptions;
|
||||
|
||||
@property({ type: Boolean, attribute: "invert-swap" })
|
||||
public invertSwap?: boolean;
|
||||
public invertSwap: boolean = false;
|
||||
|
||||
@property({ attribute: false })
|
||||
public options?: HaSortableOptions;
|
||||
|
||||
@property({ type: Boolean })
|
||||
public rollback: boolean = true;
|
||||
|
||||
protected updated(changedProperties: PropertyValues<this>) {
|
||||
if (changedProperties.has("disabled")) {
|
||||
@@ -114,26 +124,20 @@ export class HaSortable extends LitElement {
|
||||
|
||||
const options: SortableInstance.Options = {
|
||||
animation: 150,
|
||||
swapThreshold: 1,
|
||||
...this.options,
|
||||
onChoose: this._handleChoose,
|
||||
onStart: this._handleStart,
|
||||
onEnd: this._handleEnd,
|
||||
};
|
||||
|
||||
if (this.draggableSelector) {
|
||||
options.draggable = this.draggableSelector;
|
||||
}
|
||||
|
||||
if (this.swapThreshold !== undefined) {
|
||||
options.swapThreshold = this.swapThreshold;
|
||||
}
|
||||
if (this.invertSwap !== undefined) {
|
||||
options.invertSwap = this.invertSwap;
|
||||
}
|
||||
if (this.handleSelector) {
|
||||
options.handle = this.handleSelector;
|
||||
}
|
||||
if (this.draggableSelector) {
|
||||
options.draggable = this.draggableSelector;
|
||||
if (this.invertSwap !== undefined) {
|
||||
options.invertSwap = this.invertSwap;
|
||||
}
|
||||
if (this.group) {
|
||||
options.group = this.group;
|
||||
@@ -143,8 +147,9 @@ export class HaSortable extends LitElement {
|
||||
}
|
||||
|
||||
private _handleEnd = async (evt: SortableEvent) => {
|
||||
fireEvent(this, "drag-end");
|
||||
// put back in original location
|
||||
if ((evt.item as any).placeholder) {
|
||||
if (this.rollback && (evt.item as any).placeholder) {
|
||||
(evt.item as any).placeholder.replaceWith(evt.item);
|
||||
delete (evt.item as any).placeholder;
|
||||
}
|
||||
@@ -170,7 +175,12 @@ export class HaSortable extends LitElement {
|
||||
});
|
||||
};
|
||||
|
||||
private _handleStart = () => {
|
||||
fireEvent(this, "drag-start");
|
||||
};
|
||||
|
||||
private _handleChoose = (evt: SortableEvent) => {
|
||||
if (!this.rollback) return;
|
||||
(evt.item as any).placeholder = document.createComment("sort-placeholder");
|
||||
evt.item.after((evt.item as any).placeholder);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user