merge_peg.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 'use strict';
  2. /**
  3. * 配合/view/shares/merge_peg_modal.ejs
  4. * 依赖jQuery, lodash, spreadjs,请放在spreadjs_zh.js之后
  5. *
  6. * @author Mai
  7. * @date
  8. * @version
  9. */
  10. const NewMergePeg = function (setting) {
  11. let siHandle;
  12. const spread = SpreadJsObj.createNewSpread($('#mp-spread')[0]);
  13. const sheet = spread.getActiveSheet();
  14. const spreadSetting = {
  15. cols: [
  16. {title: '起始桩号', field: 'start-peg', hAlign: 0, width: 80, formatter: '@'},
  17. {title: '终止桩号', field: 'end-peg', hAlign: 0, width: 80, formatter: '@'},
  18. {title: '位置', field: 'peg-pos', hAlign: 0, width: 60, formatter: '@'},
  19. {title: '合并结果', field: 'peg', hAlign: 0, width: 180, formatter: '@'},
  20. ],
  21. emptyRows: 3,
  22. headRows: 1,
  23. headRowHeight: [32],
  24. headerFont: '12px 微软雅黑',
  25. font: '12px 微软雅黑',
  26. };
  27. const findCol = function (field) {
  28. return _.findIndex(spreadSetting.cols, function (c) {return c.field === field});
  29. };
  30. const spCol = findCol('start-peg'), epCol = findCol('end-peg'), posCol = findCol('peg-pos'), pegCol = findCol('peg');
  31. SpreadJsObj.initSheet(sheet, spreadSetting);
  32. sheet.setColumnWidth(0, 30, spreadNS.SheetArea.rowHeader);
  33. const spreadObj = {
  34. mergePeg(row) {
  35. const withPos = $('#mp-with-pos')[0].checked;
  36. const withSprChar = $('#mp-with-spr-char')[0].checked, sprChar = $('#mp-spr-char').val();
  37. const mergeRow = function (row) {
  38. const startPeg = _.trim(sheet.getText(row, spCol));
  39. const endPeg = _.trim(sheet.getText(row, epCol));
  40. let peg;
  41. if (startPeg !== '') {
  42. peg = endPeg !== ''
  43. ? (withSprChar && endPeg.indexOf(sprChar) !== 0 ? startPeg + sprChar + endPeg : startPeg + endPeg)
  44. : startPeg;
  45. } else {
  46. peg = endPeg !== '' ? endPeg : '';
  47. }
  48. if (withPos) {
  49. peg += _.trim(sheet.getText(row, posCol));
  50. }
  51. sheet.setText(row, pegCol, peg);
  52. };
  53. if (row) {
  54. const rows = row instanceof Array ? row : [row];
  55. for (const r of rows) {
  56. mergeRow(r)
  57. }
  58. } else {
  59. for (let iRow = 0, iLen = sheet.getRowCount(); iRow < iLen; iRow++) {
  60. mergeRow(iRow);
  61. }
  62. }
  63. },
  64. getPegs() {
  65. const result = [];
  66. for (let iRow = 0, iLen = sheet.getRowCount(); iRow < iLen; iRow++) {
  67. const peg = sheet.getText(iRow, pegCol);
  68. if (peg !== '') {
  69. result.push({name: peg, position: sheet.getText(iRow, posCol)});
  70. }
  71. }
  72. return result;
  73. },
  74. editStarting(e, info) {
  75. if (!info.sheet.zh_setting) return;
  76. const col = info.sheet.zh_setting.cols[info.col];
  77. switch (col.field) {
  78. case 'peg':
  79. info.cancel = true;
  80. break;
  81. }
  82. },
  83. clipboardPasted: function (e, info) {
  84. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  85. const curRow = info.cellRange.row + iRow;
  86. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  87. const curCol = info.cellRange.col+ iCol;
  88. const text = info.sheet.getText(curRow, curCol);
  89. if (curCol === spCol || curCol === epCol) {
  90. if (text.length > 10) {
  91. info.sheet.setText(curRow, curCol, text.substring(0, 10));
  92. }
  93. } else if (curCol === posCol) {
  94. if (text.length > 50) {
  95. info.sheet.setText(curRow, curCol, text.substring(0, 50));
  96. }
  97. }
  98. }
  99. }
  100. spreadObj.mergePeg();
  101. },
  102. deletePress: function (sheet) {
  103. if (!sheet.zh_setting) return;
  104. const sel = sheet.getSelections()[0], row = [];
  105. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  106. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  107. if (iCol !== pegCol) {
  108. sheet.setText(iRow, iCol, '');
  109. }
  110. }
  111. row.push(iRow);
  112. }
  113. spreadObj.mergePeg(row);
  114. },
  115. editEnded: function (e, info) {
  116. if (info.editingText) {
  117. if (info.col === spCol || info.col === epCol) {
  118. if (info.editingText.length > 10) {
  119. info.sheet.setText(info.row, info.col, info.editingText.substring(0, 10));
  120. }
  121. } else if (info.col === posCol) {
  122. if (info.editingText.length > 50) {
  123. info.sheet.setText(info.row, info.col, info.editingText.substring(0, 50));
  124. }
  125. }
  126. }
  127. spreadObj.mergePeg(info.row);
  128. },
  129. cut: function (sheet, sel, callback) {
  130. if (!sheet || !sel) return;
  131. callback();
  132. const rows = [];
  133. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  134. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  135. sheet.setText(iRow, iCol, '');
  136. }
  137. rows.push(iRow);
  138. }
  139. spreadObj.mergePeg(rows);
  140. },
  141. checkEmptyRow: function () {
  142. let nonEmptyRow = -1, count = sheet.getRowCount();
  143. for (let iRow = count - 1; iRow >= 0; iRow--) {
  144. const sp = sheet.getText(iRow, spCol);
  145. const ep = sheet.getText(iRow, epCol);
  146. const p = sheet.getText(iRow, posCol);
  147. if (sp !== '' || ep !== '' || p !== '') {
  148. nonEmptyRow = iRow;
  149. break;
  150. }
  151. }
  152. if (nonEmptyRow >= 0 && count - nonEmptyRow <= 3) {
  153. sheet.addRows(nonEmptyRow + 1, 4 + nonEmptyRow - count);
  154. }
  155. },
  156. };
  157. spread.bind(spreadNS.Events.EditStarting, spreadObj.editStarting);
  158. spread.bind(spreadNS.Events.ClipboardPasted, spreadObj.clipboardPasted);
  159. spread.bind(spreadNS.Events.EditEnded, spreadObj.editEnded);
  160. SpreadJsObj.addDeleteBind(spread, spreadObj.deletePress);
  161. SpreadJsObj.addCutEvents(spread, spreadObj.cut);
  162. $.contextMenu({
  163. selector: '#mp-spread',
  164. build: function ($trigger, e) {
  165. const target = SpreadJsObj.safeRightClickSelection($trigger, e, spread);
  166. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  167. },
  168. items: {
  169. 'create': {
  170. name: '新增行',
  171. icon: 'fa-sign-in',
  172. callback: function (key, opt) {
  173. sheet.addRows(sheet.getRowCount(), 1);
  174. },
  175. },
  176. 'delete': {
  177. name: '删除行',
  178. icon: 'fa-remove',
  179. callback: function (key, opt) {
  180. const sel = sheet.getSelections()[0];
  181. sheet.deleteRows(sel.row, sel.rowCount);
  182. },
  183. },
  184. }
  185. });
  186. // 勾选位置
  187. $('#mp-with-pos').click(() => {spreadObj.mergePeg();});
  188. // 勾选桩号连接符
  189. $('#mp-with-spr-char').click(function () {
  190. if (this.checked) {
  191. $('#mp-spr-char').show();
  192. } else {
  193. $('#mp-spr-char').hide();
  194. }
  195. spreadObj.mergePeg();
  196. });
  197. // 选择连接符
  198. $('#mp-spr-char').change(() => {spreadObj.mergePeg()});
  199. // 初始化窗口
  200. $('#merge-peg').on('show.bs.modal', function () {
  201. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(),
  202. spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  203. });
  204. $('#merge-peg').on('shown.bs.modal', function () {
  205. spread.refresh();
  206. });
  207. $('#mp-ok').click(() => {
  208. if (setting.callback) {
  209. setting.callback(spreadObj.getPegs());
  210. }
  211. clearInterval(siHandle);
  212. $('#merge-peg').modal('hide');
  213. });
  214. const showModal = function () {
  215. $('#merge-peg').modal('show');
  216. siHandle = setInterval(spreadObj.checkEmptyRow, 500);
  217. };
  218. return {show: showModal};
  219. };