jstreeAJAX.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. var test;
  2. $(document).ready(function () {
  3. const types = ["工程概要", "工程計算書", "設計圖", "數量計算書", "工程預算書", "施工規範&特定條款", "設計報告書"];
  4. var path = '';
  5. var category = '';
  6. var project_id = '';
  7. var project_name = '';
  8. var project = '';
  9. var blueprint_SN = '';
  10. var blueprint_path = '';
  11. var notes = [];
  12. var tempTable = [];
  13. var tempTable2 = [];
  14. var tree_data; //save getTree ajax data
  15. var start = true;
  16. var folderPath = "";
  17. var fileNums = [];
  18. var is_blueprint2 = false;
  19. PDFObject.embed("./assets/null.pdf", "#pdfFile");
  20. var table = $('#datatable-ajax').DataTable({
  21. bProcessing: true,
  22. responsive: true,
  23. "pagingType": "full_numbers",
  24. "bLengthChange": true,
  25. "lengthMenu": [
  26. [7, 10, 25, -1],
  27. [7, 10, 25, "全部"]
  28. ],
  29. "searching": false,
  30. "pageLength": 7,
  31. "language": {
  32. "processing": "處理中...",
  33. "loadingRecords": "載入中...",
  34. "lengthMenu": "顯示 _MENU_ 項結果",
  35. "zeroRecords": "沒有符合的結果或是沒有資料",
  36. "info": "顯示第 _START_ 至 _END_ 項結果,共 _TOTAL_ 項",
  37. "infoEmpty": "顯示第 0 至 0 項結果,共 0 項",
  38. "infoFiltered": "(從 _MAX_ 項結果中過濾)",
  39. "infoPostFix": "",
  40. "search": "搜尋:",
  41. "paginate": {
  42. "first": '<i class="fa fa-step-backward"></i>',
  43. "previous": '<i class="fa fa-backward"></i>',
  44. "next": '<i class="fa fa-forward"></i>',
  45. "last": '<i class="fa fa-step-forward"></i>'
  46. },
  47. "aria": {
  48. "sortAscending": ": 升冪排列",
  49. "sortDescending": ": 降冪排列"
  50. }
  51. },
  52. "columns": [{
  53. "width": "5%"
  54. },
  55. null,
  56. null,
  57. {
  58. "width": "5%"
  59. }, {
  60. "width": "5%"
  61. },
  62. ],
  63. data: [
  64. [
  65. " ", "", "", "", "",
  66. ],
  67. [
  68. " ", "", "", "", "",
  69. ],
  70. [
  71. " ", "", "", "", "",
  72. ],
  73. [
  74. " ", "", "", "", "",
  75. ],
  76. [
  77. " ", "", "", "", "",
  78. ],
  79. [
  80. " ", "", "", "", "",
  81. ],
  82. [
  83. " ", "", "", "", "",
  84. ]
  85. ],
  86. drawCallback: function () {
  87. if (!start) {
  88. var api = this.api();
  89. var rowCount = api.rows({
  90. page: 'current'
  91. }).count();
  92. /*for (var i = 0; i < api.page.len() - (rowCount === 0 ? 1 : rowCount); i++) {
  93. $('#datatable-ajax tbody').append($("<tr><td>&nbsp;</td><td></td><td></td><td></td></tr>"));
  94. }*/
  95. }
  96. start = false;
  97. },
  98. });
  99. $("#datatable-ajax_length").append($("#reset"))
  100. $.ajax({
  101. url: "./script/php/getTree.php",
  102. contentType: "application/json",
  103. dataType: "json",
  104. type: "GET",
  105. async: false
  106. })
  107. .success(function (response) {
  108. tree_data = response;
  109. }).error(function (error) {
  110. console.log(error);
  111. });
  112. displayDetail(false, true);
  113. $('#treeAjaxHTML').jstree({
  114. 'core': {
  115. 'themes': {
  116. variant: 'large'
  117. },
  118. 'data': tree_data,
  119. 'multiple': false,
  120. 'animation': 40,
  121. 'expand_selected_onload': true
  122. },
  123. 'types': {
  124. 'default': {
  125. 'icon': 'fa fa-folder'
  126. },
  127. 'file': {
  128. 'icon': 'fa fa-file'
  129. }
  130. },
  131. 'plugins': ['types', 'search', 'wholerow']
  132. }).bind("loaded.jstree", function (event, data) {
  133. var url_href = window.location.href;
  134. var url = new URL(url_href);
  135. var projectId = url.searchParams.get("projectId");
  136. if (projectId != null) {
  137. $(this).jstree("open_all");
  138. }
  139. }).on(
  140. 'select_node.jstree',
  141. function (event, data) {
  142. if (data.node.text.includes("-")) {
  143. $("#reset").prop("disabled", false);
  144. $("#header").empty();
  145. displayDetail(false, true);
  146. project_id = data.node.text.split("-")[0];
  147. project_name = data.node.text.split("-")[1];
  148. project = data.node.text;
  149. $("#project-id").val(project_id)
  150. $("#header").empty();
  151. category = $('#treeAjaxHTML').jstree(true).get_node(data.node.parents[0]).text;
  152. var span = $('<span>' + project + ' > </span>');
  153. span.on("click", function (e) {
  154. displayDetail(false, true);
  155. is_blueprint2 = false;
  156. for (var k = folderPath.length - 1; k > 0; k--) {
  157. $("#header").children().eq(k).remove();
  158. folderPath.pop();
  159. }
  160. let i = 1;
  161. table.clear().draw();
  162. tempTable = [];
  163. types.forEach(function (data) {
  164. console.log(data)
  165. if(fileNums[i-1] == null){
  166. total = 0;
  167. }else{
  168. total = fileNums[i-1].total;
  169. }
  170. let temp = [
  171. i,
  172. data,
  173. category,
  174. '',
  175. total,
  176. project,
  177. ];
  178. table.row.add(temp).draw();
  179. tempTable.push(temp);
  180. i++;
  181. });
  182. $("#tableDetail td").empty();
  183. });
  184. $("#header").append(span);
  185. let i = 1;
  186. table.clear().draw();
  187. tempTable = [];
  188. countFiles(project_id, "false");
  189. types.forEach(function (data) {
  190. if(fileNums[i-1] == null){
  191. total = 0;
  192. }else{
  193. total = fileNums[i-1].total;
  194. }
  195. let temp = [
  196. i,
  197. data,
  198. category,
  199. '',
  200. total,
  201. project,
  202. ];
  203. table.row.add(temp).draw();
  204. tempTable.push(temp);
  205. i++;
  206. });
  207. $("#tableDetail td").empty();
  208. } else {
  209. data.instance.toggle_node(data.node);
  210. }
  211. displayDetail(false, true);
  212. });
  213. $('#btn-search').click(function () {
  214. $("#reset").prop("disabled", true);
  215. $("#header").empty();
  216. $("#header").append("<span>搜尋結果</span>");
  217. $.ajax({
  218. url: "./script/php/search.php",
  219. data: {
  220. search: $("#search").val(),
  221. },
  222. async: false,
  223. contentType: "application/json",
  224. dataType: "json",
  225. type: "GET",
  226. })
  227. .success(function (response) {
  228. displayDetail(true, true);
  229. table.clear().draw();
  230. $("#tableDetail td").empty();
  231. response.forEach(function (data) {
  232. let temp = [
  233. data["SN"],
  234. data["original_name"],
  235. data["keyword"],
  236. '1.0.0',
  237. data["project_id"],
  238. data["project_name"],
  239. data["type_id"],
  240. data["category_name"],
  241. data["notes"],
  242. data["filename"],
  243. ];
  244. table.row.add(temp).draw();
  245. i++;
  246. });
  247. })
  248. .error(function (error) {
  249. console.log(error);
  250. })
  251. .complete(function () {});
  252. });
  253. $('#search').keyup(function (e) {
  254. if (e.keyCode == 13) {
  255. $('#btn-search').click();
  256. }
  257. });
  258. $('#reset').click(function () {
  259. if (folderPath.length != 1) {
  260. folderPath.pop();
  261. let index = folderPath.length;
  262. $("#header").children().eq(index).remove();
  263. table.clear().draw();
  264. if (is_blueprint2) {
  265. tempTable2.forEach(function (data) {
  266. table.row.add(data).draw();
  267. });
  268. is_blueprint2 = false;
  269. } else {
  270. tempTable.forEach(function (data) {
  271. table.row.add(data).draw();
  272. });
  273. }
  274. $("#tableDetail td").empty();
  275. displayDetail(false, true);
  276. }
  277. });
  278. $('#datatable-ajax tbody').on('click', 'tr', function () {
  279. $("#reset").prop("disabled", false);
  280. $(this).parent().find("tr").css('background-color', '');
  281. $(this).css("background-color", "#cccccc");
  282. $("#tableDetail td").empty();
  283. var data = table.row(this).data();
  284. console.log(data)
  285. if (data == null) {
  286. data = ["", "", "null", "", "", "", "", "", "", ""];
  287. }
  288. let project_id = data[5].split("-")[0];
  289. let SN = data[0];
  290. let is_blueprint = data[1] == "設計圖" || data[1] == "成果圖" ? true : false;
  291. let note = notes.includes(data[1]) ? data[1] : '';
  292. if (data[2] == 'null') {
  293. $("#tableDetail td").empty();
  294. } else if (data[3] == '') {
  295. path = data[1];
  296. $.ajax({
  297. url: "./script/php/getDetail.php",
  298. data: {
  299. project_id: project_id,
  300. type_id: SN,
  301. is_blueprint: is_blueprint,
  302. note: note
  303. },
  304. async: false,
  305. contentType: "application/json",
  306. dataType: "json",
  307. type: "GET",
  308. })
  309. .success(function (response) {
  310. var span = $('<span>' + data[1] + ' > </span>');
  311. span.on("click", function (e) {
  312. let clicked = $(e.target).text().split(" > ")[0];
  313. let index = folderPath.indexOf(clicked);
  314. for (k = index + 1; k < folderPath.length; k++) {
  315. $("#header").children().eq(k).remove();
  316. folderPath.pop();
  317. }
  318. addTable(data, notes, response, SN, path);
  319. is_blueprint2 = false;
  320. });
  321. $("#header").append(span);
  322. folderPath = $("#header").text().split(" > ");
  323. folderPath.pop();
  324. addTable(data, notes, response, SN, path);
  325. })
  326. .error(function (error) {
  327. console.log(error);
  328. })
  329. .complete(function () {});
  330. } else {
  331. let type = types[parseInt(data[7]) - 1];
  332. let path = '';
  333. //let category = data[8];
  334. if (data[7] == 3) {
  335. path = data[8] + "/" + data[5] + "-" + data[6] + data[10];
  336. } else {
  337. path = data[8] + "/" + data[5] + "-" + data[6] + data[10];
  338. }
  339. $("#plan_code").text(data[5]);
  340. $("#plan_name").text(data[6]);
  341. $("#version").text(data[3]);
  342. $("#fileName").text(data[1]);
  343. if(path.toLowerCase().includes("pdf")){
  344. PDFObject.embed("./assets/建置資料/" + path, "#pdfFile");
  345. $("#download").append('<img src="./assets/images/' + 'pdf-icon.png" style="height: 30px; display: inline-block;"> ');
  346. }else{
  347. PDFObject.embed("./assets/notPDF.pdf", "#pdfFile");
  348. if(path.toLowerCase().includes("doc")){
  349. $("#download").append('<img src="./assets/images/' + 'word-icon.png" style="height: 30px; display: inline-block;"> ');
  350. }else if(path.toLowerCase().includes("xls")){
  351. $("#download").append('<img src="./assets/images/' + 'excel-icon.png" style="height: 30px; display: inline-block;"> ');
  352. }
  353. }
  354. console.log( data[10]);
  355. let fileNames = data[10].split('/');
  356. let fileName = fileNames[fileNames.length-1];
  357. $("#download").append('<a target="_blank" href="./assets/建置資料/' + path + '">' + fileName + '</a>');
  358. }
  359. });
  360. function displayDetail(isFile, isBlueprint) {
  361. table.column(0).visible(isBlueprint);
  362. table.column(2).visible(isFile);
  363. table.column(3).visible(isFile);
  364. table.column(4).visible(!isFile);
  365. if (isFile) {
  366. $(".table-responsive").show();
  367. $("#SN").text("圖號");
  368. $("#name").text("圖名");
  369. $('.nav-tabs a[href="#pdfTab"]').tab('show');
  370. //test = table;
  371. if (table.rows().count() > 10) {
  372. table.page.len(10).draw();
  373. } else {
  374. table.page.len(-1).draw();
  375. }
  376. } else {
  377. $(".table-responsive").hide();
  378. $("#SN").text("項次");
  379. $("#name").text("類別");
  380. if (isBlueprint) {
  381. table.page.len(-1).draw();
  382. } else {
  383. table.page.len(7).draw();
  384. }
  385. $('.nav-tabs a[href="#arcgisTab"]').tab('show');
  386. }
  387. }
  388. function countFiles(project_id, is_blueprint) {
  389. $.ajax({
  390. url: "./script/php/get_file_count.php",
  391. data: {
  392. project_id: project_id,
  393. is_blueprint: is_blueprint,
  394. },
  395. async: false,
  396. //contentType: "application/json",
  397. dataType: "json",
  398. type: "POST",
  399. })
  400. .success(function (response) {
  401. console.log(response);
  402. fileNums = response;
  403. })
  404. .error(function (error) {
  405. console.log(error);
  406. })
  407. .complete(function () {});
  408. }
  409. function addTable(data, notes, response, SN, path) {
  410. table.clear().draw();
  411. if (data[1] != "設計圖" && data[1] != "成果圖") {
  412. for (i = 0; i < response.length; i++) {
  413. table.row.add([
  414. response[i]["SN"],
  415. response[i]["original_name"],
  416. response[i]["keyword"],
  417. '1.0.0',
  418. '',
  419. response[i]["project_id"],
  420. response[i]["project_name"],
  421. response[i]["type_id"],
  422. response[i]["category_name"],
  423. response[i]["notes"],
  424. response[i]["filename"],
  425. ])
  426. table.draw();
  427. }
  428. if (notes.includes(data[1])) {
  429. is_blueprint2 = true;
  430. displayDetail(true, true);
  431. } else {
  432. displayDetail(true, false);
  433. }
  434. } else {
  435. displayDetail(false, true);
  436. blueprint_SN = SN;
  437. blueprint_path = path;
  438. tempTable2 = [];
  439. countFiles(project_id, "true");
  440. for (i = 0; i < response.length; i++) {
  441. notes[i] = response[i]['notes'];
  442. if(fileNums[i] == null){
  443. total = 0;
  444. }else{
  445. total = fileNums[i].total;
  446. }
  447. let temp = [
  448. i + 1,
  449. response[i]['notes'],
  450. '',
  451. '',
  452. total,
  453. data[5],
  454. ];
  455. table.row.add(temp);
  456. tempTable2.push(temp);
  457. }
  458. table.draw();
  459. }
  460. }
  461. });