UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

65 lines 2.47 kB
import { NotImplementedException } from "@aurigma/design-atoms-model/Exception"; import { BaseItemsCommand } from "./BaseItemsCommand"; export class MoveItemCommand extends BaseItemsCommand { constructor(productHandler, historyArgs, args) { super(productHandler, historyArgs, args); } async _executeCommandBody() { return this.moveTo(this._args.item, this._args.newIndex, this._args.needUpdate); } moveTo(item, newIndex, needUpdate) { if (needUpdate == null) needUpdate = true; const itemContainer = item != null ? item.parentContainer : null; if (itemContainer != null) { const itemsList = itemContainer.items.toArray(); const itemsLastIdx = Math.max(itemsList.length - 1, 0); let oldIndex = itemsList.indexOf(item); // normalize indexes newIndex = Math.min(Math.max(newIndex, 0), itemsLastIdx); oldIndex = Math.min(Math.max(oldIndex, 0), itemsLastIdx); const range = this._getMoveRange(itemsList, oldIndex); if (range == null) return; if (newIndex < range[0]) newIndex = range[0]; else if (newIndex > range[1]) newIndex = range[1]; if (oldIndex === newIndex) return; itemContainer.items.move(oldIndex, newIndex); this._productHandler.redraw(); //(this._viewer.eventManager as EventManager).productUpdatedEvent.fire(); } } _getMoveRange(itemsList, index) { if (index < 0) return null; if (itemsList == null) return null; if (index >= itemsList.length) return null; let min = index; let max = index; for (let i = index - 1; i >= 0; i--) { const item = itemsList[i]; if (!item.itemPermissions.allowZOrderChange) break; min = i; } for (let i = index + 1; i < itemsList.length; i++) { const item = itemsList[i]; if (!item.itemPermissions.allowZOrderChange) break; max = i; } return [min, max]; } redo() { throw new NotImplementedException(); } undo() { throw new NotImplementedException(); } } //# sourceMappingURL=MoveItemCommand.js.map