Timeline.js 32 KB

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