project_property_projFeature.js 15 KB

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