main.js 8.1 KB

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