No Description
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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_duplicate_card() {
  44. if (card_data.length > 0) {
  45. card_data.push(ui_selected_card());
  46. var card = card_data[card_data.length - 1];
  47. card.title = card.title + " (Copy)";
  48. } else {
  49. card_data.push(card_default_data());
  50. }
  51. ui_update_card_list();
  52. ui_select_card_by_index(card_data.length - 1);
  53. }
  54. function ui_select_card_by_index(index) {
  55. $("#selected-card").val(index);
  56. ui_update_selected_card();
  57. }
  58. function ui_selected_card_index() {
  59. return parseInt($("#selected-card").val(), 10);
  60. }
  61. function ui_selected_card() {
  62. return card_data[ui_selected_card_index()];
  63. }
  64. function ui_delete_card() {
  65. card_data.splice(ui_selected_card_index(), 1);
  66. ui_update_card_list();
  67. }
  68. function ui_update_card_list() {
  69. $("#total_card_count").text("Deck contains " + card_data.length + " cards.");
  70. $('#selected-card').empty();
  71. for (var i = 0; i < card_data.length; ++i) {
  72. var card = card_data[i];
  73. $('#selected-card')
  74. .append($("<option></option>")
  75. .attr("value", i)
  76. .text(card.title));
  77. }
  78. ui_update_selected_card();
  79. }
  80. function ui_save_file() {
  81. var str = JSON.stringify(card_data, null, " ");
  82. var parts = [str];
  83. var blob = new Blob(parts, { type: 'application/json' });
  84. var url = URL.createObjectURL(blob);
  85. var a = $("#file-save-link")[0];
  86. a.href = url;
  87. a.download = "rpg_cards.json";
  88. a.click();
  89. URL.revokeObjectURL(url);
  90. }
  91. function ui_update_selected_card() {
  92. var card = ui_selected_card();
  93. if (card) {
  94. $("#card-title").val(card.title);
  95. $("#card-count").val(card.count);
  96. $("#card-icon").val(card.icon);
  97. $("#card-icon-back").val(card.icon_back);
  98. $("#card-contents").val(card.contents.join("\n"));
  99. $("#card-color").val(card.color);
  100. ui_update_card_color_selector(card.color);
  101. }
  102. ui_render_selected_card();
  103. }
  104. function ui_render_selected_card() {
  105. var card = ui_selected_card();
  106. $('#preview-container').empty();
  107. if (card) {
  108. var front = card_generate_front(card, card_options);
  109. var back = card_generate_back(card, card_options);
  110. $('#preview-container').html(front + "\n" + back);
  111. }
  112. }
  113. function ui_open_help() {
  114. window.open("http://crobi.github.io/rpg-cards/", "_blank");
  115. }
  116. function ui_select_icon() {
  117. window.open("http://game-icons.net/", "_blank");
  118. }
  119. function ui_setup_color_selector() {
  120. // Insert colors
  121. $.each(card_colors, function (name, val) {
  122. $(".colorselector-data")
  123. .append($("<option></option>")
  124. .attr("value", name)
  125. .attr("data-color", val)
  126. .text(name));
  127. });
  128. // Callbacks for when the user picks a color
  129. $('#default_color_selector').colorselector({
  130. callback: function (value, color, title) {
  131. $("#default_color").val(title);
  132. ui_set_default_color(title);
  133. }
  134. });
  135. $('#card_color_selector').colorselector({
  136. callback: function (value, color, title) {
  137. $("#card-color").val(title);
  138. ui_set_card_color(value);
  139. }
  140. });
  141. // Callbacks for when the user enters a color as a text
  142. $("#default_color").change(function (e) {
  143. var color = $("#default_color").val();
  144. if ($("#default_color_selector option[value='" + color + "']").length > 0) {
  145. $("#default_color_selector").colorselector("setValue", color);
  146. } else {
  147. $("#default_color_selector").colorselector("setValue", "");
  148. $("#default_color").val(color);
  149. }
  150. ui_set_default_color(color);
  151. });
  152. // Styling
  153. $(".dropdown-colorselector").addClass("input-group-addon color-input-addon");
  154. }
  155. function ui_set_default_color(color) {
  156. card_options.default_color = color;
  157. }
  158. function ui_change_card_title() {
  159. var title = $("#card-title").val();
  160. var card = ui_selected_card();
  161. if (card) {
  162. card.title = title;
  163. $("#selected-card option:selected").text(title);
  164. ui_render_selected_card();
  165. }
  166. }
  167. function ui_change_card_property() {
  168. var property = $(this).attr("data-property");
  169. var value = $(this).val();
  170. var card = ui_selected_card();
  171. if (card) {
  172. card[property] = value;
  173. ui_render_selected_card();
  174. }
  175. }
  176. function ui_set_card_color(value) {
  177. var card = ui_selected_card();
  178. if (card) {
  179. card.color = value;
  180. ui_render_selected_card();
  181. }
  182. }
  183. function ui_update_card_color_selector(color) {
  184. if ($("#card_color_selector option[value='" + color + "']").length > 0) {
  185. // Update the color selector to the entered value
  186. $("#card_color_selector").colorselector("setValue", color);
  187. } else {
  188. // Unknown color - select a neutral color and reset the text value
  189. $("#card_color_selector").colorselector("setValue", "");
  190. $("#card-color").val(color);
  191. }
  192. }
  193. function ui_change_card_color() {
  194. var color = $(this).val();
  195. ui_update_card_color_selector(color);
  196. ui_set_card_color(color);
  197. }
  198. function ui_change_card_contents() {
  199. var value = $(this).val();
  200. var card = ui_selected_card();
  201. if (card) {
  202. card.contents = value.split("\n");
  203. ui_render_selected_card();
  204. }
  205. }
  206. $(document).ready(function () {
  207. ui_setup_color_selector();
  208. $('.icon-list').typeahead({source:icon_names});
  209. $("#button-generate").click(ui_generate);
  210. $("#button-load").click(function () { $("#file-load").click(); });
  211. $("#file-load").change(ui_load_files);
  212. $("#button-load-sample").click(ui_load_sample);
  213. $("#button-save").click(ui_save_file);
  214. $("#button-add-card").click(ui_add_new_card);
  215. $("#button-duplicate-card").click(ui_duplicate_card);
  216. $("#button-delete-card").click(ui_delete_card);
  217. $("#button-help").click(ui_open_help);
  218. $("#selected-card").change(ui_update_selected_card);
  219. $("#card-title").change(ui_change_card_title);
  220. $("#card-icon").change(ui_change_card_property);
  221. $("#card-count").change(ui_change_card_property);
  222. $("#card-icon-back").change(ui_change_card_property);
  223. $("#card-color").change(ui_change_card_color);
  224. $("#card-contents").change(ui_change_card_contents);
  225. $(".icon-select-button").click(ui_select_icon);
  226. ui_update_card_list();
  227. });