jobContent.js 17 KB

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