Timeline.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __importDefault = (this && this.__importDefault) || function (mod) {
  9. return (mod && mod.__esModule) ? mod : { "default": mod };
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.HorizontalResizer = void 0;
  13. const dayjs_1 = __importDefault(require("dayjs"));
  14. const lit_element_1 = require("lit-element");
  15. const style_map_1 = require("lit-html/directives/style-map");
  16. const unsafe_html_1 = require("lit-html/directives/unsafe-html");
  17. const simplebar_1 = __importDefault(require("simplebar"));
  18. const SimpleBar_styles_1 = require("./styles/SimpleBar.styles");
  19. const Event_1 = require("./Event");
  20. const Ressource_1 = require("./Ressource");
  21. var horizontal_resizer_1 = require("./components/horizontal-resizer");
  22. Object.defineProperty(exports, "HorizontalResizer", { enumerable: true, get: function () { return horizontal_resizer_1.HorizontalResizer; } });
  23. const syncroScroll_1 = __importDefault(require("./utils/syncroScroll"));
  24. const Timeline_style_1 = require("./styles/Timeline.style");
  25. let Timeline = class Timeline extends lit_element_1.LitElement {
  26. constructor(options = {}) {
  27. super();
  28. this._slotDuration = 30;
  29. this._legendSpan = 2;
  30. this.rowHeight = 32;
  31. this.slotWidth = 20;
  32. this.legendUnitFormat = {
  33. y: "YYYY",
  34. M: "MMMM",
  35. d: "D",
  36. h: "H[h]",
  37. m: "m'",
  38. s: "s[s]",
  39. };
  40. this._clearSelectionHandler = (_e) => {
  41. const exclusionZone = document.querySelectorAll("[jc-timeline-keep-select]");
  42. if (Array.from(exclusionZone).some((e) => e.contains(_e.target)))
  43. return;
  44. this.clearSelectedItems();
  45. const myEvent = new CustomEvent("item-selected", {
  46. detail: { items: [] },
  47. bubbles: true,
  48. composed: true,
  49. });
  50. this.dispatchEvent(myEvent);
  51. window.removeEventListener("click", this._clearSelectionHandler);
  52. };
  53. this.rows = [];
  54. this.items = [];
  55. this._start = dayjs_1.default().startOf("day");
  56. this._end = this._start.endOf("day");
  57. this.rowsTitle = "Ressources";
  58. this.ressourceWidth = 200;
  59. this.selectedList = [];
  60. this.legend = [];
  61. this.defaultBackground = "";
  62. this.customStyle = "";
  63. if (options.ressources) {
  64. this.addRessources(options.ressources);
  65. }
  66. if (options.items) {
  67. this.addEvents(options.items);
  68. }
  69. this.updateLegend();
  70. this.render();
  71. }
  72. static get styles() {
  73. return [Timeline_style_1.TimelineStyle, SimpleBar_styles_1.SimpleBarStyle];
  74. }
  75. get start() {
  76. return this._start.toISOString();
  77. }
  78. set start(value) {
  79. const d = dayjs_1.default(value);
  80. if (d.isValid()) {
  81. this._start = d < this._end ? d : this._end;
  82. this.updateLegend();
  83. }
  84. else {
  85. console.log("Invalid Date :", value);
  86. }
  87. }
  88. get end() {
  89. return this._end.toISOString();
  90. }
  91. set end(value) {
  92. const d = dayjs_1.default(value);
  93. if (d.isValid()) {
  94. this._end = this._start < d ? d : this._start;
  95. this.updateLegend();
  96. }
  97. }
  98. get slotDuration() {
  99. return this._slotDuration;
  100. }
  101. set slotDuration(value) {
  102. this._slotDuration = value;
  103. this.updateLegend();
  104. }
  105. get legendSpan() {
  106. return this._legendSpan;
  107. }
  108. set legendSpan(value) {
  109. this._legendSpan = value;
  110. this.updateLegend();
  111. }
  112. set defaultBackground(value) {
  113. this.style.setProperty("--default-background", value);
  114. }
  115. get defaultBackground() {
  116. return this.style.getPropertyValue("--default-background");
  117. }
  118. setLegendUnitFormatAll(legend) {
  119. this.legendUnitFormat = Object.assign(Object.assign({}, this.legendUnitFormat), legend);
  120. this.updateLegend();
  121. }
  122. setLegendUnitFormat(unit, format) {
  123. this.legendUnitFormat[unit] = format;
  124. this.updateLegend();
  125. }
  126. addRessources(list) {
  127. return list.map((r) => this.addRessource(r));
  128. }
  129. addRessource(ressource) {
  130. var _a;
  131. const existingRessource = this.getRessourceFromId(ressource.id);
  132. if (existingRessource) {
  133. return existingRessource;
  134. }
  135. const r = Ressource_1.Ressource.toRessource(ressource);
  136. if (r.parent !== undefined) {
  137. r.parent = (_a = this.getRessourceFromId(r.parent.id)) !== null && _a !== void 0 ? _a : this.addRessource(r.parent);
  138. const idx = this.rows.indexOf(r.parent);
  139. if (idx > -1) {
  140. this.rows[idx].children.push(r);
  141. this.rows.splice(idx + 1, 0, r);
  142. }
  143. else {
  144. throw new Error("Not able to create ressource parent.\n" + r.id);
  145. }
  146. }
  147. else {
  148. this.rows = [...this.rows, r];
  149. }
  150. this.addRessources(r.children);
  151. this._updateEventPosition(r);
  152. return r;
  153. }
  154. removeRessourceById(id) {
  155. return this._removeRessourceById(id);
  156. }
  157. _removeRessourceById(id, depth = 0) {
  158. const output = { ressources: [], items: [], index: 0 };
  159. for (let i = 0; i < this.rows.length; i) {
  160. const ressource = this.rows[i];
  161. if (ressource.id === id) {
  162. output.index = i;
  163. output.ressources.push(ressource);
  164. if (ressource.parent && depth === 0) {
  165. ressource.parent.children = ressource.parent.children.filter((o) => o.id !== id);
  166. }
  167. this.rows.splice(i, 1);
  168. }
  169. else if (ressource.parentId === id) {
  170. const partialOutput = this._removeRessourceById(ressource.id, depth + 1);
  171. output.ressources.push(...partialOutput.ressources);
  172. output.items.push(...partialOutput.items);
  173. }
  174. else {
  175. i++;
  176. }
  177. }
  178. output.items.push(...this.items.filter((i) => i.ressourceId === id));
  179. this.items = this.items.filter((i) => i.ressourceId !== id);
  180. return output;
  181. }
  182. getRessources() {
  183. return this.rows;
  184. }
  185. getRessourceFromId(id) {
  186. const tmp = this.rows.filter((r) => r.id === id);
  187. return tmp.length > 0 ? tmp[0] : null;
  188. }
  189. updateRessource(id, key, value) {
  190. const ressource = this.getRessourceFromId(id);
  191. if (ressource) {
  192. if (key == "parent") {
  193. const content = this.removeRessourceById(id);
  194. ressource[key] = value;
  195. this.addRessource(ressource);
  196. this.addRessources(content.ressources);
  197. this.addEvents(content.items);
  198. }
  199. else {
  200. ressource[key] = value;
  201. this.rows = this.rows.map((r) => (r.id == ressource.id ? ressource : r));
  202. }
  203. this._updateEventPosition(ressource);
  204. return ressource;
  205. }
  206. return null;
  207. }
  208. setRowsTitle(title) {
  209. this.rowsTitle = title;
  210. }
  211. getEventById(id) {
  212. return this.items.find((o) => o.id == id);
  213. }
  214. addEvents(list) {
  215. return list.map((e) => this.addEvent(e));
  216. }
  217. addEvent(event) {
  218. const existingEvent = this.getEventById(event.id);
  219. if (existingEvent) {
  220. return existingEvent;
  221. }
  222. const ressource = this.rows.find((r) => r.id === event.ressourceId);
  223. if (ressource === undefined) {
  224. return undefined;
  225. }
  226. const timeslot = Event_1.Event.toTimeSlot(event);
  227. this.items = [...this.items, timeslot];
  228. timeslot.isDisplayed =
  229. timeslot.end > this._start.toDate() || timeslot.start < this._end.toDate();
  230. this._updateEventPosition(ressource);
  231. return timeslot;
  232. }
  233. removeEventById(id) {
  234. const output = this.items.filter((o) => o.id === id);
  235. this.items = this.items.filter((o) => o.id !== id);
  236. return output;
  237. }
  238. updateEventById(id) {
  239. const output = this.removeEventById(id);
  240. if (output.length > 0) {
  241. this.addEvent(output[0]);
  242. return output[0];
  243. }
  244. else {
  245. return null;
  246. }
  247. }
  248. _updateEventPosition(ressource) {
  249. const timeslots = this.items.filter((i) => i.ressourceId === ressource.id);
  250. if (timeslots.length === 0) {
  251. ressource.height = this.rowHeight + (ressource.collapseChildren ? 5 : 0);
  252. return;
  253. }
  254. const start = this._start.toDate().getTime();
  255. const end = this._end.toDate().getTime();
  256. const points = [start, end];
  257. const populateInterval = (d) => {
  258. const t = d.getTime();
  259. if (start < t && t < end && !points.includes(t)) {
  260. points.push(t);
  261. }
  262. };
  263. timeslots.forEach((element) => {
  264. populateInterval(element.start);
  265. populateInterval(element.end);
  266. });
  267. points.sort();
  268. const intervals = [];
  269. for (let i = 0; i < points.length - 1; i++) {
  270. const startTime = points[i];
  271. const endTime = points[i + 1];
  272. intervals.push({
  273. start: points[i],
  274. end: points[i + 1],
  275. slots: timeslots.filter((slot) => slot.start.getTime() <= startTime && endTime <= slot.end.getTime()),
  276. });
  277. }
  278. const lineCount = intervals.reduce((acc, interval) => Math.max(acc, interval.slots.length), 0);
  279. ressource.height =
  280. this.rowHeight * Math.max(lineCount, 1) + (ressource.collapseChildren ? 5 : 0);
  281. const sortTimeslots = (a, b) => {
  282. const t = a.start.getTime() - b.start.getTime();
  283. if (t === 0) {
  284. const tend = b.end.getTime() - a.end.getTime();
  285. return tend === 0 ? ("" + a.id).localeCompare(b.id) : tend;
  286. }
  287. return t;
  288. };
  289. timeslots.forEach((slot) => (slot.offset = -1));
  290. timeslots.sort(sortTimeslots);
  291. timeslots[0].offset = 0;
  292. const potentialOffset = [];
  293. for (let i = 0; i < lineCount; i++) {
  294. potentialOffset.push(i);
  295. }
  296. intervals.forEach((intervals) => {
  297. intervals.slots.sort(sortTimeslots);
  298. const usedOffset = intervals.slots.map((o) => o.offset).filter((i) => i > -1);
  299. const availableOffset = potentialOffset.filter((i) => !usedOffset.includes(i));
  300. intervals.slots.forEach((slot) => {
  301. if (slot.offset === -1) {
  302. slot.offset = availableOffset.shift() || 0;
  303. }
  304. });
  305. });
  306. }
  307. getEvents() {
  308. return this.items;
  309. }
  310. updateLegend() {
  311. const legend = [];
  312. const legendUnitList = ["y", "M", "d", "h", "m", "s"];
  313. const legendMinUnitSpan = this.slotDuration * this.legendSpan;
  314. const deltaT = this._end.diff(this._start, "m");
  315. const nCol = Math.floor(deltaT / legendMinUnitSpan) + 1;
  316. for (const legendUnit of legendUnitList) {
  317. const currentDate = dayjs_1.default(this._start);
  318. const format = this.legendUnitFormat[legendUnit];
  319. let nextColumn = currentDate.add(legendMinUnitSpan, "m");
  320. const isLegendPossible = this._end.diff(this._start, legendUnit) > 0 &&
  321. (nextColumn.format(format) !== currentDate.format(format) ||
  322. currentDate.add(1, legendUnit).diff(currentDate, "m") >= legendMinUnitSpan);
  323. if (isLegendPossible) {
  324. let currentHeader = currentDate.format(format);
  325. const row = [];
  326. let i = 0;
  327. for (let j = 0; j < nCol; j++) {
  328. i += this.legendSpan;
  329. nextColumn = nextColumn.add(legendMinUnitSpan, "m");
  330. const nextHeader = nextColumn.format(format);
  331. if (currentHeader !== nextHeader) {
  332. row.push({
  333. colspan: i,
  334. title: currentHeader,
  335. });
  336. i = 0;
  337. currentHeader = nextHeader;
  338. }
  339. }
  340. if (i > 0) {
  341. row.push({
  342. colspan: i,
  343. title: currentHeader,
  344. });
  345. }
  346. legend.push(row);
  347. }
  348. }
  349. this.legend = legend;
  350. }
  351. _handleResizeX(e) {
  352. e.stopPropagation();
  353. this.ressourceWidth += e.detail;
  354. if (this.ressourceWidth < 0) {
  355. this.ressourceWidth = 0;
  356. }
  357. }
  358. _grabHeader(e) {
  359. const root = this.shadowRoot;
  360. if (root !== null) {
  361. const headerContainer = root.querySelector(".jc-timeline-grid-title-container");
  362. let lastPosX = e.clientX;
  363. const scroll = function (e) {
  364. const scrollLeft = lastPosX - e.clientX;
  365. headerContainer.scrollBy({ left: scrollLeft });
  366. lastPosX = e.clientX;
  367. };
  368. const mouseUpListener = function (_e) {
  369. window.removeEventListener("mousemove", scroll);
  370. window.removeEventListener("mouseup", mouseUpListener);
  371. };
  372. window.addEventListener("mousemove", scroll);
  373. window.addEventListener("mouseup", mouseUpListener);
  374. }
  375. }
  376. _getEventResizerHandler(slot, direction) {
  377. return (evt) => {
  378. evt.stopPropagation();
  379. evt.preventDefault();
  380. const startPos = evt.clientX;
  381. const localSlot = slot;
  382. const localDir = direction;
  383. const initialDate = slot[direction];
  384. const resizeListener = (e) => {
  385. const newDate = dayjs_1.default(initialDate)
  386. .add(Math.round((e.clientX - startPos) / this.slotWidth) * this.slotDuration, "m")
  387. .toDate();
  388. if (direction === "start" ? newDate < localSlot.end : localSlot.start < newDate) {
  389. if (localSlot[localDir] !== newDate) {
  390. localSlot[localDir] = newDate;
  391. this.updateEventById(slot.id);
  392. }
  393. }
  394. };
  395. const mouseUpListener = (_e) => {
  396. window.removeEventListener("mousemove", resizeListener);
  397. window.removeEventListener("mouseup", mouseUpListener);
  398. localSlot.moving = false;
  399. this.updateEventById(localSlot.id);
  400. if (initialDate !== localSlot[localDir]) {
  401. const cEvt = new CustomEvent("event-change", {
  402. detail: { items: [localSlot] },
  403. });
  404. this.dispatchEvent(cEvt);
  405. }
  406. };
  407. localSlot.moving = true;
  408. window.addEventListener("mousemove", resizeListener);
  409. window.addEventListener("mouseup", mouseUpListener);
  410. };
  411. }
  412. _getEventGrabHandler(slot, editable, ressourceEditable, callback) {
  413. return (evt) => {
  414. evt.stopPropagation();
  415. evt.preventDefault();
  416. const startPos = evt.clientX;
  417. let hasChanged = false;
  418. const localSlot = slot;
  419. let localSlots = this.selectedList
  420. .filter((s) => s instanceof Event_1.Event)
  421. .map((s) => s);
  422. if (!localSlots.includes(localSlot)) {
  423. localSlots = [localSlot];
  424. }
  425. const startDates = localSlots.map((slot) => slot.start);
  426. const endDates = localSlots.map((slot) => slot.end);
  427. const updatePosition = editable
  428. ? (e) => {
  429. const changeTime = Math.round((e.clientX - startPos) / this.slotWidth) * this.slotDuration;
  430. return localSlots
  431. .map((slot, index) => {
  432. const prevStart = slot.start;
  433. slot.start = dayjs_1.default(startDates[index]).add(changeTime, "m").toDate();
  434. slot.end = dayjs_1.default(endDates[index]).add(changeTime, "m").toDate();
  435. return prevStart.getTime() !== slot.start.getTime();
  436. })
  437. .reduce((prev, curr) => prev || curr);
  438. }
  439. : (_e) => {
  440. return false;
  441. };
  442. const updateRessource = ressourceEditable
  443. ? (e) => {
  444. var _a, _b, _c;
  445. const rowId = (_c = (_b = (_a = this.shadowRoot) === null || _a === void 0 ? void 0 : _a.elementsFromPoint(e.clientX, e.clientY).find((e) => e.tagName == "TD")) === null || _b === void 0 ? void 0 : _b.parentElement) === null || _c === void 0 ? void 0 : _c.getAttribute("row-id");
  446. if (rowId) {
  447. const ressourceId = this.rows[Number(rowId)].id;
  448. if (ressourceId !== localSlot.ressourceId) {
  449. const oldRessource = this.getRessourceFromId(localSlot.ressourceId);
  450. localSlot.ressourceId = ressourceId;
  451. this._updateEventPosition(oldRessource);
  452. return true;
  453. }
  454. }
  455. return false;
  456. }
  457. : (_e) => {
  458. return false;
  459. };
  460. const moveListener = (e) => {
  461. const a = updatePosition(e);
  462. if (updateRessource(e) || a) {
  463. hasChanged = true;
  464. this.updateEventById(localSlot.id);
  465. }
  466. };
  467. const mouseUpListener = (e) => {
  468. window.removeEventListener("mousemove", moveListener);
  469. window.removeEventListener("mouseup", mouseUpListener);
  470. localSlot.moving = false;
  471. this.updateEventById(slot.id);
  472. if (hasChanged) {
  473. const cEvt = new CustomEvent("event-change", {
  474. detail: { items: localSlots },
  475. });
  476. this.dispatchEvent(cEvt);
  477. }
  478. callback(e, hasChanged);
  479. };
  480. localSlot.moving = true;
  481. window.addEventListener("mousemove", moveListener);
  482. window.addEventListener("mouseup", mouseUpListener);
  483. return true;
  484. };
  485. }
  486. clearSelectedItems() {
  487. this.items.forEach((selectable) => {
  488. selectable.selected = false;
  489. this.updateEventById(selectable.id);
  490. });
  491. this.rows.forEach((selectable) => (selectable.selected = false));
  492. this.selectedList = [];
  493. }
  494. _getEventClickHandler(clickedItem) {
  495. const item = clickedItem;
  496. return (e, wasModified = false) => {
  497. e.stopPropagation();
  498. const idx = this.selectedList.indexOf(item);
  499. if (idx > -1) {
  500. if (!wasModified) {
  501. if (e.ctrlKey) {
  502. this.selectedList.splice(idx, 1);
  503. item.selected = false;
  504. this.updateEventById(item.id);
  505. }
  506. else {
  507. this.clearSelectedItems();
  508. }
  509. }
  510. }
  511. else {
  512. if (this.selectedList.length > 0 && !e.ctrlKey) {
  513. this.clearSelectedItems();
  514. }
  515. item.selected = true;
  516. this.selectedList.push(item);
  517. this.updateEventById(item.id);
  518. }
  519. const myEvent = new CustomEvent("item-selected", {
  520. detail: { items: this.selectedList },
  521. bubbles: true,
  522. composed: true,
  523. });
  524. if (this.selectedList.length > 0) {
  525. window.addEventListener("click", this._clearSelectionHandler);
  526. }
  527. this.dispatchEvent(myEvent);
  528. };
  529. }
  530. firstUpdated() {
  531. const root = this.shadowRoot;
  532. if (root !== null) {
  533. const gridContainer = root.querySelector(".jc-timeline-grid-container");
  534. const simplebar = new simplebar_1.default(gridContainer).getScrollElement();
  535. syncroScroll_1.default([simplebar, root.querySelector(".jc-timeline-grid-title-container")], "h");
  536. syncroScroll_1.default([simplebar, root.querySelector(".jc-timeline-rows")], "v");
  537. }
  538. if (this.defaultBackground === "") {
  539. this.style.setProperty("--default-background", "SteelBlue");
  540. }
  541. }
  542. renderTimeslot(evt) {
  543. if (!evt.isDisplayed) {
  544. return lit_element_1.html ``;
  545. }
  546. let rowTop = 0;
  547. let ressource;
  548. let i;
  549. for (i = 0; i < this.rows.length && this.rows[i].id !== evt.ressourceId; i++) {
  550. ressource = this.rows[i];
  551. if (ressource.show) {
  552. rowTop += ressource.height ? ressource.height : this.rowHeight;
  553. }
  554. }
  555. ressource = this.rows[i];
  556. const minute2pixel = this.slotWidth / this.slotDuration;
  557. const left = dayjs_1.default(evt.start).diff(this._start, "m") * minute2pixel;
  558. const right = -dayjs_1.default(evt.end).diff(this._end, "m") * minute2pixel;
  559. const style = {
  560. height: this.rowHeight - 4 + "px",
  561. top: rowTop + evt.offset * this.rowHeight + "px",
  562. left: left + "px",
  563. right: right + "px",
  564. backgroundColor: "",
  565. };
  566. const bgColor = evt.bgColor ? evt.bgColor : ressource.eventBgColor;
  567. if (bgColor) {
  568. style.backgroundColor = bgColor;
  569. }
  570. if (!ressource.show) {
  571. style.height = "";
  572. style.top = rowTop - 6 + "px";
  573. return lit_element_1.html `<div class="jc-timeslot empty" style="${style_map_1.styleMap(style)}"></div>`;
  574. }
  575. let content = lit_element_1.html `<div class="jc-timeslot-title">${evt.title}</div>
  576. ${evt.content ? unsafe_html_1.unsafeHTML(evt.content) : ""}`;
  577. const resizer = evt.editable === null ? ressource.eventEditable : evt.editable;
  578. const editableRessource = evt.ressourceEditable === null ? ressource.eventRessourceEditable : evt.ressourceEditable;
  579. if (resizer) {
  580. content = lit_element_1.html `<div
  581. class="jc-timeslot-resizer-start"
  582. @mousedown="${this._getEventResizerHandler(evt, "start")}"
  583. ></div>
  584. ${content}
  585. <div
  586. class="jc-timeslot-resizer-end"
  587. @mousedown="${this._getEventResizerHandler(evt, "end")}"
  588. ></div>`;
  589. }
  590. return lit_element_1.html `<div
  591. class="jc-timeslot ${evt.moving ? "moving" : ""} ${evt.selected ? "selected" : ""}"
  592. start="${evt.start.getHours()}"
  593. end="${evt.end.getHours()}"
  594. style="${style_map_1.styleMap(style)}"
  595. @mousedown="${this._getEventGrabHandler(evt, resizer, editableRessource, this._getEventClickHandler(evt))}"
  596. @click="${(e) => e.stopPropagation()}"
  597. >
  598. ${content}
  599. </div>`;
  600. }
  601. _getCollapseRessourceHandler(item) {
  602. return (_e) => {
  603. item.collapseChildren = !item.collapseChildren;
  604. this._updateEventPosition(item);
  605. this.rows = [...this.rows];
  606. };
  607. }
  608. _onRessourceDragStart(item) {
  609. return (event) => {
  610. var _a;
  611. (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.setData("text", item.id);
  612. };
  613. }
  614. _onRessourceDragEnter(event) {
  615. if (event.target instanceof HTMLElement) {
  616. const tgt = event.target;
  617. tgt.classList.add("target");
  618. }
  619. }
  620. _onRessourceDragLeave(event) {
  621. if (event.target instanceof HTMLElement) {
  622. event.target.classList.remove("target");
  623. }
  624. }
  625. _onRessourceDrop(event) {
  626. var _a, _b;
  627. event.preventDefault();
  628. if (event.target instanceof HTMLElement) {
  629. event.target.classList.remove("target");
  630. const srcId = (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.getData("text");
  631. const destinationId = (_b = event.target.parentElement) === null || _b === void 0 ? void 0 : _b.getAttribute("ressourceId");
  632. if (srcId && destinationId && destinationId !== srcId) {
  633. const src = this.getRessourceFromId(srcId);
  634. const destination = this.getRessourceFromId(destinationId);
  635. if (destination.descendantOf(src)) {
  636. return;
  637. }
  638. const movedContent = this.removeRessourceById(src.id);
  639. if (event.target.classList.contains("jc-ressource")) {
  640. movedContent.ressources[0].parent = destination;
  641. }
  642. else {
  643. movedContent.ressources[0].parent = destination.parent;
  644. let idx = this.rows.findIndex((v) => v.id === destinationId);
  645. if (event.target.classList.contains("jc-ressource-below")) {
  646. idx += 1;
  647. while (idx < this.rows.length && this.rows[idx].descendantOf(destination)) {
  648. idx += 1;
  649. }
  650. }
  651. const arr = this.rows;
  652. this.rows = [...arr.splice(0, idx), src, ...arr];
  653. }
  654. this.addRessources(movedContent.ressources);
  655. this.addEvents(movedContent.items);
  656. this.dispatchEvent(new CustomEvent("reorder-ressource", {
  657. detail: { ressources: this.rows },
  658. }));
  659. }
  660. }
  661. }
  662. renderRessource(item) {
  663. const depth = item.depth;
  664. const style = `--depth:${depth};` + (item.height ? `height:${item.height}px;` : "");
  665. const hasChild = item.children.length > 0;
  666. const collapseHandler = this._getCollapseRessourceHandler(item);
  667. return lit_element_1.html `<tr>
  668. <td
  669. class="${item.selected ? "jc-ressource-selected" : ""}"
  670. style="${style}"
  671. ressourceId="${item.id}"
  672. @click="${this._getEventClickHandler(item)}"
  673. >
  674. <div class="jc-ressource-above"></div>
  675. <div class="jc-ressource" draggable="true" @dragstart="${this._onRessourceDragStart(item)}">
  676. ${Array(depth)
  677. .fill(0)
  678. .map((_i) => lit_element_1.html `<i class="jc-spacer"></i>`)}${hasChild
  679. ? lit_element_1.html `<i
  680. class="jc-spacer ${item.collapseChildren ? "extend" : "collapse"}"
  681. @click="${collapseHandler}"
  682. ></i>`
  683. : lit_element_1.html `<i class="jc-spacer"></i>`}
  684. <span>${item.title}</span>
  685. </div>
  686. <div class="jc-ressource-below"></div>
  687. </td>
  688. </tr>`;
  689. }
  690. renderGridRow(columns, rowId = -1, height = 30) {
  691. return lit_element_1.html `<tr row-id="${rowId}">
  692. ${columns.map((d, i) => lit_element_1.html `<td
  693. style="height:${height}px;"
  694. class="jc-slot ${i % this.legendSpan === 0 ? "jc-major-slot" : ""}"
  695. start="${d.toISOString()}"
  696. >
  697. &nbsp;
  698. </td>`)}
  699. </tr>`;
  700. }
  701. render() {
  702. const nCol = Math.floor(this._end.diff(this._start, "m") / this.slotDuration) + 1;
  703. const columns = [];
  704. for (let i = 0; i < nCol; i++) {
  705. columns.push(this._start.add(this.slotDuration * i, "m"));
  706. }
  707. const displayedRows = this.rows
  708. .map((r, i) => {
  709. return { i: i, r: r };
  710. })
  711. .filter((o) => o.r.show);
  712. return lit_element_1.html `
  713. <style>
  714. ${this.customStyle}
  715. </style>
  716. <div
  717. class="jc-timeline-header"
  718. style=${style_map_1.styleMap({
  719. "--width": this.ressourceWidth + "px",
  720. })}
  721. >
  722. <div class="jc-timeline-rows-title">${this.rowsTitle}</div>
  723. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  724. <div class="jc-timeline-grid-title-container">
  725. <table @mousedown="${this._grabHeader}" style="width:${nCol * this.slotWidth}px;">
  726. <colgroup>
  727. ${columns.map((_) => lit_element_1.html `<col style="min-width:${this.slotWidth}px" />`)}
  728. </colgroup>
  729. <tbody>
  730. ${this.legend.map((arr) => lit_element_1.html `<tr class="jc-timeline-grid-title">
  731. ${arr.map((o) => lit_element_1.html `<th colspan="${o.colspan}"><div>${o.title}</div></th>`)}
  732. </tr>`)}
  733. </tbody>
  734. </table>
  735. </div>
  736. </div>
  737. <div
  738. class="jc-timeline-content"
  739. style="${style_map_1.styleMap({ "--width": this.ressourceWidth + "px" })}"
  740. >
  741. <table
  742. class="jc-timeline-rows"
  743. @dragover="${(e) => e.preventDefault()}"
  744. @dragenter="${this._onRessourceDragEnter}"
  745. @dragleave="${this._onRessourceDragLeave}"
  746. @drop="${this._onRessourceDrop}"
  747. >
  748. ${this.rows.length > 0
  749. ? displayedRows.map((o) => this.renderRessource(o.r))
  750. : lit_element_1.html `<tr class="empty">
  751. <td>No ressource</td>
  752. </tr>`}
  753. </table>
  754. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  755. <div class="jc-timeline-grid-container">
  756. <table style="width:${nCol * this.slotWidth}px;">
  757. <colgroup>
  758. ${columns.map((_) => lit_element_1.html `<col style="min-width:${this.slotWidth}px" />`)}
  759. </colgroup>
  760. <tbody>
  761. ${this.rows.length > 0
  762. ? displayedRows.map((o) => this.renderGridRow(columns, o.i, o.r.height))
  763. : this.renderGridRow(columns)}
  764. </tbody>
  765. </table>
  766. <div class="jc-timeslots" style="width:${nCol * this.slotWidth}px;">
  767. ${this.items.map((slot) => this.renderTimeslot(slot))}
  768. </div>
  769. </div>
  770. </div>
  771. `;
  772. }
  773. };
  774. __decorate([
  775. lit_element_1.internalProperty()
  776. ], Timeline.prototype, "rows", void 0);
  777. __decorate([
  778. lit_element_1.internalProperty()
  779. ], Timeline.prototype, "items", void 0);
  780. __decorate([
  781. lit_element_1.property({ type: Number })
  782. ], Timeline.prototype, "ressourceWidth", void 0);
  783. __decorate([
  784. lit_element_1.property({ type: Object })
  785. ], Timeline.prototype, "_start", void 0);
  786. __decorate([
  787. lit_element_1.property({ type: String })
  788. ], Timeline.prototype, "start", null);
  789. __decorate([
  790. lit_element_1.property({ type: String })
  791. ], Timeline.prototype, "end", null);
  792. __decorate([
  793. lit_element_1.property({ type: Number })
  794. ], Timeline.prototype, "slotDuration", null);
  795. __decorate([
  796. lit_element_1.property({ type: Number })
  797. ], Timeline.prototype, "legendSpan", null);
  798. __decorate([
  799. lit_element_1.property({ type: Number })
  800. ], Timeline.prototype, "rowHeight", void 0);
  801. __decorate([
  802. lit_element_1.property({ type: Number })
  803. ], Timeline.prototype, "slotWidth", void 0);
  804. __decorate([
  805. lit_element_1.property({ type: String })
  806. ], Timeline.prototype, "rowsTitle", void 0);
  807. __decorate([
  808. lit_element_1.property({ type: Array })
  809. ], Timeline.prototype, "legend", void 0);
  810. __decorate([
  811. lit_element_1.property({ type: String })
  812. ], Timeline.prototype, "defaultBackground", null);
  813. Timeline = __decorate([
  814. lit_element_1.customElement("jc-timeline")
  815. ], Timeline);
  816. exports.default = Timeline;
  817. //# sourceMappingURL=Timeline.js.map