main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * Created by Zhong on 2017/8/14.
  3. */
  4. $(function () {
  5. let dispNameArr;
  6. let compilationsArr;
  7. let usedCom;
  8. let deleteCount = 0;
  9. let preDeleteId = null;
  10. $('#del').on('hidden.bs.modal', function () {
  11. deleteCount = 0;
  12. });
  13. getCompilationList(function (datas) {
  14. compilationsArr = datas;
  15. getAllGljLib(function (dispNames, compilationsUsedArr) {
  16. usedCom = compilationsUsedArr;
  17. dispNameArr = dispNames;
  18. //添加
  19. $('#addBtn').click(function () {
  20. let compilationName = $('#compilationSels option:selected').text();
  21. let compilationId = $('#compilationSels option:selected').val();
  22. let libName = $('#libNameTxt').val();
  23. if(libName.trim().length === 0){
  24. alert('名称不可为空!');
  25. $('#libNameTxt').val('')
  26. }
  27. else if(compilationsUsedArr.indexOf(compilationId) !== -1){// compilationsUsedArr;//在该编办下已建库,限制一个编办只能有一个库
  28. alert('该编办已被绑定!');
  29. $('#libNameTxt').val('')
  30. }
  31. else if(dispNames.indexOf(libName) !== -1){
  32. alert('此人材机库已存在!');
  33. $('#libNameTxt').val('')
  34. }
  35. else if(compilationName.trim().length === 0){
  36. alert('编办不可为空!');
  37. }
  38. else{
  39. let newGljLib = {};
  40. newGljLib.dispName = libName;
  41. newGljLib.compilationId = compilationId;
  42. newGljLib.compilationName = compilationName;
  43. newGljLib.creator = oprtor;
  44. newGljLib.appType = "建筑";
  45. $('#libNameTxt').val('');
  46. createGljLib(newGljLib, dispNameArr, usedCom);
  47. }
  48. });
  49. $('#edit').on('shown.bs.modal', function () {
  50. setTimeout(function () {
  51. $('#renameText').focus();
  52. }, 100);
  53. });
  54. $('#add').on('shown.bs.modal', function () {
  55. setTimeout(function () {
  56. $('#libNameTxt').focus();
  57. }, 100);
  58. });
  59. $('#add').on('hidden.bs.modal', function () {
  60. $('#libNameTxt').val('');
  61. });
  62. //重命名
  63. $("#showArea").on("click", "[data-target = '#edit']", function(){
  64. let renameId = $(this).parent().parent().attr("id");
  65. let compilationName = $(this).parent().parent().children()[1].textContent;
  66. $('#compilationEdit option').text(compilationName);
  67. $('#renameText').val($(this).parent().parent().find('td:first-child').text());
  68. $("#renameA").attr("renameId", renameId);
  69. });
  70. $("#renameA").click(function(){
  71. let newName = $("#renameText").val();
  72. let libId = $(this).attr("renameId");
  73. let jqSel = "#" + libId + " td:first" + " a";
  74. let orgName = $(jqSel).text();
  75. if(newName.trim().length === 0){
  76. alert("名称不可为空!");
  77. $("#renameText").val('');
  78. }
  79. else if(dispNameArr.indexOf(newName) !== -1){
  80. alert("该人材机库已存在!");
  81. $("#renameText").val('');
  82. }
  83. else{
  84. renameGljLib({ID: libId, newName: newName, orgName: orgName}, dispNameArr);
  85. }
  86. });
  87. //删除
  88. $("#showArea").on("click", "[data-target = '#del']", function(){
  89. let deleteId = $(this).parent().parent().attr("id");
  90. $("#deleteA").attr("deleteId", deleteId);
  91. let delLibName = $(`#${deleteId}`).find('td:first').text();
  92. $('#del').find('.modal-body h5').text(`准备删除 “${delLibName}”,会导致已引用此库的地方出错,确定要删除吗?`)
  93. });
  94. $("#deleteA").click(function(){
  95. let deleteId = $(this).attr("deleteId");
  96. if(preDeleteId && preDeleteId !== deleteId){
  97. deleteCount = 0;
  98. }
  99. preDeleteId = deleteId;
  100. deleteCount++;
  101. let jqSel = "#" + deleteId + " td:first" + " a";
  102. let libName = $(jqSel).text();
  103. let compilationName = $("#" + deleteId + " td:eq(1)").text();
  104. if(deleteCount === 3){
  105. for(let i = 0, len = compilationsArr.length; i < len; i++){
  106. if(compilationsArr[i].name === compilationName.trim()){
  107. usedCom.splice(usedCom.indexOf(compilationsArr[i]._id), 1);
  108. break;
  109. }
  110. }
  111. removeGljLib({libId: deleteId, libName: libName}, dispNameArr);
  112. deleteCount = 0;
  113. }
  114. });
  115. });
  116. });
  117. });
  118. function getAllGljLib(callback){
  119. $.ajax({
  120. type: 'post',
  121. url: 'api/getAllGljLib',
  122. dataType: 'json',
  123. success: function (result) {
  124. let dispNames = [];
  125. let compilationsUsedArr = [];
  126. if(result.data.length > 0){
  127. for(let i = 0; i < result.data.length; i++){
  128. compilationsUsedArr.push(result.data[i].compilationId);//已建库的编办
  129. let id = result.data[i].ID;
  130. let libName = result.data[i].dispName;
  131. let createDate = result.data[i].createDate.split(' ')[0];
  132. let compilationName = result.data[i].compilationName;
  133. let rationLibs = result.data[i].rationLibs;
  134. let rationLibsName = '';
  135. for(let j = 0; j < rationLibs.length; j++){
  136. rationLibsName += rationLibs[j].dispName + "</br>";
  137. }
  138. dispNames.push(result.data[i].dispName);
  139. $("#showArea").append(
  140. "<tr id='tempId'>" +
  141. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  142. "<td>"+compilationName+" </td>" +
  143. "<td>"+rationLibsName+" </td>" +
  144. "<td>"+createDate+" </td>" +
  145. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  146. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  147. "<i class='fa fa-remove'></i></a></td></tr>");
  148. var newHref = "/stdGljRepository/glj?gljLibId="+id;
  149. $("#tempId td:first a").attr("href", newHref);
  150. $("#tempId").attr("id", id);
  151. }
  152. }
  153. callback(dispNames, compilationsUsedArr);
  154. }
  155. });
  156. }
  157. function getCompilationList(callback){
  158. $.ajax({
  159. type: 'post',
  160. url: 'api/getCompilationList',
  161. dataType: 'json',
  162. success: function (result) {
  163. //addoptions
  164. for(let i = 0; i < result.data.length; i++){
  165. let $option = $("<option >"+ result.data[i].name +"</option>");
  166. $option.val( result.data[i]._id);
  167. $('#compilationSels').append($option);
  168. }
  169. $('#compilationSels').on("change", function () {
  170. });
  171. callback(result.data);
  172. }
  173. });
  174. }
  175. function createGljLib(gljLibObj, dispNamesArr, usedCom){
  176. $.ajax({
  177. type: 'post',
  178. url: 'api/createGljLib',
  179. data: {gljLibObj: JSON.stringify(gljLibObj)},
  180. dataType: 'json',
  181. success: function (result) {
  182. if(result.data){
  183. let id = result.data.ID;
  184. let libName = result.data.dispName;
  185. let createDate = result.data.createDate.split(' ')[0];
  186. let compilationName = result.data.compilationName;
  187. dispNamesArr.push(libName);
  188. usedCom.push(gljLibObj.compilationId);
  189. $("#showArea").append(
  190. "<tr id='tempId'>" +
  191. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  192. "<td>"+compilationName+" </td>" +
  193. "<td>"+''+" </td>" +
  194. "<td>"+createDate+" </td>" +
  195. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  196. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  197. "<i class='fa fa-remove'></i></a></td></tr>");
  198. var newHref = "/stdGljRepository/glj?gljLibId="+id;
  199. $("#tempId td:first a").attr("href", newHref);
  200. $("#tempId").attr("id", id);
  201. }
  202. $('#cancelBtn').click();
  203. }
  204. })
  205. }
  206. function renameGljLib(renameObj, dispNames){
  207. $.ajax({
  208. type: 'post',
  209. url: 'api/renameGljLib',
  210. data: {oprtor: oprtor, renameObj: JSON.stringify(renameObj)},
  211. dataType: 'json',
  212. success: function (result) {
  213. if(!result.error){
  214. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  215. $(jqSel).text(renameObj.newName);
  216. let index = dispNames.indexOf(renameObj.orgName);
  217. dispNames.splice(index, 1);
  218. dispNames.splice(index, 0, renameObj.newName);
  219. }
  220. $('#editCancelBtn').click();
  221. $('#renameText').val('');
  222. }
  223. })
  224. }
  225. function removeGljLib(delObj, dispNames){
  226. $.bootstrapLoading.start();
  227. $.ajax({
  228. type: 'post',
  229. url: 'api/removeGljLib',
  230. data: {oprtor: oprtor, libId: delObj.libId},
  231. dataType: 'json',
  232. success: function (result) {
  233. if(!result.error){
  234. if(result.message === '此人材机库已被引用!'){
  235. $('#delCancelBtn').click();
  236. alert("此人材机库已被引用,不可删除!");
  237. }
  238. else if(result.message === '删除成功'){
  239. var jqSel = "#"+ delObj.libId;
  240. $(jqSel).remove();
  241. let index = dispNames.indexOf(delObj.libName);
  242. dispNames.splice(index, 1);
  243. $('#delCancelBtn').click();
  244. }
  245. }
  246. $.bootstrapLoading.end();
  247. },
  248. error: function () {
  249. alert('服务器出错!');
  250. $.bootstrapLoading.end();
  251. }
  252. })
  253. }