Timeline.js 33 KB

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