Timeline.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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. };
  475. }
  476. clearSelectedItems() {
  477. this.items.forEach((selectable) => {
  478. selectable.selected = false;
  479. this.updateEventById(selectable.id);
  480. });
  481. this.rows.forEach((selectable) => (selectable.selected = false));
  482. this.selectedList = [];
  483. }
  484. _getEventClickHandler(clickedItem) {
  485. const item = clickedItem;
  486. return (e, wasModified = false) => {
  487. e.stopPropagation();
  488. const idx = this.selectedList.indexOf(item);
  489. if (idx > -1) {
  490. if (!wasModified) {
  491. if (e.ctrlKey) {
  492. this.selectedList.splice(idx, 1);
  493. item.selected = false;
  494. this.updateEventById(item.id);
  495. }
  496. else {
  497. this.clearSelectedItems();
  498. }
  499. }
  500. }
  501. else {
  502. if (this.selectedList.length > 0 && !e.ctrlKey) {
  503. this.clearSelectedItems();
  504. }
  505. item.selected = true;
  506. this.selectedList.push(item);
  507. this.updateEventById(item.id);
  508. }
  509. const myEvent = new CustomEvent("item-selected", {
  510. detail: { items: this.selectedList },
  511. bubbles: true,
  512. composed: true,
  513. });
  514. this.dispatchEvent(myEvent);
  515. };
  516. }
  517. firstUpdated() {
  518. const root = this.shadowRoot;
  519. if (root !== null) {
  520. const gridContainer = root.querySelector(".jc-timeline-grid-container");
  521. const simplebar = new simplebar_1.default(gridContainer).getScrollElement();
  522. syncroScroll_1.default([simplebar, root.querySelector(".jc-timeline-grid-title-container")], "h");
  523. syncroScroll_1.default([simplebar, root.querySelector(".jc-timeline-rows")], "v");
  524. }
  525. if (this.defaultBackground === "") {
  526. this.style.setProperty("--default-background", "SteelBlue");
  527. }
  528. }
  529. renderTimeslot(evt) {
  530. if (!evt.isDisplayed) {
  531. return lit_element_1.html ``;
  532. }
  533. let rowTop = 0;
  534. let ressource;
  535. let i;
  536. for (i = 0; i < this.rows.length && this.rows[i].id !== evt.ressourceId; i++) {
  537. ressource = this.rows[i];
  538. if (ressource.show) {
  539. rowTop += ressource.height ? ressource.height : this.rowHeight;
  540. }
  541. }
  542. ressource = this.rows[i];
  543. const minute2pixel = this.slotWidth / this.slotDuration;
  544. const left = dayjs_1.default(evt.start).diff(this._start, "m") * minute2pixel;
  545. const right = -dayjs_1.default(evt.end).diff(this._end, "m") * minute2pixel;
  546. const style = {
  547. height: this.rowHeight - 4 + "px",
  548. top: rowTop + evt.offset * this.rowHeight + "px",
  549. left: left + "px",
  550. right: right + "px",
  551. backgroundColor: "",
  552. };
  553. const bgColor = evt.bgColor ? evt.bgColor : ressource.eventBgColor;
  554. if (bgColor) {
  555. style.backgroundColor = bgColor;
  556. }
  557. if (!ressource.show) {
  558. style.height = "";
  559. style.top = rowTop - 6 + "px";
  560. return lit_element_1.html `<div class="jc-timeslot empty" style="${style_map_1.styleMap(style)}"></div>`;
  561. }
  562. let content = lit_element_1.html `<div class="jc-timeslot-title">${evt.title}</div>
  563. ${evt.content ? unsafe_html_1.unsafeHTML(evt.content) : ""}`;
  564. const resizer = evt.editable === null ? ressource.eventEditable : evt.editable;
  565. const editableRessource = evt.ressourceEditable === null ? ressource.eventRessourceEditable : evt.ressourceEditable;
  566. if (resizer) {
  567. content = lit_element_1.html `<div
  568. class="jc-timeslot-resizer-start"
  569. @mousedown="${this._getEventResizerHandler(evt, "start")}"
  570. ></div>
  571. ${content}
  572. <div
  573. class="jc-timeslot-resizer-end"
  574. @mousedown="${this._getEventResizerHandler(evt, "end")}"
  575. ></div>`;
  576. }
  577. return lit_element_1.html `<div
  578. class="jc-timeslot ${evt.moving ? "moving" : ""} ${evt.selected ? "selected" : ""}"
  579. start="${evt.start.getHours()}"
  580. end="${evt.end.getHours()}"
  581. style="${style_map_1.styleMap(style)}"
  582. @mousedown="${this._getEventGrabHandler(evt, resizer, editableRessource, this._getEventClickHandler(evt))}"
  583. >
  584. ${content}
  585. </div>`;
  586. }
  587. _getCollapseRessourceHandler(item) {
  588. return (_e) => {
  589. item.collapseChildren = !item.collapseChildren;
  590. this._updateEventPosition(item);
  591. this.rows = [...this.rows];
  592. };
  593. }
  594. _onRessourceDragStart(item) {
  595. return (event) => {
  596. var _a;
  597. (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.setData("text", item.id);
  598. };
  599. }
  600. _onRessourceDragEnter(event) {
  601. if (event.target instanceof HTMLElement) {
  602. const tgt = event.target;
  603. tgt.classList.add("target");
  604. }
  605. }
  606. _onRessourceDragLeave(event) {
  607. if (event.target instanceof HTMLElement) {
  608. event.target.classList.remove("target");
  609. }
  610. }
  611. _onRessourceDrop(event) {
  612. var _a, _b;
  613. event.preventDefault();
  614. if (event.target instanceof HTMLElement) {
  615. event.target.classList.remove("target");
  616. const srcId = (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.getData("text");
  617. const destinationId = (_b = event.target.parentElement) === null || _b === void 0 ? void 0 : _b.getAttribute("ressourceId");
  618. if (srcId && destinationId && destinationId !== srcId) {
  619. const src = this.getRessourceFromId(srcId);
  620. const destination = this.getRessourceFromId(destinationId);
  621. if (destination.descendantOf(src)) {
  622. return;
  623. }
  624. const movedContent = this.removeRessourceById(src.id);
  625. if (event.target.classList.contains("jc-ressource")) {
  626. movedContent.ressources[0].parent = destination;
  627. }
  628. else {
  629. movedContent.ressources[0].parent = destination.parent;
  630. let idx = this.rows.findIndex((v) => v.id === destinationId);
  631. if (event.target.classList.contains("jc-ressource-below")) {
  632. idx += 1;
  633. while (idx < this.rows.length && this.rows[idx].descendantOf(destination)) {
  634. idx += 1;
  635. }
  636. }
  637. const arr = this.rows;
  638. this.rows = [...arr.splice(0, idx), src, ...arr];
  639. }
  640. this.addRessources(movedContent.ressources);
  641. this.addEvents(movedContent.items);
  642. this.dispatchEvent(new CustomEvent("reorder-ressource", {
  643. detail: { ressources: this.rows },
  644. }));
  645. }
  646. }
  647. }
  648. renderRessource(item) {
  649. const depth = item.depth;
  650. const style = `--depth:${depth};` + (item.height ? `height:${item.height}px;` : "");
  651. const hasChild = item.children.length > 0;
  652. const collapseHandler = this._getCollapseRessourceHandler(item);
  653. return lit_element_1.html `<tr>
  654. <td
  655. class="${item.selected ? "jc-ressource-selected" : ""}"
  656. style="${style}"
  657. ressourceId="${item.id}"
  658. @click="${this._getEventClickHandler(item)}"
  659. >
  660. <div class="jc-ressource-above"></div>
  661. <div class="jc-ressource" draggable="true" @dragstart="${this._onRessourceDragStart(item)}">
  662. ${Array(depth)
  663. .fill(0)
  664. .map((_i) => lit_element_1.html `<i class="jc-spacer"></i>`)}${hasChild
  665. ? lit_element_1.html `<i
  666. class="jc-spacer ${item.collapseChildren ? "extend" : "collapse"}"
  667. @click="${collapseHandler}"
  668. ></i>`
  669. : lit_element_1.html `<i class="jc-spacer"></i>`}
  670. <span>${item.title}</span>
  671. </div>
  672. <div class="jc-ressource-below"></div>
  673. </td>
  674. </tr>`;
  675. }
  676. renderGridRow(columns, rowId = -1, height = 30) {
  677. return lit_element_1.html `<tr row-id="${rowId}">
  678. ${columns.map((d, i) => lit_element_1.html `<td
  679. style="height:${height}px;"
  680. class="jc-slot ${i % this.legendSpan === 0 ? "jc-major-slot" : ""}"
  681. start="${d.toISOString()}"
  682. >
  683. &nbsp;
  684. </td>`)}
  685. </tr>`;
  686. }
  687. render() {
  688. const nCol = Math.floor(this._end.diff(this._start, "m") / this.slotDuration) + 1;
  689. const columns = [];
  690. for (let i = 0; i < nCol; i++) {
  691. columns.push(this._start.add(this.slotDuration * i, "m"));
  692. }
  693. const displayedRows = this.rows
  694. .map((r, i) => {
  695. return { i: i, r: r };
  696. })
  697. .filter((o) => o.r.show);
  698. return lit_element_1.html `
  699. <style>
  700. ${this.customStyle}
  701. </style>
  702. <div
  703. class="jc-timeline-header"
  704. style=${style_map_1.styleMap({
  705. "--width": this.ressourceWidth + "px",
  706. })}
  707. >
  708. <div class="jc-timeline-rows-title">${this.rowsTitle}</div>
  709. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  710. <div class="jc-timeline-grid-title-container">
  711. <table @mousedown="${this._grabHeader}" style="width:${nCol * this.slotWidth}px;">
  712. <colgroup>
  713. ${columns.map((_) => lit_element_1.html `<col style="min-width:${this.slotWidth}px" />`)}
  714. </colgroup>
  715. <tbody>
  716. ${this.legend.map((arr) => lit_element_1.html `<tr class="jc-timeline-grid-title">
  717. ${arr.map((o) => lit_element_1.html `<th colspan="${o.colspan}"><div>${o.title}</div></th>`)}
  718. </tr>`)}
  719. </tbody>
  720. </table>
  721. </div>
  722. </div>
  723. <div
  724. class="jc-timeline-content"
  725. style="${style_map_1.styleMap({ "--width": this.ressourceWidth + "px" })}"
  726. >
  727. <table
  728. class="jc-timeline-rows"
  729. @dragover="${(e) => e.preventDefault()}"
  730. @dragenter="${this._onRessourceDragEnter}"
  731. @dragleave="${this._onRessourceDragLeave}"
  732. @drop="${this._onRessourceDrop}"
  733. >
  734. ${this.rows.length > 0
  735. ? displayedRows.map((o) => this.renderRessource(o.r))
  736. : lit_element_1.html `<tr class="empty">
  737. <td>No ressource</td>
  738. </tr>`}
  739. </table>
  740. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  741. <div class="jc-timeline-grid-container">
  742. <table style="width:${nCol * this.slotWidth}px;">
  743. <colgroup>
  744. ${columns.map((_) => lit_element_1.html `<col style="min-width:${this.slotWidth}px" />`)}
  745. </colgroup>
  746. <tbody>
  747. ${this.rows.length > 0
  748. ? displayedRows.map((o) => this.renderGridRow(columns, o.i, o.r.height))
  749. : this.renderGridRow(columns)}
  750. </tbody>
  751. </table>
  752. <div class="jc-timeslots" style="width:${nCol * this.slotWidth}px;">
  753. ${this.items.map((slot) => this.renderTimeslot(slot))}
  754. </div>
  755. </div>
  756. </div>
  757. `;
  758. }
  759. };
  760. __decorate([
  761. lit_element_1.internalProperty()
  762. ], Timeline.prototype, "rows", void 0);
  763. __decorate([
  764. lit_element_1.internalProperty()
  765. ], Timeline.prototype, "items", void 0);
  766. __decorate([
  767. lit_element_1.property({ type: Number })
  768. ], Timeline.prototype, "ressourceWidth", void 0);
  769. __decorate([
  770. lit_element_1.property({ type: Object })
  771. ], Timeline.prototype, "_start", void 0);
  772. __decorate([
  773. lit_element_1.property({ type: String })
  774. ], Timeline.prototype, "start", null);
  775. __decorate([
  776. lit_element_1.property({ type: String })
  777. ], Timeline.prototype, "end", null);
  778. __decorate([
  779. lit_element_1.property({ type: Number })
  780. ], Timeline.prototype, "slotDuration", null);
  781. __decorate([
  782. lit_element_1.property({ type: Number })
  783. ], Timeline.prototype, "legendSpan", null);
  784. __decorate([
  785. lit_element_1.property({ type: Number })
  786. ], Timeline.prototype, "rowHeight", void 0);
  787. __decorate([
  788. lit_element_1.property({ type: Number })
  789. ], Timeline.prototype, "slotWidth", void 0);
  790. __decorate([
  791. lit_element_1.property({ type: String })
  792. ], Timeline.prototype, "rowsTitle", void 0);
  793. __decorate([
  794. lit_element_1.property({ type: Array })
  795. ], Timeline.prototype, "legend", void 0);
  796. __decorate([
  797. lit_element_1.property({ type: String })
  798. ], Timeline.prototype, "defaultBackground", null);
  799. Timeline = __decorate([
  800. lit_element_1.customElement("jc-timeline")
  801. ], Timeline);
  802. exports.default = Timeline;
  803. //# sourceMappingURL=Timeline.js.map