tree_sheet_helper.js 21 KB

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