Timeline.js 32 KB

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