Timeline.js 34 KB

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