`; this.hud = new Garnish.HUD(this.$container, bodyHtml, { onShow: (e) => { // Hold off a sec until it's positioned... Garnish.requestAnimationFrame(() => { // Focus on the first text input this.hud.$main.find('.text:first').trigger('focus'); }); }, onSubmit: () => { this.applyHudSettings(); } }); Craft.initUiElements(this.hud.$main); if (this.requirable) { let $lightswitchField = Craft.ui.createLightswitchField({ label: Craft.t('app', 'Required'), id: `${this.key}-required`, name: 'required', on: isRequired, }).prependTo(this.hud.$main); } this.trigger('createSettingsHud'); }, applyHudSettings: function() { this.hud.$body.serializeArray().forEach(({name, value}) => { this.config[name] = value; }); this.updateConfigInput(); // update the UI let $spinner = this.hud.$body.find('.spinner').removeClass('hidden'); Craft.sendActionRequest('POST', 'fields/render-layout-element-selector', { data: { config: this.config, } }).then(response => { $spinner.addClass('hidden'); this.$editBtn.detach(); this.$container.html($(response.data.html).html()); this.initUi(); this.updateRequiredClass(); this.hud.hide(); }).catch(e => { // oh well, not worth fussing over console.error(e); $spinner.addClass('hidden'); this.updateRequiredClass(); this.hud.hide(); }); }, updatePlacementInput: function() { let $tab = this.$container.closest('.fld-tab').find('.tab span'); if (!$tab.length) { return; } let inputName = this.designer.getElementPlacementInputName($tab.text()); this.$placementInput.attr('name', inputName); }, updateConfigInput: function() { this.$configInput.val(JSON.stringify(this.config)); }, updateRequiredClass: function() { if (!this.requirable) { return; } if (this.config.required) { this.$container.addClass('fld-required'); } else { this.$container.removeClass('fld-required'); } }});Craft.FieldLayoutDesigner.BaseDrag = Garnish.Drag.extend({ designer: null, $insertion: null, showingInsertion: false, $caboose: null, /** * Constructor */ init: function(designer, settings) { this.designer = designer; this.base(this.findItems(), settings); }, /** * On Drag Start */ onDragStart: function() { this.base(); // Create the insertion this.$insertion = this.createInsertion(); // Add the caboose this.$caboose = this.createCaboose(); this.$items = $().add(this.$items.add(this.$caboose)); Garnish.$bod.addClass('dragging'); }, removeCaboose: function() { this.$items = this.$items.not(this.$caboose); this.$caboose.remove(); }, swapDraggeeWithInsertion: function() { this.$insertion.insertBefore(this.$draggee); this.$draggee.detach(); this.$items = $().add(this.$items.not(this.$draggee).add(this.$insertion)); this.showingInsertion = true; }, swapInsertionWithDraggee: function() { this.$insertion.replaceWith(this.$draggee); this.$items = $().add(this.$items.not(this.$insertion).add(this.$draggee)); this.showingInsertion = false; }, /** * Sets the item midpoints up front so we don't have to keep checking on every mouse move */ setMidpoints: function() { for (let i = 0; i < this.$items.length; i++) { let $item = $(this.$items[i]); let offset = $item.offset(); // Skip library elements if ($item.hasClass('unused')) { continue; } $item.data('midpoint', { left: offset.left + $item.outerWidth() / 2, top: offset.top + $item.outerHeight() / 2 }); } }, /** * Returns the closest item to the cursor. */ getClosestItem: function() { this.getClosestItem._closestItem = null; this.getClosestItem._closestItemMouseDiff = null; for (this.getClosestItem._i = 0; this.getClosestItem._i < this.$items.length; this.getClosestItem._i++) { this.getClosestItem._$item = $(this.$items[this.getClosestItem._i]); this.getClosestItem._midpoint = this.getClosestItem._$item.data('midpoint'); if (!this.getClosestItem._midpoint) { continue; } this.getClosestItem._mouseDiff = Garnish.getDist(this.getClosestItem._midpoint.left, this.getClosestItem._midpoint.top, this.mouseX, this.mouseY); if (this.getClosestItem._closestItem === null || this.getClosestItem._mouseDiff < this.getClosestItem._closestItemMouseDiff) { this.getClosestItem._closestItem = this.getClosestItem._$item[0]; this.getClosestItem._closestItemMouseDiff = this.getClosestItem._mouseDiff; } } return this.getClosestItem._closestItem; }, checkForNewClosestItem: function() { // Is there a new closest item? this.checkForNewClosestItem._closestItem = this.getClosestItem(); if (this.checkForNewClosestItem._closestItem === this.$insertion[0]) { return; } if (this.showingInsertion && ($.inArray(this.$insertion[0], this.$items) < $.inArray(this.checkForNewClosestItem._closestItem, this.$items)) && ($.inArray(this.checkForNewClosestItem._closestItem, this.$caboose) === -1) ) { this.$insertion.insertAfter(this.checkForNewClosestItem._closestItem); } else { this.$insertion.insertBefore(this.checkForNewClosestItem._closestItem); } this.$items = $().add(this.$items.add(this.$insertion)); this.showingInsertion = true; this.designer.tabGrid.refreshCols(true); this.setMidpoints(); }, /** * On Drag Stop */ onDragStop: function() { if (this.showingInsertion) { this.swapInsertionWithDraggee(); } this.removeCaboose(); this.designer.tabGrid.refreshCols(true); // return the helpers to the draggees let offset = this.$draggee.offset(); if (!offset || (offset.top === 0 && offset.left === 0)) { this.$draggee .css({ display: this.draggeeDisplay, visibility: 'visible', opacity: 0, }) .velocity({opacity: 1}, Garnish.FX_DURATION); this.helpers[0] .velocity({opacity: 0}, Garnish.FX_DURATION, () => { this._showDraggee(); }); } else { this.returnHelpersToDraggees(); } this.base(); Garnish.$bod.removeClass('dragging'); }});Craft.FieldLayoutDesigner.TabDrag = Craft.FieldLayoutDesigner.BaseDrag.extend({ /** * Constructor */ init: function(designer) { let settings = { handle: '.tab' }; this.base(designer, settings); }, findItems: function() { return this.designer.$tabContainer.find('> div.fld-tab'); }, /** * On Drag Start */ onDragStart: function() { this.base(); this.swapDraggeeWithInsertion(); this.setMidpoints(); }, swapDraggeeWithInsertion: function() { this.base(); this.designer.tabGrid.removeItems(this.$draggee); this.designer.tabGrid.addItems(this.$insertion); }, swapInsertionWithDraggee: function() { this.base(); this.designer.tabGrid.removeItems(this.$insertion); this.designer.tabGrid.addItems(this.$draggee); }, /** * On Drag */ onDrag: function() { this.checkForNewClosestItem(); this.base(); }, /** * On Drag Stop */ onDragStop: function() { this.base(); // "show" the tab, but make it invisible this.$draggee.css({ display: this.draggeeDisplay, visibility: 'hidden', }); }, /** * Creates the caboose */ createCaboose: function() { let $caboose = $('').appendTo(this.designer.$tabContainer); this.designer.tabGrid.addItems($caboose); return $caboose; }, /** * Removes the caboose */ removeCaboose: function() { this.base(); this.designer.tabGrid.removeItems(this.$caboose); }, /** * Creates the insertion */ createInsertion: function() { let $tab = this.$draggee.find('.tab'); return $(`
`); },});Craft.FieldLayoutDesigner.ElementDrag = Craft.FieldLayoutDesigner.BaseDrag.extend({ draggingLibraryElement: false, draggingField: false, /** * On Drag Start */ onDragStart: function() { this.base(); // Are we dragging an element from the library? this.draggingLibraryElement = this.$draggee.hasClass('unused'); // Is it a field? this.draggingField = this.$draggee.hasClass('fld-field'); // keep UI elements visible if (this.draggingLibraryElement && !this.draggingField) { this.$draggee.css({ display: this.draggeeDisplay, visibility: 'visible', }); } // Swap the draggee with the insertion if dragging a selected item if (!this.draggingLibraryElement) { this.swapDraggeeWithInsertion(); } this.setMidpoints(); }, /** * On Drag */ onDrag: function() { if (this.isDraggeeMandatory() || this.isHoveringOverTab()) { this.checkForNewClosestItem(); } else if (this.showingInsertion) { this.$insertion.remove(); this.$items = $().add(this.$items.not(this.$insertion)); this.showingInsertion = false; this.designer.tabGrid.refreshCols(true); this.setMidpoints(); } this.base(); }, isDraggeeMandatory: function() { return Garnish.hasAttr(this.$draggee, 'data-mandatory'); }, isHoveringOverTab: function() { for (let i = 0; i < this.designer.tabGrid.$items.length; i++) { if (Garnish.hitTest(this.mouseX, this.mouseY, this.designer.tabGrid.$items.eq(i))) { return true; } } return false; }, findItems: function() { // Return all of the used + unused fields return this.designer.$tabContainer.find('.fld-element') .add(this.designer.$sidebar.find('.fld-element')); }, /** * Creates the caboose */ createCaboose: function() { let $caboose = $(); let $fieldContainers = this.designer.$tabContainer.find('> .fld-tab > .fld-tabcontent'); for (let i = 0; i < $fieldContainers.length; i++) { $caboose = $caboose.add($('').appendTo($fieldContainers[i])); } return $caboose; }, /** * Creates the insertion */ createInsertion: function() { return $(``); }, /** * On Drag Stop */ onDragStop: function() { let showingInsertion = this.showingInsertion; if (showingInsertion) { if (this.draggingLibraryElement) { // Create a new element based on that one let $element = this.$draggee.clone().removeClass('unused'); this.designer.initElement($element); if (this.draggingField) { // Hide the library field this.$draggee.css({visibility: 'inherit', display: 'field'}).addClass('hidden'); // Hide the group too? if (this.$draggee.siblings('.fld-field:not(.hidden)').length === 0) { this.$draggee.closest('.fld-field-group').addClass('hidden'); } } // Set this.$draggee to the clone, as if we were dragging that all along this.$draggee = $element; // Remember it for later this.addItems($element); } } else if (!this.draggingLibraryElement) { let $libraryElement = this.draggingField ? this.designer.$fields.filter(`[data-attribute="${this.$draggee.data('attribute')}"]:first`) : this.designer.$uiLibraryElements.filter(`[data-type="${this.$draggee.data('type').replace(/\\/g, '\\\\')}"]:first`); if (this.draggingField) { // show the field in the library $libraryElement.removeClass('hidden'); $libraryElement.closest('.fld-field-group').removeClass('hidden'); } // forget the original element this.removeItems(this.$draggee); // Set this.$draggee to the library element, as if we were dragging that all along this.$draggee = $libraryElement; } this.base(); this.$draggee.css({ display: this.draggeeDisplay, visibility: this.draggingField || showingInsertion ? 'hidden' : 'visible', }); if (showingInsertion) { this.$draggee.data('fld-element').updatePlacementInput(); } }});