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 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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 || "ace";
  136. }
  137. public getIconBack(options: Options): string {
  138. return this.icon_back || this.icon || options.default_icon || "ace";
  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. return ind + '<card-icon src="/icons/' + src + '.svg"></card-icon>\n';
  212. }
  213. private _subtitle(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  214. var text = params[0] || "";
  215. return ind + '<card-subtitle>' + text + '</card-subtitle>\n';
  216. }
  217. private _ruler(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  218. return ind + '<card-rule></card-rule>\n';
  219. }
  220. private _boxes(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  221. var count = params[0] || 1;
  222. var size = params[1] || 3;
  223. return ind + '<card-boxes size="' + size + '" count="' + count + '"></card-boxes>\n';
  224. }
  225. private _property(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  226. var header = params[0] || "";
  227. var text = params[1] || "";
  228. var result = "";
  229. result += ind + '<card-property>\n';
  230. result += ind + ind0 + '<h4>' + header + '</h4>\n';
  231. result += ind + ind0 + '<p>' + text + '</p>\n';
  232. result += ind + '</card-property>\n';
  233. return result;
  234. }
  235. private _description(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  236. var header = params[0] || "";
  237. var text = params[1] || "";
  238. var result = "";
  239. result += ind + '<card-description>\n';
  240. result += ind + ind0 + '<h4>' + header + '</h4>\n';
  241. result += ind + ind0 + '<p>' + text + '</p>\n';
  242. result += ind + '</card-description>\n';
  243. return result;
  244. }
  245. private _text(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  246. var text = params[0] || "";
  247. var result = "";
  248. result += ind + '<card-description>\n';
  249. result += ind + ind0 + '<p>' + text + '</p>\n';
  250. result += ind + '</card-description>\n';
  251. return result;
  252. }
  253. private _dndstats(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  254. var stats = ["str", "dex", "con", "int", "wis", "cha"];
  255. var result = "";
  256. result += ind + '<card-dndstats';
  257. for (var i = 0; i < stats.length; ++i) {
  258. var value = params[i] || "";
  259. var stat = stats[i];
  260. result += ' ' + stat + '="' + value + '"';
  261. }
  262. result += '></card-dndstats>\n';
  263. return result;
  264. }
  265. private _bullet(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  266. var text = params[0] || "";
  267. return ind + '<card-bullet>' + text + '</card-bullet>\n';
  268. }
  269. private _section(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  270. var text = params[0] || "";
  271. return ind + '<card-section>' + text + '</card-section>\n';
  272. }
  273. private _fill(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  274. var size = params[0] || "1";
  275. return ind + '<card-fill size="' + size + '"></card-fill>\n';
  276. }
  277. private _unknown(params: string[], card: Card, options: Options, ind: string, ind0: string): string {
  278. var text = params.join(' | ');
  279. return ind + '<card-description><p>' + text + '</p></card-description>\n';
  280. }
  281. private _empty(params: string[], card: Card, options: Options, ind: string, ind0: string) {
  282. return '';
  283. }
  284. private _contents(contents: string[], card: Card, options: Options, ind: string, ind0: string): string {
  285. var result = "";
  286. result += ind + '<card-contents>\n';
  287. result += contents.map((value) => {
  288. var parts = splitParams(value);
  289. var name = parts[0];
  290. var params = parts.splice(1);
  291. var generator: ContentGeneratorFunction = null;
  292. switch (name) {
  293. case "subtitle": generator = this._subtitle; break;
  294. case "property": generator = this._property; break;
  295. case "rule": generator = this._ruler; break;
  296. case "ruler": generator = this._ruler; break;
  297. case "boxes": generator = this._boxes; break;
  298. case "description": generator = this._description; break;
  299. case "dndstats": generator = this._dndstats; break;
  300. case "text": generator = this._text; break;
  301. case "bullet": generator = this._bullet; break;
  302. case "fill": generator = this._fill; break;
  303. case "section": generator = this._section; break;
  304. case "disabled": generator = this._empty; break;
  305. case "": generator = this._empty; break;
  306. default: return this._unknown(parts, card, options, ind, ind0);
  307. }
  308. return generator(params, card, options, ind + ind0, ind);
  309. }).join("\n");
  310. result += ind + '</card-contents>\n';
  311. return result;
  312. }
  313. private _title(card: Card, options: Options, ind: string, ind0: string): string {
  314. var title = card.getTitle(options);
  315. var title_size = card.getTitleSize(options);
  316. var title_icon_text = card.getTitleIconText(options);
  317. var icon = card.getIconFront(options);
  318. var result = "";
  319. result += ind + '<card-title size="' + title_size + '">\n';
  320. result += ind + ind0 + '<h1>' + title + '</h1>\n';
  321. result += ind + ind0 + '<h2>' + title_icon_text + '</h2>\n';
  322. result += this._icon(icon, ind + ind0, ind0);
  323. result += ind + '</card-title>\n';
  324. return result;
  325. }
  326. private _card_front(card: Card, options: Options, ind: string, ind0: string): string {
  327. var result = "";
  328. result += this._title(card, options, ind + ind0, ind0);
  329. result += this._contents(card.contents, card, options, ind + ind0, ind0);
  330. return result;
  331. }
  332. private _card_back(card: Card, options: Options, ind: string, ind0: string): string {
  333. var icon = card.getIconBack(options);
  334. var result = "";
  335. result += ind + '<card-back>\n';
  336. result += this._icon(icon, ind + ind0, ind);
  337. result += ind + '</card-back>\n';
  338. return result;
  339. }
  340. private _card_empty(options: Options, ind: string, ind0: string): string {
  341. var result = "";
  342. result += ind + '<card-contents>\n';
  343. result += ind + '</card-contents>\n';
  344. return result;
  345. }
  346. private _card(options: Options, ind: string, ind0: string, content: string, color: string): string {
  347. var size = options.card_size || "25x35";
  348. var result = "";
  349. result += ind + '<rpg-card color="' + color + '" size="' + size + '">\n';
  350. result += content;
  351. result += ind + '</rpg-card>\n';
  352. return result;
  353. }
  354. /** Generates HTML for the front side of the given card */
  355. public card_front(card: Card, options: Options, indent: string): string {
  356. var content = this._card_front(card, options, "", indent);
  357. return this._card(options, "", indent, content, card.getColorFront(options));
  358. }
  359. /** Generates HTML for the back side of the given card */
  360. public card_back(card: Card, options: Options, indent: string): string {
  361. var content = this._card_back(card, options, "", indent);
  362. return this._card(options, "", indent, content, card.getColorBack(options));
  363. }
  364. /** Generates HTML for an empty given card */
  365. public card_empty(options: Options, indent: string): string {
  366. var content = this._card_empty(options, "", indent);
  367. return this._card(options, "", indent, content, options.empty_color);
  368. }
  369. }
  370. class CardPage<T> {
  371. rows: number;
  372. cols: number;
  373. cards: T[];
  374. constructor(rows: number, cols: number) {
  375. this.rows = rows;
  376. this.cols = cols;
  377. this.cards = [];
  378. }
  379. /** Returns an empty page with the same dimensions */
  380. public newPage(): CardPage<T> {
  381. return new CardPage<T>(this.rows, this.cols);
  382. }
  383. private _posToIndex(row: number, col: number): number {
  384. return row * this.cols + col;
  385. }
  386. /** Adds one card to the page */
  387. public addCard(card: T): void {
  388. if (this.capacity() === 0) {
  389. throw new Error("This page is full.");
  390. }
  391. this.cards.push(card);
  392. }
  393. /**
  394. Adds several copies of a card to the page.
  395. Returns the number of copies that did not fit on the page.
  396. */
  397. public addCards(card: T, count: number): number {
  398. while (this.capacity() > 0 && count > 0) {
  399. this.addCard(card);
  400. --count;
  401. }
  402. return count;
  403. }
  404. /** Fills all remaining slots on the current row with the given card */
  405. public fillRow(card: T): void {
  406. while (this.capacityRow() > 0) {
  407. this.addCard(card);
  408. }
  409. }
  410. /** Fills all remaining slots on the page with empty cards */
  411. public fillPage(card: T): void {
  412. while (this.capacity() > 0) {
  413. this.addCard(card);
  414. }
  415. }
  416. /** Empty slots on the page */
  417. public capacity(): number {
  418. return this.rows * this.cols - this.cards.length;
  419. }
  420. /** Empty slots on the current line */
  421. public capacityRow(): number {
  422. return this.capacity() % this.cols;
  423. }
  424. /** Flip card slots horizontally */
  425. public flipH() {
  426. if (this.capacity() > 0) {
  427. throw new Error("Cannot perform this operation while the page is not full");
  428. }
  429. for (var r = 0; r < this.rows; ++r) {
  430. for (var c = 0; c < Math.floor(this.cols / 2); ++c) {
  431. var indexL = this._posToIndex(r, c);
  432. var indexR = this._posToIndex(r, this.cols - c - 1);
  433. var cardL = this.cards[indexL];
  434. var cardR = this.cards[indexR];
  435. this.cards[indexL] = cardR;
  436. this.cards[indexR] = cardL;
  437. }
  438. }
  439. }
  440. }
  441. class CardPageSet<T> {
  442. rows: number;
  443. cols: number;
  444. pages: CardPage<T>[];
  445. constructor(rows: number, cols: number) {
  446. this.rows = rows;
  447. this.cols = cols;
  448. this.pages = [];
  449. }
  450. public lastPage(): CardPage<T> {
  451. if (this.pages.length === 0) {
  452. return null;
  453. } else {
  454. return this.pages[this.pages.length - 1];
  455. }
  456. }
  457. public addPage(): CardPage<T> {
  458. var newPage = new CardPage<T>(this.rows, this.cols);
  459. this.pages.push(newPage);
  460. return newPage;
  461. }
  462. /**
  463. Adds one card to the last page.
  464. Adds a new pages if necessary.
  465. */
  466. public addCard(card: T): void {
  467. var page = this.lastPage();
  468. if (page === null || page.capacity() === 0) {
  469. page = this.addPage();
  470. }
  471. page.addCard(card);
  472. }
  473. /**
  474. Adds several copies of a card to the last page.
  475. Adds new pages if necessary.
  476. */
  477. public addCards(card: T, count: number): void {
  478. for (var i = 0; i < count; ++i) {
  479. this.addCard(card);
  480. }
  481. }
  482. public forEach(fn: (page: CardPage<T>)=>void) {
  483. this.pages.forEach(fn);
  484. }
  485. public merge(other: CardPageSet<T>): CardPageSet<T> {
  486. if (this.pages.length !== other.pages.length) {
  487. throw new Error("This function is only for merging two equally sized page sets");
  488. }
  489. var result = new CardPageSet<T>(this.rows, this.cols);
  490. for (var i = 0; i < this.pages.length; ++i) {
  491. result.pages.push(this.pages[i]);
  492. result.pages.push(other.pages[i]);
  493. }
  494. return result;
  495. }
  496. }
  497. class BoxSize {
  498. page: string;
  499. width: string;
  500. height: string;
  501. width_px: number;
  502. height_px: number;
  503. constructor(p: string, w: string, h: string, wpx: number, hpx: number) {
  504. this.page = p;
  505. this.width = w;
  506. this.height = h;
  507. this.width_px = Math.floor(wpx);
  508. this.height_px = Math.floor(hpx);
  509. }
  510. }
  511. var boxSizes: { [id: string]: BoxSize } = {};
  512. boxSizes["auto"] = new BoxSize("auto", "auto", "auto", Infinity, Infinity);
  513. boxSizes["A2"] = new BoxSize("A2 portrait", "420mm", "594mm", 1587.401575, 2245.03937);
  514. boxSizes["A3"] = new BoxSize("A3 portrait", "297mm", "420mm", 1118.740158, 1587.401575);
  515. boxSizes["A4"] = new BoxSize("A4 portrait", "210mm", "297mm", 793.700787, 1118.740158);
  516. boxSizes["A5"] = new BoxSize("A5 portrait", "148mm", "210mm", 559.370079, 793.700787);
  517. boxSizes["Letter"] = new BoxSize("Letter portrait", "8.5in", "11in", 816, 1056);
  518. boxSizes["225x35"] = new BoxSize("2.25in 3.5in", "2.25in", "3.5in", 216, 336);
  519. boxSizes["25x35"] = new BoxSize("2.5in 3.5in", "2.5in", "3.5in", 240, 336);
  520. boxSizes["35x50"] = new BoxSize("3.5in 5.0in", "3.5in", "5.0in", 336, 480);
  521. boxSizes["50x70"] = new BoxSize("5.0in 7.0in", "5.0in", "7.0in", 480, 672);
  522. export class PageHtmlGenerator {
  523. indent: string;
  524. constructor() {
  525. this.indent = " ";
  526. }
  527. private _pageColor(page: number, options: Options): string {
  528. if ((options.card_arrangement == "doublesided") && (page % 2 == 1)) {
  529. return options.background_color;
  530. } else {
  531. return options.foreground_color;
  532. }
  533. }
  534. private _wrap(pageSet: CardPageSet<string>, options: Options) {
  535. var result = "";
  536. for (var i = 0; i < pageSet.pages.length; ++i) {
  537. var page = pageSet.pages[i];
  538. var style = ' style="background-color:' + this._pageColor(i, options) + '"';
  539. result += '<card-page' + style + '>\n';
  540. result += page.cards.join("");
  541. result += '</card-page>\n';
  542. }
  543. return result;
  544. }
  545. private _generatePagesDoublesided(cards: Card[], options: Options, rows: number, cols: number, generator: CardHtmlGenerator): CardPageSet<string> {
  546. var front_pages: CardPageSet<string> = new CardPageSet<string>(rows, cols);
  547. var back_pages: CardPageSet<string> = new CardPageSet<string>(rows, cols);
  548. var empty = generator.card_empty(options, this.indent);
  549. // Fill pages with cards
  550. for (var i = 0; i < cards.length; ++i) {
  551. var card = cards[i];
  552. var front = generator.card_front(card, options, this.indent);
  553. var back = generator.card_back(card, options, this.indent);
  554. front_pages.addCards(front, card.count);
  555. back_pages.addCards(back, card.count);
  556. }
  557. // Fill empty slots
  558. front_pages.forEach((page) => page.fillPage(empty));
  559. back_pages.forEach((page) => page.fillPage(empty));
  560. // Shuffle back cards so that they line up with their corresponding front cards
  561. back_pages.forEach((page) => page.flipH());
  562. // Interleave front and back pages so that we can print double-sided
  563. return front_pages.merge(back_pages);
  564. }
  565. private _generatePagesFrontOnly(cards: Card[], options: Options, rows: number, cols: number, generator: CardHtmlGenerator): CardPageSet<string> {
  566. var pages: CardPageSet<string> = new CardPageSet<string>(rows, cols);
  567. var empty = generator.card_empty(options, this.indent);
  568. // Fill pages with cards
  569. for (var i = 0; i < cards.length; ++i) {
  570. var card = cards[i];
  571. var front = generator.card_front(card, options, this.indent);
  572. pages.addCards(front, card.count);
  573. }
  574. // Fill empty slots
  575. pages.forEach((page) => page.fillPage(empty));
  576. return pages;
  577. }
  578. private _generatePagesSideBySide(cards: Card[], options: Options, rows: number, cols: number, generator: CardHtmlGenerator): CardPageSet<string> {
  579. if (cols < 2) {
  580. throw new Error("Need at least two columns for side-by-side");
  581. }
  582. var pages: CardPageSet<string> = new CardPageSet<string>(rows, cols);
  583. var empty = generator.card_empty(options, this.indent);
  584. // Fill pages with cards (two at a time)
  585. for (var i = 0; i < cards.length; ++i) {
  586. var card = cards[i];
  587. var front = generator.card_front(card, options, this.indent);
  588. var back = generator.card_back(card, options, this.indent);
  589. for (var j = 0; j < card.count; ++j) {
  590. if (pages.pages.length > 0 && pages.lastPage().capacityRow() < 2) {
  591. pages.lastPage().fillRow(empty);
  592. }
  593. pages.addCard(front);
  594. pages.addCard(back);
  595. }
  596. }
  597. // Fill empty slots
  598. pages.forEach((page) => page.fillPage(empty));
  599. return pages;
  600. }
  601. private _generatePages(cards: Card[], options: Options, rows: number, cols: number, generator: CardHtmlGenerator): CardPageSet<string> {
  602. switch (options.card_arrangement) {
  603. case "doublesided": return this._generatePagesDoublesided(cards, options, rows, cols, generator);
  604. case "front_only": return this._generatePagesFrontOnly(cards, options, rows, cols, generator);
  605. case "side_by_side": return this._generatePagesSideBySide(cards, options, rows, cols, generator);
  606. default: throw new Error("Unknown card arrangement");
  607. }
  608. }
  609. private _generateStyle(options) {
  610. var page_box = boxSizes[options.page_size] || boxSizes["auto"];
  611. var result = '';
  612. result += '<style type="text/css">\n';
  613. result += 'body {\n';
  614. result += ' display: block;\n';
  615. result += ' background: rgb(204, 204, 204);\n';
  616. result += '}\n';
  617. result += 'card-page {\n';
  618. result += ' width: ' + (page_box.width_px - 2) + 'px;\n';
  619. result += ' height: ' + (page_box.height_px - 2) + 'px;\n';
  620. result += '}\n';
  621. result += '@media print {\n';
  622. result += ' html, body {\n';
  623. result += ' width: ' + page_box.width_px + 'px;\n';
  624. result += ' }\n';
  625. result += ' body {\n';
  626. result += ' margin: 0;\n';
  627. result += ' padding: 0;\n';
  628. result += ' border: 0;\n';
  629. result += ' }\n';
  630. result += '}\n';
  631. result += '@page {\n';
  632. result += ' margin: 0;\n';
  633. result += ' size:' + page_box.width_px + 'px ' + page_box.height_px + 'px;\n';
  634. result += ' -webkit-print-color-adjust: exact;\n';
  635. result += '}\n';
  636. result += '</style>\n';
  637. return result;
  638. }
  639. public generateHtml(cards: Card[], options: Options) {
  640. options = options || new Options();
  641. var rows = options.page_rows || 3;
  642. var cols = options.page_columns || 3;
  643. // Generate the HTML for each card
  644. var generator = new CardHtmlGenerator();
  645. var pages: CardPageSet<string> = this._generatePages(cards, options, rows, cols, generator);
  646. // Wrap all pages in a <page> element
  647. var document = this._wrap(pages, options);
  648. // Generate the HTML for the page layout
  649. var style = this._generateStyle(options);
  650. // Wrap all pages in a <page> element and add CSS for the page size
  651. var result = "";
  652. result += style
  653. result += document;
  654. return result;
  655. }
  656. public insertInto(cards: Card[], options: Options, container: HTMLElement) {
  657. // Clear the previous content of the document
  658. while (container.hasChildNodes()) {
  659. container.removeChild(container.lastChild);
  660. }
  661. // Insert the HTML
  662. var html = this.generateHtml(cards, options);
  663. container.innerHTML = html;
  664. }
  665. }
  666. }