tree_sheet_helper.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var TREE_SHEET_HELPER = {
  5. getObjPos: function (obj) {
  6. let target = obj;
  7. let pos = { x: obj.offsetLeft, y: obj.offsetTop };
  8. target = obj.offsetParent;
  9. while (target) {
  10. pos.x += target.offsetLeft;
  11. pos.y += target.offsetTop;
  12. target = target.offsetParent;
  13. }
  14. return pos;
  15. },
  16. /**
  17. * 初始化setting,需要提示单元格text时,必须先初始化setting
  18. * @param obj
  19. * @param setting
  20. */
  21. initSetting: function (obj, setting) {
  22. setting.pos = this.getObjPos(obj);
  23. },
  24. createNewSpread: function (obj) {
  25. var spread = new GC.Spread.Sheets.Workbook(obj, { sheetCount: 1 });
  26. spread.options.tabStripVisible = false;
  27. spread.options.scrollbarMaxAlign = true;
  28. spread.options.cutCopyIndicatorVisible = false;
  29. spread.options.allowCopyPasteExcelStyle = false;
  30. spread.options.allowUserDragDrop = false;
  31. spread.options.allowContextMenu = false;
  32. spread.options.showDragFillSmartTag = false;
  33. spread.getActiveSheet().setRowCount(3);
  34. return spread;
  35. },
  36. getSheetCellStyle: function (setting) {
  37. var style = new GC.Spread.Sheets.Style();
  38. style.locked = setting.readOnly ? true : false;
  39. style.name = setting.id;
  40. style.font = setting.data.font;
  41. style.hAlign = setting.data.hAlign;
  42. style.vAlign = setting.data.vAlign;
  43. style.wordWrap = setting.data.wordWrap;
  44. if (setting.data.formatter) {
  45. style.formatter = setting.data.formatter;
  46. }
  47. return style;
  48. },
  49. loadSheetHeader: function (setting, sheet) {
  50. if (setting.frozenCols) {
  51. sheet.frozenColumnCount(setting.frozenCols);
  52. }
  53. sheet.setColumnCount(setting.cols.length);
  54. sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
  55. setting.headRowHeight.forEach(function (rowHeight, index) {
  56. sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
  57. });
  58. setting.cols.forEach(function (col, index) {
  59. var i, iRow = 0, cell;
  60. for (i = 0; i < col.head.spanCols.length; i++) {
  61. if (col.head.spanCols[i] !== 0) {
  62. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  63. cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(true);
  64. }
  65. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  66. sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  67. }
  68. iRow += col.head.spanRows[i];
  69. };
  70. sheet.setColumnWidth(index, col.width);
  71. });
  72. },
  73. protectdSheet: function (sheet) {
  74. var option = {
  75. allowSelectLockedCells: true,
  76. allowSelectUnlockedCells: true,
  77. allowResizeRows: true,
  78. allowResizeColumns: true
  79. };
  80. sheet.options.protectionOptions = option;
  81. sheet.options.isProtected = true;
  82. },
  83. massOperationSheet: function (sheet, Operation) {
  84. sheet.suspendPaint();
  85. sheet.suspendEvent();
  86. Operation();
  87. sheet.resumeEvent();
  88. sheet.resumePaint();
  89. },
  90. refreshNodesVisible: function (nodes, sheet, recursive) {
  91. nodes.forEach(function (node) {
  92. var iRow;
  93. iRow = node.serialNo();
  94. sheet.setRowVisible(iRow, node.visible, GC.Spread.Sheets.SheetArea.viewport);
  95. if (recursive) {
  96. TREE_SHEET_HELPER.refreshNodesVisible(node.children, sheet, recursive);
  97. }
  98. })
  99. },
  100. refreshTreeNodeData: function (setting, sheet, nodes, recursive) {
  101. nodes.forEach(function (node) {
  102. setting.cols.forEach(function (colSetting, iCol) {
  103. var iRow = node.serialNo();
  104. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  105. var getFieldText = function () {
  106. var fields = colSetting.data.field.split('.');
  107. var validField = fields.reduce(function (field1, field2) {
  108. if (eval('node.data.' + field1)) {
  109. return field1 + '.' + field2
  110. } else {
  111. return field1;
  112. }
  113. });
  114. if (eval('node.data.' + validField)) {
  115. return eval('node.data.' + validField);
  116. } else {
  117. return '';
  118. }
  119. };
  120. var getFieldText2 = function () {
  121. var fields = colSetting.data.field.split('.'), iField, data = node.data;
  122. for (iField = 0; iField < fields.length; iField++) {
  123. if (data[fields[iField]]) {
  124. data = data[fields[iField]];
  125. } else {
  126. return '';
  127. }
  128. }
  129. return data;
  130. };
  131. if (colSetting.data.getText && Object.prototype.toString.apply(colSetting.data.getText) === "[object Function]") {
  132. cell.value(colSetting.data.getText(node));
  133. } else {
  134. cell.value(getFieldText2());
  135. }
  136. if (colSetting.data.cellType && Object.prototype.toString.apply(colSetting.data.cellType) !== "[object String]") {
  137. if (colSetting.needCellType === undefined || colSetting.needCellType(node)) {
  138. cell.cellType(colSetting.data.cellType);
  139. }
  140. }
  141. if (colSetting.readOnly) {
  142. if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  143. cell.locked(colSetting.readOnly(node));
  144. } else {
  145. cell.locked(true);
  146. }
  147. } else {
  148. cell.locked(false);
  149. }
  150. });
  151. sheet.autoFitRow(node.serialNo());
  152. if (recursive) {
  153. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, node.children, recursive);
  154. }
  155. });
  156. },
  157. refreshChildrenVisiable: function (sheet, tree, node, row, visiable) {
  158. let iCount = node.posterityCount(), i, child;
  159. for (i = 0; i < iCount; i++) {
  160. child = tree.items[row + i + 1];
  161. sheet.setRowVisible(row + i + 1, visiable ? visiable : child.visible, GC.Spread.Sheets.SheetArea.viewport);
  162. }
  163. sheet.invalidateLayout();
  164. },
  165. showTreeData: function (setting, sheet, tree) {
  166. var indent = 20;
  167. var halfBoxLength = 5;
  168. var halfExpandLength = 3;
  169. var TreeNodeCellType = function () {
  170. };
  171. TreeNodeCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  172. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  173. if (style.backColor) {
  174. ctx.save();
  175. ctx.fillStyle = style.backColor;
  176. ctx.fillRect(x, y, w, h);
  177. ctx.restore();
  178. } else {
  179. ctx.clearRect(x, y, w, h);
  180. }
  181. // ������(x1, y1)���(��, ��), (x2, y2)�յ�(��, ��), ��ɫ
  182. var drawLine = function (canvas, x1, y1, x2, y2, color) {
  183. ctx.save();
  184. // ����ƫ����
  185. ctx.translate(0.5, 0.5);
  186. ctx.beginPath();
  187. ctx.moveTo(x1, y1);
  188. ctx.lineTo(x2, y2);
  189. ctx.strokeStyle = color;
  190. ctx.stroke();
  191. ctx.restore();
  192. };
  193. var drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  194. var rect = {}, h1, h2, offset = 1;
  195. rect.top = centerY - halfBoxLength;
  196. rect.bottom = centerY + halfBoxLength;
  197. rect.left = centerX - halfBoxLength;
  198. rect.right = centerX + halfBoxLength;
  199. if (rect.left < x + w) {
  200. rect.right = Math.min(rect.right, x + w);
  201. ctx.save();
  202. // ����ƫ����
  203. ctx.translate(0.5, 0.5);
  204. ctx.strokeStyle = 'black';
  205. ctx.beginPath();
  206. ctx.moveTo(rect.left, rect.top);
  207. ctx.lineTo(rect.left, rect.bottom);
  208. ctx.lineTo(rect.right, rect.bottom);
  209. ctx.lineTo(rect.right, rect.top);
  210. ctx.lineTo(rect.left, rect.top);
  211. ctx.stroke();
  212. ctx.fillStyle = 'white';
  213. ctx.fill();
  214. ctx.restore();
  215. // Draw Horizontal Line
  216. h1 = centerX - halfExpandLength;
  217. h2 = Math.min(centerX + halfExpandLength, x + w);
  218. if (h2 > h1) {
  219. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  220. }
  221. // Draw Vertical Line
  222. if (!expanded && (centerX < x + w)) {
  223. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  224. }
  225. }
  226. }
  227. var node = tree.items[options.row];
  228. var showTreeLine = true;
  229. if (!node) { return; }
  230. var iLevel = node.depth();
  231. var centerX = Math.floor(x) + node.depth() * indent + indent / 2;
  232. var x1 = centerX + indent / 2;
  233. var centerY = Math.floor((y + (y + h)) / 2);
  234. var y1;
  235. // Draw Sibling Line
  236. if (showTreeLine) {
  237. // Draw Horizontal Line
  238. if (centerX < x + w) {
  239. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  240. }
  241. // Draw Vertical Line
  242. if (centerX < x + w) {
  243. y1 = node.isLast() ? centerY : y + h;
  244. if (node.isFirst() && !node.parent) {
  245. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  246. } else {
  247. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  248. }
  249. }
  250. }
  251. // Draw Expand Box
  252. if (node.children.length > 0) {
  253. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  254. }
  255. // Draw Parent Line
  256. if (showTreeLine) {
  257. var parent = node.parent, parentCenterX = centerX - indent;
  258. while (parent) {
  259. if (!parent.isLast()) {
  260. if (parentCenterX < x + w) {
  261. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  262. }
  263. }
  264. parent = parent.parent;
  265. parentCenterX -= indent;
  266. }
  267. };
  268. // Draw Text
  269. x = x + (node.depth() + 1) * indent;
  270. w = w - (node.depth() + 1) * indent;
  271. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  272. };
  273. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  274. return {
  275. x: x,
  276. y: y,
  277. row: context.row,
  278. col: context.col,
  279. cellStyle: cellStyle,
  280. cellRect: cellRect,
  281. sheetArea: context.sheetArea
  282. };
  283. };
  284. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  285. var offset = -1;
  286. var node = tree.items[hitinfo.row];
  287. tree.selected = node;
  288. if (!node || node.children.length === 0) { return; }
  289. var centerX = hitinfo.cellRect.x + offset + node.depth() * indent + indent / 2;
  290. var centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  291. if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
  292. node.setExpanded(!node.expanded);
  293. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  294. var iCount = node.posterityCount(), i, child;
  295. for (i = 0; i < iCount; i++) {
  296. child = tree.items[hitinfo.row + i + 1];
  297. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  298. //hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.vis(), hitinfo.sheetArea);
  299. }
  300. hitinfo.sheet.invalidateLayout();
  301. });
  302. hitinfo.sheet.repaint();
  303. }
  304. };
  305. TreeNodeCellType.prototype.processMouseEnter = function (hitinfo) {
  306. if (hitinfo.sheet.name() === 'stdBillsGuidance_bills') {
  307. TREE_SHEET_HELPER.delayShowTips(hitinfo, setting);
  308. }
  309. };
  310. TreeNodeCellType.prototype.processMouseLeave = function (hitinfo) {
  311. TREE_SHEET_HELPER.hideTipsDiv();
  312. };
  313. let TipCellType = function () { };
  314. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  315. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  316. return {
  317. x: x,
  318. y: y,
  319. row: context.row,
  320. col: context.col,
  321. cellStyle: cellStyle,
  322. cellRect: cellRect,
  323. sheet: context.sheet,
  324. sheetArea: context.sheetArea
  325. };
  326. };
  327. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  328. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  329. if (setting.pos && text && text !== '') {
  330. if (!this._toolTipElement) {
  331. var div = document.createElement("div");
  332. $(div).css("position", "absolute")
  333. .css("border", "1px #C0C0C0 solid")
  334. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  335. .css("font", "9pt Arial")
  336. .css("background", "white")
  337. .css("padding", 5);
  338. this._toolTipElement = div;
  339. }
  340. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  341. $(this._toolTipElement).hide();
  342. document.body.insertBefore(this._toolTipElement, null);
  343. $(this._toolTipElement).show("fast");
  344. }
  345. };
  346. TipCellType.prototype.processMouseLeave = function (hininfo) {
  347. if (this._toolTipElement) {
  348. document.body.removeChild(this._toolTipElement);
  349. this._toolTipElement = null;
  350. }
  351. }
  352. TREE_SHEET_HELPER.protectdSheet(sheet);
  353. TREE_SHEET_HELPER.massOperationSheet(sheet, function () {
  354. sheet.rowOutlines.direction(GC.Spread.Sheets.Outlines.OutlineDirection.backward);
  355. sheet.showRowOutline(false);
  356. if (setting.defaultRowHeight) {
  357. sheet.defaults.rowHeight = setting.defaultRowHeight;
  358. }
  359. sheet.setRowCount(tree.count() + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  360. setting.cols.forEach(function (colSetting, iCol) {
  361. sheet.setStyle(-1, iCol, TREE_SHEET_HELPER.getSheetCellStyle(colSetting));
  362. if (colSetting.showHint) {
  363. sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
  364. }
  365. });
  366. sheet.getRange(-1, setting.treeCol, -1, 1).cellType(new TreeNodeCellType());
  367. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, tree.roots, true);
  368. TREE_SHEET_HELPER.refreshNodesVisible(tree.roots, sheet, true);
  369. });
  370. },
  371. showTipsDiv: function (text, setting, hitinfo) {
  372. if (text && text !== '') {
  373. if (text) text = replaceAll(/[\n]/, '<br>', text);
  374. if (!this._fixedTipElement) {
  375. let div = $('#fixedTip')[0];
  376. if (!div) {
  377. div = document.createElement("div");
  378. $(div).css("padding", 5)
  379. .attr("id", 'fixedTip');
  380. $(div).hide();
  381. document.body.insertBefore(div, null);
  382. }
  383. this._fixedTipElement = div;
  384. }
  385. $(this._fixedTipElement).width('');
  386. $(this._fixedTipElement).html(text);
  387. if (!this._toolTipElement) {
  388. let div = $('#autoTip')[0];
  389. if (!div) {
  390. div = document.createElement("div");
  391. $(div).css("position", "absolute")
  392. .css("border", "1px #C0C0C0 solid")
  393. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  394. .css("font", "0.9rem Calibri")
  395. .css("background", "white")
  396. .css("padding", 5)
  397. $(div).attr("id", 'autoTip');
  398. $(div).hide();
  399. document.body.insertBefore(div, null);
  400. }
  401. this._toolTipElement = div;
  402. $(this._toolTipElement).width('');
  403. //实时读取位置信息
  404. if (hitinfo.sheet && hitinfo.sheet.getParent().qo) {
  405. setting.pos = SheetDataHelper.getObjPos(hitinfo.sheet.getParent().qo);
  406. }
  407. $(this._toolTipElement).html(`<span>${text}</span><div class="triangle-border tb-border_up"></div><div class="triangle-border tb-background_up"></div>`);
  408. //清单指引、清单库做特殊处理
  409. if ($(hitinfo.sheet.name === 'stdBillsGuidance_bills')) {
  410. $(this._toolTipElement).html(`<span>${text}</span>`);
  411. let divWidth = $(this._fixedTipElement).width(),
  412. divHeight = $(this._fixedTipElement).height();
  413. if (divWidth > 600) {
  414. divWidth = 590;
  415. $(this._toolTipElement).width(divWidth);
  416. }
  417. let top = setting.pos.y + hitinfo.y - divHeight / 2 < 0 ? 0 : setting.pos.y + hitinfo.y - divHeight / 2;
  418. $(this._toolTipElement).css("top", top).css("left", hitinfo.x + 20);
  419. } else {
  420. //计算显示的初始位置
  421. /* 显示在单元格上方,三角形指向下的版本
  422. let top = setting.pos.y + hitinfo.cellRect.y -$(this._toolTipElement).height() -26;
  423. let left = setting.pos.x + hitinfo.cellRect.x;
  424. $(this._toolTipElement).css("top", top).css("left", left);*/
  425. //显示在下方,三角形指
  426. let top = setting.pos.y + hitinfo.cellRect.y + 26;
  427. let left = setting.pos.x + hitinfo.cellRect.x;
  428. $(this._toolTipElement).css("top", top).css("left", left);
  429. }
  430. $(this._toolTipElement).show("fast");
  431. TREE_SHEET_HELPER.tipDiv = 'show';//做个标记
  432. }
  433. }
  434. },
  435. hideTipsDiv: function () {
  436. TREE_SHEET_HELPER.tipTimeStamp = +new Date();//这个是为了造价书清单编号树节点的那个延时显示而打的时间戳,防止已经要隐藏的提示框,延时显示
  437. let me = TREE_SHEET_HELPER;
  438. TREE_SHEET_HELPER.tipDiv = 'hide';
  439. if (me._toolTipElement) {
  440. $(me._toolTipElement).hide();
  441. me._toolTipElement = null;
  442. }
  443. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  444. },
  445. tipDivCheck() {
  446. setTimeout(function () {
  447. let tips = $('#autoTip');
  448. if (TREE_SHEET_HELPER.tipDiv == 'show') {
  449. return;
  450. } else if (TREE_SHEET_HELPER.tipDiv == 'hide' && tips) {
  451. tips.hide();
  452. TREE_SHEET_HELPER._toolTipElement = null;
  453. }
  454. }, 600)
  455. },
  456. delayShowTips: function (hitinfo, setting, tips) {//延时显示
  457. let delayTimes = 500; //延时时间
  458. let now_timeStamp = +new Date();
  459. TREE_SHEET_HELPER.tipTimeStamp = now_timeStamp;
  460. setTimeout(function () {
  461. if (now_timeStamp - TREE_SHEET_HELPER.tipTimeStamp == 0) {//鼠标停下的时候才显示
  462. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  463. if (tips && tips != "") { //有tips的话优先显示tips
  464. tag = tips;
  465. }
  466. if (tag && tag != '') {
  467. TREE_SHEET_HELPER.showTipsDiv(tag, setting, hitinfo);
  468. }
  469. }
  470. }, delayTimes);
  471. }
  472. };