project_property_basicInfo.js 16 KB

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