Timeline.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 = (_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 gridContainer = root.querySelector(".jc-timeline-grid-container");
  353. const headerContainer = root.querySelector(".jc-timeline-grid-title-container");
  354. let lastPosX = e.clientX;
  355. const scroll = function (e) {
  356. const scrollLeft = lastPosX - e.clientX;
  357. headerContainer.scrollLeft += scrollLeft;
  358. gridContainer.scrollLeft += scrollLeft;
  359. lastPosX = e.clientX;
  360. };
  361. const mouseUpListener = function (_e) {
  362. window.removeEventListener("mousemove", scroll);
  363. window.removeEventListener("mouseup", mouseUpListener);
  364. };
  365. window.addEventListener("mousemove", scroll);
  366. window.addEventListener("mouseup", mouseUpListener);
  367. }
  368. }
  369. _getEventResizerHandler(slot, direction) {
  370. return (evt) => {
  371. evt.stopPropagation();
  372. evt.preventDefault();
  373. const startPos = evt.clientX;
  374. const localSlot = slot;
  375. const localDir = direction;
  376. const initialDate = slot[direction];
  377. const resizeListener = (e) => {
  378. const newDate = dayjs_1.default(initialDate)
  379. .add(Math.round((e.clientX - startPos) / this.slotWidth) * this.slotDuration, "m")
  380. .toDate();
  381. if (direction === "start" ? newDate < localSlot.end : localSlot.start < newDate) {
  382. if (localSlot[localDir] !== newDate) {
  383. localSlot[localDir] = newDate;
  384. this.updateEventById(slot.id);
  385. }
  386. }
  387. };
  388. const mouseUpListener = (_e) => {
  389. window.removeEventListener("mousemove", resizeListener);
  390. window.removeEventListener("mouseup", mouseUpListener);
  391. localSlot.moving = false;
  392. this.updateEventById(localSlot.id);
  393. if (initialDate !== localSlot[localDir]) {
  394. const cEvt = new CustomEvent("event-change", {
  395. detail: { items: [localSlot] },
  396. });
  397. this.dispatchEvent(cEvt);
  398. }
  399. };
  400. localSlot.moving = true;
  401. window.addEventListener("mousemove", resizeListener);
  402. window.addEventListener("mouseup", mouseUpListener);
  403. };
  404. }
  405. _getEventGrabHandler(slot, editable, ressourceEditable, callback) {
  406. return (evt) => {
  407. evt.stopPropagation();
  408. evt.preventDefault();
  409. const startPos = evt.clientX;
  410. let hasChanged = false;
  411. const localSlot = slot;
  412. let localSlots = this.selectedList
  413. .filter((s) => s instanceof Event_1.Event)
  414. .map((s) => s);
  415. if (!localSlots.includes(localSlot)) {
  416. localSlots = [localSlot];
  417. }
  418. const startDates = localSlots.map((slot) => slot.start);
  419. const endDates = localSlots.map((slot) => slot.end);
  420. const updatePosition = editable
  421. ? (e) => {
  422. const changeTime = Math.round((e.clientX - startPos) / this.slotWidth) * this.slotDuration;
  423. return localSlots
  424. .map((slot, index) => {
  425. const prevStart = slot.start;
  426. slot.start = dayjs_1.default(startDates[index]).add(changeTime, "m").toDate();
  427. slot.end = dayjs_1.default(endDates[index]).add(changeTime, "m").toDate();
  428. return prevStart.getTime() !== slot.start.getTime();
  429. })
  430. .reduce((prev, curr) => prev || curr);
  431. }
  432. : (_e) => {
  433. return false;
  434. };
  435. const updateRessource = ressourceEditable
  436. ? (e) => {
  437. var _a, _b, _c;
  438. 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");
  439. if (rowId) {
  440. const ressourceId = this.rows[Number(rowId)].id;
  441. if (ressourceId !== localSlot.ressourceId) {
  442. const oldRessource = this.getRessourceFromId(localSlot.ressourceId);
  443. localSlot.ressourceId = ressourceId;
  444. this._updateEventPosition(oldRessource);
  445. return true;
  446. }
  447. }
  448. return false;
  449. }
  450. : (_e) => {
  451. return false;
  452. };
  453. const moveListener = (e) => {
  454. const a = updatePosition(e);
  455. if (updateRessource(e) || a) {
  456. hasChanged = true;
  457. this.updateEventById(localSlot.id);
  458. }
  459. };
  460. const mouseUpListener = (e) => {
  461. window.removeEventListener("mousemove", moveListener);
  462. window.removeEventListener("mouseup", mouseUpListener);
  463. localSlot.moving = false;
  464. this.updateEventById(slot.id);
  465. if (hasChanged) {
  466. const cEvt = new CustomEvent("event-change", {
  467. detail: { items: localSlots },
  468. });
  469. this.dispatchEvent(cEvt);
  470. }
  471. callback(e, hasChanged);
  472. };
  473. localSlot.moving = true;
  474. window.addEventListener("mousemove", moveListener);
  475. window.addEventListener("mouseup", mouseUpListener);
  476. };
  477. }
  478. clearSelectedItems() {
  479. this.items.forEach((selectable) => {
  480. selectable.selected = false;
  481. this.updateEventById(selectable.id);
  482. });
  483. this.rows.forEach((selectable) => (selectable.selected = false));
  484. this.selectedList = [];
  485. }
  486. _getEventClickHandler(clickedItem) {
  487. const item = clickedItem;
  488. return (e, wasModified = false) => {
  489. e.stopPropagation();
  490. const idx = this.selectedList.indexOf(item);
  491. if (idx > -1) {
  492. if (!wasModified) {
  493. if (e.ctrlKey) {
  494. this.selectedList.splice(idx, 1);
  495. item.selected = false;
  496. this.updateEventById(item.id);
  497. }
  498. else {
  499. this.clearSelectedItems();
  500. }
  501. }
  502. }
  503. else {
  504. if (this.selectedList.length > 0 && !e.ctrlKey) {
  505. this.clearSelectedItems();
  506. }
  507. item.selected = true;
  508. this.selectedList.push(item);
  509. this.updateEventById(item.id);
  510. }
  511. const myEvent = new CustomEvent("item-selected", {
  512. detail: { items: this.selectedList },
  513. bubbles: true,
  514. composed: true,
  515. });
  516. this.dispatchEvent(myEvent);
  517. };
  518. }
  519. firstUpdated() {
  520. const root = this.shadowRoot;
  521. if (root !== null) {
  522. const gridContainer = root.querySelector(".jc-timeline-grid-container");
  523. const simplebar = new simplebar_1.default(gridContainer).getScrollElement();
  524. syncroScroll_1.default([simplebar, root.querySelector(".jc-timeline-grid-title-container")], "h");
  525. syncroScroll_1.default([simplebar, root.querySelector(".jc-timeline-rows")], "v");
  526. }
  527. if (this.defaultBackground === "") {
  528. this.style.setProperty("--default-background", "SteelBlue");
  529. }
  530. }
  531. renderTimeslot(evt) {
  532. if (!evt.isDisplayed) {
  533. return lit_element_1.html ``;
  534. }
  535. let rowTop = 0;
  536. let ressource;
  537. let i;
  538. for (i = 0; i < this.rows.length && this.rows[i].id !== evt.ressourceId; i++) {
  539. ressource = this.rows[i];
  540. if (ressource.show) {
  541. rowTop += ressource.height ? ressource.height : this.rowHeight;
  542. }
  543. }
  544. ressource = this.rows[i];
  545. const minute2pixel = this.slotWidth / this.slotDuration;
  546. const left = dayjs_1.default(evt.start).diff(this._start, "m") * minute2pixel;
  547. const right = -dayjs_1.default(evt.end).diff(this._end, "m") * minute2pixel;
  548. const style = {
  549. height: this.rowHeight - 4 + "px",
  550. top: rowTop + evt.offset * this.rowHeight + "px",
  551. left: left + "px",
  552. right: right + "px",
  553. backgroundColor: "",
  554. };
  555. const bgColor = evt.bgColor ? evt.bgColor : ressource.eventBgColor;
  556. if (bgColor) {
  557. style.backgroundColor = bgColor;
  558. }
  559. if (!ressource.show) {
  560. style.height = "";
  561. style.top = rowTop - 6 + "px";
  562. return lit_element_1.html `<div class="jc-timeslot empty" style="${style_map_1.styleMap(style)}"></div>`;
  563. }
  564. let content = lit_element_1.html `<div class="jc-timeslot-title">${evt.title}</div>
  565. ${evt.content ? unsafe_html_1.unsafeHTML(evt.content) : ""}`;
  566. const resizer = evt.editable === null ? ressource.eventEditable : evt.editable;
  567. const editableRessource = evt.ressourceEditable === null ? ressource.eventRessourceEditable : evt.ressourceEditable;
  568. if (resizer) {
  569. content = lit_element_1.html `<div
  570. class="jc-timeslot-resizer-start"
  571. @mousedown="${this._getEventResizerHandler(evt, "start")}"
  572. ></div>
  573. ${content}
  574. <div
  575. class="jc-timeslot-resizer-end"
  576. @mousedown="${this._getEventResizerHandler(evt, "end")}"
  577. ></div>`;
  578. }
  579. return lit_element_1.html `<div
  580. class="jc-timeslot ${evt.moving ? "moving" : ""} ${evt.selected ? "selected" : ""}"
  581. start="${evt.start.getHours()}"
  582. end="${evt.end.getHours()}"
  583. style="${style_map_1.styleMap(style)}"
  584. @mousedown="${this._getEventGrabHandler(evt, resizer, editableRessource, this._getEventClickHandler(evt))}"
  585. >
  586. ${content}
  587. </div>`;
  588. }
  589. _getCollapseRessourceHandler(item) {
  590. return (_e) => {
  591. item.collapseChildren = !item.collapseChildren;
  592. this._updateEventPosition(item);
  593. this.rows = [...this.rows];
  594. };
  595. }
  596. _onRessourceDragStart(item) {
  597. return (event) => {
  598. var _a;
  599. (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.setData("text", item.id);
  600. };
  601. }
  602. _onRessourceDragEnter(event) {
  603. if (event.target instanceof HTMLElement) {
  604. const tgt = event.target;
  605. tgt.classList.add("target");
  606. }
  607. }
  608. _onRessourceDragLeave(event) {
  609. if (event.target instanceof HTMLElement) {
  610. event.target.classList.remove("target");
  611. }
  612. }
  613. _onRessourceDrop(event) {
  614. var _a, _b;
  615. event.preventDefault();
  616. if (event.target instanceof HTMLElement) {
  617. event.target.classList.remove("target");
  618. const srcId = (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.getData("text");
  619. const destinationId = (_b = event.target.parentElement) === null || _b === void 0 ? void 0 : _b.getAttribute("ressourceId");
  620. if (srcId && destinationId && destinationId !== srcId) {
  621. const src = this.getRessourceFromId(srcId);
  622. const destination = this.getRessourceFromId(destinationId);
  623. if (destination.descendantOf(src)) {
  624. return;
  625. }
  626. const movedContent = this.removeRessourceById(src.id);
  627. if (event.target.classList.contains("jc-ressource")) {
  628. movedContent.ressources[0].parent = destination;
  629. }
  630. else {
  631. movedContent.ressources[0].parent = destination.parent;
  632. let idx = this.rows.findIndex((v) => v.id === destinationId);
  633. if (event.target.classList.contains("jc-ressource-below")) {
  634. idx += 1;
  635. while (idx < this.rows.length && this.rows[idx].descendantOf(destination)) {
  636. idx += 1;
  637. }
  638. }
  639. const arr = this.rows;
  640. this.rows = [...arr.splice(0, idx), src, ...arr];
  641. }
  642. this.addRessources(movedContent.ressources);
  643. this.addEvents(movedContent.items);
  644. this.dispatchEvent(new CustomEvent("reorder-ressource", {
  645. detail: { ressources: this.rows },
  646. }));
  647. }
  648. }
  649. }
  650. renderRessource(item) {
  651. const depth = item.depth;
  652. const style = `--depth:${depth};` + (item.height ? `height:${item.height}px;` : "");
  653. const hasChild = item.children.length > 0;
  654. const collapseHandler = this._getCollapseRessourceHandler(item);
  655. return lit_element_1.html `<tr>
  656. <td
  657. class="${item.selected ? "jc-ressource-selected" : ""}"
  658. style="${style}"
  659. ressourceId="${item.id}"
  660. @click="${this._getEventClickHandler(item)}"
  661. >
  662. <div class="jc-ressource-above"></div>
  663. <div class="jc-ressource" draggable="true" @dragstart="${this._onRessourceDragStart(item)}">
  664. ${Array(depth)
  665. .fill(0)
  666. .map((_i) => lit_element_1.html `<i class="jc-spacer"></i>`)}${hasChild
  667. ? lit_element_1.html `<i
  668. class="jc-spacer ${item.collapseChildren ? "extend" : "collapse"}"
  669. @click="${collapseHandler}"
  670. ></i>`
  671. : lit_element_1.html `<i class="jc-spacer"></i>`}
  672. <span>${item.title}</span>
  673. </div>
  674. <div class="jc-ressource-below"></div>
  675. </td>
  676. </tr>`;
  677. }
  678. renderGridRow(columns, rowId = -1, height = 30) {
  679. return lit_element_1.html `<tr row-id="${rowId}">
  680. ${columns.map((d, i) => lit_element_1.html `<td
  681. style="height:${height}px;"
  682. class="jc-slot ${i % this.legendSpan === 0 ? "jc-major-slot" : ""}"
  683. start="${d.toISOString()}"
  684. >
  685. &nbsp;
  686. </td>`)}
  687. </tr>`;
  688. }
  689. render() {
  690. const nCol = Math.floor(this._end.diff(this._start, "m") / this.slotDuration) + 1;
  691. const columns = [];
  692. for (let i = 0; i < nCol; i++) {
  693. columns.push(this._start.add(this.slotDuration * i, "m"));
  694. }
  695. const displayedRows = this.rows
  696. .map((r, i) => {
  697. return { i: i, r: r };
  698. })
  699. .filter((o) => o.r.show);
  700. return lit_element_1.html `
  701. <style>
  702. ${this.customStyle}
  703. </style>
  704. <div class="jc-timeline-header">
  705. <div
  706. class="jc-timeline-rows-title"
  707. style=${style_map_1.styleMap({
  708. minWidth: this.ressourceWidth + "px",
  709. width: this.ressourceWidth + "px",
  710. })}
  711. >
  712. ${this.rowsTitle}
  713. </div>
  714. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  715. <div class="jc-timeline-grid-title-container">
  716. <table @mousedown="${this._grabHeader}" style="width:${nCol * this.slotWidth}px;">
  717. <colgroup>
  718. ${columns.map((_) => lit_element_1.html `<col style="min-width:${this.slotWidth}px" />`)}
  719. </colgroup>
  720. <tbody>
  721. ${this.legend.map((arr) => lit_element_1.html `<tr class="jc-timeline-grid-title">
  722. ${arr.map((o) => lit_element_1.html `<th colspan="${o.colspan}"><div>${o.title}</div></th>`)}
  723. </tr>`)}
  724. </tbody>
  725. </table>
  726. </div>
  727. </div>
  728. <div class="jc-timeline-content">
  729. <table
  730. class="jc-timeline-rows"
  731. style="${style_map_1.styleMap({ "--width": this.ressourceWidth + "px" })}"
  732. @dragover="${(e) => e.preventDefault()}"
  733. @dragenter="${this._onRessourceDragEnter}"
  734. @dragleave="${this._onRessourceDragLeave}"
  735. @drop="${this._onRessourceDrop}"
  736. >
  737. ${this.rows.length > 0
  738. ? displayedRows.map((o) => this.renderRessource(o.r))
  739. : lit_element_1.html `<tr class="empty">
  740. <td>No ressource</td>
  741. </tr>`}
  742. </table>
  743. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  744. <div class="jc-timeline-grid-container">
  745. <table style="width:${nCol * this.slotWidth}px;">
  746. <colgroup>
  747. ${columns.map((_) => lit_element_1.html `<col style="min-width:${this.slotWidth}px" />`)}
  748. </colgroup>
  749. <tbody>
  750. ${this.rows.length > 0
  751. ? displayedRows.map((o) => this.renderGridRow(columns, o.i, o.r.height))
  752. : this.renderGridRow(columns)}
  753. </tbody>
  754. </table>
  755. <div class="jc-timeslots" style="width:${nCol * this.slotWidth}px;">
  756. ${this.items.map((slot) => this.renderTimeslot(slot))}
  757. </div>
  758. </div>
  759. </div>
  760. `;
  761. }
  762. };
  763. __decorate([
  764. lit_element_1.internalProperty()
  765. ], Timeline.prototype, "rows", void 0);
  766. __decorate([
  767. lit_element_1.internalProperty()
  768. ], Timeline.prototype, "items", void 0);
  769. __decorate([
  770. lit_element_1.property({ type: Number })
  771. ], Timeline.prototype, "ressourceWidth", void 0);
  772. __decorate([
  773. lit_element_1.property({ type: Object })
  774. ], Timeline.prototype, "_start", void 0);
  775. __decorate([
  776. lit_element_1.property({ type: String })
  777. ], Timeline.prototype, "start", null);
  778. __decorate([
  779. lit_element_1.property({ type: String })
  780. ], Timeline.prototype, "end", null);
  781. __decorate([
  782. lit_element_1.property({ type: Number })
  783. ], Timeline.prototype, "slotDuration", null);
  784. __decorate([
  785. lit_element_1.property({ type: Number })
  786. ], Timeline.prototype, "legendSpan", null);
  787. __decorate([
  788. lit_element_1.property({ type: Number })
  789. ], Timeline.prototype, "rowHeight", void 0);
  790. __decorate([
  791. lit_element_1.property({ type: Number })
  792. ], Timeline.prototype, "slotWidth", void 0);
  793. __decorate([
  794. lit_element_1.property({ type: String })
  795. ], Timeline.prototype, "rowsTitle", void 0);
  796. __decorate([
  797. lit_element_1.property({ type: Array })
  798. ], Timeline.prototype, "legend", void 0);
  799. __decorate([
  800. lit_element_1.property({ type: String })
  801. ], Timeline.prototype, "defaultBackground", null);
  802. Timeline = __decorate([
  803. lit_element_1.customElement("jc-timeline")
  804. ], Timeline);
  805. exports.default = Timeline;
  806. //# sourceMappingURL=Timeline.js.map