annotation.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /**
  2. * Created by Zhong on 2017/12/20.
  3. */
  4. //附注
  5. let annotationOprObj = {
  6. workBook: null,
  7. container: $('#editAnnoCodeSpread'),
  8. situations: {ALL: 'ALL', PARTIAL: 'PARTIAL', NONE: 'NONE'},
  9. currentSituation: null,//本项适用情况
  10. radios: $("input[name = 'fzRadios']"),
  11. fzTableAll: $('#fzTableAll'),
  12. fzTablePartial: $('#fzTablePartial'),
  13. currentOprTr: null,
  14. currentAnnotation: null,
  15. addCon: $('#fzAddCon'),//勾选编码模态框
  16. updateCon: $('#fzUpdateCon'),//编辑编码模态框
  17. setting: {
  18. header:[
  19. {headerName:"编码",headerWidth:240,dataCode:"code", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"},
  20. {headerName:"选择",headerWidth:40,dataCode:"select", hAlign: "center", vAlign: "center"},
  21. ],
  22. },
  23. buildSheet: function () {
  24. let me = this;
  25. me.workBook = sheetCommonObj.buildSheet(me.container[0], me.setting, 30);
  26. me.workBook.refresh();
  27. me.workBook.bind(GC.Spread.Sheets.Events.ButtonClicked, me.onButtonClick);
  28. let sheet = me.workBook.getSheet(0);
  29. sheet.options.isProtected = true;
  30. sheet.getRange(-1, 0, -1, 1).locked(true);
  31. sheet.getRange(-1, 1, -1, 1).locked(false);
  32. },
  33. renderSheet: function (func) {
  34. let me = this;
  35. let sheet = me.workBook.getSheet(0);
  36. sheet.suspendPaint();
  37. sheet.suspendEvent();
  38. if (func) {
  39. func();
  40. }
  41. sheet.resumePaint();
  42. sheet.resumeEvent();
  43. },
  44. onButtonClick: function (sender, args) {
  45. if (args.sheet.isEditing()) {
  46. args.sheet.endEdit();
  47. }
  48. },
  49. showData: function (datas) {
  50. let me = this,
  51. sheet = me.workBook.getSheet(0),
  52. headers = me.setting.header;
  53. let fuc = function () {
  54. sheet.setRowCount(datas.length);
  55. //复选框
  56. let checkBoxType = new GC.Spread.Sheets.CellTypes.CheckBox();
  57. sheet.setCellType(-1, 1, checkBoxType);
  58. for(let col = 0, cLen = headers.length; col < cLen; col++){
  59. if(headers[col].formatter){
  60. sheet.setFormatter(-1, col, headers[col].formatter);
  61. }
  62. sheet.getRange(-1, col, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[headers[col]['hAlign']]);
  63. sheet.getRange(-1, col, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[headers[col]['vAlign']]);
  64. for(let row = 0, rLen = datas.length; row < rLen; row++){
  65. sheet.setValue(row, col, datas[row][headers[col]['dataCode']]);
  66. }
  67. }
  68. };
  69. me.renderSheet(fuc);
  70. },
  71. clickUpdate: function (txtarea) {//解决编辑完后在未失去焦点的时候直接定额章节树
  72. let me = annotationOprObj;
  73. if(txtarea.is(':focus')){
  74. let annotation = txtarea.val();
  75. if(annotation !== me.currentAnnotation){
  76. jobContentOprObj.preTreeNode.data.annotation = annotation;
  77. me.unbindEvents(txtarea);
  78. txtarea.blur();
  79. let updateCodes = [];
  80. for(let i = 0, len = jobContentOprObj.currentRationItems.length; i < len; i++){
  81. updateCodes.push(jobContentOprObj.currentRationItems[i].code);
  82. jobContentOprObj.currentRationItems[i].annotation = annotation;
  83. }
  84. me.updateAnnotation(pageOprObj.rationLibId, me.getUpdateArr(updateCodes, annotation), function () {
  85. me.bindAllEvents(txtarea);
  86. });
  87. }
  88. else {
  89. txtarea.blur();
  90. }
  91. }
  92. },
  93. getGroup: function (rationItems) {
  94. let rst = [];//rst = [{jobContent: String, items: Array}]
  95. for(let i = 0, len = rationItems.length; i < len; i++){
  96. if(typeof rationItems[i].annotation !== 'undefined' && rationItems[i].annotation.toString().trim().length > 0){
  97. let isExist = false;
  98. for(let j = 0, jLen = rst.length; j < jLen; j++){
  99. if(rst[j].annotation === rationItems[i].annotation){
  100. isExist = true;
  101. rst[j].items.push(rationItems[i].code);
  102. break;
  103. }
  104. }
  105. if(!isExist){
  106. rst.push({annotation: rationItems[i].annotation, items: [rationItems[i].code]});
  107. }
  108. }
  109. }
  110. return rst;
  111. },
  112. hideTable: function (tableAll, tablePartial) {
  113. if(tableAll){
  114. tableAll.hide();
  115. }
  116. if(tablePartial){
  117. tablePartial.hide();
  118. }
  119. },
  120. //建table
  121. buildTablePartial: function (table, group) {
  122. let me = annotationOprObj;
  123. table.empty();
  124. let $thead = $("<thead><tr><th></th><th>编码</th><th>附注</th>/tr></thead>");
  125. let $tbody = $("<tbody></tbody>");
  126. let count = 1;
  127. for(let i = 0, len = group.length; i < len; i++){
  128. let $newTr = me.getNewTr($tbody, group[i].items, group[i].annotation);
  129. $tbody.append($newTr);
  130. count++;
  131. }
  132. let $trEnd = $("<tr><td>"+ count +"</td><td><a href data-toggle='modal' data-target='#fzEditBianma' class='m-0'>点击勾选编码</a></td><td><textarea class='form-control'></textarea></td></tr>");//勾选行
  133. $($trEnd.children().children()[0]).bind('click', function () {
  134. me.onclickFuncAdd($(this));
  135. me.currentOprTr = $trEnd;
  136. me.currentAnnotation = $(me.currentOprTr.children()[2]).children().val();
  137. });
  138. $tbody.append($trEnd);
  139. table.append($thead);
  140. table.append($tbody);
  141. },
  142. //新增一行tr
  143. getNewTr: function (tbody, codes, jobContent) {
  144. let me = annotationOprObj;
  145. let count = tbody.children().length > 0 ? tbody.children().length : 1;
  146. let $textTd = $("<td></td>");
  147. let $textarea = $("<textarea class='form-control'></textarea>");
  148. $textarea.val(jobContent);
  149. $textTd.append($textarea);
  150. let $tr = $("<tr><td>" + count + "</td><td><a href data-toggle='modal' data-target='#fzEditBianmaQ' class='m-0'>编辑编码</a></td></tr>");
  151. $tr.children().children().bind('click', function () {
  152. me.currentOprTr = $tr;
  153. me.currentAnnotation = $(me.currentOprTr.children()[2]).children().val();
  154. me.onclickFuncEdit($(this));
  155. });
  156. //文本变化;
  157. $textarea.bind('change', function () {
  158. let codes = me.getUpdateCodes($($(this).parent().parent().children()[1]).children());
  159. let annotation = $(this).val();
  160. me.updateAnnotation(pageOprObj.rationLibId, me.getUpdateArr(codes, annotation), function () {
  161. if(annotation.trim().length === 0){
  162. me.buildTablePartial(me.fzTablePartial, me.getGroup(jobContentOprObj.currentRationItems));
  163. }
  164. });
  165. });
  166. $tr.append($textTd);
  167. for(let i = 0, len = codes.length; i < len; i ++){
  168. let $p = $("<p class='m-0'>" + codes[i] + "</p>");
  169. $tr.children()[1].append($p[0]);
  170. }
  171. me.setTextareaHeight($textarea, codes.length + 1);
  172. return $tr[0];
  173. },
  174. onclickFuncAdd: function (obj) {
  175. let me = annotationOprObj;
  176. let txtarea = $(obj.parent().parent().children().children()[1]);
  177. let annotation = txtarea.val();
  178. if(annotation.trim().length > 0){
  179. /*let codesObj = me.getAddCodes(jobContentOprObj.currentRationItems);
  180. me.buildCheckCodesCon(me.addCon, codesObj.checkedCodes, codesObj.disabledCodes)*/
  181. obj.attr('data-target', '#fzEditBianmaQ');
  182. if (!me.workBook) {
  183. setTimeout(function () {
  184. me.buildSheet();
  185. me.showData(me.getShowDatas(jobContentOprObj.currentRationItems, []));
  186. }, 200);
  187. } else {
  188. me.showData(me.getShowDatas(jobContentOprObj.currentRationItems, []));
  189. }
  190. }
  191. else{
  192. obj.attr('data-target', '');
  193. alert("附注不能为空!");
  194. }
  195. },
  196. onclickFuncEdit: function (obj) {
  197. let me = annotationOprObj;
  198. //me.buildEditableCodesCon(jobContentOprObj.currentRationItems, me.updateCon, me.getUpdateCodes(obj));
  199. if (!me.workBook) {
  200. setTimeout(function () {
  201. me.buildSheet();
  202. me.showData(me.getShowDatas(jobContentOprObj.currentRationItems, me.getUpdateCodes(obj)));
  203. }, 200);
  204. } else {
  205. me.showData(me.getShowDatas(jobContentOprObj.currentRationItems, me.getUpdateCodes(obj)));
  206. }
  207. },
  208. //获取弹出选择编码的数据
  209. getShowDatas: function (rationItems, codes) {
  210. let rst = [];
  211. for(let i = 0, len = codes.length; i < len; i++){
  212. rst.push({code: codes[i], select: true});
  213. }
  214. for(let i = 0, len = rationItems.length; i < len; i++){
  215. if(!codes.includes(rationItems[i].code)){
  216. if (typeof rationItems[i].annotation === 'undefined' || rationItems[i].annotation.toString().trim().length <= 0) {
  217. rst.push({code: rationItems[i].code, select: false});
  218. }
  219. }
  220. }
  221. return rst;
  222. },
  223. //获取编码td中的编码
  224. getUpdateCodes: function (jq) {
  225. let rst = [];
  226. let nodes = jq.parent().children();
  227. for(let i = 1, len = nodes.length; i < len; i++){
  228. rst.push(nodes[i].textContent);
  229. }
  230. return rst;
  231. },
  232. //建一个编码checkbox Div
  233. buildCodeOption: function (code, attr) {
  234. let $div = $("<div class='col'><label class='form-check-label'><input class='form-check-input' type='checkbox' value= "+ code +"> "+ code +"</label></div>");
  235. let $checkBox = $div.children().children();
  236. if(attr){
  237. $checkBox.attr(attr, true);
  238. }
  239. return $div;
  240. },
  241. //建修改编码弹窗
  242. buildEditableCodesCon: function (rationItems, container, codes) {
  243. let me = annotationOprObj;
  244. let codeDivs = [];
  245. container.empty();
  246. for(let i = 0, len = codes.length; i < len; i++){
  247. codeDivs.push({code: codes[i], attr: 'checked'});
  248. }
  249. for(let i = 0, len = rationItems.length; i < len; i++){
  250. if(codes.indexOf(rationItems[i].code) === -1){
  251. if(typeof rationItems[i].annotation !== 'undefined' && rationItems[i].annotation.toString().trim().length > 0){
  252. codeDivs.push({code: rationItems[i].code, attr: 'disabled'});
  253. }
  254. else{
  255. codeDivs.push({code: rationItems[i].code, attr: ''});
  256. }
  257. }
  258. }
  259. //排序
  260. codeDivs.sort(function (a, b) {
  261. let rst = 0;
  262. if(a.code > b.code) rst = 1;
  263. else if(a.code < b.code) rst = -1;
  264. return rst;
  265. });
  266. for(let i = 0, len = codeDivs.length; i < len; i++){
  267. container.append(me.buildCodeOption(codeDivs[i].code, codeDivs[i].attr));
  268. }
  269. },
  270. //建勾选编码弹窗
  271. buildCheckCodesCon: function (container, checkedCodes, disabledCodes) {
  272. let me = annotationOprObj;
  273. container.empty();
  274. for(let i = 0, len = checkedCodes.length; i < len; i++){
  275. let $codeDiv = me.buildCodeOption(checkedCodes[i], 'checked');
  276. container.append($codeDiv);
  277. }
  278. for(let i = 0, len = disabledCodes.length; i < len; i++){
  279. let $codeDiv = me.buildCodeOption(disabledCodes[i], 'disabled');
  280. container.append($codeDiv);
  281. }
  282. },
  283. getAddCodes: function (rationItems) {
  284. let me = annotationOprObj;
  285. let rst = {checkedCodes: [], disabledCodes: []};
  286. for(let i = 0, len = rationItems.length; i < len; i++){
  287. if(typeof rationItems[i].annotation !== 'undefined' && rationItems[i].annotation.toString().trim().length > 0){
  288. rst.disabledCodes.push(rationItems[i].code);
  289. }
  290. else{
  291. rst.checkedCodes.push(rationItems[i].code);
  292. }
  293. }
  294. return rst;
  295. },
  296. //获取选择后的编码窗口的编码及状态
  297. getCodesAfterS: function () {
  298. let rst = {checked: [], unchecked: []};
  299. let sheet = this.workBook.getSheet(0);
  300. for(let i = 0; i < sheet.getRowCount(); i++){
  301. let code = sheet.getValue(i, 0),
  302. checked = sheet.getValue(i, 1);
  303. if (checked) {
  304. rst.checked.push(code);
  305. } else {
  306. rst.unchecked.push(code);
  307. }
  308. }
  309. return rst;
  310. },
  311. /* getCodesAfterS: function (checkNodes) {
  312. let rst = {checked: [], unchecked: []};
  313. for(let i = 0, len = checkNodes.length; i < len; i++){
  314. if(checkNodes[i].checked){
  315. rst.checked.push(checkNodes[i].value);
  316. }
  317. else if(!checkNodes[i].checked && !checkNodes[i].disabled){
  318. rst.unchecked.push(checkNodes[i].value);
  319. }
  320. }
  321. return rst;
  322. },*/
  323. setRadiosChecked: function (situation, radios) {
  324. let me = annotationOprObj;
  325. if(situation === me.situations.ALL){
  326. radios[0].checked = true;
  327. radios[1].checked = false;
  328. $('#fzTxtareaAll').val(jobContentOprObj.currentRationItems.length > 0 ? jobContentOprObj.currentRationItems[0].annotation : '');
  329. me.currentAnnotation = jobContentOprObj.currentRationItems.length > 0 ? jobContentOprObj.currentRationItems[0].annotation : '';
  330. me.fzTableAll.show();
  331. me.fzTablePartial.hide();
  332. }
  333. else if(situation === me.situations.PARTIAL){
  334. radios[0].checked = false;
  335. radios[1].checked = true;
  336. me.fzTableAll.hide();
  337. me.fzTablePartial.show();
  338. }
  339. else if(situation === me.situations.NONE){
  340. radios[0].checked = false;
  341. radios[1].checked = false;
  342. me.fzTableAll.hide();
  343. me.fzTablePartial.hide();
  344. }
  345. },
  346. //radios是否可用,只有在定额章节树的底层节点才可用
  347. setRadiosDisabled: function (val, radios) {
  348. let me =annotationOprObj;
  349. if(val){
  350. radios[0].checked = false;
  351. radios[1].checked = false;
  352. me.currentSituation = me.situations.NONE;
  353. }
  354. radios.attr('disabled', val);
  355. },
  356. radiosChange: function (radios, tableAll, tablePartial) {
  357. let me = annotationOprObj;
  358. radios.change(function () {
  359. let val = $("input[name = 'fzRadios']:checked").val();
  360. let selectedNode = sectionTreeObj.tree.selected;
  361. me.updateAnnoSituation(pageOprObj.rationLibId, selectedNode.getID(), val, function () {
  362. selectedNode.data.annotationSituation = val;
  363. me.currentSituation = val;
  364. if(val === me.situations.ALL){
  365. let updateCodes = [];
  366. for(let i = 0, len = jobContentOprObj.currentRationItems.length; i < len; i++){
  367. updateCodes.push(jobContentOprObj.currentRationItems[i].code);
  368. }
  369. me.updateAnnotation(pageOprObj.rationLibId, me.getUpdateArr(updateCodes, ''), function () {
  370. me.currentAnnotation = '';
  371. $('#fzTxtareaAll').val('');
  372. tableAll.show();
  373. tablePartial.hide();
  374. });
  375. }
  376. else{
  377. me.buildTablePartial(me.fzTablePartial, me.getGroup(jobContentOprObj.currentRationItems));
  378. tableAll.hide();
  379. tablePartial.show();
  380. }
  381. });
  382. });
  383. },
  384. setTextareaHeight: function (textarea, nodesCount) {
  385. const perHeight = 21.6;
  386. textarea.height(nodesCount * 21.6);
  387. },
  388. bindEvents: function (txtarea) {
  389. let me = annotationOprObj;
  390. txtarea.bind('change', function () {
  391. let annotation = txtarea.val();
  392. let jqNodes = txtarea.parent().parent().children()[1].children;
  393. let updateCodes = me.getUpdateCodes(jqNodes);
  394. txtarea.attr('disabled', true);
  395. me.updateAnnotation(pageOprObj.rationLibId, me.getUpdateArr(updateCodes, annotation), function () {
  396. txtarea.attr('disabled', false);
  397. });
  398. });
  399. },
  400. bindAllEvents: function (txtarea) {
  401. let me = annotationOprObj;
  402. txtarea.bind('change', function () {
  403. let met = this;
  404. let annotation = $(met).val();
  405. $(met).attr('disabled', true);
  406. let updateCodes = [];
  407. for(let i = 0, len = jobContentOprObj.currentRationItems.length; i < len; i++){
  408. updateCodes.push(jobContentOprObj.currentRationItems[i].code);
  409. jobContentOprObj.currentRationItems[i].annotation = annotation;
  410. }
  411. me.currentAnnotation = annotation;
  412. me.updateAnnotation(pageOprObj.rationLibId, me.getUpdateArr(updateCodes, annotation), function () {
  413. $(met).attr('disabled', false);
  414. });
  415. });
  416. },
  417. unbindEvents: function (txtarea) {
  418. txtarea.unbind();
  419. },
  420. //定额工作内容相关操作
  421. rationAnnotationOpr: function (rationItems) {
  422. let me = annotationOprObj;
  423. me.setRadiosDisabled(sectionTreeObj.tree.selected.children.length === 0 ? false : true, me.radios);
  424. me.setRadiosChecked(me.currentSituation, me.radios);
  425. me.buildTablePartial(me.fzTablePartial, me.getGroup(rationItems));
  426. },
  427. getUpdateArr: function (updateCodes, annotation) {
  428. let rst = [];
  429. for(let i = 0, len = updateCodes.length; i < len; i++){
  430. rst.push({code: updateCodes[i], annotation: annotation});
  431. }
  432. return rst;
  433. },
  434. bindAddConBtn: function () {
  435. let me = annotationOprObj;
  436. return function () {
  437. let codesObj = me.getCodesAfterS(me.addCon.children().children().children());
  438. let $tbody = $('#fzTablePartial tbody');
  439. let lastEle = $tbody[0].lastElementChild;
  440. let txtare = lastEle.lastElementChild.children[0];
  441. if(me.currentAnnotation.trim().length > 0){//工作内容不为空才可添加编码
  442. let updateArr = me.getUpdateArr(codesObj.checked, me.currentAnnotation);
  443. me.updateAnnotation(pageOprObj.rationLibId, updateArr, function () {
  444. me.buildTablePartial(me.fzTablePartial, me.getGroup(jobContentOprObj.currentRationItems));
  445. $(txtare).val('');
  446. });
  447. }
  448. else{
  449. alert("附注不能为空!");
  450. }
  451. }
  452. },
  453. bindUpdateConBtn: function () {
  454. let me = annotationOprObj;
  455. return function () {
  456. let codesObj = me.getCodesAfterS(me.updateCon.children().children().children());
  457. let updateC = me.getUpdateArr(codesObj.checked, me.currentAnnotation),
  458. updateUnC = me.getUpdateArr(codesObj.unchecked, ''),
  459. updateArr = updateC.concat(updateUnC);
  460. me.updateAnnotation(pageOprObj.rationLibId, updateArr, function () {
  461. me.buildTablePartial(me.fzTablePartial, me.getGroup(jobContentOprObj.currentRationItems));
  462. });
  463. }
  464. },
  465. //更新缓存的定额
  466. updateRationItem: function (rationItems, updateArr) {
  467. for(let i = 0, len = rationItems.length; i < len; i++){
  468. for(let j = 0, jLen = updateArr.length; j < jLen; j++){
  469. if(rationItems[i].code === updateArr[j].code){
  470. rationItems[i].annotation = updateArr[j].annotation;
  471. break;
  472. }
  473. }
  474. }
  475. },
  476. updateAnnotation: function (repId, updateArr, callback){
  477. let me = annotationOprObj;
  478. $.ajax({
  479. type: 'post',
  480. url: 'api/updateAnnotation',
  481. data: {lastOpr: userAccount, repId: repId, updateArr: JSON.stringify(updateArr)},
  482. dataType: 'json',
  483. success: function (result) {
  484. if(!result.error){
  485. me.updateRationItem(jobContentOprObj.currentRationItems, updateArr);
  486. callback();
  487. }
  488. }
  489. });
  490. },
  491. updateAnnoSituation: function (repId, nodeId, situation, callback) {
  492. let me = annotationOprObj;
  493. $.ajax({
  494. type: 'post',
  495. url: 'api/updateAnnoSituation',
  496. data: {lastOpr: userAccount, repId: repId, nodeId: nodeId, situation: situation},
  497. dataType: 'json',
  498. success: function (result) {
  499. if(!result.error){
  500. if(callback){
  501. callback();
  502. }
  503. }
  504. }
  505. })
  506. }
  507. };