project_property_basicInfo.js 17 KB

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