main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. //重命名
  50. $("#showArea").on("click", "[data-target = '#edit']", function(){
  51. let renameId = $(this).parent().parent().attr("id");
  52. let compilationName = $(this).parent().parent().children()[1].textContent;
  53. $('#compilationEdit option').text(compilationName);
  54. $("#renameA").attr("renameId", renameId);
  55. });
  56. $("#renameA").click(function(){
  57. let newName = $("#renameText").val();
  58. let libId = $(this).attr("renameId");
  59. let jqSel = "#" + libId + " td:first" + " a";
  60. let orgName = $(jqSel).text();
  61. if(newName.trim().length === 0){
  62. alert("名称不可为空!");
  63. $("#renameText").val('');
  64. }
  65. else if(dispNameArr.indexOf(newName) !== -1){
  66. alert("该人材机库已存在!");
  67. $("#renameText").val('');
  68. }
  69. else{
  70. renameGljLib({ID: libId, newName: newName, orgName: orgName}, dispNameArr);
  71. }
  72. });
  73. //删除
  74. $("#showArea").on("click", "[data-target = '#del']", function(){
  75. let deleteId = $(this).parent().parent().attr("id");
  76. $("#deleteA").attr("deleteId", deleteId);
  77. });
  78. $("#deleteA").click(function(){
  79. let deleteId = $(this).attr("deleteId");
  80. if(preDeleteId && preDeleteId !== deleteId){
  81. deleteCount = 0;
  82. }
  83. preDeleteId = deleteId;
  84. deleteCount++;
  85. let jqSel = "#" + deleteId + " td:first" + " a";
  86. let libName = $(jqSel).text();
  87. let compilationName = $("#" + deleteId + " td:eq(1)").text();
  88. if(deleteCount === 3){
  89. for(let i = 0, len = compilationsArr.length; i < len; i++){
  90. if(compilationsArr[i].name === compilationName.trim()){
  91. usedCom.splice(usedCom.indexOf(compilationsArr[i]._id), 1);
  92. break;
  93. }
  94. }
  95. removeGljLib({libId: deleteId, libName: libName}, dispNameArr);
  96. deleteCount = 0;
  97. }
  98. });
  99. });
  100. });
  101. });
  102. function getAllGljLib(callback){
  103. $.ajax({
  104. type: 'post',
  105. url: 'api/getAllGljLib',
  106. dataType: 'json',
  107. success: function (result) {
  108. let dispNames = [];
  109. let compilationsUsedArr = [];
  110. if(result.data.length > 0){
  111. for(let i = 0; i < result.data.length; i++){
  112. compilationsUsedArr.push(result.data[i].compilationId);//已建库的编办
  113. let id = result.data[i].ID;
  114. let libName = result.data[i].dispName;
  115. let createDate = result.data[i].createDate.split(' ')[0];
  116. let compilationName = result.data[i].compilationName;
  117. let rationLibs = result.data[i].rationLibs;
  118. let rationLibsName = '';
  119. for(let j = 0; j < rationLibs.length; j++){
  120. rationLibsName += rationLibs[j].dispName + "</br>";
  121. }
  122. dispNames.push(result.data[i].dispName);
  123. $("#showArea").append(
  124. "<tr id='tempId'>" +
  125. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  126. "<td>"+compilationName+" </td>" +
  127. "<td>"+rationLibsName+" </td>" +
  128. "<td>"+createDate+" </td>" +
  129. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  130. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  131. "<i class='fa fa-remove'></i></a></td></tr>");
  132. var newHref = "/stdGljRepository/glj?gljLibId="+id;
  133. $("#tempId td:first a").attr("href", newHref);
  134. $("#tempId").attr("id", id);
  135. }
  136. }
  137. callback(dispNames, compilationsUsedArr);
  138. }
  139. });
  140. }
  141. function getCompilationList(callback){
  142. $.ajax({
  143. type: 'post',
  144. url: 'api/getCompilationList',
  145. dataType: 'json',
  146. success: function (result) {
  147. //addoptions
  148. for(let i = 0; i < result.data.length; i++){
  149. let $option = $("<option >"+ result.data[i].name +"</option>");
  150. $option.val( result.data[i]._id);
  151. $('#compilationSels').append($option);
  152. }
  153. $('#compilationSels').on("change", function () {
  154. });
  155. callback(result.data);
  156. }
  157. });
  158. }
  159. function createGljLib(gljLibObj, dispNamesArr, usedCom){
  160. $.ajax({
  161. type: 'post',
  162. url: 'api/createGljLib',
  163. data: {gljLibObj: JSON.stringify(gljLibObj)},
  164. dataType: 'json',
  165. success: function (result) {
  166. if(result.data){
  167. let id = result.data.ID;
  168. let libName = result.data.dispName;
  169. let createDate = result.data.createDate.split(' ')[0];
  170. let compilationName = result.data.compilationName;
  171. dispNamesArr.push(libName);
  172. usedCom.push(gljLibObj.compilationId);
  173. $("#showArea").append(
  174. "<tr id='tempId'>" +
  175. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  176. "<td>"+compilationName+" </td>" +
  177. "<td>"+''+" </td>" +
  178. "<td>"+createDate+" </td>" +
  179. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  180. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  181. "<i class='fa fa-remove'></i></a></td></tr>");
  182. var newHref = "/stdGljRepository/glj?gljLibId="+id;
  183. $("#tempId td:first a").attr("href", newHref);
  184. $("#tempId").attr("id", id);
  185. }
  186. $('#cancelBtn').click();
  187. }
  188. })
  189. }
  190. function renameGljLib(renameObj, dispNames){
  191. $.ajax({
  192. type: 'post',
  193. url: 'api/renameGljLib',
  194. data: {oprtor: oprtor, renameObj: JSON.stringify(renameObj)},
  195. dataType: 'json',
  196. success: function (result) {
  197. if(!result.error){
  198. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  199. $(jqSel).text(renameObj.newName);
  200. let index = dispNames.indexOf(renameObj.orgName);
  201. dispNames.splice(index, 1);
  202. dispNames.splice(index, 0, renameObj.newName);
  203. }
  204. $('#editCancelBtn').click();
  205. $('#renameText').val('');
  206. }
  207. })
  208. }
  209. function removeGljLib(delObj, dispNames){
  210. $.bootstrapLoading.start();
  211. $.ajax({
  212. type: 'post',
  213. url: 'api/removeGljLib',
  214. data: {oprtor: oprtor, libId: delObj.libId},
  215. dataType: 'json',
  216. success: function (result) {
  217. if(!result.error){
  218. if(result.message === '此人材机库已被引用!'){
  219. $('#delCancelBtn').click();
  220. alert("此人材机库已被引用,不可删除!");
  221. }
  222. else if(result.message === '删除成功'){
  223. var jqSel = "#"+ delObj.libId;
  224. $(jqSel).remove();
  225. let index = dispNames.indexOf(delObj.libName);
  226. dispNames.splice(index, 1);
  227. $('#delCancelBtn').click();
  228. }
  229. }
  230. $.bootstrapLoading.end();
  231. },
  232. error: function () {
  233. alert('服务器出错!');
  234. $.bootstrapLoading.end();
  235. }
  236. })
  237. }