Timeline.js 33 KB

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