annotation.js 17 KB

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