jobContent.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 style='display: block'><th></th><th>编码</th><th>工作内容</th>/tr></thead>");
  77. let $tbody = $("<tbody id='partialBody' style='display:block; overflow: auto;'></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><a href data-toggle='modal' data-target='#editBianma' class='m-0'>点击勾选编码</a></td><td><textarea class='form-control'></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. autoFlashHeight();
  94. },
  95. //新增一行tr
  96. getNewTr: function (tbody, codes, jobContent) {
  97. let me = jobContentOprObj;
  98. let count = tbody.children().length > 0 ? tbody.children().length : 1;
  99. let $textTd = $("<td></td>");
  100. let $textarea = $("<textarea class='form-control'></textarea>");
  101. $textarea.val(jobContent);
  102. $textTd.append($textarea);
  103. let $tr = $("<tr><td>" + count + "</td><td><a href data-toggle='modal' data-target='#editBianmaQ' class='m-0'>编辑编码</a></td></tr>");
  104. $tr.children().children().bind('click', function () {
  105. me.currentOprTr = $tr;
  106. me.currentJobContent = $(me.currentOprTr.children()[2]).children().val();
  107. me.onclickFuncEdit($(this));
  108. });
  109. //文本变化;
  110. $textarea.bind('change', function () {
  111. let codes = me.getUpdateCodes($($(this).parent().parent().children()[1]).children());
  112. let jobContent = $(this).val();
  113. me.updateJobContent(pageOprObj.rationLibId, me.getUpdateArr(codes, jobContent), function () {
  114. if(jobContent.trim().length === 0){
  115. me.buildTablePartial(me.tablePartial, me.getGroup(me.currentRationItems));
  116. }
  117. });
  118. });
  119. $tr.append($textTd);
  120. for(let i = 0, len = codes.length; i < len; i ++){
  121. let $p = $("<p class='m-0'>" + codes[i] + "</p>");
  122. $tr.children()[1].append($p[0]);
  123. }
  124. me.setTextareaHeight($textarea, codes.length + 1);
  125. return $tr[0];
  126. },
  127. onclickFuncAdd: function (obj) {
  128. let me = jobContentOprObj;
  129. let txtarea = $(obj.parent().parent().children().children()[1]);
  130. let jobContent = txtarea.val();
  131. if(jobContent.trim().length > 0){//工作内容不为空才可添加编码
  132. let codesObj = me.getAddCodes(me.currentRationItems);
  133. me.buildCheckCodesCon(me.addCon, codesObj.checkedCodes, codesObj.disabledCodes)
  134. obj.attr('data-target', '#editBianma');
  135. }
  136. else{
  137. obj.attr('data-target', '');
  138. alert("工作内容不能为空!");
  139. }
  140. },
  141. onclickFuncEdit: function (obj) {
  142. let me = jobContentOprObj;
  143. me.buildEditableCodesCon(me.currentRationItems, me.updateCon, me.getUpdateCodes(obj));
  144. },
  145. //获取编码td中的编码
  146. getUpdateCodes: function (jq) {
  147. let rst = [];
  148. let nodes = jq.parent().children();
  149. for(let i = 1, len = nodes.length; i < len; i++){
  150. rst.push(nodes[i].textContent);
  151. }
  152. return rst;
  153. },
  154. //建一个编码checkbox Div
  155. buildCodeOption: function (code, attr) {
  156. let $div = $("<div class='col'><label class='form-check-label'><input class='form-check-input' type='checkbox' value= "+ code +"> "+ code +"</label></div>");
  157. let $checkBox = $div.children().children();
  158. if(attr){
  159. $checkBox.attr(attr, true);
  160. }
  161. return $div;
  162. },
  163. //建修改编码弹窗
  164. buildEditableCodesCon: function (rationItems, container,codes) {
  165. let me = jobContentOprObj;
  166. let codeDivs = [];
  167. container.empty();
  168. for(let i = 0, len = codes.length; i < len; i++){
  169. codeDivs.push({code: codes[i], attr: 'checked'});
  170. }
  171. for(let i = 0, len = rationItems.length; i < len; i++){
  172. if(codes.indexOf(rationItems[i].code) === -1){
  173. if(typeof rationItems[i].jobContent !== 'undefined' && rationItems[i].jobContent.toString().trim().length > 0){
  174. codeDivs.push({code: rationItems[i].code, attr: 'disabled'});
  175. }
  176. else{
  177. codeDivs.push({code: rationItems[i].code, attr: ''});
  178. }
  179. }
  180. }
  181. //排序
  182. codeDivs.sort(function (a, b) {
  183. let rst = 0;
  184. if(a.code > b.code) rst = 1;
  185. else if(a.code < b.code) rst = -1;
  186. return rst;
  187. });
  188. for(let i = 0, len = codeDivs.length; i < len; i++){
  189. container.append(me.buildCodeOption(codeDivs[i].code, codeDivs[i].attr));
  190. }
  191. },
  192. //建勾选编码弹窗
  193. buildCheckCodesCon: function (container, checkedCodes, disabledCodes) {
  194. let me = jobContentOprObj;
  195. container.empty();
  196. for(let i = 0, len = checkedCodes.length; i < len; i++){
  197. let $codeDiv = me.buildCodeOption(checkedCodes[i], 'checked');
  198. container.append($codeDiv);
  199. }
  200. for(let i = 0, len = disabledCodes.length; i < len; i++){
  201. let $codeDiv = me.buildCodeOption(disabledCodes[i], 'disabled');
  202. container.append($codeDiv);
  203. }
  204. },
  205. getAddCodes: function (rationItems) {
  206. let me = jobContentOprObj;
  207. let rst = {checkedCodes: [], disabledCodes: []};
  208. for(let i = 0, len = rationItems.length; i < len; i++){
  209. if(typeof rationItems[i].jobContent !== 'undefined' && rationItems[i].jobContent.toString().trim().length > 0){
  210. rst.disabledCodes.push(rationItems[i].code);
  211. }
  212. else{
  213. rst.checkedCodes.push(rationItems[i].code);
  214. }
  215. }
  216. return rst;
  217. },
  218. //获取选择后的编码窗口的编码及状态
  219. getCodesAfterS: function (checkNodes) {
  220. let rst = {checked: [], unchecked: []};
  221. for(let i = 0, len = checkNodes.length; i < len; i++){
  222. if(checkNodes[i].checked){
  223. rst.checked.push(checkNodes[i].value);
  224. }
  225. else if(!checkNodes[i].checked && !checkNodes[i].disabled){
  226. rst.unchecked.push(checkNodes[i].value);
  227. }
  228. }
  229. return rst;
  230. },
  231. setRadiosChecked: function (situation, radios) {
  232. let me = jobContentOprObj;
  233. if(situation === me.situations.ALL){
  234. radios[0].checked = true;
  235. radios[1].checked = false;
  236. $('#txtareaAll').val(me.currentRationItems.length > 0 ? me.currentRationItems[0].jobContent : '');
  237. me.currentJobContent = me.currentRationItems.length > 0 ? me.currentRationItems[0].jobContent : '';
  238. me.tableAll.show();
  239. me.tablePartial.hide();
  240. }
  241. else if(situation === me.situations.PARTIAL){
  242. radios[0].checked = false;
  243. radios[1].checked = true;
  244. me.tableAll.hide();
  245. me.tablePartial.show();
  246. }
  247. else if(situation === me.situations.NONE){
  248. radios[0].checked = false;
  249. radios[1].checked = false;
  250. me.tableAll.hide();
  251. me.tablePartial.hide();
  252. }
  253. },
  254. //radios是否可用,只有在定额章节树的底层节点才可用
  255. setRadiosDisabled: function (val, radios) {
  256. let me =jobContentOprObj;
  257. if(val){
  258. radios[0].checked = false;
  259. radios[1].checked = false;
  260. me.currentSituation = me.situations.NONE;
  261. }
  262. radios.attr('disabled', val);
  263. },
  264. radiosChange: function (radios, tableAll, tablePartial) {
  265. let me = jobContentOprObj;
  266. radios.change(function () {
  267. let val = $("input[name = 'optionsRadios']:checked").val();
  268. let selectedNode = sectionTreeObj.tree.selected;
  269. me.updateSituation(pageOprObj.rationLibId, selectedNode.getID(), val, function () {
  270. selectedNode.data.jobContentSituation = val;
  271. me.currentSituation = val;
  272. if(val === me.situations.ALL){
  273. let updateCodes = [];
  274. for(let i = 0, len = me.currentRationItems.length; i < len; i++){
  275. updateCodes.push(me.currentRationItems[i].code);
  276. }
  277. me.updateJobContent(pageOprObj.rationLibId, me.getUpdateArr(updateCodes, ''), function () {
  278. me.currentJobContent = '';
  279. $('#txtareaAll').val('');
  280. tableAll.show();
  281. tablePartial.hide();
  282. });
  283. }
  284. else{
  285. me.buildTablePartial(me.tablePartial, me.getGroup(me.currentRationItems));
  286. tableAll.hide();
  287. tablePartial.show();
  288. }
  289. });
  290. });
  291. },
  292. setTextareaHeight: function (textarea, nodesCount) {
  293. const perHeight = 21.6;
  294. textarea.height(nodesCount * 21.6);
  295. },
  296. bindEvents: function (txtarea) {
  297. let me = jobContentOprObj;
  298. txtarea.bind('change', function () {
  299. let jobContent = txtarea.val();
  300. let jqNodes = txtarea.parent().parent().children()[1].children;
  301. let updateCodes = me.getUpdateCodes(jqNodes);
  302. txtarea.attr('disabled', true);
  303. me.updateJobContent(pageOprObj.rationLibId, me.getUpdateArr(updateCodes, jobContent), function () {
  304. txtarea.attr('disabled', false);
  305. });
  306. });
  307. },
  308. bindAllEvents: function (txtarea) {
  309. let me = jobContentOprObj;
  310. txtarea.bind('change', function () {
  311. let met = this;
  312. let jobContent = $(met).val();
  313. $(met).attr('disabled', true);
  314. let updateCodes = [];
  315. for(let i = 0, len = me.currentRationItems.length; i < len; i++){
  316. updateCodes.push(me.currentRationItems[i].code);
  317. me.currentRationItems[i].jobContent = jobContent;
  318. }
  319. me.currentJobContent = jobContent;
  320. me.updateJobContent(pageOprObj.rationLibId, me.getUpdateArr(updateCodes, jobContent), function () {
  321. $(met).attr('disabled', false);
  322. });
  323. });
  324. },
  325. unbindEvents: function (txtarea) {
  326. txtarea.unbind();
  327. },
  328. //定额工作内容相关操作
  329. rationJobContentOpr: function (rationItems) {
  330. let me = jobContentOprObj;
  331. me.setRadiosDisabled(me.currentRationItems.length > 0 ? false : true, me.radios);
  332. me.setRadiosChecked(me.currentSituation, me.radios);
  333. me.buildTablePartial(me.tablePartial, me.getGroup(rationItems));
  334. },
  335. getUpdateArr: function (updateCodes, jobContent) {
  336. let rst = [];
  337. for(let i = 0, len = updateCodes.length; i < len; i++){
  338. rst.push({code: updateCodes[i], jobContent: jobContent});
  339. }
  340. return rst;
  341. },
  342. bindAddConBtn: function () {
  343. let me = jobContentOprObj;
  344. return function () {
  345. let codesObj = me.getCodesAfterS(me.addCon.children().children().children());
  346. let $tbody = $('#tablePartial tbody');
  347. let lastEle = $tbody[0].lastElementChild;
  348. let txtare = lastEle.lastElementChild.children[0];
  349. if(me.currentJobContent.trim().length > 0){//工作内容不为空才可添加编码
  350. let updateArr = me.getUpdateArr(codesObj.checked, me.currentJobContent);
  351. me.updateJobContent(pageOprObj.rationLibId, updateArr, function () {
  352. me.buildTablePartial(me.tablePartial, me.getGroup(me.currentRationItems));
  353. $(txtare).val('');
  354. });
  355. }
  356. else{
  357. alert("工作内容不能为空!");
  358. }
  359. }
  360. },
  361. bindUpdateConBtn: function () {
  362. let me = jobContentOprObj;
  363. return function () {
  364. let codesObj = me.getCodesAfterS(me.updateCon.children().children().children());
  365. let updateC = me.getUpdateArr(codesObj.checked, me.currentJobContent),
  366. updateUnC = me.getUpdateArr(codesObj.unchecked, ''),
  367. updateArr = updateC.concat(updateUnC);
  368. me.updateJobContent(pageOprObj.rationLibId, updateArr, function () {
  369. me.buildTablePartial(me.tablePartial, me.getGroup(me.currentRationItems));
  370. });
  371. }
  372. },
  373. //更新缓存的定额
  374. updateRationItem: function (rationItems, updateArr) {
  375. for(let i = 0, len = rationItems.length; i < len; i++){
  376. for(let j = 0, jLen = updateArr.length; j < jLen; j++){
  377. if(rationItems[i].code === updateArr[j].code){
  378. rationItems[i].jobContent = updateArr[j].jobContent;
  379. break;
  380. }
  381. }
  382. }
  383. },
  384. updateJobContent: function (repId, updateArr, callback){
  385. let me = jobContentOprObj;
  386. $.ajax({
  387. type: 'post',
  388. url: 'api/updateJobContent',
  389. data: {lastOpr: userAccount, repId: pageOprObj.rationLibId, updateArr: JSON.stringify(updateArr)},
  390. dataType: 'json',
  391. success: function (result) {
  392. if(!result.error){
  393. me.updateRationItem(jobContentOprObj.currentRationItems, updateArr);
  394. callback();
  395. }
  396. }
  397. });
  398. },
  399. updateSituation: function (repId, nodeId, situation, callback) {
  400. let me = jobContentOprObj;
  401. $.ajax({
  402. type: 'post',
  403. url: 'api/updateSituation',
  404. data: {lastOpr: userAccount, repId: pageOprObj.rationLibId, nodeId: nodeId, situation: situation},
  405. dataType: 'json',
  406. success: function (result) {
  407. if(!result.error){
  408. if(callback){
  409. callback();
  410. }
  411. }
  412. }
  413. })
  414. }
  415. };
  416. $(document).ready(function () {
  417. $('#gznr').on('shown.bs.tab', function () {
  418. autoFlashHeight();
  419. });
  420. });