Timeline.js 32 KB

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