설명 없음
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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Ugly global variable holding the current card deck
  2. var card_data = [];
  3. var card_options = card_default_options();
  4. function ui_generate() {
  5. // Generate output HTML
  6. var card_html = card_pages_generate_html(card_data, card_options);
  7. // Open a new window for the output
  8. // Use a separate window to avoid CSS conflicts
  9. var tab = window.open("output.html", 'rpg-cards-output');
  10. // Send the generated HTML to the new window
  11. // Use a delay to give the new window time to set up a message listener
  12. setTimeout(function () { tab.postMessage(card_html, '*') }, 100);
  13. }
  14. function ui_load_sample() {
  15. card_data = card_data_example;
  16. ui_update_card_list();
  17. }
  18. function ui_clear_all() {
  19. card_data = [];
  20. ui_update_card_list();
  21. }
  22. function ui_load_files(evt) {
  23. ui_clear_all();
  24. var files = evt.target.files;
  25. for (var i = 0, f; f = files[i]; i++) {
  26. var reader = new FileReader();
  27. reader.onload = function (reader) {
  28. var data = JSON.parse(this.result);
  29. ui_add_cards(data);
  30. };
  31. reader.readAsText(f);
  32. }
  33. }
  34. function ui_add_cards(data) {
  35. card_data = card_data.concat(data);
  36. ui_update_card_list();
  37. }
  38. function ui_add_new_card() {
  39. card_data.push(card_default_data());
  40. ui_update_card_list();
  41. ui_select_card_by_index(card_data.length - 1);
  42. }
  43. function ui_select_card_by_index(index) {
  44. $("#selected-card").val(index);
  45. ui_update_selected_card();
  46. }
  47. function ui_selected_card_index() {
  48. return parseInt($("#selected-card").val(), 10);
  49. }
  50. function ui_selected_card() {
  51. return card_data[ui_selected_card_index()];
  52. }
  53. function ui_delete_card() {
  54. card_data.splice(ui_selected_card_index(), 1);
  55. ui_update_card_list();
  56. }
  57. function ui_update_card_list() {
  58. $("#total_card_count").text("Deck contains " + card_data.length + " cards.");
  59. $('#selected-card').empty();
  60. for (var i = 0; i < card_data.length; ++i) {
  61. var card = card_data[i];
  62. $('#selected-card')
  63. .append($("<option></option>")
  64. .attr("value", i)
  65. .text(card.title));
  66. }
  67. ui_update_selected_card();
  68. }
  69. function ui_save_file() {
  70. var str = JSON.stringify(card_data, null, " ");
  71. var parts = [str];
  72. var blob = new Blob(parts, { type: 'application/json' });
  73. var url = URL.createObjectURL(blob);
  74. var a = $("#file-save-link")[0];
  75. a.href = url;
  76. a.download = "rpg_cards.json";
  77. a.click();
  78. URL.revokeObjectURL(url);
  79. }
  80. function ui_update_selected_card() {
  81. var card = ui_selected_card();
  82. if (card) {
  83. $("#card-title").val(card.title);
  84. $("#card-count").val(card.count);
  85. $("#card-icon").val(card.icon);
  86. $("#card-icon-back").val(card.icon_back);
  87. $("#card-contents").val(card.contents.join("\n"));
  88. $("#card-color").val(card.color);
  89. ui_update_card_color_selector(card.color);
  90. }
  91. ui_render_selected_card();
  92. }
  93. function ui_render_selected_card() {
  94. var card = ui_selected_card();
  95. $('#preview-container').empty();
  96. if (card) {
  97. var front = card_generate_front(card, card_options);
  98. var back = card_generate_back(card, card_options);
  99. $('#preview-container').html(front + "\n" + back);
  100. }
  101. }
  102. function ui_open_help() {
  103. window.open("http://crobi.github.io/rpg-cards/", "_blank");
  104. }
  105. function ui_select_icon() {
  106. window.open("http://game-icons.net/", "_blank");
  107. }
  108. function ui_setup_color_selector() {
  109. // Insert colors
  110. $.each(card_colors, function (name, val) {
  111. $(".colorselector-data")
  112. .append($("<option></option>")
  113. .attr("value", name)
  114. .attr("data-color", val)
  115. .text(name));
  116. });
  117. // Callbacks for when the user picks a color
  118. $('#default_color_selector').colorselector({
  119. callback: function (value, color, title) {
  120. $("#default_color").val(title);
  121. ui_set_default_color(title);
  122. }
  123. });
  124. $('#card_color_selector').colorselector({
  125. callback: function (value, color, title) {
  126. $("#card-color").val(title);
  127. ui_set_card_color(value);
  128. }
  129. });
  130. // Callbacks for when the user enters a color as a text
  131. $("#default_color").change(function (e) {
  132. var color = $("#default_color").val();
  133. if ($("#default_color_selector option[value='" + color + "']").length > 0) {
  134. $("#default_color_selector").colorselector("setValue", color);
  135. } else {
  136. $("#default_color_selector").colorselector("setValue", "");
  137. $("#default_color").val(color);
  138. }
  139. ui_set_default_color(color);
  140. });
  141. // Styling
  142. $(".dropdown-colorselector").addClass("input-group-addon color-input-addon");
  143. }
  144. function ui_set_default_color(color) {
  145. card_options.default_color = color;
  146. }
  147. function ui_change_card_title() {
  148. var title = $("#card-title").val();
  149. var card = ui_selected_card();
  150. if (card) {
  151. card.title = title;
  152. $("#selected-card option:selected").text(title);
  153. ui_render_selected_card();
  154. }
  155. }
  156. function ui_change_card_property() {
  157. var property = $(this).attr("data-property");
  158. var value = $(this).val();
  159. var card = ui_selected_card();
  160. if (card) {
  161. card[property] = value;
  162. ui_render_selected_card();
  163. }
  164. }
  165. function ui_set_card_color(value) {
  166. var card = ui_selected_card();
  167. if (card) {
  168. card.color = value;
  169. ui_render_selected_card();
  170. }
  171. }
  172. function ui_update_card_color_selector(color) {
  173. if ($("#card_color_selector option[value='" + color + "']").length > 0) {
  174. // Update the color selector to the entered value
  175. $("#card_color_selector").colorselector("setValue", color);
  176. } else {
  177. // Unknown color - select a neutral color and reset the text value
  178. $("#card_color_selector").colorselector("setValue", "");
  179. $("#card-color").val(color);
  180. }
  181. }
  182. function ui_change_card_color() {
  183. var color = $(this).val();
  184. ui_update_card_color_selector(color);
  185. ui_set_card_color(color);
  186. }
  187. function ui_change_card_contents() {
  188. var value = $(this).val();
  189. var card = ui_selected_card();
  190. if (card) {
  191. card.contents = value.split("\n");
  192. ui_render_selected_card();
  193. }
  194. }
  195. $(document).ready(function () {
  196. ui_setup_color_selector();
  197. $("#button-generate").click(ui_generate);
  198. $("#button-load").click(function () { $("#file-load").click(); });
  199. $("#file-load").change(ui_load_files);
  200. $("#button-load-sample").click(ui_load_sample);
  201. $("#button-save").click(ui_save_file);
  202. $("#button-add-card").click(ui_add_new_card);
  203. $("#button-delete-card").click(ui_delete_card);
  204. $("#button-help").click(ui_open_help);
  205. $("#selected-card").change(ui_update_selected_card);
  206. $("#card-title").change(ui_change_card_title);
  207. $("#card-icon").change(ui_change_card_property);
  208. $("#card-count").change(ui_change_card_property);
  209. $("#card-icon-back").change(ui_change_card_property);
  210. $("#card-color").change(ui_change_card_color);
  211. $("#card-contents").change(ui_change_card_contents);
  212. $(".icon-select-button").click(ui_select_icon);
  213. ui_update_card_list();
  214. });