main.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**
  2. * Created by Syusuke on 2017/3/17.
  3. */
  4. /*$(function(){
  5. let libNames = [];
  6. getCompilationList();
  7. getRationLibs(libNames);
  8. })
  9. function addEvent_Addressdirect(){
  10. $("tr:gt(0)").each(function() {
  11. var td, a, tdId = $("td:eq(3)",$(this));
  12. td = $("td:eq(0)", $(this));
  13. a = $('a', td);
  14. a.attr('href', "/rationRepository/ration" + "?repository=" + tdId.text());
  15. })
  16. }
  17. function addEvent_DeleteLib(){
  18. $("tr:gt(0)").each(function() {
  19. var td0, a1,td2,a2;
  20. td2 = $("td:eq(2)", $(this));
  21. td0 = $("td:eq(0)", $(this));
  22. a1 = $('a', td0);
  23. var str = a1.text();
  24. a2 = $("a:eq(1)",td2);
  25. a2.click(function(){
  26. $("#did").val(str)
  27. })
  28. // a.attr('href', "/ration/rationLib" + "?rationname=" + a.text());
  29. })
  30. }
  31. function addEvent_EditLib(){
  32. $("tr:gt(0)").each(function() {
  33. var td0, a1,td2,a2;
  34. td2 = $("td:eq(2)", $(this));
  35. td0 = $("td:eq(0)", $(this));
  36. a1 = $('a', td0);
  37. var str = a1.text();
  38. a2 = $("a:eq(0)",td2);
  39. a2.click(function(){
  40. $("#did").val(str)
  41. })
  42. })
  43. }
  44. $("#deleteLib").click(function(){
  45. var rationLibName = $("#did").val();
  46. let lastOperator = userAccount;
  47. $.ajax({
  48. type:"POST",
  49. url:"api/deleteRationLibs",
  50. dataType:"json",
  51. data:{"rationName": rationLibName, "lastOperator": lastOperator},
  52. cache:false,
  53. timeout:50000,
  54. success:function(result){
  55. getRationLibs();
  56. },
  57. error:function(iqXHR,textStatus,errorThrown){
  58. }
  59. })
  60. });
  61. function getCompilationList(){
  62. $.ajax({
  63. type: 'post',
  64. url: 'api/getCompilationList',
  65. dataType: 'json',
  66. success: function (result) {
  67. console.log(result);
  68. //addoptions
  69. for(let i = 0; i < result.data.compilation.length; i++){
  70. let $option = $("<option >"+ result.data.compilation[i].name +"</option>");
  71. $option.val( result.data.compilation[i]._id);
  72. $('#compilationSels').append($option);
  73. }
  74. //初始工料机库选项
  75. if(result.data.compilation.length > 0 && result.data.gljLibs.length > 0){
  76. let compilationId = result.data.compilation[0]._id;
  77. //console.log(compilationId);
  78. let gljLibOps = getGljLibOps(compilationId, result.data.gljLibs);
  79. console.log(gljLibOps);
  80. for(let i = 0; i < gljLibOps.length; i++){
  81. let $option = $("<option >"+ gljLibOps[i].dispName +"</option>");
  82. $option.val(gljLibOps[i].ID);
  83. $('#gljLibSels').append($option);
  84. }
  85. }
  86. $('#compilationSels').on("change", function () {
  87. //刷新工料机库选项
  88. $('#gljLibSels').children().remove();
  89. let newGljLibOps = getGljLibOps(this.selectedOptions[0].value, result.data.gljLibs);
  90. for(let i = 0; i < newGljLibOps.length; i++){
  91. let $option = $("<option >"+ newGljLibOps[i].dispName +"</option>");
  92. $option.val(newGljLibOps[i].ID);
  93. $('#gljLibSels').append($option);
  94. }
  95. });
  96. }
  97. });
  98. }
  99. function getGljLibOps(compilationId, gljLibs){
  100. let rst = [];
  101. for(let i = 0; i < gljLibs.length; i++){
  102. if(gljLibs[i]){
  103. if(compilationId === gljLibs[i].compilationId){
  104. rst.push(gljLibs[i]);
  105. }
  106. }
  107. }
  108. return rst;
  109. }
  110. function getRationLibs(libNames){
  111. $.ajax({
  112. type:"POST",
  113. url:"api/getRationDisplayNames",
  114. dataType:"json",
  115. cache:false,
  116. timeout:5000,
  117. success:function(result){
  118. $("tbody tr").html("");
  119. for(var i=0;i<result.data.length;i++){
  120. addLibTag(result.data[i].dispName, result.data[i].ID, result.data[i].createDate);
  121. storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data[i].ID, result.data[i].dispName);
  122. libNames.push(result.data[i].dispName);
  123. }
  124. console.log(libNames);
  125. addEvent_Addressdirect();
  126. addEvent_DeleteLib();
  127. addEvent_EditLib();
  128. },
  129. error:function(iqXHR,textStatus,errorThrown){
  130. alert("error "+textStatus+" "+errorThrown);
  131. }
  132. })
  133. }
  134. function addLibTag(string, ID, date){
  135. var $tr = $('<tr><td><a href="#"></a></td><td></td><td><a href="javacript:void(0);" data-toggle="modal" data-target="#edit" title="编辑"><i class="fa fa-pencil-square-o"></i></a> <a href="javacript:void(0);" data-toggle="modal" data-target="#del" class="text-danger" title="删除"><i class="fa fa-remove"></i></a></td><td style="display:none"></td></tr>')
  136. var $td0 = $("td:eq(0)",$tr);
  137. var $td1 = $("td:eq(1)",$tr);
  138. var $tdId = $("td:eq(3)",$tr);
  139. var a = $("a",$td0);
  140. var str = string;
  141. a.text(str);
  142. $tdId.text(ID);
  143. var newDate = date.split(' ')[0];
  144. $td1.text(newDate);
  145. $tr.appendTo("tbody");
  146. }
  147. function addRationRepository(rationObj, $tdId) {
  148. $.ajax({
  149. type:"POST",
  150. url:"api/addRationRepository",
  151. dataType:"json",
  152. data:{"rationRepObj": JSON.stringify(rationObj)},
  153. cache:false,
  154. timeout:5000,
  155. success:function(result){
  156. $tdId.text(result.data.ID);
  157. storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data.ID, result.data.dispName);
  158. addEvent_Addressdirect();
  159. addEvent_DeleteLib();
  160. },
  161. error:function(iqXHR,textStatus,errorThrown){
  162. alert("error "+textStatus+" "+errorThrown);
  163. }
  164. })
  165. }
  166. $("#rationAdd").click(function(){
  167. var $tr = $('<tr><td><a href="#"></a></td><td></td><td><a href="javacript:void(0);" data-toggle="modal" data-target="#edit" title="编辑"><i class="fa fa-pencil-square-o"></i></a> <a href="javacript:void(0);" data-toggle="modal" data-target="#del" class="text-danger" title="删除"><i class="fa fa-remove"></i></a></td><td style="display:none"></td></tr>')
  168. var $td0 = $("td:eq(0)",$tr);
  169. var $td1 = $("td:eq(1)",$tr);
  170. var $tdId = $("td:eq(3)",$tr);
  171. var a = $("a",$td0);
  172. var str = $("#addInput").val();
  173. a.text(str);
  174. var date = new Date().Format("yyyy-MM-dd");
  175. $td1.text(date);
  176. $tr.appendTo("tbody");
  177. var newRationRepObj = {};
  178. newRationRepObj.dispName = str;
  179. newRationRepObj.appType = "建筑";
  180. newRationRepObj.descr = "";
  181. newRationRepObj.creator = userAccount;
  182. newRationRepObj.lastOperator = userAccount;
  183. addRationRepository(newRationRepObj, $tdId);
  184. });
  185. $("#edtOK").click(function(){
  186. var str = $("#did").val();
  187. var newStr = $("#inputRation").val();
  188. let lastOperator = userAccount;
  189. $.ajax({
  190. type:"POST",
  191. url:"api/editRationLibs",
  192. dataType:"json",
  193. data:{"rationName": str,"newName":newStr, "lastOperator": lastOperator},
  194. cache:false,
  195. timeout:50000,
  196. success:function(result){
  197. getRationLibs();
  198. },
  199. error:function(jqXHR){
  200. var err = JSON.parse(jqXHR.responseText);
  201. alert(err.error);
  202. }
  203. })
  204. });
  205. autoFlashHeight();*/
  206. $(function () {
  207. let dispNameArr;
  208. getAllRationLib(function (dispNames) {
  209. dispNameArr = dispNames;
  210. //添加
  211. $('#addBtn').click(function () {
  212. let compilationName = $('#compilationSels option:selected').text();
  213. let compilationId = $('#compilationSels option:selected').val();
  214. let gljLibName = $('#gljLibSels option:selected').text();
  215. let gljLibId = $('#gljLibSels option:selected').val();
  216. let libName = $('#libNameTxt').val();
  217. if(libName.trim().length === 0){
  218. alert('名称不可为空!');
  219. $('#libNameTxt').val('')
  220. }
  221. else if(dispNames.indexOf(libName) !== -1){
  222. alert('此定额库已存在!');
  223. $('#libNameTxt').val('')
  224. }
  225. else if(compilationName.trim().length === 0){
  226. alert('编办不可为空!');
  227. }
  228. else if(gljLibName.trim().length === 0){
  229. alert("请选择工料机库!");
  230. }
  231. else{
  232. let newRationLib = {};
  233. newRationLib.dispName = libName;
  234. newRationLib.compilationId = compilationId;
  235. newRationLib.compilationName = compilationName;
  236. newRationLib.gljLib = gljLibId;
  237. newRationLib.creator = userAccount;
  238. newRationLib.appType = "建筑";
  239. $('#libNameTxt').val('');
  240. createRationLib(newRationLib, dispNameArr);
  241. }
  242. });
  243. //重命名
  244. $("#showArea").on("click", "[data-target = '#edit']", function(){
  245. let renameId = $(this).parent().parent().attr("id");
  246. $("#renameA").attr("renameId", renameId);
  247. });
  248. $("#renameA").click(function(){
  249. let newName = $("#renameText").val();
  250. let libId = $(this).attr("renameId");
  251. let jqSel = "#" + libId + " td:first" + " a";
  252. let orgName = $(jqSel).text();
  253. if(newName.trim().length === 0){
  254. alert("名称不可为空!");
  255. $("#renameText").val('');
  256. }
  257. else if(dispNameArr.indexOf(newName) !== -1){
  258. alert("该工料机库已存在!");
  259. $("#renameText").val('');
  260. }
  261. else{
  262. renameRationLib({ID: libId, newName: newName, orgName: orgName}, dispNameArr);
  263. }
  264. });
  265. //删除
  266. $("#showArea").on("click", "[data-target = '#del']", function(){
  267. let deleteId = $(this).parent().parent().attr("id");
  268. $("#deleteA").attr("deleteId", deleteId);
  269. });
  270. $("#deleteA").click(function(){
  271. let deleteId = $(this).attr("deleteId");
  272. let jqSel = "#" + deleteId + " td:first" + " a";
  273. let libName = $(jqSel).text();
  274. removeRationLib({libId: deleteId, libName: libName}, dispNameArr);
  275. });
  276. });
  277. getCompilationList();
  278. });
  279. function getAllRationLib(callback){
  280. $.ajax({
  281. type: 'post',
  282. url: 'api/getRationDisplayNames',
  283. dataType: 'json',
  284. success: function (result) {
  285. let dispNames = [];
  286. if(result.data.length > 0){
  287. for(let i = 0; i < result.data.length; i++){
  288. storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data[i].ID, result.data[i].dispName);
  289. if(result.data[i].gljLib){
  290. storageUtil.setSessionCache("gljLib","repositoryID_" + result.data[i].ID, result.data[i].gljLib);
  291. }
  292. let id = result.data[i].ID;
  293. let libName = result.data[i].dispName;
  294. let createDate = result.data[i].createDate.split(' ')[0];
  295. dispNames.push(result.data[i].dispName);
  296. $("#showArea").append(
  297. "<tr id='tempId'>" +
  298. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  299. "<td>"+createDate+" </td>" +
  300. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  301. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  302. "<i class='fa fa-remove'></i></a></td></tr>");
  303. var newHref = "/rationRepository/ration?repository="+id;
  304. $("#tempId td:first a").attr("href", newHref);
  305. $("#tempId").attr("id", id);
  306. }
  307. }
  308. callback(dispNames);
  309. }
  310. });
  311. }
  312. function getCompilationList(){
  313. $.ajax({
  314. type: 'post',
  315. url: 'api/getCompilationList',
  316. dataType: 'json',
  317. success: function (result) {
  318. console.log(result);
  319. //addoptions
  320. for(let i = 0; i < result.data.compilation.length; i++){
  321. let $option = $("<option >"+ result.data.compilation[i].name +"</option>");
  322. $option.val( result.data.compilation[i]._id);
  323. $('#compilationSels').append($option);
  324. }
  325. //初始工料机库选项
  326. if(result.data.compilation.length > 0 && result.data.gljLibs.length > 0){
  327. let compilationId = result.data.compilation[0]._id;
  328. //console.log(compilationId);
  329. let gljLibOps = getGljLibOps(compilationId, result.data.gljLibs);
  330. console.log(gljLibOps);
  331. for(let i = 0; i < gljLibOps.length; i++){
  332. let $option = $("<option >"+ gljLibOps[i].dispName +"</option>");
  333. $option.val(gljLibOps[i].ID);
  334. $('#gljLibSels').append($option);
  335. }
  336. }
  337. $('#compilationSels').on("change", function () {
  338. //刷新工料机库选项
  339. $('#gljLibSels').children().remove();
  340. let newGljLibOps = getGljLibOps(this.selectedOptions[0].value, result.data.gljLibs);
  341. for(let i = 0; i < newGljLibOps.length; i++){
  342. let $option = $("<option >"+ newGljLibOps[i].dispName +"</option>");
  343. $option.val(newGljLibOps[i].ID);
  344. $('#gljLibSels').append($option);
  345. }
  346. });
  347. }
  348. });
  349. }
  350. function getGljLibOps(compilationId, gljLibs){
  351. let rst = [];
  352. for(let i = 0; i < gljLibs.length; i++){
  353. if(gljLibs[i]){
  354. if(compilationId === gljLibs[i].compilationId){
  355. rst.push(gljLibs[i]);
  356. }
  357. }
  358. }
  359. return rst;
  360. }
  361. function createRationLib(rationObj, dispNamesArr){
  362. $.ajax({
  363. type: 'post',
  364. url: 'api/addRationRepository',
  365. data: {rationRepObj: JSON.stringify(rationObj)},
  366. dataType: 'json',
  367. success: function (result) {
  368. if(result.data){
  369. storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data.ID, result.data.dispName);
  370. if(result.data.gljLib){
  371. storageUtil.setSessionCache("gljLib","repositoryID_" + result.data.ID, result.data.gljLib);
  372. }
  373. let id = result.data.ID;
  374. let libName = result.data.dispName;
  375. let createDate = result.data.createDate.split(' ')[0];
  376. let compilationName = result.data.compilationName;
  377. dispNamesArr.push(libName);
  378. $("#showArea").append(
  379. "<tr id='tempId'>" +
  380. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  381. "<td>"+createDate+" </td>" +
  382. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  383. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  384. "<i class='fa fa-remove'></i></a></td></tr>");
  385. var newHref = "/rationRepository/ration?repository="+id;
  386. $("#tempId td:first a").attr("href", newHref);
  387. $("#tempId").attr("id", id);
  388. }
  389. $('#cancelBtn').click();
  390. }
  391. })
  392. }
  393. function renameRationLib(renameObj, dispNames){
  394. $.ajax({
  395. type: 'post',
  396. url: 'api/editRationLibs',
  397. data: {oprtor: userAccount, renameObj: JSON.stringify(renameObj)},
  398. dataType: 'json',
  399. success: function (result) {
  400. if(!result.error){
  401. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  402. $(jqSel).text(renameObj.newName);
  403. let index = dispNames.indexOf(renameObj.orgName);
  404. dispNames.splice(index, 1);
  405. dispNames.splice(index, 0, renameObj.newName);
  406. }
  407. $('#editCancelBtn').click();
  408. $('#renameText').val('');
  409. }
  410. })
  411. }
  412. function removeRationLib(delObj, dispNames){
  413. $.ajax({
  414. type: 'post',
  415. url: 'api/deleteRationLibs',
  416. data: {oprtor: userAccount, libId: delObj.libId},
  417. dataType: 'json',
  418. success: function (result) {
  419. if(!result.error){
  420. var jqSel = "#"+ delObj.libId;
  421. $(jqSel).remove();
  422. let index = dispNames.indexOf(delObj.libName);
  423. dispNames.splice(index, 1);
  424. $('#delCancelBtn').click();
  425. }
  426. }
  427. })
  428. }