spreadjs_zh.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. this.massOperationSheet(sheet, function () {
  154. sheet.setColumnCount(sheet.zh_setting.cols.length);
  155. for (const i in sheet.zh_setting.cols) {
  156. const col = sheet.zh_setting.cols[i];
  157. const cell = sheet.getCell(0, i, spreadNS.SheetArea.colHeader);
  158. cell.text(col.title);
  159. sheet.setColumnWidth(i, col.width);
  160. }
  161. sheet.rowOutlines.direction(spreadNS.Outlines.OutlineDirection.backward);
  162. sheet.showRowOutline(false);
  163. if (sheet.zh_setting.defaultRowHeight) {
  164. sheet.defaults.rowHeight = sheet.zh_setting.defaultRowHeight;
  165. }
  166. });
  167. },
  168. /**
  169. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  170. * @param {GC.Spread.Sheets.Worksheet} sheet
  171. * @param setting
  172. */
  173. initSheet: function (sheet, setting) {
  174. sheet.zh_setting = setting;
  175. this.initSheetHeader(sheet);
  176. sheet.setRowCount(sheet.zh_setting.emptyRows);
  177. sheet.extendCellType = {};
  178. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly);
  179. },
  180. /**
  181. * 整个sheet重新加载数据
  182. * @param sheet
  183. */
  184. reLoadSheetData: function (sheet) {
  185. const self = this;
  186. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  187. this.beginMassOperation(sheet);
  188. try {
  189. // 设置总行数
  190. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  191. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  192. // 控制空白行
  193. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  194. emptyRows.locked(sheet.zh_dataType === 'tree');
  195. // 单元格写入数据
  196. sortData.forEach(function (data, i) {
  197. sheet.zh_setting.cols.forEach(function (col, j) {
  198. const cell = sheet.getCell(i, j);
  199. if (col.field !== '' && data[col.field]) {
  200. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  201. } else {
  202. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false);
  203. }
  204. });
  205. });
  206. // 设置列单元格格式
  207. sheet.zh_setting.cols.forEach(function (col, j) {
  208. if (!col.cellType) { return; }
  209. if (col.cellType === 'tree') {
  210. if (!sheet.extendCellType.tree) {
  211. sheet.extendCellType.tree = self.CellType.getTreeNodeCellType();
  212. }
  213. sheet.getRange(-1, j, -1, 1).cellType(sheet.extendCellType.tree);
  214. } else if (col.cellType === 'tip') {
  215. if (!sheet.extendCellType.tip) {
  216. sheet.extendCellType.tip = self.CellType.getTipCellType();
  217. }
  218. sheet.getRange(-1, j, -1, 1).cellType(sheet.extendCellType.tip);
  219. }
  220. });
  221. this.endMassOperation(sheet);
  222. } catch (err) {
  223. this.endMassOperation(sheet);
  224. }
  225. },
  226. /**
  227. * 重新加载部分数据行
  228. * @param {GC.Spread.Sheets.Worksheet} sheet
  229. * @param {Number} row
  230. * @param {Number} count
  231. */
  232. reLoadRowData: function (sheet, row, count) {
  233. const self = this;
  234. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  235. this.beginMassOperation(sheet);
  236. try {
  237. // 清空原单元格数据
  238. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  239. // 单元格重新写入数据
  240. for (let i = row; i < row + count; i++) {
  241. const data = sortData[i];
  242. if (!data) { continue; }
  243. sheet.zh_setting.cols.forEach(function (col, j) {
  244. const cell = sheet.getCell(i, j);
  245. if (col.field !== '' && data[col.field]) {
  246. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  247. } else {
  248. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false);
  249. }
  250. });
  251. };
  252. // 设置列单元格格式
  253. sheet.zh_setting.cols.forEach(function (col, j) {
  254. if (!col.cellType) { return; }
  255. if (col.cellType === 'tree') {
  256. if (!sheet.extendCellType.tree) {
  257. sheet.extendCellType.tree = self.CellType.getTreeNodeCellType();
  258. }
  259. sheet.getRange(row, j, count, 1).cellType(sheet.extendCellType.tree);
  260. } else if (col.cellType === 'tip') {
  261. if (!sheet.extendCellType.tip) {
  262. sheet.extendCellType.tip = self.CellType.getTipCellType();
  263. }
  264. sheet.getRange(row, j, count, 1).cellType(sheet.extendCellType.tip);
  265. }
  266. });
  267. this.endMassOperation(sheet);
  268. } catch (err) {
  269. this.endMassOperation(sheet);
  270. }
  271. },
  272. /**
  273. * 重新加载部分行数据
  274. * @param {GC.Spread.Sheets.Worksheet} sheet
  275. * @param {Array} rows
  276. */
  277. reLoadRowsData: function (sheet, rows) {
  278. const self = this;
  279. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  280. this.beginMassOperation(sheet);
  281. try {
  282. for (const row of rows) {
  283. // 清空原单元格数据
  284. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  285. const data = sortData[row];
  286. // 单元格重新写入数据
  287. sheet.zh_setting.cols.forEach(function (col, j) {
  288. // 设置值
  289. const cell = sheet.getCell(row, j);
  290. if (col.field !== '' && data[col.field]) {
  291. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  292. } else {
  293. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false);
  294. }
  295. // 设置单元格格式
  296. if (col.cellType) {
  297. if (col.cellType === 'tree') {
  298. if (!sheet.extendCellType.tree) {
  299. sheet.extendCellType.tree = self.CellType.getTreeNodeCellType();
  300. }
  301. sheet.getRange(row, j, 1, 1).cellType(sheet.extendCellType.tree);
  302. } else if (col.cellType === 'tip') {
  303. if (!sheet.extendCellType.tip) {
  304. sheet.extendCellType.tip = self.CellType.getTipCellType();
  305. }
  306. sheet.getRange(row, j, 1, 1).cellType(sheet.extendCellType.tip);
  307. }
  308. }
  309. });
  310. };
  311. this.endMassOperation(sheet);
  312. } catch (err) {
  313. this.endMassOperation(sheet);
  314. }
  315. },
  316. /**
  317. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  318. * @param {GC.Spread.Sheets.Worksheet} sheet
  319. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  320. * @param {Array|PathTree} data - 对dataType对应
  321. */
  322. loadSheetData: function (sheet, dataType, data){
  323. sheet.zh_dataType = dataType;
  324. if (dataType === 'tree') {
  325. sheet.zh_tree = data;
  326. } else {
  327. sheet.zh_data = data;
  328. }
  329. this.protectedSheet(sheet);
  330. this.reLoadSheetData(sheet);
  331. },
  332. /**
  333. * 获取复制数据HTML格式(过滤不可见单元格)
  334. * @param {GC.Spread.Sheets.Worksheet} sheet
  335. * @returns {string}
  336. */
  337. getFilterCopyHTML: function (sheet) {
  338. const sel = sheet.getSelections()[0];
  339. const html = [];
  340. html.push('<table>');
  341. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  342. // 跳过隐藏行
  343. if (!sheet.getCell(i, -1).visible()) { continue; }
  344. const rowHtml = [];
  345. rowHtml.push('<tr>');
  346. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  347. const data = sheet.getText(i, j);
  348. rowHtml.push('<td>' + data + '</td>');
  349. }
  350. rowHtml.push('</tr>');
  351. html.push(rowHtml.join(''));
  352. }
  353. html.push('</table>');
  354. return html.join('');
  355. },
  356. /**
  357. * 获取复制数据Text格式(过滤不可见单元格)
  358. * @param {GC.Spread.Sheets.Worksheet} sheet
  359. * @returns {string}
  360. */
  361. getFilterCopyText: function (sheet) {
  362. const copyData = [];
  363. const sel = sheet.getSelections()[0];
  364. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  365. // 跳过隐藏行
  366. if (!sheet.getCell(i, -1).visible()) { continue; }
  367. const rowText = [];
  368. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  369. const data = sheet.getText(i, j);
  370. rowText.push(data);
  371. }
  372. copyData.push(rowText.join('\t'));
  373. }
  374. return copyData.join('\n');
  375. },
  376. CellType: {
  377. /**
  378. * 获取树结构CellType
  379. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  380. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  381. * @returns {TreeNodeCellType}
  382. */
  383. getTreeNodeCellType: function () {
  384. const indent = 20;
  385. const levelIndent = -5;
  386. const halfBoxLength = 5;
  387. const halfExpandLength = 3;
  388. /**
  389. * 画一条线段
  390. * @param canvas - 画布
  391. * @param x1 - 线段起点 x
  392. * @param y1 - 线段起点 y
  393. * @param x2 - 线段终点 x
  394. * @param y2 - 线段终点 y
  395. * @param color - 线段颜色
  396. */
  397. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  398. canvas.save();
  399. // 设置偏移量
  400. canvas.translate(0.5, 0.5);
  401. canvas.beginPath();
  402. canvas.moveTo(x1, y1);
  403. canvas.lineTo(x2, y2);
  404. canvas.strokeStyle = color;
  405. canvas.stroke();
  406. canvas.restore();
  407. };
  408. /**
  409. * 画一个方框
  410. * @param {Object} canvas - 画布
  411. * @param {Object} rect - 方框区域
  412. * @param {String} lineColor - 画线颜色
  413. * @param {String} fillColor - 填充颜色
  414. */
  415. const drawBox = function (canvas, rect, lineColor, fillColor) {
  416. canvas.save();
  417. // 设置偏移量
  418. canvas.translate(0.5, 0.5);
  419. canvas.strokeStyle = lineColor;
  420. canvas.beginPath();
  421. canvas.moveTo(rect.left, rect.top);
  422. canvas.lineTo(rect.left, rect.bottom);
  423. canvas.lineTo(rect.right, rect.bottom);
  424. canvas.lineTo(rect.right, rect.top);
  425. canvas.lineTo(rect.left, rect.top);
  426. canvas.stroke();
  427. canvas.fillStyle = fillColor;
  428. canvas.fill();
  429. canvas.restore();
  430. };
  431. /**
  432. * 画树结构-展开收起按钮
  433. * @param {Object} canvas - 画布
  434. * @param {Number} x - 单元格左顶点坐标 x
  435. * @param {Number} y - 单元格左顶点坐标 y
  436. * @param {Number} w - 单元格宽度
  437. * @param {Number} h - 单元格高度
  438. * @param {Number} centerX - 按钮中央坐标
  439. * @param {Number} centerY - 按钮中央坐标
  440. * @param {Boolean} expanded - 当前节点展开收起状态
  441. */
  442. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded) {
  443. let rect = {
  444. top: centerY - halfBoxLength,
  445. bottom: centerY + halfBoxLength,
  446. left: centerX - halfBoxLength,
  447. right: centerX + halfBoxLength
  448. };
  449. let h1, h2, offset = 1;
  450. if (rect.left < x + w) {
  451. // 方框超出单元格宽度时,超出部分不画。
  452. rect.right = Math.min(rect.right, x + w);
  453. drawBox(canvas, rect, 'black', 'white');
  454. // 画中心十字
  455. // 画十字横线
  456. h1 = centerX - halfExpandLength;
  457. h2 = Math.min(centerX + halfExpandLength, x + w);
  458. if (h2 > h1) {
  459. drawLine(canvas, h1, centerY, h2, centerY, 'black');
  460. }
  461. // 画十字竖线
  462. if (!expanded && (centerX < x + w)) {
  463. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  464. }
  465. }
  466. };
  467. let TreeNodeCellType = function (){};
  468. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  469. const proto = TreeNodeCellType.prototype;
  470. /**
  471. * 绘制方法
  472. * @param {Object} canvas - 画布
  473. * @param value - cell.value
  474. * @param {Number} x - 单元格左顶点坐标 x
  475. * @param {Number} y - 单元格左顶点坐标 y
  476. * @param {Number} w - 单元格宽度
  477. * @param {Number} h - 单元格高度
  478. * @param {Object} style - cell.style
  479. * @param {Object} options
  480. */
  481. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  482. // 清理 画布--单元格范围 旧数据
  483. if (style.backColor) {
  484. canvas.save();
  485. canvas.fillStyle = style.backColor;
  486. canvas.fillRect(x, y, w, h);
  487. canvas.restore();
  488. } else {
  489. canvas.clearRect(x, y, w, h);
  490. }
  491. const tree = options.sheet.zh_tree;
  492. // 使用TreeCellType前,需定义sheet.tree
  493. if (tree) {
  494. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  495. if (node) {
  496. const showTreeLine = true;
  497. const centerX = Math.floor(x) + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  498. const centerY = Math.floor((y + (y + h)) / 2);
  499. // Draw Sibling Line
  500. if (showTreeLine) {
  501. // Draw Horizontal Line
  502. if (centerX < x + w) {
  503. const x1 = centerX + indent / 2;
  504. drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  505. }
  506. // Draw Vertical Line
  507. if (centerX < x + w) {
  508. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  509. const parent = tree.getParent(node);
  510. const y2 = y1 - centerY;
  511. if (node.order === 1 && !parent) {
  512. drawLine(canvas, centerX, centerY, centerX, y1, 'gray');
  513. } else {
  514. drawLine(canvas, centerX, y, centerX, y1, 'gray');
  515. }
  516. }
  517. }
  518. // Draw Expand Box
  519. if (!node.is_leaf) {
  520. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded);
  521. }
  522. // Draw Parent Line
  523. if (showTreeLine) {
  524. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  525. while (parent) {
  526. if (!tree.isLastSibling(parent)) {
  527. if (parentCenterX < x + w) {
  528. drawLine(canvas, parentCenterX, y, parentCenterX, y + h, 'gray');
  529. }
  530. }
  531. parent = tree.getParent(parent);
  532. parentCenterX -= (indent + levelIndent);
  533. }
  534. };
  535. // 重定位x
  536. x = x + (node.level + 1) * indent + (node.level) * levelIndent;
  537. w = w - (node.level - 1) * indent - (node.level) * levelIndent;
  538. }
  539. }
  540. // Drawing Text
  541. spreadNS.CellTypes.Text.prototype.paint.apply(this, arguments);
  542. };
  543. /**
  544. * 获取点击信息
  545. * @param {Number} x
  546. * @param {Number} y
  547. * @param {Object} cellStyle
  548. * @param {Object} cellRect
  549. * @param {Object} context
  550. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  551. */
  552. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  553. return {
  554. x: x,
  555. y: y,
  556. row: context.row,
  557. col: context.col,
  558. cellStyle: cellStyle,
  559. cellRect: cellRect,
  560. sheet: context.sheet,
  561. sheetArea: context.sheetArea
  562. };
  563. };
  564. /**
  565. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  566. * @param {Object} hitinfo - 见getHitInfo
  567. */
  568. proto.processMouseDown = function (hitinfo) {
  569. const offset = -1;
  570. const tree = hitinfo.sheet.zh_tree;
  571. if (!tree) { return; }
  572. const node = tree.nodes[hitinfo.row];
  573. if (!node) { return; }
  574. let centerX = hitinfo.cellRect.x + offset + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  575. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  576. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  577. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  578. if (!node.expanded && !node.is_leaf && tree.getChildren(node).length === 0) {
  579. tree.loadChildren(node, function () {
  580. node.expanded = true;
  581. const children = tree.getChildren(node);
  582. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  583. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  584. });
  585. } else {
  586. tree.setExpanded(node, !node.expanded);
  587. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  588. const posterity = tree.getPosterity(node);
  589. for (const child of posterity) {
  590. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  591. }
  592. });
  593. hitinfo.sheet.repaint();
  594. }
  595. }
  596. }
  597. return new TreeNodeCellType();
  598. },
  599. /**
  600. * 获取带悬浮提示CellType
  601. * @returns {TipCellType}
  602. */
  603. getTipCellType: function () {
  604. const TipCellType = function () {};
  605. // 继承 SpreadJs定义的 普通的TextCellType
  606. TipCellType.prototype = new spreadNS.CellTypes.Text();
  607. const proto = TipCellType.prototype;
  608. /**
  609. * 获取点击信息
  610. * @param {Number} x
  611. * @param {Number} y
  612. * @param {Object} cellStyle
  613. * @param {Object} cellRect
  614. * @param {Object} context
  615. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  616. */
  617. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  618. return {
  619. x: x,
  620. y: y,
  621. row: context.row,
  622. col: context.col,
  623. cellStyle: cellStyle,
  624. cellRect: cellRect,
  625. sheet: context.sheet,
  626. sheetArea: context.sheetArea
  627. };
  628. };
  629. /**
  630. * 鼠标进入单元格事件 - 显示悬浮提示
  631. * @param {Object} hitinfo - 见getHitInfo返回值
  632. */
  633. proto.processMouseEnter = function (hitinfo) {
  634. const text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  635. const setting = hitinfo.sheet.setting;
  636. if (setting.pos && text && text !== '') {
  637. if (!this._toolTipElement) {
  638. let div = $('#autoTip')[0];
  639. if (!div) {
  640. div = document.createElement("div");
  641. $(div).css("position", "absolute")
  642. .css("border", "1px #C0C0C0 solid")
  643. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  644. .css("font", "9pt Arial")
  645. .css("background", "white")
  646. .css("padding", 5)
  647. .attr("id", 'autoTip');
  648. $(div).hide();
  649. document.body.insertBefore(div, null);
  650. }
  651. this._toolTipElement = div;
  652. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  653. $(this._toolTipElement).show("fast");
  654. }
  655. }
  656. };
  657. /**
  658. * 鼠标移出单元格事件 - 隐藏悬浮提示
  659. * @param {Object} hitinfo - 见getHitInfo返回值
  660. */
  661. proto.processMouseLeave = function (hitinfo) {
  662. if (this._toolTipElement) {
  663. $(this._toolTipElement).hide();
  664. this._toolTipElement = null;
  665. }
  666. };
  667. return new TipCellType();
  668. }
  669. }
  670. };