project_property_basicInfo.js 18 KB

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