Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ui.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Ugly global variable holding the current card deck
  2. var card_data = [];
  3. function ui_generate() {
  4. // Generate output HTML
  5. var card_html = card_pages_generate_html(card_data);
  6. // Open a new window for the output
  7. // Use a separate window to avoid CSS conflicts
  8. var tab = window.open("output.html", 'rpg-cards-output');
  9. // Send the generated HTML to the new window
  10. // Use a delay to give the new window time to set up a message listener
  11. setTimeout(function () { tab.postMessage(card_html, '*') }, 100);
  12. }
  13. function ui_load_sample() {
  14. card_data = card_data_example;
  15. ui_update_card_list();
  16. }
  17. function ui_clear_all() {
  18. card_data = [];
  19. ui_update_card_list();
  20. }
  21. function ui_load_files(evt) {
  22. ui_clear_all();
  23. var files = evt.target.files;
  24. for (var i = 0, f; f = files[i]; i++) {
  25. var reader = new FileReader();
  26. reader.onload = function (reader) {
  27. var data = JSON.parse(this.result);
  28. ui_add_cards(data);
  29. };
  30. reader.readAsText(f);
  31. }
  32. }
  33. function ui_add_cards(data) {
  34. card_data = card_data.concat(data);
  35. ui_update_card_list();
  36. }
  37. function ui_add_new_card() {
  38. card_data.push(card_default_data());
  39. ui_update_card_list();
  40. }
  41. function ui_update_card_list() {
  42. $("#total_card_count").text("Deck contains " + card_data.length + " cards.");
  43. $('#selected_card').empty();
  44. for (var i = 0; i < card_data.length; ++i) {
  45. var card = card_data[i];
  46. $('#selected_card')
  47. .append($("<option></option>")
  48. .attr("value", i)
  49. .text(card.title));
  50. }
  51. }
  52. function ui_save_file() {
  53. var str = JSON.stringify(card_data, null, " ");
  54. var parts = [str];
  55. var blob = new Blob(parts, { type: 'application/json' });
  56. var url = URL.createObjectURL(blob);
  57. var a = $("#file-save-link")[0];
  58. a.href = url;
  59. a.download = "rpg_cards.json";
  60. a.click();
  61. URL.revokeObjectURL(url);
  62. }
  63. $(document).ready(function () {
  64. $("#button-generate").click(ui_generate);
  65. $("#button-load").click(function () { $("#file-load").click(); });
  66. $("#file-load").change(ui_load_files);
  67. $("#button-load-sample").click(ui_load_sample);
  68. $("#button-save").click(ui_save_file);
  69. ui_update_card_list();
  70. });