Timeline.js 30 KB

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