tree_sheet_helper.js 22 KB

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