/** global: Craft */ /** global: Garnish */ Craft.FieldLayoutDesigner = Garnish.Base.extend({ $container: null, $tabContainer: null, $newTabBtn: null, $sidebar: null, $libraryToggle: null, $selectedLibrary: null, $fieldLibrary: null, $uiLibrary: null, $uiLibraryElements: null, $fieldSearch: null, $clearFieldSearchBtn: null, $fieldGroups: null, $fields: null, tabGrid: null, elementDrag: null, init: function(container, settings) { this.$container = $(container); this.setSettings(settings, Craft.FieldLayoutDesigner.defaults); let $workspace = this.$container.children('.fld-workspace'); this.$tabContainer = $workspace.children('.fld-tabs'); this.$newTabBtn = $workspace.children('.fld-new-tab-btn'); this.$sidebar = this.$container.children('.fld-sidebar'); this.$fieldLibrary = this.$selectedLibrary = this.$sidebar.children('.fld-field-library'); let $fieldSearchContainer = this.$fieldLibrary.children('.search'); this.$fieldSearch = $fieldSearchContainer.children('input'); this.$clearFieldSearchBtn = $fieldSearchContainer.children('.clear'); this.$fieldGroups = this.$sidebar.find('.fld-field-group'); this.$fields = this.$fieldGroups.children('.fld-element'); this.$uiLibrary = this.$sidebar.children('.fld-ui-library'); this.$uiLibraryElements = this.$uiLibrary.children(); // Set up the layout grids this.tabGrid = new Craft.Grid(this.$tabContainer, { itemSelector: '.fld-tab', minColWidth: 24 * 11, fillMode: 'grid', snapToGrid: 24 }); let $tabs = this.$tabContainer.children(); for (let i = 0; i < $tabs.length; i++) { this.initTab($($tabs[i])); } // create a placeholder input so *something* gets posted even if there are no tabs/elements $('', { type: 'hidden', name: this.settings.elementPlacementInputName.replace('[__TAB_NAME__][]', ''), value: '', }).insertBefore(this.$container); this.elementDrag = new Craft.FieldLayoutDesigner.ElementDrag(this); if (this.settings.customizableTabs) { this.tabDrag = new Craft.FieldLayoutDesigner.TabDrag(this); this.addListener(this.$newTabBtn, 'activate', 'addTab'); } // Set up the sidebar if (this.settings.customizableUi) { let $libraryPicker = this.$sidebar.children('.btngroup'); new Craft.Listbox($libraryPicker, { onChange: $selectedOption => { this.$selectedLibrary.addClass('hidden'); this.$selectedLibrary = this[`$${$selectedOption.data('library')}Library`] .removeClass('hidden'); }, }); } this.addListener(this.$fieldSearch, 'input', () => { let val = this.$fieldSearch.val().toLowerCase().replace(/['"]/g, ''); if (!val) { this.$fieldLibrary.find('.filtered').removeClass('filtered'); this.$clearFieldSearchBtn.addClass('hidden'); return; } this.$clearFieldSearchBtn.removeClass('hidden'); let $matches = this.$fields.filter(`[data-keywords*="${val}"]`) .add(this.$fieldGroups.filter(`[data-name*="${val}"]`).children('.fld-element')) .removeClass('filtered'); this.$fields.not($matches).addClass('filtered'); // hide any groups that don't have any results for (let i = 0; i < this.$fieldGroups.length; i++) { let $group = this.$fieldGroups.eq(i); if ($group.find('.fld-element:not(.hidden):not(.filtered)').length) { $group.removeClass('filtered'); } else { $group.addClass('filtered'); } } }); this.addListener(this.$fieldSearch, 'keydown', ev => { if (ev.keyCode === Garnish.ESC_KEY) { this.$fieldSearch.val('').trigger('input'); } }); // Clear the search when the X button is clicked this.addListener(this.$clearFieldSearchBtn, 'click', () => { this.$fieldSearch.val('').trigger('input'); }); }, initTab: function($tab) { if (this.settings.customizableTabs) { let $editBtn = $tab.find('.tabs .settings'); $('