pm_share.js 42 KB

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