project_property_projFeature.js 20 KB

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