Timeline.js 32 KB

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