project_property_projFeature.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /**
  2. * Created by Zhong on 2017/11/24.
  3. */
  4. let projFeatureView = {
  5. orgDatas: [],//for compare
  6. datas: [],//just for view
  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: 120, vAlign: 'center', hAlign: 'center'}
  13. ],
  14. options: {
  15. tabStripVisible: false,
  16. allowCopyPasteExcelStyle : false,
  17. allowExtendPasteRange: false,
  18. allowUserDragDrop : false,
  19. allowUserDragFill: false,
  20. scrollbarMaxAlign : true
  21. },
  22. combos: [
  23. {row: 1, key: 'projType', items: ['住宅', '公共建筑', '厂房', '办公楼']},
  24. {row: 2, key: 'structureType', items: ['排架结构', '框架结构', '现浇、框架结构', '预制、砖混结构', '外砖内模', '内浇外挂', '钢结构']},
  25. {row: 3, key: 'baseType', items: ['带基', '框排架柱距6m以内', '框排架柱距6m以外', '满基筏式', '满基板式', '满基箱式', '独立基础']},
  26. {row: 4, key: 'buildingFeature', items: ['点式', '凹式', '凸式', 'Y式', '其他']}
  27. ],
  28. numRows: [5, 6, 7, 8, 9, 10, 11, 12, 13],
  29. dateRows: [],
  30. locked: {
  31. rows: [0],
  32. cols: [0]
  33. }
  34. },
  35. renderSheetFuc: function (sheet, fuc) {
  36. sheet.suspendPaint();
  37. sheet.suspendEvent();
  38. fuc();
  39. sheet.resumePaint();
  40. sheet.resumeEvent();
  41. },
  42. setOptions: function (workbook, opts) {
  43. for(let opt in opts){
  44. workbook.options[opt] = opts[opt];
  45. }
  46. },
  47. setCombo: function (sheet, row, items) {
  48. let dynamicCombo = sheetCommonObj.getDynamicCombo();
  49. dynamicCombo.items(items);
  50. dynamicCombo.editable(false);
  51. sheet.getCell(row, 1).cellType(dynamicCombo);
  52. },
  53. getComboItemsByRow: function (row) {
  54. for(let i = 0, len = this.setting.combos.length; i < len; i++){
  55. if(this.setting.combos[i].row === row){
  56. return this.setting.combos[i].items;
  57. }
  58. }
  59. return null;
  60. },
  61. buildHeader: function (sheet, headers) {
  62. let me = projFeatureView;
  63. let fuc = function () {
  64. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  65. sheet.setColumnCount(headers.length);
  66. sheet.setRowHeight(0, 40, GC.Spread.Sheets.SheetArea.colHeader);
  67. for(let i = 0, len = headers.length; i < len; i++){
  68. sheet.setValue(0, i, headers[i].name, GC.Spread.Sheets.SheetArea.colHeader);
  69. sheet.setColumnWidth(i, headers[i].width, GC.Spread.Sheets.SheetArea.colHeader);
  70. }
  71. };
  72. me.renderSheetFuc(sheet, fuc);
  73. },
  74. buildSheet: function () {
  75. if(!this.workBook){
  76. this.workBook = new GC.Spread.Sheets.Workbook($('#projFeatureSpread')[0], {sheetCount: 1});
  77. this.setOptions(this.workBook, this.setting.options);
  78. this.buildHeader(this.workBook.getActiveSheet(), this.setting.header);
  79. this.bindEvent(this.workBook);
  80. }
  81. },
  82. bindEvent: function (workBook) {
  83. const _events = GC.Spread.Sheets.Events;
  84. let sheet = workBook.getActiveSheet();
  85. sheet.bind(_events.EditStarting, this.onEditStarting);
  86. sheet.bind(_events.EditEnded, this.onEditEnded);
  87. sheet.bind(_events.EnterCell, this.onEnterCell);
  88. sheet.bind(_events.ClipboardPasting, this.onClipboardPasting);
  89. sheet.bind(_events.ClipboardPasted, this.onClipboardPasted);
  90. },
  91. showData(datas){
  92. let me = projFeatureView;
  93. let sheet = this.workBook.getActiveSheet();
  94. let cols = this.setting.header;
  95. let fuc = function () {
  96. sheet.setRowCount(datas.length);
  97. me.initTree(sheet, true, datas);
  98. sheet.setFormatter(-1, 1, '@');
  99. //setCombo
  100. for(let i = 0, len = me.setting.combos.length; i < len; i++){
  101. me.setCombo(sheet, me.setting.combos[i].row, me.setting.combos[i].items);
  102. }
  103. for(let col = 0, cLen = cols.length; col < cLen; col++){
  104. sheet.getRange(-1, col, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[cols[col]['hAlign']]);
  105. sheet.getRange(-1, col, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[cols[col]['vAlign']]);
  106. for(let row = 0, rLen = datas.length; row < rLen; row++){
  107. sheet.setValue(row, col, datas[row][cols[col]['dataCode']]);
  108. }
  109. }
  110. };
  111. this.renderSheetFuc(sheet, fuc);
  112. },
  113. onEditStarting: function (sender, args) {
  114. let me = projFeatureView;
  115. if(me.setting.locked.cols.indexOf(args.col) !== -1){
  116. args.cancel = true;
  117. }
  118. if(args.col === 1 && me.setting.locked.rows.indexOf(args.row) !== -1){
  119. args.cancel = true;
  120. }
  121. },
  122. onEditEnded: function (sender, args) {
  123. let me = projFeatureView;
  124. let v = args.editingText ? args.editingText.toString().trim() : '';
  125. if(args.row < me.datas.length){
  126. if(me.setting.numRows.indexOf(args.row) !== -1){//控制数值
  127. if(!me.isNum(v)){
  128. alert('只能输入数值');
  129. args.sheet.setValue(args.row, args.col, me.datas[args.row].value && me.isNum(me.datas[args.row].value) ? me.datas[args.row].value : '');
  130. }
  131. }
  132. me.datas[args.row].value = v;
  133. }
  134. },
  135. onEnterCell: function (sender, args) {
  136. args.sheet.repaint();
  137. },
  138. onClipboardPasting: function (sender, args) {
  139. let me = projFeatureView;
  140. if(me.setting.locked.cols.indexOf(args.cellRange.col) !== -1){
  141. args.cancel = true;
  142. }
  143. },
  144. onClipboardPasted: function (sender, args) {
  145. let me = projFeatureView;
  146. let items = sheetCommonObj.analyzePasteData(me.setting, args);
  147. let recRows = [];
  148. if(items.length === 0){
  149. return;
  150. }
  151. for(let i = 0, len = items.length; i < len; i++){
  152. let row = i + args.cellRange.row;
  153. let comboItems = me.getComboItemsByRow(row);
  154. if(me.setting.locked.rows.indexOf(row) !== -1){
  155. recRows.push(row);
  156. }
  157. //粘贴下拉框数据过滤
  158. else if(comboItems && comboItems.indexOf(items[i].value) !== -1){
  159. recRows.push(row);
  160. }
  161. else if(me.setting.numRows.indexOf(row) !== -1 && !me.isNum(items[i].value)){
  162. recRows.push(row);
  163. }
  164. else {
  165. me.datas[row].value = items[i].value;
  166. }
  167. }
  168. if(recRows.length > 0){
  169. me.renderSheetFuc(args.sheet, function () {
  170. for(let i = 0, len = recRows.length; i < len; i++){
  171. let staticV = me.datas[recRows[i]].value || '';
  172. args.sheet.setValue(recRows[i], args.cellRange.col, staticV);
  173. }
  174. })
  175. }
  176. },
  177. getFeature: function (featureKey) {
  178. for(let feature of this.datas){
  179. if(feature.key === featureKey){
  180. return feature.value;
  181. }
  182. }
  183. return null;
  184. },
  185. isDef: function (v) {
  186. return v !== undefined && v !== null;
  187. },
  188. isNum: function(v){
  189. return this.isDef(v) && !isNaN(v);
  190. },
  191. copyObj: function(obj){
  192. let newObj = {};
  193. for(let attr in obj){
  194. newObj[attr] = obj[attr];
  195. }
  196. return newObj;
  197. },
  198. initDatas: function (datas) {
  199. this.datas = [];
  200. for(let i = 0, len = datas.length; i < len; i++){
  201. this.datas.push(this.copyObj(datas[i]));
  202. }
  203. },
  204. //数据库读到的数据转换为展示的结构
  205. toViewDatas: function (datas) {
  206. let rst = [];
  207. this.firstData.items = datas;
  208. rst.push(this.firstData);
  209. rst = rst.concat(datas);
  210. return rst;
  211. },
  212. //展示的结构转换为入库的结构
  213. toSaveDatas: function (datas) {
  214. let rst = [].concat(datas);
  215. rst.splice(0, 1);
  216. return rst;
  217. },
  218. toUpdate: function (orgDatas, newDatas) {
  219. if(orgDatas.length !== newDatas.length){
  220. return true;
  221. }
  222. for(let i = 0, len = orgDatas.length; i < len; i++){
  223. let orgObj = orgDatas[i], newObj = newDatas[i];
  224. if(orgObj.value !== newObj.value){
  225. return true;
  226. }
  227. }
  228. return false;
  229. },
  230. a_updateInfo: function (datas) {
  231. let me = this;
  232. let url = '/pm/api/updateProjects',
  233. updateData = {
  234. updateType: 'update',
  235. updateData: {ID: parseInt(scUrlUtil.GetQueryString('project')), 'property.projectFeature': datas}
  236. },
  237. postData = {
  238. user_id: userID,
  239. updateData: [updateData]
  240. };
  241. CommonAjax.post(url, postData, function (rstData) {
  242. me.orgDatas = me.toViewDatas(datas);
  243. });
  244. },
  245. initTree:function (sheet, init, datas) {
  246. sheet.getRange(-1, 0, -1, 1).cellType(this.getTreeNodeCellType(datas));
  247. for(let i =0, len = datas.length; i < len; i++){
  248. if(datas[i].hasOwnProperty('items')){
  249. let collapsed = false;
  250. if(init){
  251. datas[i].collapsed=false;
  252. collapsed = true;
  253. }else {
  254. collapsed = datas[i].collapsed == undefined ? true : datas[i].collapsed;
  255. }
  256. //sheet.getRange(i+1, -1, datas[i].items.length, -1).visible(!collapsed);
  257. }
  258. }
  259. },
  260. getTreeNodeCellType:function (data) {
  261. var ns = GC.Spread.Sheets;
  262. var rectW = 10;
  263. var rectH = 10;
  264. var margin = 3;
  265. function TreeNodeCellType() {
  266. }
  267. function drowRect(ctx,x,y,w,h) {
  268. ctx.save();
  269. ctx.strokeStyle = "gray";
  270. ctx.translate(0.5,0.5);
  271. ctx.beginPath();
  272. var rectX = x+margin;
  273. var rectY = y+ Math.round(h/2)-rectH/2;
  274. ctx.moveTo(rectX, rectY);
  275. ctx.lineTo(rectX, rectY+rectH);
  276. ctx.lineTo(rectX+rectW, rectY+rectH);
  277. ctx.lineTo(rectX+rectW, rectY);
  278. ctx.lineTo(rectX, rectY);
  279. ctx.moveTo(rectX+rectW, y+Math.round(h/2));
  280. ctx.lineTo(rectX+rectW+5, y+Math.round(h/2));
  281. ctx.stroke();
  282. ctx.restore();
  283. }
  284. function drowSymbol(ctx,x,y,w,h,collapsed) {
  285. ctx.save();
  286. ctx.strokeStyle = "#000000";
  287. ctx.translate(0.5, 0.5);
  288. ctx.beginPath();
  289. ctx.moveTo(x+margin+2, y+Math.round(h/2));
  290. ctx.lineTo(x+margin+8, y+Math.round(h/2));
  291. var rectY = y+ Math.round(h/2)-rectH/2;
  292. if(collapsed){
  293. ctx.moveTo(x+margin+rectW/2,rectY+2);
  294. ctx.lineTo(x+margin+rectW/2,rectY+2+6);
  295. }
  296. ctx.stroke();
  297. ctx.restore();
  298. }
  299. function drowSubItem(ctx,x,y,w,h,offset,nextItem) {
  300. offset+=6;
  301. ctx.save();
  302. ctx.strokeStyle = "gray";
  303. ctx.translate(0.5, 0.5);
  304. ctx.beginPath();
  305. ctx.moveTo(x+offset, y);
  306. ctx.lineTo(x+offset, y+Math.round(h/2));
  307. offset+=9;
  308. ctx.lineTo(x+offset, y+Math.round(h/2));
  309. if(nextItem&&!nextItem.hasOwnProperty('items')){
  310. ctx.moveTo(x+offset-9, y+Math.round(h/2));
  311. ctx.lineTo(x+offset-9, y+h);
  312. }
  313. ctx.stroke();
  314. ctx.restore();
  315. return offset;
  316. }
  317. TreeNodeCellType.prototype = new ns.CellTypes.Text();
  318. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  319. if(value!=null){
  320. var offset = margin+rectW+6;
  321. var recode = data[options.row];
  322. if(recode&&recode.hasOwnProperty('items')){
  323. drowRect(ctx,x,y,w,h);
  324. var collapsed = recode.collapsed==undefined?true:recode.collapsed;//options.sheet.getTag(options.row,options.col);
  325. drowSymbol(ctx,x,y,w,h,collapsed);
  326. }else if(recode&&!recode.hasOwnProperty('items')){
  327. offset= drowSubItem(ctx,x,y,w,h,offset,data[options.row+1]);
  328. offset+=1;
  329. }
  330. ctx.fillText(value,x+offset+ctx.measureText(value).width,y+h-5);
  331. }
  332. };
  333. // override getHitInfo to allow cell type get mouse messages
  334. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  335. return {
  336. x: x,
  337. y: y,
  338. row: context.row,
  339. col: context.col,
  340. cellStyle: cellStyle,
  341. cellRect: cellRect,
  342. sheetArea: context.sheetArea
  343. };
  344. }
  345. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  346. var recode = data[hitinfo.row];
  347. if(recode&&recode.hasOwnProperty('items')){
  348. var hoffset= hitinfo.cellRect.x+3;
  349. if (hitinfo.x > hoffset && hitinfo.x < hoffset + 10){
  350. var collapsed = recode.collapsed==undefined?true:recode.collapsed;
  351. collapsed = !collapsed
  352. recode.collapsed=collapsed;
  353. //hitinfo.sheet.setTag(hitinfo.row,hitinfo.col,collapsed);
  354. hitinfo.sheet.getRange(hitinfo.row+1, -1, recode.items.length, -1).visible(!collapsed);
  355. hitinfo.sheet.invalidateLayout();
  356. hitinfo.sheet.repaint();
  357. }
  358. }
  359. };
  360. return new TreeNodeCellType()
  361. },
  362. };
  363. $(document).ready(function () {
  364. $('#poj-set').on('shown.bs.modal', function (e) {
  365. //init Spread
  366. projFeatureView.initDatas(projFeatureView.orgDatas);
  367. projFeatureView.buildSheet();
  368. projFeatureView.showData(projFeatureView.datas);
  369. });
  370. $('#poj-set').on('hidden.bs.modal', function (e) {
  371. //destroy Spread
  372. if(projFeatureView.workBook){
  373. projFeatureView.workBook.destroy();
  374. projFeatureView.workBook = null;
  375. }
  376. projFeatureView.datas = [];
  377. });
  378. $('#tab_poj-settings-projFeature').on('shown.bs.tab', function () {
  379. projFeatureView.workBook.refresh();
  380. });
  381. });