main.js 11 KB

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