main.js 8.6 KB

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