project_property_basicInfo.js 18 KB

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