index.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  2. import { formats } from "dayjs/locale/*";
  3. import Ressource from "../src/Ressource";
  4. import TimeLine from "../src/Timeline";
  5. import "../src/Ressource";
  6. import { IEvent } from "../src/Event";
  7. import dayjs from "dayjs";
  8. const container = document.createElement("div");
  9. document.body.appendChild(container);
  10. // TimeLine.extendStyle.push(new_css)
  11. const emptyTimeline: TimeLine = document.createElement("jc-timeline") as TimeLine;
  12. let title = document.createElement("h2");
  13. title.innerText = "Empty Timeline";
  14. container.appendChild(title);
  15. container.appendChild(emptyTimeline);
  16. emptyTimeline.setLegendUnitFormat("d", "dddd D MMMM");
  17. emptyTimeline.setAttribute("legendSpan", "2");
  18. emptyTimeline.setAttribute("slotduration", "30");
  19. emptyTimeline.setAttribute("slotWidth", "30");
  20. emptyTimeline.end = "2021-09-12T15:59:59.999Z";
  21. emptyTimeline.start = "2021-09-10T13:00:00.000Z";
  22. const timeline: TimeLine = document.createElement("jc-timeline") as TimeLine;
  23. timeline.customStyle = `.bubble{
  24. height: 20px;
  25. width: 20px;
  26. display: flex;
  27. justify-content: center;
  28. align-items: center;
  29. position: absolute;
  30. right: -4px;
  31. bottom: -4px;
  32. border-radius: 50%;
  33. font-size: 12px;
  34. font-weight: bold;
  35. background: green;
  36. }
  37. bubble.red{
  38. background: red;
  39. }
  40. bubble.orange{
  41. background: orange;
  42. }`;
  43. // timeline.defaultBackground = "maroon";
  44. const rows: Array<Ressource> = [
  45. { id: "1", title: "Ressource 1 DarkSlate", eventBgColor: "DarkSlateBlue" },
  46. { id: "3", title: "Ressource 3" },
  47. { id: "4", title: "Ressource 4" },
  48. { id: "5", title: "Ressource 5" },
  49. ].map((i) => new Ressource(i));
  50. rows[2].parent = rows[1];
  51. rows[0].parent = rows[1];
  52. const data = dayjs().startOf("hour").hour(9);
  53. const timeslots: Array<IEvent> = [
  54. {
  55. id: "1",
  56. title: "Fixed ressource",
  57. ressourceId: "3",
  58. start: data.subtract(3, "h").toDate(),
  59. end: data.toDate(),
  60. ressourceEditable: false,
  61. bgColor: "darkgreen",
  62. },
  63. {
  64. id: "2",
  65. title: "Fixed time",
  66. ressourceId: "3",
  67. start: data.toDate(),
  68. end: data.endOf("hour").add(1, "h").toDate(),
  69. editable: false,
  70. bgColor: "FireBrick",
  71. },
  72. {
  73. id: "3",
  74. ressourceId: "4",
  75. start: data.add(1, "h").toDate(),
  76. end: data.endOf("hour").add(2, "h").toDate(),
  77. content: '<div class="bubble">0</div>',
  78. },
  79. {
  80. id: "4",
  81. ressourceId: "3",
  82. start: data.startOf("day").subtract(1, "h").toDate(),
  83. end: dayjs().endOf("hour").add(1, "h").toDate(),
  84. },
  85. ];
  86. for (let i = 5; i < 10; i++) {
  87. timeslots.push({
  88. id: i.toFixed(0),
  89. title: "event " + i,
  90. ressourceId: "1",
  91. start: data.add(i, "h").toDate(),
  92. end: data.add(i, "h").endOf("hour").add(1, "h").toDate(),
  93. });
  94. }
  95. timeline.addRessource(rows[1]);
  96. timeline.addRessource(rows[0]);
  97. timeline.addRessource(rows[2]);
  98. timeline.addRessource(rows[3]);
  99. timeline.addEvents(timeslots);
  100. timeline.addEventListener("item-selected", (e) => {
  101. console.log((e as CustomEvent).detail);
  102. });
  103. timeline.addEventListener("change-event", (e) => {
  104. console.log((e as CustomEvent).detail);
  105. });
  106. timeline.addEventListener("reorder-ressource", (e) => {
  107. console.log((e as CustomEvent).detail);
  108. });
  109. title = document.createElement("h2");
  110. title.innerText = "Generic Timeline";
  111. container.appendChild(title);
  112. container.appendChild(timeline);
  113. const nestedTimeline: TimeLine = document.createElement("jc-timeline") as TimeLine;
  114. title = document.createElement("h2");
  115. title.innerText = "Multiple level Timeline";
  116. container.appendChild(title);
  117. container.appendChild(nestedTimeline);
  118. const ressources = Array(8)
  119. .fill(0)
  120. .map((_, i) => new Ressource({ id: "" + i, title: "level " + i }));
  121. for (let i = 0; i < ressources.length - 1; i++) {
  122. ressources[i + 1].parent = ressources[i];
  123. }
  124. ressources.forEach((r, idx) => nestedTimeline.addRessource(r, idx));
  125. ressources[0].collapseChildren = true;
  126. const nItem = 32;
  127. const timeslots2 = Array(nItem)
  128. .fill(0)
  129. .map((_, i) => {
  130. return {
  131. id: "" + i,
  132. ressourceId: "" + (i % 8),
  133. title: "Item " + i,
  134. start: data.add(i / 2 - 5, "h").toDate(),
  135. end: data.add(i / 2 - 4, "h").toDate(),
  136. bgColor: "hsl(" + Math.round((i / (nItem - 1)) * 360) + ", 100%, 50%)",
  137. };
  138. });
  139. nestedTimeline.addEvents(timeslots2);
  140. nestedTimeline.style.display = "flex";
  141. nestedTimeline.style.flexDirection = "column";
  142. nestedTimeline.style.height = "200px";
  143. const nestedInsertionTest: TimeLine = document.createElement("jc-timeline") as TimeLine;
  144. title = document.createElement("h2");
  145. title.innerText = "Insertion Test Timeline";
  146. container.appendChild(title);
  147. container.appendChild(nestedInsertionTest);
  148. const f = (id: string, p: Ressource, offset: Offset = "above") => {
  149. nestedInsertionTest.addRessource(
  150. new Ressource({ id: offset + " " + id, parent: p }),
  151. nestedInsertionTest.getRessources().findIndex((o) => o.id == id) + (offset == "above" ? 0 : 1)
  152. );
  153. };
  154. const addChild = function (p, id) {
  155. const child = new Ressource({ id, parent: p });
  156. nestedInsertionTest.addRessource(child);
  157. f(id, p);
  158. f(id, p, "below");
  159. return child;
  160. };
  161. type Offset = "above" | "below";
  162. for (let i = 0; i < 2; i++) {
  163. const parentRessource = new Ressource({ id: "Parent-" + i });
  164. const child = addChild(parentRessource, "child " + i);
  165. const grandchild = addChild(child, "grand-child " + i + ".1");
  166. addChild(child, "grand-child " + i + ".2");
  167. addChild(grandchild, "gg-child " + i);
  168. }
  169. const p: HTMLDivElement = document.createElement("p");
  170. p.innerHTML = nestedInsertionTest
  171. .getRessources()
  172. .map((o) => o.id)
  173. .join(", ");
  174. container.appendChild(p);