'use strict'; /** * 配合/view/shares/merge_peg_modal.ejs * 依赖jQuery, lodash, spreadjs,请放在spreadjs_zh.js之后 * * @author Mai * @date * @version */ const NewMergePeg = function (setting) { let siHandle; const spread = SpreadJsObj.createNewSpread($('#mp-spread')[0]); const sheet = spread.getActiveSheet(); const spreadSetting = { cols: [ {title: '起始桩号', field: 'start-peg', hAlign: 0, width: 80, formatter: '@'}, {title: '终止桩号', field: 'end-peg', hAlign: 0, width: 80, formatter: '@'}, {title: '位置', field: 'peg-pos', hAlign: 0, width: 60, formatter: '@'}, {title: '合并结果', field: 'peg', hAlign: 0, width: 180, formatter: '@'}, ], emptyRows: 3, headRows: 1, headRowHeight: [32], headerFont: '12px 微软雅黑', font: '12px 微软雅黑', }; const findCol = function (field) { return _.findIndex(spreadSetting.cols, function (c) {return c.field === field}); }; const spCol = findCol('start-peg'), epCol = findCol('end-peg'), posCol = findCol('peg-pos'), pegCol = findCol('peg'); SpreadJsObj.initSheet(sheet, spreadSetting); sheet.setColumnWidth(0, 30, spreadNS.SheetArea.rowHeader); const spreadObj = { mergePeg(row) { const withPos = $('#mp-with-pos')[0].checked; const withSprChar = $('#mp-with-spr-char')[0].checked, sprChar = $('#mp-spr-char').val(); const mergeRow = function (row) { const startPeg = _.trim(sheet.getText(row, spCol)); const endPeg = _.trim(sheet.getText(row, epCol)); let peg; if (startPeg !== '') { peg = endPeg !== '' ? (withSprChar && endPeg.indexOf(sprChar) !== 0 ? startPeg + sprChar + endPeg : startPeg + endPeg) : startPeg; } else { peg = endPeg !== '' ? endPeg : ''; } if (withPos) { peg += _.trim(sheet.getText(row, posCol)); } sheet.setText(row, pegCol, peg); }; if (row) { const rows = row instanceof Array ? row : [row]; for (const r of rows) { mergeRow(r) } } else { for (let iRow = 0, iLen = sheet.getRowCount(); iRow < iLen; iRow++) { mergeRow(iRow); } } }, getPegs() { const result = []; for (let iRow = 0, iLen = sheet.getRowCount(); iRow < iLen; iRow++) { const peg = sheet.getText(iRow, pegCol); if (peg !== '') { result.push({name: peg, position: sheet.getText(iRow, posCol)}); } } return result; }, editStarting(e, info) { if (!info.sheet.zh_setting) return; const col = info.sheet.zh_setting.cols[info.col]; switch (col.field) { case 'peg': info.cancel = true; break; } }, clipboardPasted: function (e, info) { for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) { const curRow = info.cellRange.row + iRow; for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) { const curCol = info.cellRange.col+ iCol; const text = info.sheet.getText(curRow, curCol); if (curCol === spCol || curCol === epCol) { if (text.length > 10) { info.sheet.setText(curRow, curCol, text.substring(0, 10)); } } else if (curCol === posCol) { if (text.length > 50) { info.sheet.setText(curRow, curCol, text.substring(0, 50)); } } } } spreadObj.mergePeg(); }, deletePress: function (sheet) { if (!sheet.zh_setting) return; const sel = sheet.getSelections()[0], row = []; for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) { for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) { if (iCol !== pegCol) { sheet.setText(iRow, iCol, ''); } } row.push(iRow); } spreadObj.mergePeg(row); }, editEnded: function (e, info) { if (info.editingText) { if (info.col === spCol || info.col === epCol) { if (info.editingText.length > 10) { info.sheet.setText(info.row, info.col, info.editingText.substring(0, 10)); } } else if (info.col === posCol) { if (info.editingText.length > 50) { info.sheet.setText(info.row, info.col, info.editingText.substring(0, 50)); } } } spreadObj.mergePeg(info.row); }, cut: function (sheet, sel, callback) { if (!sheet || !sel) return; callback(); const rows = []; for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) { for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) { sheet.setText(iRow, iCol, ''); } rows.push(iRow); } spreadObj.mergePeg(rows); }, checkEmptyRow: function () { let nonEmptyRow = -1, count = sheet.getRowCount(); for (let iRow = count - 1; iRow >= 0; iRow--) { const sp = sheet.getText(iRow, spCol); const ep = sheet.getText(iRow, epCol); const p = sheet.getText(iRow, posCol); if (sp !== '' || ep !== '' || p !== '') { nonEmptyRow = iRow; break; } } if (nonEmptyRow >= 0 && count - nonEmptyRow <= 3) { sheet.addRows(nonEmptyRow + 1, 4 + nonEmptyRow - count); } }, }; spread.bind(spreadNS.Events.EditStarting, spreadObj.editStarting); spread.bind(spreadNS.Events.ClipboardPasted, spreadObj.clipboardPasted); spread.bind(spreadNS.Events.EditEnded, spreadObj.editEnded); SpreadJsObj.addDeleteBind(spread, spreadObj.deletePress); SpreadJsObj.addCutEvents(spread, spreadObj.cut); $.contextMenu({ selector: '#mp-spread', build: function ($trigger, e) { const target = SpreadJsObj.safeRightClickSelection($trigger, e, spread); return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader; }, items: { 'create': { name: '新增行', icon: 'fa-sign-in', callback: function (key, opt) { sheet.addRows(sheet.getRowCount(), 1); }, }, 'delete': { name: '删除行', icon: 'fa-remove', callback: function (key, opt) { const sel = sheet.getSelections()[0]; sheet.deleteRows(sel.row, sel.rowCount); }, }, } }); // 勾选位置 $('#mp-with-pos').click(() => {spreadObj.mergePeg();}); // 勾选桩号连接符 $('#mp-with-spr-char').click(function () { if (this.checked) { $('#mp-spr-char').show(); } else { $('#mp-spr-char').hide(); } spreadObj.mergePeg(); }); // 选择连接符 $('#mp-spr-char').change(() => {spreadObj.mergePeg()}); // 初始化窗口 $('#merge-peg').on('show.bs.modal', function () { sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data); }); $('#merge-peg').on('shown.bs.modal', function () { spread.refresh(); }); $('#mp-ok').click(() => { if (setting.callback) { setting.callback(spreadObj.getPegs()); } clearInterval(siHandle); $('#merge-peg').modal('hide'); }); const showModal = function () { $('#merge-peg').modal('show'); siHandle = setInterval(spreadObj.checkEmptyRow, 500); }; return {show: showModal}; };