project_property_basicInfo.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /**
  2. * Created by Zhong on 2017/11/23.
  3. */
  4. let basicInfoView = {
  5. orgDatas: [],//for compare
  6. datas: [],//just for view
  7. workBook: null,
  8. setting:{
  9. header: [
  10. {name: '属性', dataCode: 'dispName', width: 200, vAlign: 'center', hAlign: 'left'},
  11. {name: '值', dataCode: 'value', width: 300, vAlign: 'center', hAlign: 'left'}
  12. ],
  13. options: {
  14. allowContextMenu: false,
  15. tabStripVisible: false,
  16. allowCopyPasteExcelStyle : false,
  17. allowExtendPasteRange: false,
  18. allowUserDragDrop : false,
  19. allowUserDragFill: false,
  20. scrollbarMaxAlign : true
  21. },
  22. dateRows: [],
  23. numRows: [],
  24. locked: {
  25. rows: [],
  26. cols: [0]
  27. }
  28. },
  29. renderSheetFuc: function (sheet, fuc) {
  30. sheet.suspendPaint();
  31. sheet.suspendEvent();
  32. fuc();
  33. sheet.resumePaint();
  34. sheet.resumeEvent();
  35. },
  36. setOptions: function (workbook, opts) {
  37. for(let opt in opts){
  38. workbook.options[opt] = opts[opt];
  39. }
  40. },
  41. getRowByField: function (field) {
  42. let me = basicInfoView;
  43. for(let i = 0; i < me.orgDatas.length; i++){
  44. let data = me.orgDatas[i];
  45. if(data.key === field){
  46. return i;
  47. }
  48. }
  49. return null;
  50. },
  51. setDatePicker: function (sheet, dateRows) {
  52. let me = this;
  53. this.renderSheetFuc(sheet, function () {
  54. for(let i = 0, len = dateRows.length; i < len; i++){
  55. sheet.getCell(dateRows[i], 1).cellType(me.getDatePickerCellType()).width(100).formatter('yyyy-mm-dd');
  56. }
  57. });
  58. },
  59. buildHeader: function (sheet, headers) {
  60. let me = basicInfoView;
  61. let fuc = function () {
  62. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  63. sheet.setColumnCount(headers.length);
  64. sheet.setRowHeight(0, 40, GC.Spread.Sheets.SheetArea.colHeader);
  65. for(let i = 0, len = headers.length; i < len; i++){
  66. sheet.setValue(0, i, headers[i].name, GC.Spread.Sheets.SheetArea.colHeader);
  67. sheet.setColumnWidth(i, headers[i].width, GC.Spread.Sheets.SheetArea.colHeader);
  68. sheet.getRange(-1, i, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[headers[i]['hAlign']]);
  69. sheet.getRange(-1, i, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[headers[i]['vAlign']]);
  70. }
  71. };
  72. me.renderSheetFuc(sheet, fuc);
  73. },
  74. buildSheet: function () {
  75. if(!this.workBook){
  76. this.workBook = new GC.Spread.Sheets.Workbook($('#basicInfoSpread')[0], {sheetCount: 1});
  77. sheetCommonObj.spreadDefaultStyle(this.workBook);
  78. sheetCommonObj.bindEscKey(this.workBook, [{sheet: this.workBook.getSheet(0), editStarting: this.onEditStarting, editEnded: this.onEditEnded}]);
  79. this.setOptions(this.workBook, this.setting.options);
  80. this.buildHeader(this.workBook.getActiveSheet(), this.setting.header);
  81. this.bindEvent(this.workBook);
  82. }
  83. },
  84. bindEvent: function (workBook) {
  85. const _events = GC.Spread.Sheets.Events;
  86. let sheet = workBook.getActiveSheet();
  87. sheet.bind(_events.EditStarting, this.onEditStarting);
  88. sheet.bind(_events.EditEnded, this.onEditEnded);
  89. sheet.bind(_events.EnterCell, this.onEnterCell);
  90. sheet.bind(_events.ClipboardPasting, this.onClipboardPasting);
  91. sheet.bind(_events.ClipboardPasted, this.onClipboardPasted);
  92. },
  93. showData(datas){
  94. let me = basicInfoView;
  95. let sheet = this.workBook.getActiveSheet();
  96. let cols = this.setting.header;
  97. let fuc = function () {
  98. sheet.setRowCount(datas.length);
  99. me.initTree(sheet, true, datas);
  100. sheet.setFormatter(-1, 1, '@');
  101. //兼容旧数据,旧数据可能没有设置cellType
  102. let compatNumKeys = ['grossArea'],
  103. compatDateKeys = ['constructionDate', 'establishDate'];
  104. for(let row = 0;row < datas.length ; row ++){
  105. if(datas[row].cellType == 'number' || compatNumKeys.includes(datas[row].key)){
  106. me.setting.numRows.push(row);
  107. } else if (datas[row].cellType === 'date' || compatDateKeys.includes(datas[row].key)) {
  108. me.setting.dateRows.push(row);
  109. } else if(datas[row].cellType === 'comboBox'){
  110. let options = datas[row].options ? datas[row].options.split("@") : [];
  111. projFeatureView.setCombo(sheet, row, options);
  112. }
  113. let readOnly = typeof datas[row].readOnly === 'string' ? JSON.parse(datas[row].readOnly) : datas[row].readOnly;
  114. if (readOnly || datas[row].items) {
  115. me.setting.locked.rows.push(row);
  116. }
  117. for(let col = 0;col < cols.length;col++){
  118. sheet.setValue(row, col, datas[row][cols[col]['dataCode']]);
  119. }
  120. }
  121. me.setDatePicker(sheet, me.setting.dateRows);
  122. };
  123. this.renderSheetFuc(sheet, fuc);
  124. },
  125. onEditStarting: function (sender, args) {
  126. let me = basicInfoView;
  127. if(args.col === 0){
  128. args.cancel = true;
  129. }
  130. if(me.setting.locked.rows.indexOf(args.row) !== -1){
  131. args.cancel = true;
  132. }
  133. },
  134. onEditEnded: function (sender, args) {
  135. let me = basicInfoView;
  136. let v = args.editingText ? args.editingText.toString().trim() : '';
  137. if(args.row < me.datas.length){
  138. let required = typeof me.datas[args.row].required === 'string' ? JSON.parse(me.datas[args.row].required) : me.datas[args.row].required;
  139. required = false; // 临时屏蔽
  140. //date
  141. if (required && !v) {
  142. v = me.datas[args.row].value;
  143. args.sheet.setValue(args.row, args.col, v);
  144. } else if(me.setting.dateRows.indexOf(args.row) !== -1){
  145. if(v.length > 0){
  146. v = formatDate(new Date(v), 'yyyy-MM-dd');
  147. v = me.filtDate(v);
  148. if(!v){
  149. alert('请输入正确的日期格式yyyy-mm-dd');
  150. args.sheet.setValue(args.row, args.col, me.datas[args.row].value ? me.datas[args.row].value : '');
  151. return;
  152. }
  153. }
  154. } else if(me.setting.numRows.indexOf(args.row) !== -1){//控制数值
  155. if(!me.isNum(v)){
  156. alert('只能输入数值');
  157. v = me.datas[args.row].value && me.isNum(me.datas[args.row].value) ? me.datas[args.row].value : '';
  158. args.sheet.setValue(args.row, args.col, v);
  159. }
  160. }
  161. me.datas[args.row].value = v;
  162. }
  163. },
  164. onEnterCell: function (sender, args) {
  165. args.sheet.repaint();
  166. },
  167. onClipboardPasting: function (sender, args) {
  168. let me = basicInfoView;
  169. if(args.cellRange.col === 0){
  170. args.cancel = true;
  171. }
  172. },
  173. onClipboardPasted: function (sender, args) {
  174. let me = basicInfoView;
  175. let items = sheetCommonObj.analyzePasteData(me.setting, args);
  176. let recRows = [];
  177. for(let i = 0, len = items.length; i < len; i++){
  178. let row = i + args.cellRange.row;
  179. let comboItems = projFeatureView.getComboItemsByRow.call(me, row);
  180. let required = typeof me.datas[row].required === 'string' ? JSON.parse(me.datas[row].required) : me.datas[row].required;
  181. if(me.setting.locked.rows.indexOf(row) !== -1){
  182. recRows.push(row);
  183. } else if(comboItems && !comboItems.includes(items[i].value)){//粘贴下拉框数据过滤
  184. recRows.push(row);
  185. } else if(me.setting.dateRows.indexOf(row) !== -1){
  186. items[i].value = me.filtDate(items[i].value);
  187. if(!me.isDef(items[i].value)){
  188. recRows.push(row);
  189. } else {
  190. me.datas[row].value = items[i].value;
  191. }
  192. } else if(me.setting.numRows.indexOf(row) !== -1 && !me.isNum(items[i].value)){
  193. recRows.push(row);
  194. } else if (required && !items[i].value) {
  195. recRows.push(row);
  196. } else
  197. {me.datas[row].value = items[i].value;
  198. }
  199. }
  200. if(recRows.length > 0){
  201. me.renderSheetFuc(args.sheet, function () {
  202. for(let i = 0, len = recRows.length; i < len; i++){
  203. let staticV = me.datas[recRows[i]].value || '';
  204. args.sheet.setValue(recRows[i], args.cellRange.col, staticV);
  205. }
  206. });
  207. }
  208. },
  209. isDef: function (v) {
  210. return v !== undefined && v !== null;
  211. },
  212. isNum: function(v){
  213. return this.isDef(v) && !isNaN(v);
  214. },
  215. filtDate: function (v) {
  216. let re = /([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))/
  217. let reDate = re.exec(v);
  218. let rst = reDate ? reDate[0] : null;
  219. return rst;
  220. },
  221. copyObj: function(obj){
  222. let newObj = {};
  223. for(let attr in obj){
  224. newObj[attr] = obj[attr];
  225. }
  226. return newObj;
  227. },
  228. initDatas: function (datas) {
  229. this.datas = _.cloneDeep(datas);
  230. },
  231. //数据库读到的数据转换为展示的结构
  232. toViewDatas: function (datas) {
  233. // 将数据打平(原数据一些子项数据会嵌套在items数组中)
  234. let curID = 1;
  235. return extractDataFromItems(datas);
  236. // 为了与sheet_common.js -> getTreeNodeCellType共用树结构功能
  237. // 需要设置数据的ID、ParentID
  238. function extractDataFromItems(items, parentID) {
  239. const rst = [];
  240. items.forEach(item => {
  241. // 为了与sheet_common.js -> getTreeNodeCellType共用树结构功能
  242. // 需要设置数据的ID、ParentID
  243. item.ID = curID++;
  244. item.ParentID = parentID || null;
  245. rst.push(item);
  246. if (item.items && item.items.length) {
  247. rst.push(...extractDataFromItems(item.items, item.ID));
  248. }
  249. });
  250. return rst;
  251. }
  252. },
  253. //展示的结构转换为入库的结构
  254. toSaveDatas: function (datas) {
  255. const saveData = datas.filter(item => item.ParentID === null);
  256. clearJunkAttrs(saveData);
  257. function clearJunkAttrs(items) {
  258. items.forEach(item => {
  259. delete item.ID && delete item.ParentID && delete item.collapsed;
  260. if (item.items && item.items.length) {
  261. clearJunkAttrs(item.items);
  262. }
  263. })
  264. }
  265. return saveData;
  266. },
  267. toUpdate: function (orgDatas, newDatas) {
  268. if(orgDatas.length !== newDatas.length){
  269. return true;
  270. }
  271. for(let i = 0, len = orgDatas.length; i < len; i++){
  272. let orgObj = orgDatas[i], newObj = newDatas[i];
  273. if(orgObj.value !== newObj.value){
  274. return true;
  275. }
  276. }
  277. return false;
  278. },
  279. a_updateInfo: function (datas) {
  280. let me = this;
  281. let url = '/pm/api/updateProjects',
  282. updateData = {
  283. updateType: 'update',
  284. updateData: {ID: parseInt(scUrlUtil.GetQueryString('project')), 'property.basicInformation': datas}
  285. },
  286. postData = {
  287. user_id: userID,
  288. updateData: [updateData]
  289. };
  290. CommonAjax.post(url, postData, function (rstData) {
  291. me.orgDatas = me.toViewDatas(datas);
  292. });
  293. },
  294. initTree: function (sheet, init, datas) {
  295. //sheet.getRange(-1, 0, -1, 1).cellType(this.getTreeNodeCellType(datas));
  296. const parentType = _.groupBy(datas, 'ParentID');
  297. const paint = (ctx, value, x, y, w, h, style, rectInfo, data) => {
  298. const required = typeof data.required === 'string' ? JSON.parse(data.required) : data.required;
  299. const readOnly = typeof data.readOnly === 'string' ? JSON.parse(data.readOnly) : data.readOnly;
  300. if (required && !readOnly) {
  301. const { rectW, margin } = rectInfo;
  302. ctx.save();
  303. ctx.fillStyle = 'red';
  304. const paddingLeft = 8;
  305. const paintX = x + margin + paddingLeft; // 第一层盒子处[+] / [-]
  306. const paintY = y + Math.round(h / 2) + paddingLeft;
  307. ctx.font = "14px Calibri"
  308. ctx.fillText('*', paintX, paintY);
  309. ctx.restore();
  310. }
  311. };
  312. for (let i = 0, len = datas.length; i < len; i++) {
  313. if (datas[i].hasOwnProperty('items')) {
  314. let collapsed = false;
  315. if (init) {
  316. datas[i].collapsed = false;
  317. collapsed = true;
  318. } else {
  319. collapsed = datas[i].collapsed == undefined ? true : datas[i].collapsed;
  320. }
  321. }
  322. sheet.getCell(i, 0).cellType(sheetCommonObj.getTreeNodeCellType(datas, i, parentType, 0, paint))
  323. }
  324. },
  325. getTreeNodeCellType:function (data) {
  326. var ns = GC.Spread.Sheets;
  327. var rectW = 10;
  328. var rectH = 10;
  329. var margin = 3;
  330. function TreeNodeCellType() {
  331. }
  332. function drowRect(ctx,x,y,w,h) {///
  333. ctx.save();
  334. ctx.strokeStyle = "gray";
  335. ctx.translate(0.5,0.5);
  336. ctx.beginPath();
  337. var rectX = x+margin;
  338. var rectY = y+ Math.round(h/2)-rectH/2;
  339. ctx.moveTo(rectX, rectY);
  340. ctx.lineTo(rectX, rectY+rectH);
  341. ctx.lineTo(rectX+rectW, rectY+rectH);
  342. ctx.lineTo(rectX+rectW, rectY);
  343. ctx.lineTo(rectX, rectY);
  344. ctx.moveTo(rectX+rectW, y+Math.round(h/2));
  345. ctx.lineTo(rectX+rectW+5, y+Math.round(h/2));
  346. ctx.stroke();
  347. ctx.restore();
  348. }
  349. function drowSymbol(ctx,x,y,w,h,collapsed) {
  350. ctx.save();
  351. ctx.strokeStyle = "#000000";
  352. ctx.translate(0.5, 0.5);
  353. ctx.beginPath();
  354. ctx.moveTo(x+margin+2, y+Math.round(h/2));
  355. ctx.lineTo(x+margin+8, y+Math.round(h/2));
  356. var rectY = y+ Math.round(h/2)-rectH/2;
  357. if(collapsed){
  358. ctx.moveTo(x+margin+rectW/2,rectY+2);
  359. ctx.lineTo(x+margin+rectW/2,rectY+2+6);
  360. }
  361. ctx.stroke();
  362. ctx.restore();
  363. }
  364. function drowSubItem(ctx,x,y,w,h,offset,nextItem) {
  365. offset+=6;
  366. ctx.save();
  367. ctx.strokeStyle = "gray";
  368. ctx.translate(0.5, 0.5);
  369. ctx.beginPath();
  370. ctx.moveTo(x+offset, y);
  371. ctx.lineTo(x+offset, y+Math.round(h/2));
  372. offset+=9;
  373. ctx.lineTo(x+offset, y+Math.round(h/2));
  374. if(nextItem&&!nextItem.hasOwnProperty('items')){
  375. ctx.moveTo(x+offset-9, y+Math.round(h/2));
  376. ctx.lineTo(x+offset-9, y+h);
  377. }
  378. ctx.stroke();
  379. ctx.restore();
  380. return offset;
  381. }
  382. TreeNodeCellType.prototype = new ns.CellTypes.Text();
  383. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  384. if(value!=null){
  385. var offset = margin+rectW+6;
  386. var recode = data[options.row];
  387. if(recode&&recode.hasOwnProperty('items')){
  388. drowRect(ctx,x,y,w,h);
  389. var collapsed = recode.collapsed==undefined?true:recode.collapsed;//options.sheet.getTag(options.row,options.col);
  390. drowSymbol(ctx,x,y,w,h,collapsed);
  391. }else if(recode&&!recode.hasOwnProperty('items')){
  392. offset= drowSubItem(ctx,x,y,w,h,offset,data[options.row+1]);
  393. offset+=1;
  394. }
  395. arguments[2] = x + offset;
  396. arguments[4] = w - offset;
  397. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  398. }
  399. };
  400. // override getHitInfo to allow cell type get mouse messages
  401. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  402. return {
  403. x: x,
  404. y: y,
  405. row: context.row,
  406. col: context.col,
  407. cellStyle: cellStyle,
  408. cellRect: cellRect,
  409. sheetArea: context.sheetArea
  410. };
  411. }
  412. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  413. var recode = data[hitinfo.row];
  414. let offset = -1,
  415. indent = 20,
  416. halfBoxLength = 5;
  417. let centerX = hitinfo.cellRect.x + offset + indent / 2;
  418. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2
  419. if(recode&&recode.hasOwnProperty('items')){
  420. //方框外1像素内都有效
  421. const boxLengh = 10;
  422. if (hitinfo.x >= centerX - halfBoxLength - 1 && hitinfo.x <= centerX + halfBoxLength + 1 &&
  423. hitinfo.y >= centerY - halfBoxLength - 1 && hitinfo.y <= centerY + halfBoxLength + 1) {
  424. var collapsed = recode.collapsed==undefined?true:recode.collapsed;
  425. collapsed = !collapsed
  426. recode.collapsed=collapsed;
  427. //hitinfo.sheet.setTag(hitinfo.row,hitinfo.col,collapsed);
  428. hitinfo.sheet.getRange(hitinfo.row+1, -1, recode.items.length, -1).visible(!collapsed);
  429. hitinfo.sheet.invalidateLayout();
  430. hitinfo.sheet.repaint();
  431. }
  432. }
  433. };
  434. return new TreeNodeCellType()
  435. },
  436. getDatePickerCellType: function () {
  437. let ns = GC.Spread.Sheets;
  438. function DatePickerCellType() {
  439. }
  440. DatePickerCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  441. DatePickerCellType.prototype.createEditorElement = function (context) {
  442. //Create input presenter.
  443. return document.createElement("input");
  444. };
  445. DatePickerCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  446. //Initialize input editor.
  447. if (editorContext) {
  448. $editor = $(editorContext);
  449. //DatePickerCellType.prototype.activateEditor.apply(this, arguments);
  450. $editor.datepicker({dateFormat: 'yy-mm-dd'});
  451. $editor.css("position", "absolute");
  452. $editor.attr("gcUIElement", "gcEditingInput");
  453. $(".ui-datepicker").attr("gcUIElement", "gcEditingInput");
  454. }
  455. }
  456. DatePickerCellType.prototype.deactivateEditor = function (editorContext, context) {
  457. //Remove input editor when end editor status.
  458. if (editorContext) {
  459. var element = editorContext;
  460. $(element).datepicker("hide");
  461. $(element).datepicker("destroy");
  462. }
  463. // DatePickerCellType.prototype.deactivateEditor.apply(this, arguments)
  464. };
  465. DatePickerCellType.prototype.setEditorValue = function (editor, value, context) {
  466. //Sync value from Cell value to editor value.
  467. $(editor).datepicker("setDate", value);
  468. };
  469. DatePickerCellType.prototype.getEditorValue = function (editor, context) {
  470. //Sync value from editor value to cell value.
  471. return $(editor).datepicker("getDate");
  472. };
  473. DatePickerCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  474. if (editorContext) {
  475. $editor = $(editorContext);
  476. $editor.css("width", cellRect.width - 1);
  477. $editor.css("height", cellRect.height - 3);
  478. }
  479. };
  480. return new DatePickerCellType();
  481. }
  482. };
  483. $(document).ready(function () {
  484. $('#poj-set').on('shown.bs.modal', function (e) {
  485. //init Spread
  486. basicInfoView.initDatas(basicInfoView.orgDatas);
  487. basicInfoView.buildSheet();
  488. basicInfoView.showData(basicInfoView.datas);
  489. if(projectReadOnly){
  490. sheetCommonObj.disableSpread(basicInfoView.workBook);
  491. }
  492. });
  493. $('#poj-set').on('hidden.bs.modal', function (e) {
  494. //destroy Spread
  495. if(basicInfoView.workBook){
  496. basicInfoView.workBook.destroy();
  497. basicInfoView.workBook = null;
  498. }
  499. basicInfoView.datas = [];
  500. });
  501. $('#tab_poj-settings-basicInfo').on('shown.bs.tab', function () {
  502. sheetCommonObj.refreshWorkbookDelDefer(basicInfoView.workBook, 100);
  503. });
  504. $('#openProjSet').click(function () {
  505. $('[data-toggle="tooltip"]').tooltip('hide');
  506. $('#poj-set').modal('show');
  507. })
  508. /* $('#property_ok').bind('click', function () {
  509. if(basicInfoView.toUpdate(basicInfoView.orgDatas, basicInfoView.datas)){
  510. basicInfoView.a_updateInfo(basicInfoView.toSaveDatas(basicInfoView.datas));
  511. }
  512. });*/
  513. });