(function () { let grid = null; let currentComponent = null; // 生成唯一id var generateUniqueId = function () { const timestamp = Date.now().toString(36); const randomNum = Math.random().toString(36).substring(2); return timestamp + randomNum; }; /** 初始化 */ var init = function () { grid = GridStack.init({ // 一行高度 cellHeight: 80, // 间距 margin: 5, minRow: 8, acceptWidgets: true, float: true, }); GridStack.setupDragIn('#components-panel > .component-item', undefined, [ { w: 12, h: 2, type: 'banner', name: '我是轮播图', fontSize: 14, color: '#333', background: '#fff', detail: '', icon: 'fa-image', link: '', }, { w: 2, h: 1 }, ]); console.log(grid, 'grid实例'); var initData = [ // { // x: 0, // y: 0, // w: 2, // h: 2, // content: 'item 1', // data: { // name: 'item 1', // }, // }, // { // x: 2, // y: 3, // w: 3, // content: 'item 2', // data: { // name: 'item 2', // }, // }, ]; // grid.load(initData); grid.engine.nodes.forEach((item) => { handleAddComponent(item); }); grid.on('added', function (_event, itemArray) { console.log(itemArray, '这里触发了添加了added事件'); itemArray.forEach((item) => { handleAddComponent(item); }); }); }; // 处理添加组件之后的操作 var handleAddComponent = (component) => { component.id = `${component.type}_${generateUniqueId()}`; setComponentView(component); component.el.addEventListener('click', () => { console.log('点击组件', component); if (currentComponent && currentComponent.id === component.id) { return; } currentComponent = component; // 右侧显示组件属性列表 if (!$('#props-panel').find('form').hasClass('show')) { $('#props-panel').find('.wait-box').addClass('hidden'); $('#props-panel').find('form').attr('class', 'show'); } // 将当前选中组件的属性显示在右侧列表中 setCurrentComponentProps(currentComponent); }); }; //将当前选中组件的属性显示在右侧列表中 var setCurrentComponentProps = function (component) { const form = $('#props-panel').find('form'); form.find('#name').val(component.name); form.find('#background').val(component.background); form.find('#fontSize').val(component.fontSize); form.find('#color').val(component.color); form.find('#icon').val(component.icon); form.find('#detail').val(component.detail); form.find('#link').val(component.link); }; // 定义组件设置配置策略 const componentStrategies = { name: function (el, value) { el.find('span').text(value); }, background: function (el, value) { el.find('.grid-stack-item-content').css('background', value); }, fontSize: function (el, value) { el.find('span').css('font-size', value + 'px'); }, color: function (el, value) { el.find('span').css('color', value); }, icon: function (el, value) { el.find('i').attr('class', `fas ${value}`); }, // detail, link不需要反显页面所有不需要修改dom }; // 设置当前传递组件的视图展示 var setComponentView = function (component, fields = null) { const el = $(component.el); // 确定要处理的属性 const propsToHandle = fields || Object.keys(componentStrategies); // 遍历并执行对应的策略 propsToHandle.forEach((prop) => { if (componentStrategies[prop] && component[prop] !== undefined) { componentStrategies[prop](el, component[prop]); } }); }; // 定义映射关系:元素ID -> 事件类型 -> 组件属性 const elementMappings = { name: { event: 'blur', property: 'name' }, background: { event: 'blur', property: 'background' }, fontSize: { event: 'blur', property: 'fontSize' }, color: { event: 'blur', property: 'color' }, icon: { event: 'blur', property: 'icon' }, detail: { event: 'blur', property: 'detail' }, link: { event: 'blur', property: 'link' }, // icon: { event: 'change', property: 'icon' }, // select使用change }; // 统一绑定函数 var bindComponentEvents = function () { Object.keys(elementMappings).forEach((elementId) => { const mapping = elementMappings[elementId]; const $element = $('#' + elementId); if ($element.length) { // 组件名称添加失去焦点事件或者change事件 $element.off(mapping.event).on(mapping.event, function () { const value = $(this).val(); currentComponent[mapping.property] = value; setComponentView(currentComponent, [mapping.property]); }); } }); }; $('.global-save-button').click(function () { grid.addWidget({ w: 2, content: 'item 1' }); console.log(grid.save()); console.log(grid); }); /** 执行方法 */ $(function () { init(); // 调用绑定 bindComponentEvents(); }); })();