main.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/5/29
  7. * @version
  8. */
  9. const billsGuidanceMain = (function () {
  10. const updateType = {create: 'create', update: 'update', delete: 'delete'};
  11. let guidanceLibs = [];
  12. let curLib = null;
  13. //上一个选择的库(三次确认删除同一库时用)
  14. let preLib = null;
  15. let deleteCount = 0;
  16. //获取编办及编办下清单库
  17. //@return {void}
  18. function getComBillsLibInfo(){
  19. CommonAjax.post('/billsGuidance/api/getComBillsLibInfo', {}, function (rstData) {
  20. const comSels = $('#comSels');
  21. const billsLibSels = $('#billsLibSels');
  22. //设置编办及清单规则库选择
  23. comSels.empty();
  24. function setBillsLib(libs){
  25. billsLibSels.empty();
  26. for(let lib of libs){
  27. let libOpt = `<option value="${lib.billsLibId}">${lib.billsLibName}</option>`;
  28. billsLibSels.append(libOpt);
  29. }
  30. }
  31. let comIndex = {};
  32. for(let i = 0; i < rstData.length; i++){
  33. let compilation = rstData[i];
  34. comIndex[compilation._id] = compilation;
  35. let comOpt = `<option value = "${compilation._id}">${compilation.name}</option>`;
  36. comSels.append(comOpt);
  37. //设置初始选择的清单规则库
  38. if(i === 0){
  39. setBillsLib(compilation.billsLibs);
  40. }
  41. }
  42. //变更编办选择
  43. comSels.on('change', function () {
  44. let curComId = $(this).select().val();
  45. let curCom = comIndex[curComId];
  46. setBillsLib(curCom.billsLibs);
  47. });
  48. });
  49. }
  50. //html新增库
  51. //@param {Object}tbody {Object}lib @return {void}
  52. function addLibToView(tbody, lib){
  53. let tr = `<tr id="${lib.ID}">
  54. <td><a href="/billsGuidance/guidance/?libID=${lib.ID}">${lib.name}</a>
  55. <td>${lib.compilationName}</td><td>${lib.billsLibName}</td>
  56. <td>${lib.createDate.split(' ')[0]}</td>
  57. <td><a href="javascript:void(0);" data-toggle="modal" data-target="#edit" title="编辑"><i class="fa fa-pencil-square-o"></i></a>
  58. <a href="javascript:void(0);" data-toggle="modal" data-target="#del" class="text-danger" title="删除"><i class="fa fa-remove"></i></a></td></tr>`;
  59. tbody.append(tr);
  60. }
  61. //获取清单指引库
  62. //@return {void}
  63. function getLibs(){
  64. CommonAjax.post('/billsGuidance/api/getBillsGuideLibs', {}, function (rstData) {
  65. guidanceLibs = rstData;
  66. const tbody = $('.main').find('tbody');
  67. tbody.empty();
  68. for(let lib of rstData){
  69. addLibToView(tbody, lib);
  70. }
  71. });
  72. }
  73. //是否已存在此库
  74. //@param {Object}findSet {Array}libs @return {Object}
  75. function existLib(findSet, libs) {
  76. for(let lib of libs){
  77. if(lib[findSet.k] === findSet.v){
  78. return lib;
  79. }
  80. }
  81. return null;
  82. }
  83. //监听事件
  84. //@return {void}
  85. function eventListener(){
  86. //新建库确认按钮事件
  87. $('#addY').click(function () {
  88. try{
  89. let cName = $('#createName').val();
  90. if(!cName || cName.trim() === ''){
  91. throw '请输入名称!';
  92. }
  93. if(existLib({k: 'name', v: cName}, guidanceLibs)){
  94. throw '已存在此库!';
  95. }
  96. let compilationId = $('#comSels').select().val();
  97. let compilationName = $('#comSels').select().children('option:selected').text();
  98. if(!compilationId){
  99. throw '请选择编办!';
  100. }
  101. let billsLibId = $('#billsLibSels').select().val();
  102. let billsLibName = $('#billsLibSels').select().children('option:selected').text();
  103. if(!billsLibId){
  104. throw '请选择清单规则库';
  105. }
  106. //新建
  107. $.bootstrapLoading.start();
  108. let createData = {ID: uuid.v1(), name: cName, compilationId: compilationId, compilationName: compilationName, billsLibId: parseInt(billsLibId), billsLibName:billsLibName};
  109. let updateData = {updateType: updateType.create, updateData: createData};
  110. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  111. guidanceLibs.push(rstData);
  112. addLibToView($('.main').find('tbody'), rstData);
  113. $('#add').modal('hide');
  114. $.bootstrapLoading.end();
  115. }, function () {
  116. $.bootstrapLoading.end();
  117. });
  118. }
  119. catch(err){
  120. alert(err);
  121. $('#createName').focus();
  122. }
  123. });
  124. //新建模态框
  125. $('#add').on('hidden.bs.modal', function () {
  126. $('#createName').val('');
  127. });
  128. $('#add').on('shown.bs.modal', function () {
  129. $('#createName').focus();
  130. });
  131. //所有编辑按钮
  132. $('.main').find('tbody').on('click', '[data-target="#edit"]', function () {
  133. let tr = $(this).parent().parent();
  134. let selLib = existLib({k: 'ID', v: tr.attr('id')}, guidanceLibs);
  135. curLib = selLib;
  136. $('#edName').val(curLib.name);
  137. $('#edComSels').select().children('option:selected').text(curLib.compilationName);
  138. $('#edBillsLibSels').select().children('option:selected').text(curLib.billsLibName);
  139. });
  140. //编辑确认
  141. $('#editY').click(function(){
  142. try{
  143. let newName = $('#edName').val();
  144. if(newName.trim() === curLib.name){
  145. $('#edit').modal('hide');
  146. return;
  147. }
  148. if(!newName || newName.trim() === ''){
  149. throw '名称不能为空!';
  150. }
  151. if(existLib({k: 'name', v: newName}, guidanceLibs)){
  152. throw '该库已存在!';
  153. }
  154. let updateData = {updateType: updateType.update, findData: {ID: curLib.ID}, updateData: {name: newName}};
  155. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  156. curLib.name = newName;
  157. $(`#${curLib.ID} td:first a`).text(newName);
  158. $('#edit').modal('hide');
  159. });
  160. }
  161. catch(err){
  162. alert(err);
  163. $('#edName').focus();
  164. }
  165. });
  166. //编辑模态框
  167. $('#edit').on('shown.bs.modal', function () {
  168. $('#edName').focus();
  169. });
  170. //所有删除按钮
  171. $('.main').find('tbody').on('click', '[data-target="#del"]', function () {
  172. let tr = $(this).parent().parent();
  173. curLib = existLib({k: 'ID', v: tr.attr('id')}, guidanceLibs);
  174. console.log(curLib);
  175. });
  176. //删除确认
  177. $('#delY').click(function () {
  178. try{
  179. if(!curLib){
  180. throw '不存在该库!';
  181. }
  182. if(preLib && preLib.ID !== curLib.ID){
  183. deleteCount = 0;
  184. }
  185. deleteCount++;
  186. preLib = curLib;
  187. if(deleteCount === 3){
  188. $('#del').modal('hide');
  189. $.bootstrapLoading.start();
  190. let updateData = {updateType: updateType.delete, findData: {ID: curLib.ID}, updateData: {deleted: true}};
  191. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  192. $(`#${curLib.ID}`).remove();
  193. curLib = null;
  194. _.remove(guidanceLibs, function (lib) {
  195. return lib.ID === updateData.findData.ID;
  196. });
  197. $.bootstrapLoading.end();
  198. }, function () {
  199. $.bootstrapLoading.end();
  200. });
  201. }
  202. }
  203. catch(err){
  204. alert(err);
  205. }
  206. });
  207. //删除确认窗口关闭,重新计数
  208. $('#del').on('hidden.bs.modal', function () {
  209. deleteCount = 0;
  210. });
  211. }
  212. return {getComBillsLibInfo, getLibs, eventListener};
  213. })();
  214. $(document).ready(function () {
  215. billsGuidanceMain.getComBillsLibInfo();
  216. billsGuidanceMain.getLibs();
  217. billsGuidanceMain.eventListener();
  218. });