project_property_basicInfo.js 14 KB

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