Timeline.js 34 KB

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