main.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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}&locked=true">${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>
  51. <a class="lock-btn-control disabled" href="javascript:void(0);" data-toggle="modal" data-target="#edit" title="编辑"><i class="fa fa-pencil-square-o"></i></a>
  52. <a class="lock-btn-control disabled text-danger" href="javascript:void(0);" data-toggle="modal" data-target="#del" title="删除"><i class="fa fa-remove"></i></a>
  53. <a class="lock" data-locked="true" href="javascript:void(0);" title="解锁"><i class="fa fa-unlock-alt"></i></a>
  54. </td></tr>`;
  55. tbody.append(tr);
  56. }
  57. //获取清单指引库
  58. //@return {void}
  59. function getLibs(){
  60. CommonAjax.post('/billsGuidance/api/getBillsGuideLibs', {}, function (rstData) {
  61. guidanceLibs = rstData;
  62. const tbody = $('.main').find('tbody');
  63. tbody.empty();
  64. for(let lib of rstData){
  65. addLibToView(tbody, lib);
  66. }
  67. });
  68. }
  69. //是否已存在此库
  70. //@param {Object}findSet {Array}libs @return {Object}
  71. function existLib(findSet, libs) {
  72. for(let lib of libs){
  73. if(lib[findSet.k] === findSet.v){
  74. return lib;
  75. }
  76. }
  77. return null;
  78. }
  79. //监听事件
  80. //@return {void}
  81. function eventListener(){
  82. //新建库确认按钮事件
  83. $('#addY').click(function () {
  84. try{
  85. let cName = $('#createName').val();
  86. if(!cName || cName.trim() === ''){
  87. throw '请输入名称!';
  88. }
  89. if(existLib({k: 'name', v: cName}, guidanceLibs)){
  90. throw '已存在此库!';
  91. }
  92. let compilationId = $('#comSels').select().val();
  93. let compilationName = $('#comSels').select().children('option:selected').text();
  94. if(!compilationId){
  95. throw '请选择编办!';
  96. }
  97. let billsLibId = $('#billsLibSels').select().val();
  98. let billsLibName = $('#billsLibSels').select().children('option:selected').text();
  99. if(!billsLibId){
  100. throw '请选择清单规则库';
  101. }
  102. //库类型
  103. let addType = $('#add').find('input:checked');
  104. if(!addType){
  105. throw '请选择库类型';
  106. }
  107. //新建
  108. $.bootstrapLoading.start();
  109. $('#addY').addClass('disabled');
  110. let createData = {type: parseInt(addType.val()), ID: uuid.v1(), name: cName, compilationId: compilationId, compilationName: compilationName, billsLibId: parseInt(billsLibId), billsLibName:billsLibName};
  111. let updateData = {updateType: updateType.create, updateData: createData};
  112. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  113. guidanceLibs.push(rstData);
  114. addLibToView($('.main').find('tbody'), rstData);
  115. $('#add').modal('hide');
  116. $('#addY').removeClass('disabled');
  117. $.bootstrapLoading.end();
  118. }, function () {
  119. $('#addY').removeClass('disabled');
  120. $.bootstrapLoading.end();
  121. });
  122. }
  123. catch(err){
  124. alert(err);
  125. $('#createName').focus();
  126. }
  127. });
  128. //新建模态框
  129. $('#add').on('hidden.bs.modal', function () {
  130. $('#createName').val('');
  131. });
  132. $('#add').on('shown.bs.modal', function () {
  133. $('#createName').focus();
  134. });
  135. //所有编辑按钮
  136. $('.main').find('tbody').on('click', '[data-target="#edit"]', function () {
  137. let tr = $(this).parent().parent();
  138. let selLib = existLib({k: 'ID', v: tr.attr('id')}, guidanceLibs);
  139. curLib = selLib;
  140. $('#edName').val(curLib.name);
  141. $('#edComSels').select().children('option:selected').text(curLib.compilationName);
  142. $('#edBillsLibSels').select().children('option:selected').text(curLib.billsLibName);
  143. });
  144. //编辑确认
  145. $('#editY').click(function(){
  146. try{
  147. let newName = $('#edName').val();
  148. if(newName.trim() === curLib.name){
  149. $('#edit').modal('hide');
  150. return;
  151. }
  152. if(!newName || newName.trim() === ''){
  153. throw '名称不能为空!';
  154. }
  155. if(existLib({k: 'name', v: newName}, guidanceLibs)){
  156. throw '该库已存在!';
  157. }
  158. let updateData = {updateType: updateType.update, findData: {ID: curLib.ID}, updateData: {name: newName}};
  159. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  160. curLib.name = newName;
  161. $(`#${curLib.ID} td:first a`).text(newName);
  162. $('#edit').modal('hide');
  163. });
  164. }
  165. catch(err){
  166. alert(err);
  167. $('#edName').focus();
  168. }
  169. });
  170. //编辑模态框
  171. $('#edit').on('shown.bs.modal', function () {
  172. $('#edName').focus();
  173. });
  174. //所有删除按钮
  175. $('.main').find('tbody').on('click', '[data-target="#del"]', function () {
  176. let tr = $(this).parent().parent();
  177. curLib = existLib({k: 'ID', v: tr.attr('id')}, guidanceLibs);
  178. console.log(curLib);
  179. });
  180. //删除确认
  181. $('#delY').click(function () {
  182. try{
  183. if(!curLib){
  184. throw '不存在该库!';
  185. }
  186. if(preLib && preLib.ID !== curLib.ID){
  187. deleteCount = 0;
  188. }
  189. deleteCount++;
  190. preLib = curLib;
  191. if(deleteCount === 3){
  192. $('#del').modal('hide');
  193. $.bootstrapLoading.start();
  194. let updateData = {updateType: updateType.delete, findData: {ID: curLib.ID}, updateData: {deleted: true}};
  195. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  196. $(`#${curLib.ID}`).remove();
  197. curLib = null;
  198. _.remove(guidanceLibs, function (lib) {
  199. return lib.ID === updateData.findData.ID;
  200. });
  201. $.bootstrapLoading.end();
  202. }, function () {
  203. $.bootstrapLoading.end();
  204. });
  205. }
  206. }
  207. catch(err){
  208. alert(err);
  209. }
  210. });
  211. //删除确认窗口关闭,重新计数
  212. $('#del').on('hidden.bs.modal', function () {
  213. deleteCount = 0;
  214. });
  215. // 锁定、解锁
  216. $('.main').find('tbody').on('click', '.lock', function () {
  217. lockUtil.handleLockClick($(this));
  218. });
  219. }
  220. return {getComBillsLibInfo, getLibs, eventListener};
  221. })();
  222. $(document).ready(function () {
  223. billsGuidanceMain.getComBillsLibInfo();
  224. billsGuidanceMain.getLibs();
  225. billsGuidanceMain.eventListener();
  226. });