pm_share.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/6/28
  7. * @version
  8. */
  9. const pmShare = (function () {
  10. const spreadDom = $('#shareSpread');
  11. let shareSeleted = null;
  12. let spreadObj = {workBook: null, sheet: null};
  13. let preSelection = null;
  14. //项目分享类型,由别人分享给自己的,和自己分享给别人的
  15. const shareType = {receive: 'receive', shareTo: 'shareTo'};
  16. //操作类型
  17. const oprType = {copy: 'copy', cancel: 'cancel'};
  18. let tree = null,
  19. actualIDShareInfo = {};//项目真实树ID与项目分享信息映射
  20. let curCopySelTree = null; //拷贝工程建设项目选项树
  21. const treeCol = 0;
  22. const treeSetting = {
  23. tree: {
  24. id: 'ID',
  25. pid: 'ParentID',
  26. nid: 'NextSiblingID',
  27. rootId: -1,
  28. autoUpdate: false
  29. }
  30. };
  31. const headers = [
  32. {name: '工程列表', dataCode: 'name', width: 300, rateWidth: 0.55, vAlign: 'center', hAlign: 'left'},
  33. {name: '来自', dataCode: 'from', width: 80, rateWidth: 0.15, vAlign: 'center', hAlign: 'left'},
  34. {name: '分享时间', dataCode: 'shareDate', width: 140, rateWidth: 0.15, vAlign: 'center', hAlign: 'left'},
  35. {name: '工程造价', dataCode: 'engineeringCost', width: 100, vAlign: 'center', hAlign: 'right', formatter: '0.00'},
  36. {name: '分部分项合计', dataCode: 'subEngineering', width: 100, vAlign: 'center', hAlign: 'right', formatter: '0.00'},
  37. {name: '措施项目合计', dataCode: 'measure', width: 100, vAlign: 'center', hAlign: 'right', formatter: '0.00'},
  38. {name: '其他项目合计', dataCode: 'other', width: 100, vAlign: 'center', hAlign: 'right', formatter: '0.00'},
  39. {name: '规费', dataCode: 'charge', width: 100, vAlign: 'center', hAlign: 'right', formatter: '0.00'},
  40. {name: '税金', dataCode: 'tax', width: 100, vAlign: 'center', hAlign: 'right', formatter: '0.00'},
  41. {name: '占造价比例(%)', dataCode: 'rate', width: 100, vAlign: 'center', hAlign: 'right', formatter: '0.00'},
  42. {name: '建筑面积', dataCode: 'buildingArea', width: 100, vAlign: 'center', hAlign: 'right', formatter: '0.00'},
  43. {name: '单方造价', dataCode: 'perCost', width: 100, vAlign: 'center', hAlign: 'right', formatter: '0.00'},
  44. {name: '工程专业', dataCode: 'feeStandardName', width: 100, vAlign: 'center', hAlign: 'left'},
  45. ];
  46. const spreadOpts = {
  47. workBook: {
  48. tabStripVisible: false,
  49. allowContextMenu: false,
  50. allowCopyPasteExcelStyle : false,
  51. allowExtendPasteRange: false,
  52. allowUserDragDrop : false,
  53. allowUserDragFill: false,
  54. scrollbarMaxAlign : true
  55. },
  56. sheet: {
  57. isProtected: true,
  58. frozenlineColor : '#ababab',
  59. protectionOptions: {allowResizeRows: true, allowResizeColumns: true},
  60. clipBoardOptions: GC.Spread.Sheets.ClipboardPasteOptions.values
  61. }
  62. };
  63. const spreadEvents = {
  64. SelectionChanging: function (sender, info) {
  65. initSelection(info.newSelections[0], info.oldSelections[0]);
  66. }
  67. };
  68. //设置选中行底色
  69. //@param
  70. function setSelStyle(sel, backColor,sheet) {
  71. sel.row = sel.row === -1 ? 0 : sel.row;
  72. renderSheetFunc(sheet, function () {
  73. let style = projTreeObj.getSelStyle(backColor);
  74. for(let i = 0; i < sel.rowCount; i++){
  75. let row = i + sel.row;
  76. sheet.setStyle(row, -1, style);
  77. }
  78. });
  79. }
  80. //初始化焦点
  81. //@param {Object}newSel {Object}oldSel @return {void}
  82. function initSelection(newSel, oldSel = null) {
  83. let node = tree.items[newSel.row];
  84. tree.selected = node;
  85. shareSeleted = node;
  86. //恢复底色
  87. if(oldSel){
  88. setSelStyle(oldSel, projTreeObj.setting.style.defalutBackColor, spreadObj.sheet);
  89. }
  90. //设置选中行底色
  91. if(newSel){
  92. setSelStyle(newSel, projTreeObj.setting.style.selectedColor, spreadObj.sheet);
  93. }
  94. preSelection = newSel;
  95. }
  96. //渲染时方法,停止渲染
  97. //@param {Object}sheet {Function}func @return {void}
  98. function renderSheetFunc(sheet, func){
  99. sheet.suspendEvent();
  100. sheet.suspendPaint();
  101. if(func){
  102. func();
  103. }
  104. sheet.resumeEvent();
  105. sheet.resumePaint();
  106. }
  107. //设置表选项
  108. //@param {Object}workBook {Object}opts @return {void}
  109. function setSpreadOptions (workBook, opts) {
  110. for(let opt in opts.workBook){
  111. workBook.options[opt] = opts.workBook[opt];
  112. }
  113. for(let opt in opts.sheet){
  114. workBook.getActiveSheet().options[opt] = opts.sheet[opt];
  115. }
  116. }
  117. //建表头
  118. //@param {Object}sheet {Array}headers @return {void}
  119. function buildHeader(sheet, headers) {
  120. let fuc = function () {
  121. sheet.setColumnCount(headers.length);
  122. sheet.setRowHeight(0, 40, GC.Spread.Sheets.SheetArea.colHeader);
  123. //let spreadWidth = getWorkBookWidth();
  124. for(let i = 0, len = headers.length; i < len; i++){
  125. sheet.setColumnWidth(i, headers[i].width, GC.Spread.Sheets.SheetArea.colHeader);
  126. if(headers[i].formatter){
  127. sheet.setFormatter(-1, i, headers[i].formatter);
  128. }
  129. sheet.setValue(0, i, headers[i].name, GC.Spread.Sheets.SheetArea.colHeader);
  130. sheet.getRange(-1, i, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[headers[i]['hAlign']]);
  131. sheet.getRange(-1, i, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[headers[i]['vAlign']]);
  132. }
  133. };
  134. renderSheetFunc(sheet, fuc);
  135. }
  136. //表监听事件
  137. //@param {Object}workBook @return {void}
  138. function bindEvent(workBook, events) {
  139. if(Object.keys(events).length === 0){
  140. return;
  141. }
  142. const Events = GC.Spread.Sheets.Events;
  143. for(let event in events){
  144. workBook.bind(Events[event], events[event]);
  145. }
  146. }
  147. //建表
  148. //@return {void}
  149. function buildSheet(){
  150. spreadObj.workBook = new GC.Spread.Sheets.Workbook(spreadDom[0], {sheetCount: 1});
  151. sheetCommonObj.spreadDefaultStyle(spreadObj.workBook);
  152. spreadObj.sheet = spreadObj.workBook.getActiveSheet();
  153. setSpreadOptions(spreadObj.workBook, spreadOpts);
  154. bindEvent(spreadObj.workBook, spreadEvents);
  155. buildHeader(spreadObj.sheet, headers);
  156. //全表不可编辑
  157. spreadObj.sheet.getRange(-1, -1, -1, -1).locked(true);
  158. }
  159. //此项目的分享权限信息(可能会被父级项目覆盖,以新为准)
  160. //@param {String}userID(本用户id) {Object}project(项目) @return {Object} || {Null}
  161. function getShareInfo(userID, project) {
  162. if (!project.actualTreeInfo) {
  163. return null;
  164. }
  165. //获取跟本用户和选中项目相关的分享信息
  166. let shareList = [];
  167. let actualID = project.actualTreeInfo.ID,
  168. actualData = actualIDShareInfo[actualID];
  169. while (actualData) {
  170. for (let data of actualData.shareInfo) {
  171. if (data.userID === userID) {
  172. shareList.push(data);
  173. break;
  174. }
  175. }
  176. actualData = actualIDShareInfo[actualData.ParentID];
  177. }
  178. //获取最新分享
  179. shareList.sort(function (a, b) {
  180. //return Date.parse(b.shareDate) - Date.parse(a.shareDate)
  181. return Date.parse(b.updateDate) - Date.parse(a.updateDate);
  182. });
  183. return shareList[0] || null;
  184. }
  185. //此项目是否可以拷贝
  186. //@param {String}userID {Object}project @return {Boolean}
  187. function isAllowCopy(userID, project){
  188. let myShareInfo = getShareInfo(userID, project);
  189. if (!myShareInfo) {
  190. return false;
  191. }
  192. return !!myShareInfo.allowCopy;
  193. }
  194. //此项目是否可以协作
  195. //@param {String}userID {Object}project @return {Boolean}
  196. function isAllowCoop(userID, project) {
  197. let myShareInfo = getShareInfo(userID, project);
  198. if (!myShareInfo) {
  199. return false;
  200. }
  201. return !!myShareInfo.allowCooperate;
  202. }
  203. //获取树节点
  204. //@param {Object}tree @return {Object}
  205. function getTreeNodeCell(tree){
  206. let indent = 20;
  207. let levelIndent = -5;
  208. let halfBoxLength = 5;
  209. let halfExpandLength = 3;
  210. let imgWidth = 18;
  211. let imgHeight = 14;
  212. let TreeNodeCellType = function () {
  213. };
  214. TreeNodeCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  215. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  216. if (style.backColor) {
  217. ctx.save();
  218. ctx.fillStyle = style.backColor;
  219. ctx.fillRect(x, y, w, h);
  220. ctx.restore();
  221. } else {
  222. ctx.clearRect(x, y, w, h);
  223. }
  224. let drawLine = function (canvas, x1, y1, x2, y2, color) {
  225. ctx.save();
  226. ctx.translate(0.5, 0.5);
  227. ctx.beginPath();
  228. ctx.moveTo(x1, y1);
  229. ctx.lineTo(x2, y2);
  230. ctx.strokeStyle = color;
  231. ctx.stroke();
  232. ctx.restore();
  233. };
  234. let drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  235. let rect = {}, h1, h2, offset = 1;
  236. rect.top = centerY - halfBoxLength;
  237. rect.bottom = centerY + halfBoxLength;
  238. rect.left = centerX - halfBoxLength;
  239. rect.right = centerX + halfBoxLength;
  240. if (rect.left < x + w) {
  241. rect.right = Math.min(rect.right, x + w);
  242. ctx.save();
  243. ctx.translate(0.5, 0.5);
  244. ctx.strokeStyle = 'black';
  245. ctx.beginPath();
  246. ctx.moveTo(rect.left, rect.top);
  247. ctx.lineTo(rect.left, rect.bottom);
  248. ctx.lineTo(rect.right, rect.bottom);
  249. ctx.lineTo(rect.right, rect.top);
  250. ctx.lineTo(rect.left, rect.top);
  251. ctx.stroke();
  252. ctx.fillStyle = 'white';
  253. ctx.fill();
  254. ctx.restore();
  255. // Draw Horizontal Line
  256. h1 = centerX - halfExpandLength;
  257. h2 = Math.min(centerX + halfExpandLength, x + w);
  258. if (h2 > h1) {
  259. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  260. }
  261. // Draw Vertical Line
  262. if (!expanded && (centerX < x + w)) {
  263. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  264. }
  265. }
  266. }
  267. let node = tree.items[options.row];
  268. let showTreeLine = true;
  269. if (!node) { return; }
  270. let centerX = Math.floor(x) + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  271. let x1 = centerX + indent / 2;
  272. let centerY = Math.floor((y + (y + h)) / 2);
  273. let y1;
  274. // Draw Sibling Line
  275. if (showTreeLine) {
  276. // Draw Horizontal Line
  277. if (centerX < x + w) {
  278. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  279. let img;
  280. if(node.data.projType === projectType.folder){
  281. img = document.getElementById('folder_open_pic');
  282. imgWidth = 15;
  283. }
  284. else if(node.data.projType === projectType.project){
  285. img = document.getElementById('proj_pic');
  286. imgWidth = 18;
  287. }
  288. else if(node.data.projType === projectType.engineering){
  289. img = document.getElementById('eng_pic');
  290. imgWidth = 14;
  291. }
  292. else if(node.data.projType === projectType.tender){
  293. img = document.getElementById('tender_pic');
  294. imgWidth = 14;
  295. }
  296. ctx.drawImage(img, centerX+indent/2+3, centerY - 7, imgWidth,imgHeight);
  297. }
  298. // Draw Vertical Line
  299. if (centerX < x + w) {
  300. y1 = node.isLast() ? centerY : y + h;
  301. if (node.isFirst() && !node.parent.parent) {
  302. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  303. } else {
  304. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  305. }
  306. }
  307. }
  308. // Draw Expand Box
  309. if (node.children.length > 0) {
  310. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  311. }
  312. // Draw Parent Line
  313. if (showTreeLine) {
  314. var parent = node.parent, parentCenterX = centerX - indent - levelIndent;
  315. while (parent.parent) {
  316. if (!parent.isLast()) {
  317. if (parentCenterX < x + w) {
  318. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  319. }
  320. }
  321. parent = parent.parent;
  322. parentCenterX -= (indent + levelIndent);
  323. }
  324. };
  325. // Draw Text
  326. arguments[2] = x + (node.depth() + 1) * indent + node.depth() * levelIndent + imgWidth + 3;
  327. arguments[4] = w - (node.depth() + 1) * indent - node.depth() * levelIndent - imgWidth - 3;
  328. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  329. };
  330. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  331. let info = {x: x, y: y, row: context.row, col: context.col, cellStyle: cellStyle, cellRect: cellRect, sheetArea: context.sheetArea};
  332. let node = tree.items[info.row];
  333. if (node && node.data.projType === projectType.tender) {
  334. info.isReservedLocation = true;
  335. }
  336. return info;
  337. };
  338. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  339. let offset = -1;
  340. let node = tree.items[hitinfo.row];
  341. let centerX = hitinfo.cellRect.x + offset + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  342. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  343. //点击单位工程
  344. if(node.data.projType === projectType.tender){
  345. let thisClick = Date.now(),
  346. open = false;
  347. if (this.preNode === node && this.preClick && thisClick - this.preClick <= 300) {
  348. open = true;
  349. projTreeObj.openTender(node.data.actualTreeInfo.ID, node.parent, 200);
  350. }
  351. this.preClick = open ? null : thisClick;
  352. this.preNode = open ? null : node;
  353. }
  354. if (!node || node.children.length === 0) {
  355. return;
  356. }
  357. if (hitinfo.x >= centerX - halfBoxLength - 2 && hitinfo.x <= centerX + halfBoxLength + 2 &&
  358. hitinfo.y >= centerY - halfBoxLength - 2 && hitinfo.y <= centerY + halfBoxLength + 2) {
  359. node.setExpanded(!node.expanded);
  360. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  361. let iCount = node.posterityCount(), i, child;
  362. for (i = 0; i < iCount; i++) {
  363. child = tree.items[hitinfo.row + i + 1];
  364. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  365. }
  366. hitinfo.sheet.invalidateLayout();
  367. });
  368. hitinfo.sheet.repaint();
  369. }
  370. };
  371. TreeNodeCellType.prototype.processMouseEnter = function (hitinfo) {
  372. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  373. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  374. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  375. zoom = hitinfo.sheet.zoom();
  376. let node = tree.items[hitinfo.row];
  377. let nodeIndent = node ? (node.depth() + 1) * indent + node.depth() * levelIndent + imgWidth + 3 : 0;
  378. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  379. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  380. if(textLength > cellWidth - nodeIndent){
  381. TREE_SHEET_HELPER.showTipsDiv(text,{pos: {}},hitinfo);
  382. }
  383. };
  384. TreeNodeCellType.prototype.processMouseLeave = function (hitinfo) {
  385. TREE_SHEET_HELPER.tipDiv = 'hide';
  386. if (TREE_SHEET_HELPER._toolTipElement) {
  387. $(TREE_SHEET_HELPER._toolTipElement).hide();
  388. TREE_SHEET_HELPER._toolTipElement = null;
  389. };
  390. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  391. }
  392. return new TreeNodeCellType();
  393. }
  394. //互动单元格
  395. function getInteractionCell() {
  396. let editImg = document.getElementById('edit_pic'),
  397. workImgWidth = 13,
  398. workImgHeight = 13,
  399. copyImg = document.getElementById('copy_pic'),
  400. copyImgWidth = 13,
  401. copyImgHeight = 13;
  402. let InteractionCell = function () {
  403. };
  404. InteractionCell.prototype = new GC.Spread.Sheets.CellTypes.Text();
  405. InteractionCell.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  406. if (style.backColor) {
  407. ctx.save();
  408. ctx.fillStyle = style.backColor;
  409. ctx.fillRect(x, y, w, h);
  410. ctx.restore();
  411. } else {
  412. ctx.clearRect(x, y, w, h);
  413. }
  414. let node = tree.items[options.row];
  415. // Draw Text
  416. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  417. if (node && node.data.projType === projectType.tender) {
  418. let text = options.sheet.getText(options.row, options.col);
  419. let acStyle = options.sheet.getActualStyle(options.row, options.col),
  420. zoom = options.sheet.zoom();
  421. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: options.sheet, row: options.row, col: options.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  422. const imgIndent = 5;
  423. let nowX = Math.floor(x) + textLength + imgIndent,
  424. nowY = Math.floor((y + (y + h)) / 2) - 7;
  425. if (node.data.allowCooperate) {
  426. ctx.drawImage(editImg, nowX, nowY, workImgWidth, workImgHeight);
  427. nowX += workImgWidth;
  428. }
  429. if (node.data.allowCopy) {
  430. ctx.drawImage(copyImg, nowX, nowY, copyImgWidth, copyImgHeight);
  431. }
  432. }
  433. };
  434. InteractionCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  435. return {
  436. x: x,
  437. y: y,
  438. row: context.row,
  439. col: context.col,
  440. cellStyle: cellStyle,
  441. cellRect: cellRect,
  442. sheetArea: context.sheetArea,
  443. isReservedLocation: true
  444. };
  445. };
  446. InteractionCell.prototype.processMouseDown = function (hitinfo) {
  447. let dataCode = headers[hitinfo.col]['dataCode'];
  448. let node = tree.items[hitinfo.row];
  449. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  450. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  451. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  452. zoom = hitinfo.sheet.zoom();
  453. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  454. if(hitinfo.x - hitinfo.cellRect.x > 0 && hitinfo.x - hitinfo.cellRect.x < textLength){
  455. //由..分享,弹出分享者信息
  456. if(dataCode === 'from'){
  457. if(node.data.shareType === shareType.receive){
  458. $('#userinfo').find('h4').text(node.data.userInfo.name);
  459. $('#userinfo').find('h6').text(node.data.userInfo.company);
  460. let mobileHtml = `<i class="fa fa-tablet"> ${node.data.userInfo.mobile ? node.data.userInfo.mobile : ''}</i>`;
  461. $('#userinfo').find('li:first-child').html(mobileHtml);
  462. let emailHtml = `<i class="fa fa-envelope-o"> ${node.data.userInfo.email ? node.data.userInfo.email : ''}</i>`;
  463. $('#userinfo').find('li:last-child').html(emailHtml);
  464. $('#userinfo').modal('show');
  465. }
  466. }
  467. }
  468. };
  469. InteractionCell.prototype.processMouseMove = function (hitInfo) {
  470. let dataCode = headers[hitInfo.col]['dataCode'];
  471. let node = tree.items[hitInfo.row];
  472. let sheet = hitInfo.sheet;
  473. let div = sheet.getParent().getHost();
  474. let canvasId = div.id + "vp_vp";
  475. let canvas = $(`#${canvasId}`)[0];
  476. //改变鼠标图案
  477. let text = hitInfo.sheet.getText(hitInfo.row, hitInfo.col);
  478. let value = hitInfo.sheet.getValue(hitInfo.row, hitInfo.col);
  479. let acStyle = hitInfo.sheet.getActualStyle(hitInfo.row, hitInfo.col),
  480. zoom = hitInfo.sheet.zoom();
  481. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitInfo.sheet, row: hitInfo.row, col: hitInfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  482. if (sheet && hitInfo.x - hitInfo.cellRect.x > 0 && hitInfo.x - hitInfo.cellRect.x < textLength) {
  483. canvas.style.cursor='pointer';
  484. return true;
  485. }else{
  486. canvas.style.cursor='default';
  487. }
  488. return false;
  489. };
  490. return new InteractionCell();
  491. }
  492. const foreColor = '#007bff';
  493. const cancelForeColor = 'red';
  494. //显示树结构数据
  495. //@param {Array}nodes {Array}headers @return {void}
  496. function showTreeData(nodes, headers){
  497. let sheet = spreadObj.workBook.getActiveSheet();
  498. let fuc = function(){
  499. sheet.setRowCount(nodes.length);
  500. for(let i = 0; i < nodes.length; i++){
  501. let treeNodeCell = getTreeNodeCell(tree);
  502. sheet.getCell(i, treeCol).cellType(treeNodeCell);
  503. for(let j = 0; j < headers.length; j++){
  504. sheet.getRange(-1, j, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[headers[j]['hAlign']]);
  505. sheet.getRange(-1, j, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[headers[j]['vAlign']]);
  506. let dataCode = headers[j].dataCode;
  507. if(dataCode === 'from'){
  508. let style = new GC.Spread.Sheets.Style();
  509. style.foreColor = foreColor;
  510. sheet.setStyle(i, j, style);
  511. sheet.getCell(i, j).cellType(getInteractionCell());
  512. }
  513. sheet.setValue(i, j, nodes[i].data[dataCode] !== null && typeof nodes[i].data[dataCode] !== 'undefined' ? nodes[i].data[dataCode] : '');
  514. }
  515. }
  516. };
  517. renderSheetFunc(sheet, fuc);
  518. }
  519. //同一棵树,可能存在相同数据显示多条的问题(传入的datas中不存在相同数据)
  520. //将真实树结构数据存在actualTreeInfo中,外部树结构数据用uuid重置。
  521. //@param {Array}datas
  522. function setTreeInfo(datas) {
  523. let IDMapping = {};
  524. for (let data of datas) {
  525. //项目真实ID与项目分享信息映射,方便确定项目的权限
  526. if (!actualIDShareInfo[data.ID]) {
  527. actualIDShareInfo[data.ID] = {ID: data.ID, ParentID: data.ParentID, NextSiblingID: data.NextSiblingID, shareInfo: data.shareInfo};
  528. }
  529. IDMapping[data.ID] = uuid.v1();
  530. }
  531. for (let data of datas) {
  532. data.actualTreeInfo = {ID: data.ID, ParentID: data.ParentID, NextSiblingID: data.NextSiblingID};
  533. data.ID = IDMapping[data.ID];
  534. data.NextSiblingID = IDMapping[data.NextSiblingID] ? IDMapping[data.NextSiblingID] : -1;
  535. data.ParentID = IDMapping[data.ParentID] ? IDMapping[data.ParentID] : -1;
  536. }
  537. }
  538. //整理同层数据的NextSiblingID,ParentID
  539. //@param {Array}datas {Number || String}pid @return {void}
  540. function sortSameDepthData(datas, pid) {
  541. for (let i = 0; i < datas.length; i++) {
  542. let data = datas[i],
  543. nextData = datas[i + 1];
  544. data.NextSiblingID = nextData ? nextData.ID : -1;
  545. data.ParentID = pid;
  546. }
  547. }
  548. //给项目设置分享信息:由xx分享、分享时间、分享给我,含有userInfo信息的文件为他人直接分享的文件,他人分享父级文件,子文件不含有userInfo信息
  549. //@param {Array}datas @return {void}
  550. function setShareInfo(datas) {
  551. for (let data of datas) {
  552. if (data.userInfo) {
  553. //shareInfo中我的条目
  554. let selfInfo = _.find(data.shareInfo, {userID: userID});
  555. data.shareDate = selfInfo ? selfInfo.shareDate : ''
  556. data.from = data.userInfo.name;
  557. data.to = '分享给 我';
  558. data.cancel = '清除';
  559. }
  560. }
  561. }
  562. //给项目设置汇总信息
  563. //@param {Array}projs {Object}summaryInfo
  564. function setSummaryInfo(grouped, summaryInfo) {
  565. if (!summaryInfo) {
  566. return;
  567. }
  568. let allDatas = [];
  569. for (let data of grouped) {
  570. allDatas.push(data);
  571. if (data.children && data.children.length > 0) {
  572. allDatas = allDatas.concat(data.children);
  573. }
  574. }
  575. for(let proj of allDatas){
  576. let summaryProj = summaryInfo[proj.ID];
  577. if(summaryProj){
  578. proj.engineeringCost = summaryProj.engineeringCost;
  579. proj.subEngineering = summaryProj.subEngineering;
  580. proj.measure = summaryProj.measure;
  581. proj.safetyConstruction = summaryProj.safetyConstruction;
  582. proj.other = summaryProj.other;
  583. proj.charge = summaryProj.charge;
  584. proj.tax = summaryProj.tax;
  585. proj.rate = summaryProj.rate;
  586. proj.buildingArea = summaryProj.buildingArea;
  587. proj.perCost = summaryProj.perCost;
  588. }
  589. }
  590. }
  591. //从同层树数据获取第一个节点ID
  592. //@param {Array}treeDatas树的数据 @return {String}第一个节点的虚拟树ID
  593. function getFirstID(treeDatas) {
  594. let treeMapping = {};
  595. //建立ID索引
  596. for (let data of treeDatas) {
  597. //新建一个简单对象,防止污染treeDatas的数据
  598. treeMapping[data.ID] = {ID: data.ID, prev: null, next: null};
  599. }
  600. //绑定prev next
  601. for (let data of treeDatas) {
  602. let me = treeMapping[data.ID],
  603. next = treeMapping[data.NextSiblingID];
  604. if (next) {
  605. me.next = next;
  606. next.prev = me;
  607. }
  608. }
  609. //返回没有prev属性的数据
  610. let result = _.find(treeDatas, function (data) {
  611. return !treeMapping[data.ID].prev
  612. });
  613. return result ? result.ID : -1;
  614. }
  615. //获取可成树的数据
  616. //@param {Array}datas @return {Array}
  617. function getTreeDatas(groupedDatas, ungroupedDatas){
  618. //设置新的树结构数据
  619. for (let data of groupedDatas) {
  620. setTreeInfo([data].concat(data.children));
  621. }
  622. let engs = _.filter(ungroupedDatas, {projType: projectType.engineering}),
  623. tenders = _.filter(ungroupedDatas, {projType: projectType.tender});
  624. for (let data of engs) {
  625. setTreeInfo([data].concat(data.children));
  626. }
  627. setTreeInfo(tenders);
  628. let rst = [];
  629. //整理树结构
  630. sortSameDepthData(groupedDatas, -1);
  631. //第一个根节点数据
  632. let firstID = getFirstID(groupedDatas);
  633. //新建未分类建设项目及单项工程
  634. let ungroupedProj = {ID: uuid.v1(), ParentID: -1, NextSiblingID: firstID, name: '未分类建设项目', projType: projectType.project};
  635. let ungroupedEng = {ID: uuid.v1(), ParentID: ungroupedProj.ID, NextSiblingID: -1, name: '未分类单项工程', projType: projectType.engineering};
  636. //将未分类的数据归类
  637. sortSameDepthData(engs, ungroupedProj.ID);
  638. sortSameDepthData(tenders, ungroupedEng.ID);
  639. //未分类单项工程处于同层第一个节点
  640. ungroupedEng.NextSiblingID = engs[0] ? engs[0].ID : ungroupedEng.NextSiblingID;
  641. let allDatas = groupedDatas.concat(ungroupedDatas);
  642. //设置分享信息及操作信息
  643. setShareInfo(allDatas);
  644. for (let data of allDatas) {
  645. rst.push(data);
  646. if (data.children) {
  647. rst = rst.concat(data.children);
  648. }
  649. }
  650. rst.push(ungroupedProj);
  651. rst.push(ungroupedEng);
  652. return rst;
  653. }
  654. //按照时间排序
  655. //@param {Array}datas @return {void}
  656. function sortByDate(datas){
  657. datas.sort(function (a, b) {
  658. let shareInfoA = _.find(a.shareInfo, {userID}),
  659. shareInfoB = _.find(b.shareInfo, {userID});
  660. let aV = shareInfoA ? Date.parse(shareInfoA.shareDate) : 0,
  661. bV = shareInfoB ? Date.parse(shareInfoB.shareDate) : 0;
  662. //时间越晚越靠前
  663. if (aV > bV) {
  664. return -1;
  665. } else if (aV < bV) {
  666. return 1;
  667. }
  668. return 0;
  669. });
  670. }
  671. //设置节点数据权限
  672. //@param {Array}datas项目数据
  673. function setPermissionsInfo(datas) {
  674. //data.allowCopy与shareInfo里allowCopy的区别:
  675. //data.allowCopy为该单位实际的权限(跟着最新的分享信息走,可能随着父项)
  676. for (let data of datas) {
  677. if (data.projType === projectType.tender) {
  678. data.allowCopy = isAllowCopy(userID, data);
  679. data.allowCooperate = isAllowCoop(userID, data);
  680. }
  681. }
  682. }
  683. //建立树
  684. //@return void
  685. function initShareTree(){
  686. $.bootstrapLoading.start();
  687. //获取分享数据
  688. CommonAjax.post('/pm/api/receiveProjects', {user_id: userID}, function (rstData) {
  689. // 排序 --分享的文件按照时间先后顺序排序,分享文件下的子文件,按照原本树结构显示,不需要排序
  690. sortByDate(rstData.grouped);
  691. sortByDate(rstData.ungrouped);
  692. //设置汇总信息
  693. if (rstData.summaryInfo) {
  694. setSummaryInfo(rstData.grouped, rstData.summaryInfo.grouped);
  695. setSummaryInfo(rstData.ungrouped, rstData.summaryInfo.ungrouped);
  696. }
  697. let treeDatas = getTreeDatas(rstData.grouped, rstData.ungrouped);
  698. setPermissionsInfo(treeDatas);
  699. tree = pmTree.createNew(treeSetting, treeDatas);
  700. tree.selected = tree.items[0];
  701. showTreeData(tree.items, headers);
  702. //初始选择
  703. let initSel = spreadObj.sheet.getSelections()[0] ? spreadObj.sheet.getSelections()[0] : {row: 0, rowCount: 1};
  704. initSelection(initSel);
  705. autoFlashHeight();
  706. spreadObj.sheet.frozenColumnCount(4);
  707. spreadObj.workBook.refresh();
  708. $.bootstrapLoading.end();
  709. });
  710. }
  711. //初始化右键菜单
  712. function initContextMenu() {
  713. $.contextMenu({
  714. selector: '#shareSpread',
  715. build: function ($trigger, e) {
  716. let target = SheetDataHelper.safeRightClickSelection($trigger, e, spreadObj.workBook);
  717. initSelection({row: target.row, rowCount: 1}, preSelection ? preSelection : null, spreadObj.sheet);
  718. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  719. },
  720. items: {
  721. "copy": {
  722. name: "拷贝工程",
  723. icon: 'fa-copy',
  724. disabled: function () {
  725. let selected = tree.selected;
  726. return !(selected && selected.data.allowCopy);
  727. },
  728. callback: function (key, opt) {
  729. $('#copyShare').modal('show');
  730. }
  731. },
  732. "cancel": {
  733. name: "清除",
  734. icon: 'fa-remove',
  735. disabled: function () {
  736. let selected = tree.selected;
  737. return !(selected && selected.data.cancel && selected.data.cancel === '清除');
  738. },
  739. callback: function (key, opt) {
  740. let $p = $('<p>').text(`点“确定”按钮,确认清除分享文件 “${tree.selected.data.name}”。`);
  741. $('#cancelShare').find('.modal-body').empty();
  742. $('#cancelShare').find('.modal-body').append($p);
  743. $('#cancelShare').modal('show');
  744. }
  745. }
  746. }
  747. });
  748. }
  749. //初始化视图
  750. //@return void
  751. function initView(){
  752. if(tree){
  753. tree = null;
  754. }
  755. if(spreadObj.workBook){
  756. spreadObj.workBook.destroy();
  757. spreadObj.workBook = null;
  758. }
  759. initContextMenu();
  760. buildSheet();
  761. initShareTree();
  762. }
  763. //根据建设项目获取单项工程
  764. //@param {Number}projID @return {void}
  765. function setEng(projID){
  766. let engQuery = {$or: [{deleteInfo: null}, {'deleteInfo.deleted': false}], projType: projectType.engineering, userID: userID, ParentID: projID};
  767. CommonAjax.post('/pm/api/getProjectsByQuery', {user_id: userID, query: engQuery, options: '-_id -property'}, function (rstData) {
  768. $('#copyShare_selectEng').empty();
  769. for(let eng of rstData){
  770. let opt = $('<option>').val(eng.ID).text(eng.name);
  771. $('#copyShare_selectEng').append(opt);
  772. }
  773. });
  774. }
  775. //从其他建设项目中复制中,建设项目的文件层次结构名称和顺序
  776. //@param {Array}treeData @return {Array}
  777. function getFileHierarchyInfo(treeData){
  778. curCopySelTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1});
  779. curCopySelTree.loadDatas(treeData);
  780. let items = curCopySelTree.items;
  781. let rst = [];
  782. function getFileHierarchyName(node){
  783. let nodeName = node.data.name;
  784. let name = [];
  785. while (node.parent){
  786. name.push(node.parent.data.name ? node.parent.data.name : '');
  787. node = node.parent;
  788. }
  789. name = name.reverse();
  790. name.push(nodeName);
  791. return name.join('\\');
  792. }
  793. for(let node of items){
  794. if(node.children.length === 0 ){//project
  795. rst.push({ID: node.data.ID, fileHierarchyName: getFileHierarchyName(node)})
  796. }
  797. }
  798. return rst;
  799. }
  800. //设置拷贝工程下拉选择
  801. //@return {void}
  802. function setCopyModal(){
  803. //获取建设项目
  804. let projQuery = {$or: [{deleteInfo: null}, {'deleteInfo.deleted': false}], projType: {$in: [projectType.project, projectType.folder]}, userID: userID};
  805. CommonAjax.post('/pm/api/getProjectsByQuery', {user_id: userID, query: projQuery, options: '-_id'}, function (rstData) {
  806. let fileHierarchyData = getFileHierarchyInfo(rstData);
  807. $('#copyShare_selectProj').empty();
  808. for(let proj of fileHierarchyData){
  809. let opt = $('<option>').val(proj.ID).text(proj.fileHierarchyName);
  810. $('#copyShare_selectProj').append(opt);
  811. }
  812. //初始选择
  813. if(fileHierarchyData.length > 0){
  814. setEng(fileHierarchyData[0].ID);
  815. }
  816. });
  817. }
  818. //拷贝分享的工程
  819. //@param {Object}selected {Number}parentID @return {void}
  820. async function copyShareProject(selected, projID, engID){
  821. try {
  822. if(!engID || !selected){
  823. return;
  824. }
  825. let copyMap = {copy: null, update: null};
  826. let newName = getCopyName(selected);
  827. //获取单项工程的单位工程
  828. let tenderQuery = {$or: [{deleteInfo: null}, {'deleteInfo.deleted': false}], userID: userID, ParentID: engID};
  829. const rstData = await ajaxPost('/pm/api/getProjectsByQuery', {user_id: userID, query: tenderQuery, options: '-_id -property'}, false, 10000);
  830. let updateTender = null;
  831. for(let tender of rstData){
  832. if(tender.name === newName){
  833. $('#copyShare_name').text('已存在此单位工程。');
  834. $('#copyShare_name').addClass('text-danger');
  835. return;
  836. }
  837. if(tender.NextSiblingID == -1){
  838. updateTender = tender;
  839. }
  840. }
  841. //更新前节点
  842. if(updateTender){
  843. copyMap.update = {query: {ID: updateTender.ID}};
  844. }
  845. //拷贝
  846. let copyData = {
  847. userID: userID,
  848. ID: selected.data.actualTreeInfo.ID,
  849. NextSiblingID: -1,
  850. ParentID: engID,
  851. name: newName,
  852. shareInfo: [],
  853. compilation: selected.data.compilation,
  854. createDateTime: selected.data.createDateTime,
  855. fileVer: selected.data.fileVer ? selected.data.fileVer : '',
  856. projType: selected.data.projType,
  857. property: {},
  858. recentDateTime: selected.data.recentDateTime,
  859. fullFolder: selected.data.fullFolder
  860. };
  861. copyData.property.rootProjectID = projID;
  862. copyMap.copy = {document: copyData};
  863. $('#copyShare').modal('hide');
  864. $.bootstrapLoading.progressStart('拷贝项目', true);
  865. $("#progress_modal_body").text('正在拷贝项目,请稍候……');
  866. await ajaxPost('/pm/api/copyProjects', {projectMap: copyMap, user_id: userID, tenderCount: 1});
  867. importProcessChecking();
  868. } catch (err) {
  869. alert(err);
  870. }
  871. }
  872. //获取拷贝后的名称
  873. //@param {Object}node @return {String}
  874. function getCopyName(node) {
  875. //当前单位工程可能没有分享的用户信息,可能他人分享的是父级文件,userInfo在父级文件中
  876. let orgName = node.data.name,
  877. userInfo = node.data.userInfo;
  878. while (node && !userInfo) {
  879. node = node.parent;
  880. userInfo = node.data.userInfo;
  881. }
  882. return `${orgName} (${userInfo.name}分享拷贝)`;
  883. }
  884. //事件监听器
  885. //@return void
  886. function eventListener(){
  887. //tab
  888. $('#tab_pm_share').on('shown.bs.tab', function () {
  889. //侧滑隐藏
  890. $('.slide-sidebar').removeClass('open');
  891. $('.slide-sidebar').css('width', '0');
  892. projTreeObj.tree = null;
  893. if(projTreeObj.workBook){
  894. projTreeObj.workBook.destroy();
  895. projTreeObj.workBook = null;
  896. }
  897. gcTreeObj.tree = null;
  898. if(gcTreeObj.workBook){
  899. gcTreeObj.workBook.destroy();
  900. gcTreeObj.workBook = null;
  901. }
  902. initView();
  903. });
  904. //关闭拷贝工程
  905. $('#copyShare').on('hidden.bs.modal', function () {
  906. $('#copyShareProj-info').hide();
  907. $('#copyShareEng-info').hide();
  908. });
  909. //打开拷贝工程
  910. $('#copyShare').on('shown.bs.modal', function () {
  911. setCopyModal();
  912. //更改显示名称
  913. let newName = getCopyName(shareSeleted);
  914. $('#copyShare_name').html(`拷贝后,工程将重命名为 "<b>${newName}</b>"`);
  915. $('#copyShare_name').removeClass('text-danger');
  916. });
  917. //拷贝工程改变选择建设项目
  918. $('#copyShare_selectProj').change(function () {
  919. //更改显示名称
  920. let newName = getCopyName(shareSeleted);
  921. $('#copyShare_name').html(`拷贝后,工程将重命名为 "<b>${newName}</b>"`);
  922. $('#copyShare_name').removeClass('text-danger');
  923. $('#copyShareProj-info').hide();
  924. $('#copyShareEng-info').hide();
  925. let curSelID = $(this).select().val();
  926. setEng(parseInt(curSelID));
  927. });
  928. //拷贝工程改变选择单项工程
  929. $('#copyShare_selectEng').change(function () {
  930. //更改显示名称
  931. let newName = getCopyName(shareSeleted);
  932. $('#copyShare_name').html(`拷贝后,工程将重命名为 "<b>${newName}</b>"`);
  933. $('#copyShare_name').removeClass('text-danger');
  934. });
  935. //确认拷贝
  936. $('#copyShare_confirm').click(function () {
  937. let selProj = $('#copyShare_selectProj').select().val();
  938. if(!selProj){
  939. $('#copyShareProj-info').show();
  940. return;
  941. }
  942. //目标建设项目的计税方法与单位工程的一致时,才可拷贝
  943. if (curCopySelTree) {
  944. let projectNode = curCopySelTree.nodes[curCopySelTree.prefix + selProj];
  945. if (!projectNode || !projectNode.data.property || projectNode.data.property.taxType != tree.selected.data.taxType){
  946. alert('当前单位工程计税方法与目标建设项目不一致,不可复制。');
  947. return;
  948. }
  949. }
  950. let selEng = $('#copyShare_selectEng').select().val();
  951. if(!selEng){
  952. $('#copyShareEng-info').show();
  953. return;
  954. }
  955. copyShareProject(tree.selected, parseInt(selProj), parseInt(selEng));
  956. });
  957. //清除分享
  958. //清除了该节点后,可能还有该节点的数据在树上(树允许有重复数据),需要更新分享信息
  959. function updateAfterCancel(userID, projectID) {
  960. for (let item of tree.items) {
  961. if (item.data.actualTreeInfo && item.data.actualTreeInfo.ID === projectID) {
  962. _.remove(item.data.shareInfo, function (data) {
  963. return data.userID === userID;
  964. });
  965. }
  966. }
  967. }
  968. $('#cancelShareConfirm').click(function () {
  969. $.bootstrapLoading.start();
  970. let cancelProjID = tree.selected.data.actualTreeInfo.ID;
  971. CommonAjax.post('/pm/api/share', {user_id: userID, type: oprType.cancel, projectID: cancelProjID, shareData:[{userID: userID}]}, function (rstData) {
  972. tree.removeNode(tree.selected);
  973. //更新与清除节点数据相同,且为被清除缓存分享信息
  974. updateAfterCancel(userID, cancelProjID);
  975. //重新设置actualIDShareInfo,以正确更新权限(清除了分享信息后,可能会导致权限变化 eg:清除了新的分享,则存留的分享项目采用旧的)
  976. actualIDShareInfo = {};
  977. let treeDatas = [];
  978. for (let item of tree.items) {
  979. treeDatas.push(item.data);
  980. let actualTreeInfo = item.data.actualTreeInfo;
  981. if (actualTreeInfo && !actualIDShareInfo[actualTreeInfo.ID]) {
  982. actualIDShareInfo[actualTreeInfo.ID] = {
  983. ID: actualTreeInfo.ID,
  984. ParentID: actualTreeInfo.ParentID,
  985. NextSiblingID: actualTreeInfo.NextSiblingID,
  986. shareInfo: item.data.shareInfo
  987. };
  988. }
  989. }
  990. //重新设置权限
  991. setPermissionsInfo(treeDatas);
  992. showTreeData(tree.items, headers);
  993. $.bootstrapLoading.end();
  994. }, function () {
  995. $.bootstrapLoading.end();
  996. });
  997. });
  998. }
  999. return {spreadObj, headers, initView, eventListener}
  1000. })();
  1001. $(document).ready(function () {
  1002. pmShare.eventListener();
  1003. });