project_property_basicInfo.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. console.log('enter');
  135. for(let i = 0, len = items.length; i < len; i++){
  136. let row = i + args.cellRange.row;
  137. if(me.setting.locked.rows.indexOf(row) !== -1){
  138. recRows.push(row);
  139. }
  140. else if(me.setting.dateRows.indexOf(row) !== -1){
  141. items[i].value = me.filtDate(items[i].value);
  142. console.log(items.value);
  143. if(!me.isDef(items[i].value)){
  144. console.log('aa');
  145. recRows.push(row);
  146. }
  147. else {
  148. me.datas[row].value = items[i].value;
  149. }
  150. }
  151. else {
  152. me.datas[row].value = items[i].value;
  153. }
  154. }
  155. console.log(recRows);
  156. console.log(me.datas);
  157. if(recRows.length > 0){
  158. me.renderSheetFuc(args.sheet, function () {
  159. for(let i = 0, len = recRows.length; i < len; i++){
  160. let staticV = me.datas[recRows[i]].value || '';
  161. args.sheet.setValue(recRows[i], args.cellRange.col, staticV);
  162. }
  163. })
  164. }
  165. },
  166. isDef: function (v) {
  167. return v !== undefined && v !== null;
  168. },
  169. filtDate: function (v) {
  170. 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])))/
  171. let reDate = re.exec(v);
  172. let rst = reDate ? reDate[0] : null;
  173. return rst;
  174. },
  175. copyObj: function(obj){
  176. let newObj = {};
  177. for(let attr in obj){
  178. newObj[attr] = obj[attr];
  179. }
  180. return newObj;
  181. },
  182. initDatas: function (datas) {
  183. this.datas = [];
  184. for(let i = 0, len = datas.length; i < len; i++){
  185. this.datas.push(this.copyObj(datas[i]));
  186. }
  187. },
  188. //数据库读到的数据转换为展示的结构
  189. toViewDatas: function (datas) {
  190. let rst = [];
  191. for(let i = 0, len = datas.length; i < len; i++){
  192. let items = datas[i].items || null;
  193. if(items){
  194. rst.push(datas[i]);
  195. for(let j = 0, jLen = items.length; j < jLen; j++){
  196. rst.push(items[j]);
  197. }
  198. }
  199. }
  200. return rst;
  201. },
  202. //展示的结构转换为入库的结构
  203. toSaveDatas: function (datas) {
  204. let rst = [];
  205. let index = -1;
  206. for(let i = 0, len = datas.length; i < len; i++){
  207. let items = datas[i].items || null;
  208. if(items){
  209. delete datas[i].collapsed;
  210. datas[i].items = [];
  211. rst.push(datas[i]);
  212. index++;
  213. }
  214. else {
  215. rst[index]['items'].push(datas[i]);
  216. }
  217. }
  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.basicInformation': 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=true;
  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. getDatePickerCellType: function () {
  365. let ns = GC.Spread.Sheets;
  366. function DatePickerCellType() {
  367. }
  368. DatePickerCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  369. DatePickerCellType.prototype.createEditorElement = function (context) {
  370. //Create input presenter.
  371. return document.createElement("input");
  372. };
  373. DatePickerCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  374. //Initialize input editor.
  375. if (editorContext) {
  376. $editor = $(editorContext);
  377. //DatePickerCellType.prototype.activateEditor.apply(this, arguments);
  378. $editor.datepicker({dateFormat: 'yy-mm-dd'});
  379. $editor.css("position", "absolute");
  380. $editor.attr("gcUIElement", "gcEditingInput");
  381. $(".ui-datepicker").attr("gcUIElement", "gcEditingInput");
  382. }
  383. }
  384. DatePickerCellType.prototype.deactivateEditor = function (editorContext, context) {
  385. //Remove input editor when end editor status.
  386. if (editorContext) {
  387. var element = editorContext;
  388. $(element).datepicker("hide");
  389. $(element).datepicker("destroy");
  390. }
  391. // DatePickerCellType.prototype.deactivateEditor.apply(this, arguments)
  392. };
  393. DatePickerCellType.prototype.setEditorValue = function (editor, value, context) {
  394. //Sync value from Cell value to editor value.
  395. $(editor).datepicker("setDate", value);
  396. };
  397. DatePickerCellType.prototype.getEditorValue = function (editor, context) {
  398. //Sync value from editor value to cell value.
  399. return $(editor).datepicker("getDate");
  400. };
  401. DatePickerCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  402. if (editorContext) {
  403. $editor = $(editorContext);
  404. $editor.css("width", cellRect.width - 1);
  405. $editor.css("height", cellRect.height - 3);
  406. }
  407. };
  408. return new DatePickerCellType();
  409. }
  410. };
  411. $(document).ready(function () {
  412. $('#poj-set').on('shown.bs.modal', function (e) {
  413. //init Spread
  414. basicInfoView.initDatas(basicInfoView.orgDatas);
  415. basicInfoView.buildSheet();
  416. basicInfoView.showData(basicInfoView.datas);
  417. });
  418. $('#poj-set').on('hidden.bs.modal', function (e) {
  419. //destroy Spread
  420. if(basicInfoView.workBook){
  421. basicInfoView.workBook.destroy();
  422. basicInfoView.workBook = null;
  423. }
  424. basicInfoView.datas = [];
  425. });
  426. $('#tab_poj-settings-basicInfo').on('shown.bs.tab', function () {
  427. basicInfoView.workBook.refresh();
  428. });
  429. $('#property_ok').bind('click', function () {
  430. if(basicInfoView.toUpdate(basicInfoView.orgDatas, basicInfoView.datas)){
  431. basicInfoView.a_updateInfo(basicInfoView.toSaveDatas(basicInfoView.datas));
  432. }
  433. });
  434. });