index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const $loading = $('#loading');
  2. $loading.hide();
  3. $(document.body).on('click', '.block', function() {
  4. if ($(this).hasClass('selected')) {
  5. $(this).removeClass('selected');
  6. } else {
  7. $(this).addClass('selected');
  8. }
  9. });
  10. function JSON_to_URLEncoded(element, key, list) {
  11. list = list || [];
  12. if (typeof (element) === 'object') {
  13. for (const idx in element) {
  14. JSON_to_URLEncoded(element[idx], key ? key + '[' + idx + ']' : idx, list);
  15. }
  16. } else {
  17. list.push(key + '=' + encodeURIComponent(element));
  18. }
  19. return list.join('&');
  20. }
  21. $('#select-and-build').on('click', () => {
  22. $loading.show();
  23. const ids = $.map($('.selected.block'), item => $(item).data('index'));
  24. const xhr = new XMLHttpRequest();
  25. xhr.open('POST', '/bundle', true);
  26. // xhr.setRequestHeader('Content-type', 'application/json');
  27. xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  28. xhr.responseType = 'blob';
  29. xhr.onreadystatechange = () => { // Call a function when the state changes.
  30. if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
  31. // Request finished. Do processing here.
  32. $loading.hide();
  33. window.saveAs(xhr.response, 'g2-dist.zip');
  34. }
  35. };
  36. xhr.send(JSON_to_URLEncoded({ ids }));
  37. // $.ajax({
  38. // type: 'POST',
  39. // url: '/bundle',
  40. // data: { ids },
  41. // beforeSend: jqXHR => {
  42. // jqXHR.responseType = 'binary';
  43. // }
  44. // }).done(data => {
  45. // const binaryData = [];
  46. // binaryData.push(data);
  47. // window.saveAs(new Blob(binaryData, { type: 'application/zip' }), 'g2-dist.zip');
  48. // });
  49. });
  50. $('#select-all').on('click', () => {
  51. $('.block').addClass('selected');
  52. });
  53. $('#cancel-select').on('click', () => {
  54. $('.selected').removeClass('selected');
  55. });