{"version":3,"file":"matrixExtendedJs-10dfc73b.js","sources":["../../../../../static/src/js/matrixExtended.ts"],"sourcesContent":["(function (window: any) {\n const {Craft, Garnish, $} = window;\n if (!Craft || !Garnish || !$) {\n return;\n }\n\n /**\n * @see https://github.com/craftcms/cms/blob/5.x/src/web/assets/matrix/src/MatrixInput.js\n * @todo check, that it only initializes on Inline Editable views\n */\n Craft.MatrixExtended = Garnish.Base.extend({\n settings: {}, childParent: {}, helper: undefined, itemDrag: undefined,\n\n init: function (config: { settings: any, childParent: any }, helper: any) {\n const self = this;\n this.settings = config.settings || {};\n this.childParent = config.childParent || {};\n this.helper = helper;\n\n if (!Garnish.DisclosureMenu || !Craft.MatrixInput) {\n return;\n }\n\n const disclosureMenuShowFn = Garnish.DisclosureMenu.prototype.show;\n Garnish.DisclosureMenu.prototype.show = function (...args: any[]) {\n self.initDisclosureMenu(this);\n disclosureMenuShowFn.apply(this, args);\n };\n\n const disclosureMenuInitFn = Garnish.DisclosureMenu.prototype.init;\n Garnish.DisclosureMenu.prototype.init = function (...args: any[]) {\n disclosureMenuInitFn.apply(this, args);\n self.initAddButtonMenu(this);\n self.prepareEntryDropZones();\n };\n\n const matrixUpdateAddEntryBtnFn = Craft.MatrixInput.prototype.updateAddEntryBtn;\n Craft.MatrixInput.prototype.updateAddEntryBtn = function (...args: any[]) {\n matrixUpdateAddEntryBtnFn.apply(this, args);\n self.checkAddBtn(this);\n };\n\n if (!this.settings.experimentalFeatures || !this.settings.enableDragDrop) {\n return;\n }\n\n const matrixInitFn = Craft.MatrixInput.prototype.init;\n Craft.MatrixInput.prototype.init = function (...args: any[]) {\n matrixInitFn.apply(this, args);\n this.entrySort.allowDragging = () => false;\n this.entrySort.destroy();\n self.prepareEntryDropZones();\n };\n\n Craft.MatrixInput.prototype.canAddMoreEntries = function () {\n return (\n !this.maxEntries ||\n this.$entriesContainer.children(':not(.matrix-extended-drop-target):not(.matrix-extended-buttons)').length < this.maxEntries\n );\n };\n\n this.itemDrag = new Garnish.DragDrop({\n activeDropTargetClass: 'active',\n minMouseDist: 10,\n hideDraggee: false,\n moveHelperToCursor: true,\n handle: (item: any) => $(item).find('> .actions > .move, > .titlebar'),\n filter: () => this.itemDrag.$targetItem.closest('.matrixblock'),\n dropTargets: () => {\n if (!this.childParent) {\n return [];\n }\n\n const {entry, typeId} = this.itemDrag.$draggee.closest('.matrixblock').data();\n const matrix = entry.matrix;\n if (!matrix || !typeId) {\n return [];\n }\n // TODO check `canAddMore`, but only if drag and drop is between fields\n // const $allDropTargets = $('.matrix-extended-drop-target').filter((_, x) => !!$(x).data('canAddMore'));\n const $allDropTargets = $('.matrix-extended-drop-target')\n .filter((_: any, x: any) => (this.childParent[typeId] || []).includes($(x).data('entryTypeId')));\n\n return $allDropTargets.toArray().reverse();\n },\n onDragStart: () => {\n Garnish.$bod.addClass('dragging');\n this.itemDrag.$draggee.closest('.matrixblock').addClass('draggee');\n this.$dropEntry = this.itemDrag.$draggee.data('entry').$container;\n this.$pullBlock = this.$dropEntry.closest('.matrix-field');\n },\n // TODO find better way, like changing the owner. Currently clone & delete is used\n onDragStop: async () => {\n this.itemDrag.$draggee.closest('.matrixblock').removeClass('draggee');\n Garnish.$bod.removeClass('dragging');\n if (!this.$dropEntry || !this.$pullBlock) {\n return this.itemDrag.returnHelpersToDraggees();\n }\n const $dropEntry = this.$dropEntry;\n const $dropTarget = this.itemDrag.$activeDropTarget;\n\n if (!$dropEntry || !$dropTarget) {\n return this.itemDrag.returnHelpersToDraggees();\n }\n\n let $relationEntry;\n let relationPosition = 'insertBefore';\n if ($dropTarget.data('position') === 'button') {\n $relationEntry = $dropTarget.closest('.matrix-field').find('> .blocks');\n relationPosition = 'appendTo';\n } else {\n $relationEntry = $dropTarget.next('.matrixblock');\n }\n\n const $pullBlock = this.$pullBlock;\n const $dropBlock = $dropTarget.closest('.matrix-field');\n if ($pullBlock.is($dropBlock)) {\n if (relationPosition === 'appendTo') {\n $dropEntry.appendTo($relationEntry);\n } else {\n $dropEntry.insertBefore($relationEntry);\n }\n\n // only update one block\n $pullBlock.data('matrix').entrySelect.resetItemOrder();\n } else {\n const matrix = $dropBlock.data('matrix');\n const matrix2 = $pullBlock.data('matrix');\n const entry = $dropEntry.data('entry');\n const typeId = $dropEntry.data('typeId');\n await this.duplicateWithNewOwner($relationEntry, relationPosition, typeId, entry, matrix, matrix2)\n // matrix.entrySelect.resetItemOrder();\n // matrix2.entrySelect.resetItemOrder();\n }\n\n this.itemDrag.returnHelpersToDraggees();\n this.prepareEntryDropZones();\n this.$dropEntry = undefined;\n this.$pullBlock = undefined;\n },\n });\n },\n\n prepareEntryDropZones() {\n if (!this.settings.experimentalFeatures || !this.settings.enableDragDrop) {\n return;\n }\n\n const $fields = $('.matrix-field');\n const $blocks = $fields.find('.matrixblock');\n\n $('.matrix-extended-drop-target').remove();\n for (const block of $blocks) {\n const $block = $(block);\n\n let $entryTypeId = null;\n const entry = $block.data('entry');\n if (!entry) {\n return;\n }\n const matrix = entry.matrix;\n if (!matrix) {\n return;\n }\n\n $entryTypeId = matrix.settings.fieldId;\n\n const $dropTargetBefore = $(`