Timeline.js 33 KB

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