{"version":3,"file":"sort-children-of-modal.element-BmQuIY7F.js","sources":["../../../src/packages/core/tree/entity-actions/sort-children-of/modal/sort-children-of-modal.element.ts"],"sourcesContent":["import type { UmbSortChildrenOfModalData, UmbSortChildrenOfModalValue } from './sort-children-of-modal.token.js';\nimport type { PropertyValueMap } from '@umbraco-cms/backoffice/external/lit';\nimport { html, customElement, css, state, repeat, nothing } from '@umbraco-cms/backoffice/external/lit';\nimport { UmbTextStyles } from '@umbraco-cms/backoffice/style';\nimport { UmbModalBaseElement } from '@umbraco-cms/backoffice/modal';\nimport { UmbSorterController } from '@umbraco-cms/backoffice/sorter';\nimport { createExtensionApiByAlias } from '@umbraco-cms/backoffice/extension-registry';\nimport type { UmbTreeRepository, UmbTreeItemModel, UmbSortChildrenOfRepository } from '@umbraco-cms/backoffice/tree';\nimport { UmbPaginationManager } from '@umbraco-cms/backoffice/utils';\nimport { observeMultiple } from '@umbraco-cms/backoffice/observable-api';\n\nconst elementName = 'umb-sort-children-of-modal';\n\n@customElement(elementName)\nexport class UmbSortChildrenOfModalElement extends UmbModalBaseElement<\n\tUmbSortChildrenOfModalData,\n\tUmbSortChildrenOfModalValue\n> {\n\t@state()\n\t_children: Array = [];\n\n\t@state()\n\t_currentPage = 1;\n\n\t@state()\n\t_totalPages = 1;\n\n\t#pagination = new UmbPaginationManager();\n\t#sortedUniques = new Set();\n\t#sorter?: UmbSorterController;\n\n\tconstructor() {\n\t\tsuper();\n\t\tthis.#pagination.setPageSize(50);\n\n\t\tthis.observe(\n\t\t\tobserveMultiple([this.#pagination.currentPage, this.#pagination.totalPages]),\n\t\t\t([currentPage, totalPages]) => {\n\t\t\t\tthis._currentPage = currentPage;\n\t\t\t\tthis._totalPages = totalPages;\n\t\t\t},\n\t\t\t'umbPaginationObserver',\n\t\t);\n\t}\n\n\tprotected override async firstUpdated(\n\t\t_changedProperties: PropertyValueMap | Map,\n\t): Promise {\n\t\tsuper.firstUpdated(_changedProperties);\n\t\tthis.#requestChildren();\n\t}\n\n\tasync #requestChildren() {\n\t\tif (this.data?.unique === undefined) throw new Error('unique is required');\n\t\tif (!this.data?.treeRepositoryAlias) throw new Error('treeRepositoryAlias is required');\n\n\t\tconst treeRepository = await createExtensionApiByAlias>(\n\t\t\tthis,\n\t\t\tthis.data.treeRepositoryAlias,\n\t\t);\n\n\t\tconst { data } = await treeRepository.requestTreeItemsOf({\n\t\t\tparent: {\n\t\t\t\tunique: this.data.unique,\n\t\t\t\tentityType: this.data.entityType,\n\t\t\t},\n\t\t\tskip: this.#pagination.getSkip(),\n\t\t\ttake: this.#pagination.getPageSize(),\n\t\t});\n\n\t\tif (data) {\n\t\t\tthis._children = [...this._children, ...data.items];\n\t\t\tthis.#pagination.setTotalItems(data.total);\n\n\t\t\tif (data.total > 0) {\n\t\t\t\tthis.#initSorter();\n\t\t\t\tthis.#sorter?.setModel(this._children);\n\t\t\t}\n\t\t}\n\t}\n\n\t#initSorter() {\n\t\tif (this.#sorter) return;\n\n\t\tthis.#sorter = new UmbSorterController(this, {\n\t\t\tgetUniqueOfElement: (element) => {\n\t\t\t\treturn element.dataset.unique;\n\t\t\t},\n\t\t\tgetUniqueOfModel: (modelEntry) => {\n\t\t\t\treturn modelEntry.unique;\n\t\t\t},\n\t\t\tidentifier: 'Umb.SorterIdentifier.SortChildrenOfModal',\n\t\t\titemSelector: 'uui-ref-node',\n\t\t\tcontainerSelector: 'uui-ref-list',\n\t\t\tonChange: ({ model }) => {\n\t\t\t\tconst oldValue = this._children;\n\t\t\t\tthis._children = model;\n\t\t\t\tthis.requestUpdate('_children', oldValue);\n\t\t\t},\n\t\t\tonEnd: ({ item }) => {\n\t\t\t\tthis.#sortedUniques.add(item.unique);\n\t\t\t},\n\t\t});\n\t}\n\n\t#onLoadMore(event: PointerEvent) {\n\t\tevent.stopPropagation();\n\t\tif (this._currentPage >= this._totalPages) return;\n\t\tthis.#pagination.setCurrentPageNumber(this._currentPage + 1);\n\t\tthis.#requestChildren();\n\t}\n\n\tasync #onSubmit(event: PointerEvent) {\n\t\tevent?.stopPropagation();\n\t\tif (!this.data?.sortChildrenOfRepositoryAlias) throw new Error('sortChildrenOfRepositoryAlias is required');\n\n\t\tconst sortChildrenOfRepository = await createExtensionApiByAlias(\n\t\t\tthis,\n\t\t\tthis.data.sortChildrenOfRepositoryAlias,\n\t\t);\n\n\t\tconst { error } = await sortChildrenOfRepository.sortChildrenOf({\n\t\t\tunique: this.data.unique,\n\t\t\tsorting: this.#getSortOrderOfSortedItems(),\n\t\t});\n\n\t\tif (!error) {\n\t\t\tthis._submitModal();\n\t\t}\n\t}\n\n\t#getSortOrderOfSortedItems() {\n\t\tconst sorting = [];\n\n\t\t// get the new sort order from the sorted uniques\n\t\tfor (const value of this.#sortedUniques) {\n\t\t\tconst index = this._children.findIndex((child) => child.unique === value);\n\t\t\tif (index !== -1) {\n\t\t\t\tconst entry = {\n\t\t\t\t\tunique: value,\n\t\t\t\t\tsortOrder: index,\n\t\t\t\t};\n\n\t\t\t\tsorting.push(entry);\n\t\t\t}\n\t\t}\n\n\t\treturn sorting;\n\t}\n\n\toverride render() {\n\t\treturn html`\n\t\t\t\n\t\t\t\t${this.#renderChildren()}\n\t\t\t\t\n\t\t\t\t\n\t\t\t\n\t\t`;\n\t}\n\n\t#renderChildren() {\n\t\tif (this._children.length === 0) return html`There are no children`;\n\t\treturn html`\n\t\t\t\n\t\t\t\t${repeat(\n\t\t\t\t\tthis._children,\n\t\t\t\t\t(child) => child.unique,\n\t\t\t\t\t(child) => this.#renderChild(child),\n\t\t\t\t)}\n\t\t\t\n\n\t\t\t${this._currentPage < this._totalPages\n\t\t\t\t? html`\n\t\t\t\t\t\tLoad More (${this._currentPage}/${this._totalPages})\n\t\t\t\t\t`\n\t\t\t\t: nothing}\n\t\t`;\n\t}\n\n\t#renderChild(item: UmbTreeItemModel) {\n\t\treturn html``;\n\t}\n\n\tstatic override styles = [\n\t\tUmbTextStyles,\n\t\tcss`\n\t\t\t#loadMoreButton {\n\t\t\t\twidth: 100%;\n\t\t\t}\n\t\t`,\n\t];\n}\n\nexport { UmbSortChildrenOfModalElement as element };\n\ndeclare global {\n\tinterface HTMLElementTagNameMap {\n\t\t[elementName]: UmbSortChildrenOfModalElement;\n\t}\n}\n"],"names":["_pagination","_sortedUniques","_sorter","_UmbSortChildrenOfModalElement_instances","requestChildren_fn","initSorter_fn","onLoadMore_fn","onSubmit_fn","getSortOrderOfSortedItems_fn","renderChildren_fn","renderChild_fn","elementName","UmbSortChildrenOfModalElement","UmbModalBaseElement","__privateAdd","UmbPaginationManager","__privateGet","observeMultiple","currentPage","totalPages","_changedProperties","__privateMethod","html","treeRepository","createExtensionApiByAlias","data","__privateSet","UmbSorterController","element","modelEntry","model","oldValue","item","event","sortChildrenOfRepository","error","sorting","value","index","child","entry","repeat","nothing","UmbTextStyles","css","__decorateClass","state","customElement"],"mappings":";;;;;;;;;;;;;wYAAAA,GAAAC,GAAAC,GAAAC,GAAAC,GAAAC,GAAAC,GAAAC,GAAAC,GAAAC,GAAAC;AAWA,MAAMC,IAAc;AAGP,IAAAC,IAAN,cAA4CC,EAGjD;AAAA,EAcD,cAAc;AACP,aAlBDC,EAAA,MAAAX,CAAA,GAKN,KAAA,YAAqC,IAGtB,KAAA,eAAA,GAGD,KAAA,cAAA,GAEdW,EAAA,MAAAd,GAAc,IAAIe,EAAqB,CAAA,GACvCD,EAAA,MAAAb,uBAAqB,IAAY,CAAA,GACjCa,EAAA,MAAAZ,CAAA,GAIMc,EAAA,MAAAhB,CAAA,EAAY,YAAY,EAAE,GAE1B,KAAA;AAAA,MACJiB,EAAgB,CAACD,EAAK,MAAAhB,CAAA,EAAY,aAAagB,EAAK,MAAAhB,CAAA,EAAY,UAAU,CAAC;AAAA,MAC3E,CAAC,CAACkB,GAAaC,CAAU,MAAM;AAC9B,aAAK,eAAeD,GACpB,KAAK,cAAcC;AAAA,MACpB;AAAA,MACA;AAAA,IAAA;AAAA,EAEF;AAAA,EAEA,MAAyB,aACxBC,GACgB;AAChB,UA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