import BsConfirm from "../Bootstrap/Confirm.js"; import DropGrid from '../Drop/Grid.js' import DashboardItem from "./Item.js"; import CachedWidgetLoader from "../../composables/Dashboard/CachedWidgetLoader.js"; import WidgetIcon from "./Widget/WidgetIcon.js" export default { name: 'Section', components: { DropGrid, DashboardItem, WidgetIcon, }, inject: { widgetsSetup:{ type: Array, default: [], }, adminMode: { type: Boolean, default: false }, editMode: { type: Boolean, default: false } }, props: [ "name", "widgets" ], emits: [ "widgetAdd", "widgetUpdate", "widgetRemove" ], data() { return { configOpened: false, gridWidth: 1, gridHeight: null, draggedItem:null, additionalRow:false, } }, provide() { return { editModeIsActive: Vue.computed(() => this.editModeIsActive ), sectionName: Vue.computed(() => this.name), } }, computed: { computedWidgetsSetup(){ if(!this.widgetsSetup) return {}; return this.widgetsSetup.reduce((acc, setup)=>{ acc[setup.widget_id] = setup.setup; return acc; },{}) }, editModeIsActive() { return (this.editMode || this.adminMode) && !this.configOpened }, getSectionStyle() { return 'margin-bottom: 8px;'; }, items() { // reuses the nearest placement of the widget from another viewport /* const computeNearestPlace = (item, gridWidth) =>{ let place; if (Object.keys(item.place).length > 0) { const nearestIndex = Object.keys(item.place) .sort((a, b) => Math.abs(a - gridWidth) - Math.abs(b - gridWidth)) .shift(); place = item.place[nearestIndex]; } else{ place = { x: 0, y: 0, w: 1, h: 1 }; } return place; } */ let placedItems = this.widgets.map(item => { if(!item?.widgetid && item?.id){ item.widgetid = item.id; } return { ...item, reorder: false, ...(item.place[this.gridWidth] || { reorder: true, ...{ x: 0, y: 0, w: 1, h: 1 } })}; }); return placedItems; }, }, methods: { sectionNameTranslation(){ switch(this.name){ case "general": return this.$p.t('dashboard',this.name); break; case "custom": return this.$p.t('dashboard',this.name); break; default: return this.name; break; } }, showSectionInformation(){ if (this.name == "general"){ return this.$p.t('dashboard', 'dashboardGeneralSectionDescription'); } else if(this.name == "custom"){ return this.$p.t('dashboard', 'dashboardCustomSectionDescription'); } else{ return this.$p.t('dashboard', 'dashboardSectionDescription', [this.name]); } }, handleConfigOpened() { this.configOpened = true }, handleConfigClosed() { this.configOpened = false }, checkResizeLimit(item, w, h) { // NOTE(chris): widgets needs to be loaded for this to work let widget = CachedWidgetLoader.getWidget(item.widget); if (widget) { let minmaxW = widget.setup.width; if (minmaxW.max) minmaxW.min = minmaxW.min || 1; else minmaxW = {min:minmaxW,max:minmaxW}; if (w < minmaxW.min) w = minmaxW.min; if (w > minmaxW.max) w = minmaxW.max; let minmaxH = widget.setup.height; if (minmaxH.max) minmaxH.min = minmaxH.min || 1; else minmaxH = {min:minmaxH,max:minmaxH}; if (h < minmaxH.min) h = minmaxH.min; if (h > minmaxH.max) h = minmaxH.max; } return [w, h]; }, removeWidget(item, revert) { if (item.custom) { BsConfirm.popup('Are you sure you want to delete this widget?').then(() => this.$emit('widgetRemove', this.name, item.id)); } else { let update = {}; update[item.id] = { hidden: !revert }; this.updatePreset(update); } }, saveConfig(config, item) { let payload = {}; payload[item.id] = { config }; this.updatePreset(payload); }, updatePositions(updated, pinned=false) { let result = {}; updated.forEach(update => { let item = {...update.item}; if (!item.placeholder) { if (!item.place[this.gridWidth]) item.place[this.gridWidth] = {x: 0, y: 0, w: 1, h: 1}; delete item.x; delete item.y; delete item.w; delete item.h; delete item.place[this.gridWidth].pinned; if (update.x !== undefined) item.place[this.gridWidth].x = update.x; if (update.y !== undefined) item.place[this.gridWidth].y = update.y; if (update.w !== undefined) item.place[this.gridWidth].w = update.w; if (update.h !== undefined) item.place[this.gridWidth].h = update.h; if (pinned){ item.place[this.gridWidth].pinned = true; } result[item.id] = item; } }); this.updatePreset(result); }, updatePreset(update) { let payload = {}; payload[this.name] = update; this.$emit('widgetUpdate', this.name, payload); } }, mounted() { let self = this; let cont = self.$refs.container; self.gridWidth = parseInt(window.getComputedStyle(cont).getPropertyValue('--fhc-dashboard-grid-size')); window.addEventListener('resize', () => { self.gridWidth = parseInt(window.getComputedStyle(cont).getPropertyValue('--fhc-dashboard-grid-size')); }); }, template: `