Brak opisu
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.

card.ts 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. module RpgCards {
  2. function normalizeTag(tag: string): string {
  3. return tag.trim().toLowerCase();
  4. }
  5. function splitParams(value: string): string[] {
  6. return value.split("|").map(function (str) { return str.trim(); });
  7. }
  8. export class Options {
  9. foreground_color: string;
  10. background_color: string;
  11. empty_color: string;
  12. default_color: string;
  13. default_icon: string;
  14. default_title_size: string;
  15. page_size: string;
  16. page_rows: number;
  17. page_columns: number;
  18. card_arrangement: string;
  19. card_size: string;
  20. card_count: number;
  21. icon_inline: boolean;
  22. constructor() {
  23. this.foreground_color = "white";
  24. this.background_color = "white";
  25. this.empty_color = "black";
  26. this.default_color = "black";
  27. this.default_icon = "ace";
  28. this.default_title_size = "13";
  29. this.page_size = "A4";
  30. this.page_rows = 3;
  31. this.page_columns = 3;
  32. this.card_arrangement = "doublesided";
  33. this.card_size = "25x35";
  34. this.card_count = null;
  35. this.icon_inline = true;
  36. }
  37. }
  38. export class Card {
  39. count: number;
  40. title: string;
  41. title_size: string;
  42. title_icon_text: string;
  43. color: string;
  44. color_front: string;
  45. color_back: string;
  46. icon: string;
  47. icon_front: string;
  48. icon_back: string;
  49. contents: string[];
  50. tags: string[];
  51. userData: any;
  52. constructor() {
  53. this.count = 1;
  54. this.title = "New card";
  55. this.title_size = null;
  56. this.title_icon_text = null;
  57. this.color = null;
  58. this.color_front = null;
  59. this.color_back = null;
  60. this.icon = null;
  61. this.icon_front = null;
  62. this.icon_back = null;
  63. this.contents = [];
  64. this.tags = [];
  65. this.userData = null;
  66. }
  67. static fromJSON(json: any): Card {
  68. var result = new Card;
  69. result.count = json.count || 1;
  70. result.title = json.title || "";
  71. result.title_size = json.title_size || null;
  72. result.title_icon_text = json.title_icon_text || null;
  73. result.color = json.color || null;
  74. result.color_front = json.color_front || null;
  75. result.color_back = json.color_back || null;
  76. result.icon = json.icon || null;
  77. result.icon_front = json.icon_front || null;
  78. result.icon_back = json.icon_back || null;
  79. result.contents = json.contents || [];
  80. result.tags = json.tags || [];
  81. return result;
  82. }
  83. public toJSON(): any {
  84. return {
  85. count: this.count,
  86. title: this.title,
  87. title_size: this.title_size,
  88. title_icon_text: this.title_icon_text,
  89. color: this.color,
  90. color_front: this.color_front,
  91. color_back: this.color_back,
  92. icon: this.icon,
  93. icon_front: this.icon_front,
  94. icon_back: this.icon_back,
  95. contents: this.contents.slice(),
  96. tags: this.tags.slice()
  97. }
  98. }
  99. public duplicate(): Card {
  100. var result = Card.fromJSON(this.toJSON());
  101. result.title += " (Copy)";
  102. return result;
  103. }
  104. public hasTag(tag: string): boolean {
  105. var index = this.tags.indexOf(normalizeTag(tag));
  106. return index > -1;
  107. }
  108. public addTag(tag: string): void {
  109. if (!this.hasTag(tag)) {
  110. this.tags.push(normalizeTag(tag));
  111. }
  112. }
  113. public removeTag(tag: string): void {
  114. var ntag = normalizeTag(tag);
  115. this.tags = this.tags.filter(function (t) {
  116. return ntag != t;
  117. });
  118. }
  119. public getTitle(options: Options): string {
  120. return this.title || "";
  121. }
  122. public getTitleSize(options: Options): string {
  123. return this.title_size || options.default_title_size || "13";
  124. }
  125. public getTitleIconText(options: Options): string {
  126. return this.title_icon_text || "";
  127. }
  128. public getColorFront(options: Options): string {
  129. return this.color_front || this.color || options.default_color || "black";
  130. }
  131. public getColorBack(options: Options): string {
  132. return this.color_back || this.color || options.default_color || "black";
  133. }
  134. public getIconFront(options: Options): string {
  135. return this.icon_front || this.icon || options.default_icon || "";
  136. }
  137. public getIconBack(options: Options): string {
  138. return this.icon_back || this.icon || options.default_icon || "";
  139. }
  140. };
  141. interface CardAction {
  142. fn: string;
  143. card: Card;
  144. ref: Card;
  145. }
  146. export class CardDeck {
  147. cards: Card[];
  148. private _actions: CardAction[];
  149. constructor() {
  150. this.cards = [];
  151. this._actions = [];
  152. }
  153. public toJSON(): any {
  154. return this.cards.map((card) => card.toJSON());
  155. }
  156. public static fromJSON(data: any): CardDeck {
  157. if (Array.isArray(data)) {
  158. var result = new CardDeck;
  159. for (var i = 0; i < data.length; ++i) {
  160. result.cards.push(Card.fromJSON(data[i]));
  161. }
  162. return result;
  163. } else {
  164. throw new Error("Invalid data");
  165. }
  166. }
  167. public addCards(cards: Card[]): void {
  168. cards.forEach((card) => {
  169. this._actions.push({fn:"add", card: card, ref: null});
  170. });
  171. }
  172. public addNewCard(): Card {
  173. var newCard = new Card();
  174. this._actions.push({ fn: "add", card: newCard, ref: null });
  175. return newCard;
  176. }
  177. public duplicateCard(card: Card): Card {
  178. var newCard = card.duplicate();
  179. this._actions.push({ fn: "add", card: newCard, ref: card });
  180. return newCard;
  181. }
  182. public deleteCard(card: Card): void {
  183. this._actions.push({ fn: "del", card: card, ref: null });
  184. }
  185. public commit() {
  186. for (var i = 0; i < this._actions.length; ++i) {
  187. var action = this._actions[i];
  188. if (action.fn === "add") {
  189. var index = this.cards.indexOf(action.ref);
  190. if (index > -1) {
  191. this.cards.splice(index + 1, 0, action.card);
  192. } else {
  193. this.cards.push(action.card);
  194. }
  195. } else if (action.fn === "del") {
  196. var index = this.cards.indexOf(action.card);
  197. if (index > -1) {
  198. this.cards.splice(index, 1);
  199. }
  200. }
  201. }
  202. this._actions = [];
  203. }
  204. }
  205. type ContentGeneratorFunction = (params: string[], card: Card, options: Options, ind: string, ind0: string) => string;
  206. type CardGeneratorFunction = (card: Card, options: Options, ind: string, ind0: string) => string;
  207. export class CardHtmlGenerator {
  208. constructor() {
  209. }
  210. private _icon(src: string, ind: string, ind0: string): string {
  211. if (src.length > 0) {
  212. return ind + '<card-icon src="/icons/' + src + '.svg"></card-icon>\n';
  213. } else {
  214. return "";
  215. }
  216. }
  217. private _subtitle(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  218. var text = params[0] || "";
  219. return ind + '<card-subtitle>' + text + '</card-subtitle>\n';
  220. }
  221. private _ruler(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  222. return ind + '<card-rule></card-rule>\n';
  223. }
  224. private _boxes(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  225. var count = params[0] || 1;
  226. var size = params[1] || 3;
  227. return ind + '<card-boxes size="' + size + '" count="' + count + '"></card-boxes>\n';
  228. }
  229. private _property(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  230. var header = params[0] || "";
  231. var text = params[1] || "";
  232. var result = "";
  233. result += ind + '<card-property>\n';
  234. result += ind + ind0 + '<h4>' + header + '</h4>\n';
  235. result += ind + ind0 + '<p>' + text + '</p>\n';
  236. result += ind + '</card-property>\n';
  237. return result;
  238. }
  239. private _description(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  240. var header = params[0] || "";
  241. var text = params[1] || "";
  242. var result = "";
  243. result += ind + '<card-description>\n';
  244. result += ind + ind0 + '<h4>' + header + '</h4>\n';
  245. result += ind + ind0 + '<p>' + text + '</p>\n';
  246. result += ind + '</card-description>\n';
  247. return result;
  248. }
  249. private _text(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  250. var text = params[0] || "";
  251. var result = "";
  252. result += ind + '<card-description>\n';
  253. result += ind + ind0 + '<p>' + text + '</p>\n';
  254. result += ind + '</card-description>\n';
  255. return result;
  256. }
  257. private _dndstats(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  258. var stats = ["str", "dex", "con", "int", "wis", "cha"];
  259. var result = "";
  260. result += ind + '<card-dndstats';
  261. for (var i = 0; i < stats.length; ++i) {
  262. var value = params[i] || "";
  263. var stat = stats[i];
  264. result += ' ' + stat + '="' + value + '"';
  265. }
  266. result += '></card-dndstats>\n';
  267. return result;
  268. }
  269. private _bullet(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  270. var text = params[0] || "";
  271. return ind + '<card-bullet>' + text + '</card-bullet>\n';
  272. }
  273. private _section(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  274. var text = params[0] || "";
  275. return ind + '<card-section>' + text + '</card-section>\n';
  276. }
  277. private _fill(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  278. var size = params[0] || "1";
  279. return ind + '<card-fill size="' + size + '"></card-fill>\n';
  280. }
  281. private _unknown(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  282. var text = params.join(' | ');
  283. return ind + '<card-description><p>' + text + '</p></card-description>\n';
  284. }
  285. private _empty(params: string[], card: Card, options: Options, ind: string, ind0: string) {
  286. return '';
  287. }
  288. private _contents(contents: string[], card: Card, options: Options, ind: string, ind0: string): string {
  289. var result = "";
  290. result += ind + '<card-contents>\n';
  291. result += contents.map((value) => {
  292. var parts = splitParams(value);
  293. var name = parts[0];
  294. var params = parts.splice(1);
  295. var generator: ContentGeneratorFunction = null;
  296. switch (name) {
  297. case "subtitle": generator = this._subtitle; break;
  298. case "property": generator = this._property; break;
  299. case "rule": generator = this._ruler; break;
  300. case "ruler": generator = this._ruler; break;
  301. case "boxes": generator = this._boxes; break;
  302. case "description": generator = this._description; break;
  303. case "dndstats": generator = this._dndstats; break;
  304. case "text": generator = this._text; break;
  305. case "bullet": generator = this._bullet; break;
  306. case "fill": generator = this._fill; break;
  307. case "section": generator = this._section; break;
  308. case "disabled": generator = this._empty; break;
  309. case "": generator = this._empty; break;
  310. default: return this._unknown(parts, card, options, ind, ind0);
  311. }
  312. return generator(params, card, options, ind + ind0, ind);
  313. }).join("\n");
  314. result += ind + '</card-contents>\n';
  315. return result;
  316. }
  317. private _title(card: Card, options: Options, ind: string, ind0: string): string {
  318. var title = card.getTitle(options);
  319. var title_size = card.getTitleSize(options);
  320. var title_icon_text = card.getTitleIconText(options);
  321. var icon = card.getIconFront(options);
  322. var result = "";
  323. result += ind + '<card-title size="' + title_size + '">\n';
  324. result += ind + ind0 + '<h1>' + title + '</h1>\n';
  325. result += ind + ind0 + '<h2>' + title_icon_text + '</h2>\n';
  326. result += this._icon(icon, ind + ind0, ind0);
  327. result += ind + '</card-title>\n';
  328. return result;
  329. }
  330. private _card_front(card: Card, options: Options, ind: string, ind0: string): string {
  331. var result = "";
  332. result += this._title(card, options, ind + ind0, ind0);
  333. result += this._contents(card.contents, card, options, ind + ind0, ind0);
  334. return result;
  335. }
  336. private _card_back(card: Card, options: Options, ind: string, ind0: string): string {
  337. var icon = card.getIconBack(options);
  338. var result = "";
  339. result += ind + '<card-back>\n';
  340. result += this._icon(icon, ind + ind0, ind);
  341. result += ind + '</card-back>\n';
  342. return result;
  343. }
  344. private _card_empty(options: Options, ind: string, ind0: string): string {
  345. var result = "";
  346. result += ind + '<card-contents>\n';
  347. result += ind + '</card-contents>\n';
  348. return result;
  349. }
  350. private _card(options: Options, ind: string, ind0: string, content: string, color: string): string {
  351. var size = options.card_size || "25x35";
  352. var result = "";
  353. result += ind + '<rpg-card color="' + color + '" size="' + size + '">\n';
  354. result += content;
  355. result += ind + '</rpg-card>\n';
  356. return result;
  357. }
  358. /** Generates HTML for the front side of the given card */
  359. public card_front(card: Card, options: Options, indent: string): string {
  360. var content = this._card_front(card, options, "", indent);
  361. return this._card(options, "", indent, content, card.getColorFront(options));
  362. }
  363. /** Generates HTML for the back side of the given card */
  364. public card_back(card: Card, options: Options, indent: string): string {
  365. var content = this._card_back(card, options, "", indent);
  366. return this._card(options, "", indent, content, card.getColorBack(options));
  367. }
  368. /** Generates HTML for an empty given card */
  369. public card_empty(options: Options, indent: string): string {
  370. var content = this._card_empty(options, "", indent);
  371. return this._card(options, "", indent, content, options.empty_color);
  372. }
  373. }
  374. class CardPage<T> {
  375. rows: number;
  376. cols: number;
  377. cards: T[];
  378. constructor(rows: number, cols: number) {
  379. this.rows = rows;
  380. this.cols = cols;
  381. this.cards = [];
  382. }
  383. /** Returns an empty page with the same dimensions */
  384. public newPage(): CardPage<T> {
  385. return new CardPage<T>(this.rows, this.cols);
  386. }
  387. private _posToIndex(row: number, col: number): number {
  388. return row * this.cols + col;
  389. }
  390. /** Adds one card to the page */
  391. public addCard(card: T): void {
  392. if (this.capacity() === 0) {
  393. throw new Error("This page is full.");
  394. }
  395. this.cards.push(card);
  396. }
  397. /**
  398. Adds several copies of a card to the page.
  399. Returns the number of copies that did not fit on the page.
  400. */
  401. public addCards(card: T, count: number): number {
  402. while (this.capacity() > 0 && count > 0) {
  403. this.addCard(card);
  404. --count;
  405. }
  406. return count;
  407. }
  408. /** Fills all remaining slots on the current row with the given card */
  409. public fillRow(card: T): void {
  410. while (this.capacityRow() > 0) {
  411. this.addCard(card);
  412. }
  413. }
  414. /** Fills all remaining slots on the page with empty cards */
  415. public fillPage(card: T): void {
  416. while (this.capacity() > 0) {
  417. this.addCard(card);
  418. }
  419. }
  420. /** Empty slots on the page */
  421. public capacity(): number {
  422. return this.rows * this.cols - this.cards.length;
  423. }
  424. /** Empty slots on the current line */
  425. public capacityRow(): number {
  426. return this.capacity() % this.cols;
  427. }
  428. /** Flip card slots horizontally */
  429. public flipH() {
  430. if (this.capacity() > 0) {
  431. throw new Error("Cannot perform this operation while the page is not full");
  432. }
  433. for (var r = 0; r < this.rows; ++r) {
  434. for (var c = 0; c < Math.floor(this.cols / 2); ++c) {
  435. var indexL = this._posToIndex(r, c);
  436. var indexR = this._posToIndex(r, this.cols - c - 1);
  437. var cardL = this.cards[indexL];
  438. var cardR = this.cards[indexR];
  439. this.cards[indexL] = cardR;
  440. this.cards[indexR] = cardL;
  441. }
  442. }
  443. }
  444. }
  445. class CardPageSet<T> {
  446. rows: number;
  447. cols: number;
  448. pages: CardPage<T>[];
  449. constructor(rows: number, cols: number) {
  450. this.rows = rows;
  451. this.cols = cols;
  452. this.pages = [];
  453. }
  454. public lastPage(): CardPage<T> {
  455. if (this.pages.length === 0) {
  456. return null;
  457. } else {
  458. return this.pages[this.pages.length - 1];
  459. }
  460. }
  461. public addPage(): CardPage<T> {
  462. var newPage = new CardPage<T>(this.rows, this.cols);
  463. this.pages.push(newPage);
  464. return newPage;
  465. }
  466. /**
  467. Adds one card to the last page.
  468. Adds a new pages if necessary.
  469. */
  470. public addCard(card: T): void {
  471. var page = this.lastPage();
  472. if (page === null || page.capacity() === 0) {
  473. page = this.addPage();
  474. }
  475. page.addCard(card);
  476. }
  477. /**
  478. Adds several copies of a card to the last page.
  479. Adds new pages if necessary.
  480. */
  481. public addCards(card: T, count: number): void {
  482. for (var i = 0; i < count; ++i) {
  483. this.addCard(card);
  484. }
  485. }
  486. public forEach(fn: (page: CardPage<T>)=>void) {
  487. this.pages.forEach(fn);
  488. }
  489. public merge(other: CardPageSet<T>): CardPageSet<T> {
  490. if (this.pages.length !== other.pages.length) {
  491. throw new Error("This function is only for merging two equally sized page sets");
  492. }
  493. var result = new CardPageSet<T>(this.rows, this.cols);
  494. for (var i = 0; i < this.pages.length; ++i) {
  495. result.pages.push(this.pages[i]);
  496. result.pages.push(other.pages[i]);
  497. }
  498. return result;
  499. }
  500. }
  501. class BoxSize {
  502. page: string;
  503. width: string;
  504. height: string;
  505. width_px: number;
  506. height_px: number;
  507. constructor(p: string, w: string, h: string, wpx: number, hpx: number) {
  508. this.page = p;
  509. this.width = w;
  510. this.height = h;
  511. this.width_px = Math.floor(wpx);
  512. this.height_px = Math.floor(hpx);
  513. }
  514. }
  515. var boxSizes: { [id: string]: BoxSize } = {};
  516. boxSizes["auto"] = new BoxSize("auto", "auto", "auto", Infinity, Infinity);
  517. boxSizes["A2"] = new BoxSize("A2 portrait", "420mm", "594mm", 1587.401575, 2245.03937);
  518. boxSizes["A3"] = new BoxSize("A3 portrait", "297mm", "420mm", 1118.740158, 1587.401575);
  519. boxSizes["A4"] = new BoxSize("A4 portrait", "210mm", "297mm", 793.700787, 1118.740158);
  520. boxSizes["A5"] = new BoxSize("A5 portrait", "148mm", "210mm", 559.370079, 793.700787);
  521. boxSizes["Letter"] = new BoxSize("Letter portrait", "8.5in", "11in", 816, 1056);
  522. boxSizes["225x35"] = new BoxSize("2.25in 3.5in", "2.25in", "3.5in", 216, 336);
  523. boxSizes["25x35"] = new BoxSize("2.5in 3.5in", "2.5in", "3.5in", 240, 336);
  524. boxSizes["35x50"] = new BoxSize("3.5in 5.0in", "3.5in", "5.0in", 336, 480);
  525. boxSizes["50x70"] = new BoxSize("5.0in 7.0in", "5.0in", "7.0in", 480, 672);
  526. export class PageHtmlGenerator {
  527. indent: string;
  528. constructor() {
  529. this.indent = " ";
  530. }
  531. private _pageColor(page: number, options: Options): string {
  532. if ((options.card_arrangement == "doublesided") && (page % 2 == 1)) {
  533. return options.background_color;
  534. } else {
  535. return options.foreground_color;
  536. }
  537. }
  538. private _wrap(pageSet: CardPageSet<string>, options: Options) {
  539. var result = "";
  540. for (var i = 0; i < pageSet.pages.length; ++i) {
  541. var page = pageSet.pages[i];
  542. var style = ' style="background-color:' + this._pageColor(i, options) + '"';
  543. result += '<card-page' + style + '>\n';
  544. result += page.cards.join("");
  545. result += '</card-page>\n';
  546. }
  547. return result;
  548. }
  549. private _generatePagesDoublesided(cards: Card[], options: Options, rows: number, cols: number, generator: CardHtmlGenerator): CardPageSet<string> {
  550. var front_pages: CardPageSet<string> = new CardPageSet<string>(rows, cols);
  551. var back_pages: CardPageSet<string> = new CardPageSet<string>(rows, cols);
  552. var empty = generator.card_empty(options, this.indent);
  553. // Fill pages with cards
  554. for (var i = 0; i < cards.length; ++i) {
  555. var card = cards[i];
  556. var front = generator.card_front(card, options, this.indent);
  557. var back = generator.card_back(card, options, this.indent);
  558. front_pages.addCards(front, card.count);
  559. back_pages.addCards(back, card.count);
  560. }
  561. // Fill empty slots
  562. front_pages.forEach((page) => page.fillPage(empty));
  563. back_pages.forEach((page) => page.fillPage(empty));
  564. // Shuffle back cards so that they line up with their corresponding front cards
  565. back_pages.forEach((page) => page.flipH());
  566. // Interleave front and back pages so that we can print double-sided
  567. return front_pages.merge(back_pages);
  568. }
  569. private _generatePagesFrontOnly(cards: Card[], options: Options, rows: number, cols: number, generator: CardHtmlGenerator): CardPageSet<string> {
  570. var pages: CardPageSet<string> = new CardPageSet<string>(rows, cols);
  571. var empty = generator.card_empty(options, this.indent);
  572. // Fill pages with cards
  573. for (var i = 0; i < cards.length; ++i) {
  574. var card = cards[i];
  575. var front = generator.card_front(card, options, this.indent);
  576. pages.addCards(front, card.count);
  577. }
  578. // Fill empty slots
  579. pages.forEach((page) => page.fillPage(empty));
  580. return pages;
  581. }
  582. private _generatePagesSideBySide(cards: Card[], options: Options, rows: number, cols: number, generator: CardHtmlGenerator): CardPageSet<string> {
  583. if (cols < 2) {
  584. throw new Error("Need at least two columns for side-by-side");
  585. }
  586. var pages: CardPageSet<string> = new CardPageSet<string>(rows, cols);
  587. var empty = generator.card_empty(options, this.indent);
  588. // Fill pages with cards (two at a time)
  589. for (var i = 0; i < cards.length; ++i) {
  590. var card = cards[i];
  591. var front = generator.card_front(card, options, this.indent);
  592. var back = generator.card_back(card, options, this.indent);
  593. for (var j = 0; j < card.count; ++j) {
  594. if (pages.pages.length > 0 && pages.lastPage().capacityRow() < 2) {
  595. pages.lastPage().fillRow(empty);
  596. }
  597. pages.addCard(front);
  598. pages.addCard(back);
  599. }
  600. }
  601. // Fill empty slots
  602. pages.forEach((page) => page.fillPage(empty));
  603. return pages;
  604. }
  605. private _generatePages(cards: Card[], options: Options, rows: number, cols: number, generator: CardHtmlGenerator): CardPageSet<string> {
  606. switch (options.card_arrangement) {
  607. case "doublesided": return this._generatePagesDoublesided(cards, options, rows, cols, generator);
  608. case "front_only": return this._generatePagesFrontOnly(cards, options, rows, cols, generator);
  609. case "side_by_side": return this._generatePagesSideBySide(cards, options, rows, cols, generator);
  610. default: throw new Error("Unknown card arrangement");
  611. }
  612. }
  613. private _generateStyle(options) {
  614. var page_box = boxSizes[options.page_size] || boxSizes["auto"];
  615. var result = '';
  616. result += '<style type="text/css">\n';
  617. result += 'body {\n';
  618. result += ' display: block;\n';
  619. result += ' background: rgb(204, 204, 204);\n';
  620. result += '}\n';
  621. result += 'card-page {\n';
  622. result += ' width: ' + (page_box.width_px) + 'px;\n';
  623. result += ' height: ' + (page_box.height_px) + 'px;\n';
  624. result += '}\n';
  625. result += '@media print {\n';
  626. result += ' html, body {\n';
  627. result += ' width: ' + page_box.width_px + 'px;\n';
  628. result += ' }\n';
  629. result += ' body {\n';
  630. result += ' margin: 0;\n';
  631. result += ' padding: 0;\n';
  632. result += ' border: 0;\n';
  633. result += ' }\n';
  634. result += '}\n';
  635. result += '@page {\n';
  636. result += ' margin: 0;\n';
  637. result += ' size:' + page_box.width_px + 'px ' + page_box.height_px + 'px;\n';
  638. result += ' -webkit-print-color-adjust: exact;\n';
  639. result += '}\n';
  640. result += '</style>\n';
  641. return result;
  642. }
  643. public generateHtml(cards: Card[], options: Options) {
  644. options = options || new Options();
  645. var rows = options.page_rows || 3;
  646. var cols = options.page_columns || 3;
  647. // Generate the HTML for each card
  648. var generator = new CardHtmlGenerator();
  649. var pages: CardPageSet<string> = this._generatePages(cards, options, rows, cols, generator);
  650. // Wrap all pages in a <page> element
  651. var document = this._wrap(pages, options);
  652. // Generate the HTML for the page layout
  653. var style = this._generateStyle(options);
  654. // Wrap all pages in a <page> element and add CSS for the page size
  655. var result = "";
  656. result += style
  657. result += document;
  658. return result;
  659. }
  660. public insertInto(cards: Card[], options: Options, container: HTMLElement) {
  661. // Clear the previous content of the document
  662. while (container.hasChildNodes()) {
  663. container.removeChild(container.lastChild);
  664. }
  665. // Insert the HTML
  666. var html = this.generateHtml(cards, options);
  667. container.innerHTML = html;
  668. }
  669. }
  670. }