spreadjs_zh.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /**
  2. * Spreadjs 通用方法集
  3. *
  4. * @author Mai
  5. * @date 2018/02/06
  6. * @version
  7. */
  8. const spreadNS = GC.Spread.Sheets;
  9. const SpreadJsObj = {
  10. /**
  11. * 创建Spread(默认1张表,3行数据)
  12. * @param obj 用于创建spreadjs的Dom元素
  13. * @returns {GC.Spread.Sheets.Workbook}
  14. */
  15. createNewSpread: function (obj) {
  16. const spread = new spreadNS.Workbook(obj, {sheetCount: 1});
  17. spread.options.tabStripVisible = false;
  18. spread.options.scrollbarMaxAlign = true;
  19. spread.options.cutCopyIndicatorVisible = false;
  20. spread.options.allowCopyPasteExcelStyle = false;
  21. spread.options.allowUserDragDrop = false;
  22. spread.getActiveSheet().setRowCount(3);
  23. return spread;
  24. },
  25. /**
  26. * 保护sheet(需设置保护后, 单元格的locked等属性方可生效)
  27. * @param {GC.Spread.Sheets.Worksheet} sheet
  28. */
  29. protectedSheet: function (sheet) {
  30. const option = {
  31. allowSelectLockedCells: true,
  32. allowSelectUnlockedCells: true,
  33. allowResizeRows: true,
  34. allowResizeColumns: true
  35. };
  36. sheet.options.protectionOptions = option;
  37. sheet.options.isProtected = true;
  38. sheet.options.allowCellOverflow = false;
  39. },
  40. /**
  41. * sheet批量操作优化(sheet操作大批量数据时, 屏蔽数据刷新, 可优化大量时间)
  42. * @param {GC.Spread.Sheets.Worksheet} sheet
  43. * @param {function} operation
  44. */
  45. beginMassOperation: function (sheet) {
  46. sheet.suspendPaint();
  47. sheet.suspendEvent();
  48. },
  49. endMassOperation: function (sheet) {
  50. sheet.resumeEvent();
  51. sheet.resumePaint();
  52. },
  53. massOperationSheet: function (sheet, operation) {
  54. this.beginMassOperation(sheet);
  55. operation();
  56. this.endMassOperation(sheet);
  57. },
  58. /**
  59. * 获取Obj左顶点位置(部分功能需通过spreadjs左顶点位置计算)
  60. * @param obj
  61. * @returns {{x: number, y: number}}
  62. */
  63. getObjPos: function (obj) {
  64. let target = obj;
  65. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  66. target = obj.offsetParent;
  67. while (target) {
  68. pos.x += target.offsetLeft;
  69. pos.y += target.offsetTop;
  70. target = target.offsetParent;
  71. }
  72. return pos;
  73. },
  74. /**
  75. * 以下四个方法来自Spread示例, 参见官网或文档
  76. */
  77. getHitTest: function (obj, e, sheet) {
  78. var offset = obj.offset(),
  79. x = e.pageX - offset.left,
  80. y = e.pageY - offset.top;
  81. return sheet.hitTest(x, y);
  82. },
  83. getTargetSelection: function (sheet, target) {
  84. if (target.hitTestType === spreadNS.SheetArea.colHeader) {
  85. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  86. } else if (target.hitTestType === spreadNS.SheetArea.rowHeader) {
  87. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  88. } else if (target.hitTestType === spreadNS.SheetArea.viewport) {
  89. return sheet.getRange(target.row, target.col, 1, 1);
  90. } else if (target.hitTestType === spreadNS.SheetArea.corner) {
  91. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  92. };
  93. },
  94. getCellInSelections: function (selections, row, col) {
  95. const count = selections.length;
  96. let range;
  97. for (var i = 0; i < count; i++) {
  98. range = selections[i];
  99. if (range.contains(row, col)) {
  100. return range;
  101. }
  102. }
  103. return null;
  104. },
  105. checkTargetInSelection: function (selections, range) {
  106. var count = selections.length, sel;
  107. for (var i = 0; i < count; i++) {
  108. sel = selections[i];
  109. if (sel.containsRange(range)) {
  110. return true;
  111. }
  112. }
  113. return false;
  114. },
  115. /**
  116. * 获取 spread 在鼠标右键时, spread的选中区域
  117. * viewport, 选中鼠标点击单元格X, X不在原选中区域内, 则选中X
  118. * colHeader, 选中整列
  119. * rowHeader, 选中整行
  120. * corner, 全选
  121. * 该方法返回不符合需求时,可通过getHitTest/getTargetSelection/getCellInSelections/checkTargetInSelection来确定鼠标右键点击时,spread当前Sheet应选中的单元格
  122. * @param obj: 创建Spread的Dom元素(jquery-contextmenu.build方法的第一个变量可读取)
  123. * @param e: jquery-contextmenu.build方法的第二个变量
  124. * @param {GC.Spread.Sheets.Workbook} spread
  125. * @returns {*}
  126. */
  127. safeRightClickSelection: function (obj, e, spread) {
  128. const sheet = spread.getActiveSheet();
  129. const selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  130. if (!this.checkTargetInSelection(selections, range)) {
  131. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  132. }
  133. return target;
  134. },
  135. /**
  136. * sheet中 使用delete键,触发EndEdited事件
  137. * @param {GC.Spreads.Sheets.Workbook} spread
  138. * @param {function} fun
  139. */
  140. addDeleteBind: function (spread, fun) {
  141. spread.commandManager().register('deleteEvent', function () {
  142. fun(spread.getActiveSheet());
  143. });
  144. spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  145. spread.commandManager().setShortcutKey('deleteEvent', GC.Spread.Commands.Key.del, false, false, false, false);
  146. },
  147. /**
  148. * 根据sheet.zh_setting初始化sheet表头
  149. * @param {GC.Spread.Sheets.Worksheet} sheet
  150. */
  151. _initSheetHeader: function (sheet) {
  152. if (!sheet.zh_setting) { return; }
  153. sheet.setColumnCount(sheet.zh_setting.cols.length);
  154. sheet.setRowCount(sheet.zh_setting.headRows, spreadNS.SheetArea.colHeader);
  155. for (let iRow = 0; iRow < sheet.zh_setting.headRowHeight.length; iRow ++) {
  156. sheet.setRowHeight(iRow, sheet.zh_setting.headRowHeight[iRow], spreadNS.SheetArea.colHeader);
  157. }
  158. for (let iCol = 0; iCol < sheet.zh_setting.cols.length; iCol++) {
  159. const col = sheet.zh_setting.cols[iCol];
  160. const title = col.title.split('|');
  161. const colSpan = col.colSpan ? col.colSpan.split('|'): ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|'): ['1'];
  162. for (let i = 0; i < title.length; i++) {
  163. const cell = sheet.getCell(i, iCol, spreadNS.SheetArea.colHeader);
  164. cell.text(title[i]);
  165. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  166. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]), spreadNS.SheetArea.colHeader);
  167. }
  168. }
  169. sheet.setColumnWidth(iCol, col.width);
  170. }
  171. sheet.rowOutlines.direction(spreadNS.Outlines.OutlineDirection.backward);
  172. sheet.showRowOutline(false);
  173. if (sheet.zh_setting.defaultRowHeight) {
  174. sheet.defaults.rowHeight = sheet.zh_setting.defaultRowHeight;
  175. }
  176. },
  177. /**
  178. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  179. * @param {GC.Spread.Sheets.Worksheet} sheet
  180. * @param setting
  181. */
  182. initSheet: function (sheet, setting) {
  183. this.beginMassOperation(sheet);
  184. sheet.zh_setting = setting;
  185. this._initSheetHeader(sheet);
  186. sheet.setRowCount(sheet.zh_setting.emptyRows);
  187. sheet.extendCellType = {};
  188. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly);
  189. this.endMassOperation(sheet);
  190. },
  191. /**
  192. * 整个sheet重新加载数据
  193. * @param sheet
  194. */
  195. reLoadSheetData: function (sheet) {
  196. const self = this;
  197. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  198. this.beginMassOperation(sheet);
  199. try {
  200. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data)
  201. // 设置总行数
  202. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  203. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  204. // 控制空白行
  205. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  206. emptyRows.locked(sheet.zh_dataType === 'tree');
  207. // 单元格写入数据
  208. sortData.forEach(function (data, i) {
  209. sheet.zh_setting.cols.forEach(function (col, j) {
  210. const cell = sheet.getCell(i, j);
  211. if (col.field !== '' && data[col.field]) {
  212. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  213. } else {
  214. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  215. }
  216. });
  217. });
  218. // 设置列单元格格式
  219. sheet.zh_setting.cols.forEach(function (col, j) {
  220. if (!col.cellType) { return; }
  221. if (col.cellType === 'tree') {
  222. if (!sheet.extendCellType.tree) {
  223. sheet.extendCellType.tree = self.CellType.getTreeNodeCellType();
  224. }
  225. sheet.getRange(-1, j, -1, 1).cellType(sheet.extendCellType.tree);
  226. } else if (col.cellType === 'tip') {
  227. if (!sheet.extendCellType.tip) {
  228. sheet.extendCellType.tip = self.CellType.getTipCellType();
  229. }
  230. sheet.getRange(-1, j, -1, 1).cellType(sheet.extendCellType.tip);
  231. }
  232. if (col.formatter) {
  233. sheet.getRange(-1, j, -1, 1).format(col.formatter);
  234. }
  235. });
  236. this.endMassOperation(sheet);
  237. } catch (err) {
  238. this.endMassOperation(sheet);
  239. }
  240. },
  241. _loadRowData: function (sheet, data, row) {
  242. // 单元格重新写入数据
  243. if (!data) { return }
  244. sheet.zh_setting.cols.forEach(function (col, j) {
  245. const cell = sheet.getCell(row, j);
  246. if (col.field !== '' && data[col.field]) {
  247. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  248. } else {
  249. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  250. }
  251. if (col.formatter) {
  252. cell.formatter(col.formatter);
  253. }
  254. });
  255. },
  256. /**
  257. * 重新加载部分数据行
  258. * @param {GC.Spread.Sheets.Worksheet} sheet
  259. * @param {Number} row
  260. * @param {Number} count
  261. */
  262. reLoadRowData: function (sheet, row, count) {
  263. const self = this;
  264. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  265. this.beginMassOperation(sheet);
  266. try {
  267. // 清空原单元格数据
  268. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  269. // 单元格重新写入数据
  270. for (let i = row; i < row + count; i++) {
  271. const data = sortData[i];
  272. if (!data) { continue; }
  273. sheet.zh_setting.cols.forEach(function (col, j) {
  274. const cell = sheet.getCell(i, j);
  275. if (col.field !== '' && data[col.field]) {
  276. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  277. } else {
  278. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  279. }
  280. if (col.formatter) {
  281. cell.formatter(col.formatter);
  282. }
  283. });
  284. }
  285. this.endMassOperation(sheet);
  286. } catch (err) {
  287. this.endMassOperation(sheet);
  288. }
  289. },
  290. /**
  291. * 重新加载部分行数据
  292. * @param {GC.Spread.Sheets.Worksheet} sheet
  293. * @param {Array} rows
  294. */
  295. reLoadRowsData: function (sheet, rows) {
  296. const self = this;
  297. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  298. this.beginMassOperation(sheet);
  299. try {
  300. for (const row of rows) {
  301. // 清空原单元格数据
  302. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  303. const data = sortData[row];
  304. // 单元格重新写入数据
  305. sheet.zh_setting.cols.forEach(function (col, j) {
  306. // 设置值
  307. const cell = sheet.getCell(row, j);
  308. if (col.field !== '' && data[col.field]) {
  309. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  310. } else {
  311. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  312. }
  313. // 设置单元格格式
  314. if (col.formatter) {
  315. cell.formatter(col.formatter);
  316. }
  317. });
  318. };
  319. this.endMassOperation(sheet);
  320. } catch (err) {
  321. this.endMassOperation(sheet);
  322. }
  323. },
  324. reLoadNodesData: function (sheet, nodes) {
  325. this.beginMassOperation(sheet);
  326. nodes = nodes instanceof Array ? nodes : [nodes];
  327. for (const node of nodes) {
  328. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  329. this._loadRowData(sheet, node, sortData.indexOf(node));
  330. }
  331. this.endMassOperation(sheet);
  332. },
  333. /**
  334. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  335. * @param {GC.Spread.Sheets.Worksheet} sheet
  336. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  337. * @param {Array|PathTree} data - 对dataType对应
  338. */
  339. loadSheetData: function (sheet, dataType, data){
  340. sheet.zh_dataType = dataType;
  341. if (dataType === 'tree') {
  342. sheet.zh_tree = data;
  343. } else {
  344. sheet.zh_data = data;
  345. }
  346. this.protectedSheet(sheet);
  347. this.reLoadSheetData(sheet);
  348. },
  349. /**
  350. * 获取复制数据HTML格式(过滤不可见单元格)
  351. * @param {GC.Spread.Sheets.Worksheet} sheet
  352. * @returns {string}
  353. */
  354. getFilterCopyHTML: function (sheet) {
  355. const sel = sheet.getSelections()[0];
  356. const html = [];
  357. html.push('<table>');
  358. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  359. // 跳过隐藏行
  360. if (!sheet.getCell(i, -1).visible()) { continue; }
  361. const rowHtml = [];
  362. rowHtml.push('<tr>');
  363. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  364. const data = sheet.getText(i, j);
  365. rowHtml.push('<td>' + data + '</td>');
  366. }
  367. rowHtml.push('</tr>');
  368. html.push(rowHtml.join(''));
  369. }
  370. html.push('</table>');
  371. return html.join('');
  372. },
  373. /**
  374. * 获取复制数据Text格式(过滤不可见单元格)
  375. * @param {GC.Spread.Sheets.Worksheet} sheet
  376. * @returns {string}
  377. */
  378. getFilterCopyText: function (sheet) {
  379. const copyData = [];
  380. const sel = sheet.getSelections()[0];
  381. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  382. // 跳过隐藏行
  383. if (!sheet.getCell(i, -1).visible()) { continue; }
  384. const rowText = [];
  385. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  386. const data = sheet.getText(i, j);
  387. rowText.push(data);
  388. }
  389. copyData.push(rowText.join('\t'));
  390. }
  391. return copyData.join('\n');
  392. },
  393. locateTreeNode: function (sheet, id) {
  394. const tree = sheet.zh_tree;
  395. if (!tree) { return }
  396. const node = tree.getItems(id);
  397. if (!node) { return }
  398. const index = tree.nodes.indexOf(node);
  399. const sels = sheet.getSelections();
  400. sheet.setSelection(index, sels[0].col, 1, 1);
  401. sheet.showRow(index, spreadNS.VerticalPosition.center);
  402. },
  403. CellType: {
  404. /**
  405. * 获取树结构CellType
  406. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  407. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  408. * @returns {TreeNodeCellType}
  409. */
  410. getTreeNodeCellType: function () {
  411. const indent = 20;
  412. const levelIndent = -5;
  413. const halfBoxLength = 5;
  414. const halfExpandLength = 3;
  415. /**
  416. * 画一条线段
  417. * @param canvas - 画布
  418. * @param x1 - 线段起点 x
  419. * @param y1 - 线段起点 y
  420. * @param x2 - 线段终点 x
  421. * @param y2 - 线段终点 y
  422. * @param color - 线段颜色
  423. */
  424. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  425. canvas.save();
  426. // 设置偏移量
  427. canvas.translate(0.5, 0.5);
  428. canvas.beginPath();
  429. canvas.moveTo(x1, y1);
  430. canvas.lineTo(x2, y2);
  431. canvas.strokeStyle = color;
  432. canvas.stroke();
  433. canvas.restore();
  434. };
  435. /**
  436. * 画一个方框
  437. * @param {Object} canvas - 画布
  438. * @param {Object} rect - 方框区域
  439. * @param {String} lineColor - 画线颜色
  440. * @param {String} fillColor - 填充颜色
  441. */
  442. const drawBox = function (canvas, rect, lineColor, fillColor) {
  443. canvas.save();
  444. // 设置偏移量
  445. canvas.translate(0.5, 0.5);
  446. canvas.strokeStyle = lineColor;
  447. canvas.beginPath();
  448. canvas.moveTo(rect.left, rect.top);
  449. canvas.lineTo(rect.left, rect.bottom);
  450. canvas.lineTo(rect.right, rect.bottom);
  451. canvas.lineTo(rect.right, rect.top);
  452. canvas.lineTo(rect.left, rect.top);
  453. canvas.stroke();
  454. canvas.fillStyle = fillColor;
  455. canvas.fill();
  456. canvas.restore();
  457. };
  458. /**
  459. * 画树结构-展开收起按钮
  460. * @param {Object} canvas - 画布
  461. * @param {Number} x - 单元格左顶点坐标 x
  462. * @param {Number} y - 单元格左顶点坐标 y
  463. * @param {Number} w - 单元格宽度
  464. * @param {Number} h - 单元格高度
  465. * @param {Number} centerX - 按钮中央坐标
  466. * @param {Number} centerY - 按钮中央坐标
  467. * @param {Boolean} expanded - 当前节点展开收起状态
  468. */
  469. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded) {
  470. let rect = {
  471. top: centerY - halfBoxLength,
  472. bottom: centerY + halfBoxLength,
  473. left: centerX - halfBoxLength,
  474. right: centerX + halfBoxLength
  475. };
  476. let h1, h2, offset = 1;
  477. if (rect.left < x + w) {
  478. // 方框超出单元格宽度时,超出部分不画。
  479. rect.right = Math.min(rect.right, x + w);
  480. drawBox(canvas, rect, 'black', 'white');
  481. // 画中心十字
  482. // 画十字横线
  483. h1 = centerX - halfExpandLength;
  484. h2 = Math.min(centerX + halfExpandLength, x + w);
  485. if (h2 > h1) {
  486. drawLine(canvas, h1, centerY, h2, centerY, 'black');
  487. }
  488. // 画十字竖线
  489. if (!expanded && (centerX < x + w)) {
  490. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  491. }
  492. }
  493. };
  494. let TreeNodeCellType = function (){};
  495. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  496. const proto = TreeNodeCellType.prototype;
  497. /**
  498. * 绘制方法
  499. * @param {Object} canvas - 画布
  500. * @param value - cell.value
  501. * @param {Number} x - 单元格左顶点坐标 x
  502. * @param {Number} y - 单元格左顶点坐标 y
  503. * @param {Number} w - 单元格宽度
  504. * @param {Number} h - 单元格高度
  505. * @param {Object} style - cell.style
  506. * @param {Object} options
  507. */
  508. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  509. // 清理 画布--单元格范围 旧数据
  510. if (style.backColor) {
  511. canvas.save();
  512. canvas.fillStyle = style.backColor;
  513. canvas.fillRect(x, y, w, h);
  514. canvas.restore();
  515. } else {
  516. canvas.clearRect(x, y, w, h);
  517. }
  518. const tree = options.sheet.zh_tree;
  519. // 使用TreeCellType前,需定义sheet.tree
  520. if (tree) {
  521. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  522. if (node) {
  523. const showTreeLine = true;
  524. const centerX = Math.floor(x) + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  525. const centerY = Math.floor((y + (y + h)) / 2);
  526. // Draw Sibling Line
  527. if (showTreeLine) {
  528. // Draw Horizontal Line
  529. if (centerX < x + w) {
  530. const x1 = centerX + indent / 2;
  531. drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  532. }
  533. // Draw Vertical Line
  534. if (centerX < x + w) {
  535. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  536. const parent = tree.getParent(node);
  537. const y2 = y1 - centerY;
  538. if (node.order === 1 && !parent) {
  539. drawLine(canvas, centerX, centerY, centerX, y1, 'gray');
  540. } else {
  541. drawLine(canvas, centerX, y, centerX, y1, 'gray');
  542. }
  543. }
  544. }
  545. // Draw Expand Box
  546. if (!node.is_leaf) {
  547. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded);
  548. }
  549. // Draw Parent Line
  550. if (showTreeLine) {
  551. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  552. while (parent) {
  553. if (!tree.isLastSibling(parent)) {
  554. if (parentCenterX < x + w) {
  555. drawLine(canvas, parentCenterX, y, parentCenterX, y + h, 'gray');
  556. }
  557. }
  558. parent = tree.getParent(parent);
  559. parentCenterX -= (indent + levelIndent);
  560. }
  561. };
  562. // 重定位x
  563. x = x + (node.level + 1) * indent + (node.level) * levelIndent;
  564. w = w - (node.level - 1) * indent - (node.level) * levelIndent;
  565. }
  566. }
  567. // Drawing Text
  568. spreadNS.CellTypes.Text.prototype.paint.apply(this, arguments);
  569. };
  570. /**
  571. * 获取点击信息
  572. * @param {Number} x
  573. * @param {Number} y
  574. * @param {Object} cellStyle
  575. * @param {Object} cellRect
  576. * @param {Object} context
  577. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  578. */
  579. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  580. return {
  581. x: x,
  582. y: y,
  583. row: context.row,
  584. col: context.col,
  585. cellStyle: cellStyle,
  586. cellRect: cellRect,
  587. sheet: context.sheet,
  588. sheetArea: context.sheetArea
  589. };
  590. };
  591. /**
  592. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  593. * @param {Object} hitinfo - 见getHitInfo
  594. */
  595. proto.processMouseDown = function (hitinfo) {
  596. const offset = -1;
  597. const tree = hitinfo.sheet.zh_tree;
  598. if (!tree) { return; }
  599. const node = tree.nodes[hitinfo.row];
  600. if (!node) { return; }
  601. let centerX = hitinfo.cellRect.x + offset + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  602. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  603. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  604. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  605. const children = tree.getChildren(node);
  606. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  607. tree.loadChildren(node, function () {
  608. node.expanded = true;
  609. const children = tree.getChildren(node);
  610. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  611. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  612. });
  613. } else {
  614. tree.setExpanded(node, !node.expanded);
  615. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  616. const posterity = tree.getPosterity(node);
  617. for (const child of posterity) {
  618. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  619. }
  620. });
  621. hitinfo.sheet.repaint();
  622. }
  623. }
  624. };
  625. return new TreeNodeCellType();
  626. },
  627. /**
  628. * 获取带悬浮提示CellType
  629. * @returns {TipCellType}
  630. */
  631. getTipCellType: function () {
  632. const TipCellType = function () {};
  633. // 继承 SpreadJs定义的 普通的TextCellType
  634. TipCellType.prototype = new spreadNS.CellTypes.Text();
  635. const proto = TipCellType.prototype;
  636. /**
  637. * 获取点击信息
  638. * @param {Number} x
  639. * @param {Number} y
  640. * @param {Object} cellStyle
  641. * @param {Object} cellRect
  642. * @param {Object} context
  643. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  644. */
  645. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  646. return {
  647. x: x,
  648. y: y,
  649. row: context.row,
  650. col: context.col,
  651. cellStyle: cellStyle,
  652. cellRect: cellRect,
  653. sheet: context.sheet,
  654. sheetArea: context.sheetArea
  655. };
  656. };
  657. /**
  658. * 鼠标进入单元格事件 - 显示悬浮提示
  659. * @param {Object} hitinfo - 见getHitInfo返回值
  660. */
  661. proto.processMouseEnter = function (hitinfo) {
  662. const text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  663. const setting = hitinfo.sheet.setting;
  664. if (setting.pos && text && text !== '') {
  665. if (!this._toolTipElement) {
  666. let div = $('#autoTip')[0];
  667. if (!div) {
  668. div = document.createElement("div");
  669. $(div).css("position", "absolute")
  670. .css("border", "1px #C0C0C0 solid")
  671. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  672. .css("font", "9pt Arial")
  673. .css("background", "white")
  674. .css("padding", 5)
  675. .attr("id", 'autoTip');
  676. $(div).hide();
  677. document.body.insertBefore(div, null);
  678. }
  679. this._toolTipElement = div;
  680. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  681. $(this._toolTipElement).show("fast");
  682. }
  683. }
  684. };
  685. /**
  686. * 鼠标移出单元格事件 - 隐藏悬浮提示
  687. * @param {Object} hitinfo - 见getHitInfo返回值
  688. */
  689. proto.processMouseLeave = function (hitinfo) {
  690. if (this._toolTipElement) {
  691. $(this._toolTipElement).hide();
  692. this._toolTipElement = null;
  693. }
  694. };
  695. return new TipCellType();
  696. }
  697. }
  698. };