main.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. $('#addY').addClass('disabled');
  107. let createData = {type: parseInt(addType.val()), ID: uuid.v1(), name: cName, compilationId: compilationId, compilationName: compilationName, billsLibId: parseInt(billsLibId), billsLibName:billsLibName};
  108. let updateData = {updateType: updateType.create, updateData: createData};
  109. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  110. guidanceLibs.push(rstData);
  111. addLibToView($('.main').find('tbody'), rstData);
  112. $('#add').modal('hide');
  113. $('#addY').removeClass('disabled');
  114. $.bootstrapLoading.end();
  115. }, function () {
  116. $('#addY').removeClass('disabled');
  117. $.bootstrapLoading.end();
  118. });
  119. }
  120. catch(err){
  121. alert(err);
  122. $('#createName').focus();
  123. }
  124. });
  125. //新建模态框
  126. $('#add').on('hidden.bs.modal', function () {
  127. $('#createName').val('');
  128. });
  129. $('#add').on('shown.bs.modal', function () {
  130. $('#createName').focus();
  131. });
  132. //所有编辑按钮
  133. $('.main').find('tbody').on('click', '[data-target="#edit"]', function () {
  134. let tr = $(this).parent().parent();
  135. let selLib = existLib({k: 'ID', v: tr.attr('id')}, guidanceLibs);
  136. curLib = selLib;
  137. $('#edName').val(curLib.name);
  138. $('#edComSels').select().children('option:selected').text(curLib.compilationName);
  139. $('#edBillsLibSels').select().children('option:selected').text(curLib.billsLibName);
  140. });
  141. //编辑确认
  142. $('#editY').click(function(){
  143. try{
  144. let newName = $('#edName').val();
  145. if(newName.trim() === curLib.name){
  146. $('#edit').modal('hide');
  147. return;
  148. }
  149. if(!newName || newName.trim() === ''){
  150. throw '名称不能为空!';
  151. }
  152. if(existLib({k: 'name', v: newName}, guidanceLibs)){
  153. throw '该库已存在!';
  154. }
  155. let updateData = {updateType: updateType.update, findData: {ID: curLib.ID}, updateData: {name: newName}};
  156. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  157. curLib.name = newName;
  158. $(`#${curLib.ID} td:first a`).text(newName);
  159. $('#edit').modal('hide');
  160. });
  161. }
  162. catch(err){
  163. alert(err);
  164. $('#edName').focus();
  165. }
  166. });
  167. //编辑模态框
  168. $('#edit').on('shown.bs.modal', function () {
  169. $('#edName').focus();
  170. });
  171. //所有删除按钮
  172. $('.main').find('tbody').on('click', '[data-target="#del"]', function () {
  173. let tr = $(this).parent().parent();
  174. curLib = existLib({k: 'ID', v: tr.attr('id')}, guidanceLibs);
  175. console.log(curLib);
  176. });
  177. //删除确认
  178. $('#delY').click(function () {
  179. try{
  180. if(!curLib){
  181. throw '不存在该库!';
  182. }
  183. if(preLib && preLib.ID !== curLib.ID){
  184. deleteCount = 0;
  185. }
  186. deleteCount++;
  187. preLib = curLib;
  188. if(deleteCount === 3){
  189. $('#del').modal('hide');
  190. $.bootstrapLoading.start();
  191. let updateData = {updateType: updateType.delete, findData: {ID: curLib.ID}, updateData: {deleted: true}};
  192. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  193. $(`#${curLib.ID}`).remove();
  194. curLib = null;
  195. _.remove(guidanceLibs, function (lib) {
  196. return lib.ID === updateData.findData.ID;
  197. });
  198. $.bootstrapLoading.end();
  199. }, function () {
  200. $.bootstrapLoading.end();
  201. });
  202. }
  203. }
  204. catch(err){
  205. alert(err);
  206. }
  207. });
  208. //删除确认窗口关闭,重新计数
  209. $('#del').on('hidden.bs.modal', function () {
  210. deleteCount = 0;
  211. });
  212. }
  213. return {getComBillsLibInfo, getLibs, eventListener};
  214. })();
  215. $(document).ready(function () {
  216. billsGuidanceMain.getComBillsLibInfo();
  217. billsGuidanceMain.getLibs();
  218. billsGuidanceMain.eventListener();
  219. });