Timeline.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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.renderRoot;
  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;
  404. const rowId = (_b = (_a = document.elementsFromPoint(e.clientX, e.clientY)
  405. .find((e) => e.tagName == "TD")) === null || _a === void 0 ? void 0 : _a.parentElement) === null || _b === void 0 ? void 0 : _b.getAttribute('row-id');
  406. if (rowId) {
  407. const ressourceId = this.rows[Number(rowId)].id;
  408. if (ressourceId !== localSlot.ressourceId) {
  409. const oldRessource = this.getRessourceFromId(localSlot.ressourceId);
  410. localSlot.ressourceId = ressourceId;
  411. this._updateEventPosition(oldRessource);
  412. return true;
  413. }
  414. }
  415. return false;
  416. } : (_e) => { return false; };
  417. const moveListener = (e) => {
  418. const a = updatePosition(e);
  419. if (updateRessource(e) || a) {
  420. hasChanged = true;
  421. this.updateEventById(localSlot.id);
  422. }
  423. };
  424. const mouseUpListener = (e) => {
  425. window.removeEventListener("mousemove", moveListener);
  426. window.removeEventListener("mouseup", mouseUpListener);
  427. localSlot.moving = false;
  428. this.updateEventById(slot.id);
  429. if (hasChanged) {
  430. const cEvt = new CustomEvent("event-change", {
  431. detail: { items: localSlots }
  432. });
  433. this.dispatchEvent(cEvt);
  434. }
  435. callback(e, hasChanged);
  436. };
  437. localSlot.moving = true;
  438. window.addEventListener("mousemove", moveListener);
  439. window.addEventListener("mouseup", mouseUpListener);
  440. };
  441. }
  442. clearSelectedItems() {
  443. this.items.forEach((selectable) => {
  444. selectable.selected = false;
  445. this.updateEventById(selectable.id);
  446. });
  447. this.rows.forEach((selectable) => selectable.selected = false);
  448. this.selectedList = [];
  449. }
  450. _getEventClickHandler(clickedItem) {
  451. const item = clickedItem;
  452. return (e, wasModified = false) => {
  453. e.stopPropagation();
  454. const idx = this.selectedList.indexOf(item);
  455. if (idx > -1) {
  456. if (!wasModified) {
  457. if (e.ctrlKey) {
  458. this.selectedList.splice(idx, 1);
  459. item.selected = false;
  460. this.updateEventById(item.id);
  461. }
  462. else {
  463. this.clearSelectedItems();
  464. }
  465. }
  466. }
  467. else {
  468. if (this.selectedList.length > 0 && !e.ctrlKey) {
  469. this.clearSelectedItems();
  470. }
  471. item.selected = true;
  472. this.selectedList.push(item);
  473. this.updateEventById(item.id);
  474. }
  475. const myEvent = new CustomEvent('item-selected', {
  476. detail: { items: this.selectedList },
  477. bubbles: true,
  478. composed: true
  479. });
  480. this.dispatchEvent(myEvent);
  481. };
  482. }
  483. firstUpdated() {
  484. const root = this.renderRoot;
  485. if (root !== null) {
  486. const gridContainer = root.querySelector(".jc-timeline-grid-container");
  487. const simplebar = new simplebar_1.default(gridContainer).getScrollElement();
  488. syncroScroll_1.default([simplebar, root.querySelector(".jc-timeline-grid-title-container")], "h");
  489. syncroScroll_1.default([simplebar, root.querySelector(".jc-timeline-rows")], "v");
  490. }
  491. if (this.defaultBackground === "") {
  492. this.style.setProperty("--default-background", "SteelBlue");
  493. }
  494. }
  495. renderTimeslot(evt) {
  496. if (!evt.isDisplayed) {
  497. return lit_element_1.html ``;
  498. }
  499. let rowTop = 0;
  500. let ressource;
  501. let i;
  502. for (i = 0; i < this.rows.length && this.rows[i].id !== evt.ressourceId; i++) {
  503. ressource = this.rows[i];
  504. if (ressource.show) {
  505. rowTop += ressource.height ? ressource.height : this.rowHeight;
  506. }
  507. }
  508. ressource = this.rows[i];
  509. const minute2pixel = this.slotWidth / this.slotDuration;
  510. const left = dayjs_1.default(evt.start).diff(this._start, "m") * minute2pixel;
  511. const right = -dayjs_1.default(evt.end).diff(this._end, "m") * minute2pixel;
  512. const style = {
  513. height: this.rowHeight - 4 + "px",
  514. top: rowTop + evt.offset * this.rowHeight + "px",
  515. left: left + "px",
  516. right: right + "px",
  517. backgroundColor: ""
  518. };
  519. const bgColor = evt.bgColor ? evt.bgColor : ressource.eventBgColor;
  520. if (bgColor) {
  521. style.backgroundColor = bgColor;
  522. }
  523. if (!ressource.show) {
  524. style.height = "";
  525. style.top = rowTop - 6 + "px";
  526. return lit_element_1.html `<div class="jc-timeslot empty" style="${style_map_1.styleMap(style)}"></div>`;
  527. }
  528. let content = lit_element_1.html `<div class="jc-timeslot-title">${evt.title}</div>${evt.content ? unsafe_html_1.unsafeHTML(evt.content) : ""}`;
  529. const resizer = evt.editable === null ? ressource.eventEditable : evt.editable;
  530. const editableRessource = evt.ressourceEditable === null ? ressource.eventRessourceEditable : evt.ressourceEditable;
  531. if (resizer) {
  532. content = lit_element_1.html `<div class="jc-timeslot-resizer-start" @mousedown="${this._getEventResizerHandler(evt, "start")}"></div>${content}
  533. <div class="jc-timeslot-resizer-end" @mousedown="${this._getEventResizerHandler(evt, "end")}"></div>`;
  534. }
  535. return lit_element_1.html `<div class="jc-timeslot ${evt.moving ? "moving" : ""} ${evt.selected ? "selected" : ""}"
  536. start="${evt.start.getHours()}"
  537. end="${evt.end.getHours()}"
  538. style="${style_map_1.styleMap(style)}"
  539. @mousedown="${this._getEventGrabHandler(evt, resizer, editableRessource, this._getEventClickHandler(evt))}"
  540. >${content}</div>`;
  541. }
  542. _getCollapseRessourceHandler(item) {
  543. return (_e) => {
  544. item.collapseChildren = !item.collapseChildren;
  545. this._updateEventPosition(item);
  546. this.rows = [...this.rows];
  547. };
  548. }
  549. _onRessourceDragStart(item) {
  550. return (event) => {
  551. var _a;
  552. (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.setData("text", item.id);
  553. };
  554. }
  555. _onRessourceDragEnter(event) {
  556. if (event.target instanceof HTMLElement) {
  557. const tgt = event.target;
  558. tgt.classList.add("target");
  559. }
  560. }
  561. _onRessourceDragLeave(event) {
  562. if (event.target instanceof HTMLElement) {
  563. event.target.classList.remove("target");
  564. }
  565. }
  566. _onRessourceDrop(event) {
  567. var _a, _b;
  568. event.preventDefault();
  569. if (event.target instanceof HTMLElement) {
  570. event.target.classList.remove("target");
  571. const srcId = (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.getData("text");
  572. const destinationId = (_b = event.target.parentElement) === null || _b === void 0 ? void 0 : _b.getAttribute("ressourceId");
  573. if (srcId && destinationId && (destinationId !== srcId)) {
  574. const src = this.getRessourceFromId(srcId);
  575. const destination = this.getRessourceFromId(destinationId);
  576. if (destination.descendantOf(src)) {
  577. return;
  578. }
  579. const movedContent = this.removeRessourceById(src.id);
  580. if (event.target.classList.contains("jc-ressource")) {
  581. movedContent.ressources[0].parent = destination;
  582. }
  583. else {
  584. movedContent.ressources[0].parent = destination.parent;
  585. let idx = this.rows.findIndex(v => v.id === destinationId);
  586. if (event.target.classList.contains("jc-ressource-below")) {
  587. idx += 1;
  588. while ((idx < this.rows.length)
  589. && this.rows[idx].descendantOf(destination)) {
  590. idx += 1;
  591. }
  592. }
  593. const arr = this.rows;
  594. this.rows = [...arr.splice(0, idx), src, ...arr];
  595. }
  596. this.addRessources(movedContent.ressources);
  597. this.addEvents(movedContent.items);
  598. this.dispatchEvent(new CustomEvent("reorder-ressource", {
  599. detail: { ressources: this.rows }
  600. }));
  601. }
  602. }
  603. }
  604. renderRessource(item) {
  605. const depth = item.depth;
  606. const style = `--depth:${depth};` + (item.height ? `height:${item.height}px;` : "");
  607. const hasChild = item.children.length > 0;
  608. const collapseHandler = this._getCollapseRessourceHandler(item);
  609. return lit_element_1.html `<tr>
  610. <td class="${item.selected ? "jc-ressource-selected" : ""}" style="${style}" ressourceId="${item.id}" @click="${this._getEventClickHandler(item)}">
  611. <div class="jc-ressource-above"></div>
  612. <div class="jc-ressource" draggable="true" @dragstart="${this._onRessourceDragStart(item)}">
  613. ${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>`}
  614. <span>${item.title}</span>
  615. </div>
  616. <div class="jc-ressource-below"></div>
  617. </td>
  618. </tr>`;
  619. }
  620. renderGridRow(columns, rowId = -1, height = 30) {
  621. 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>`;
  622. }
  623. render() {
  624. const nCol = Math.floor(this._end.diff(this._start, 'm') / this.slotDuration) + 1;
  625. const columns = [];
  626. for (let i = 0; i < nCol; i++) {
  627. columns.push(this._start.add(this.slotDuration * i, 'm'));
  628. }
  629. const displayedRows = this.rows.map((r, i) => { return { i: i, r: r }; }).filter(o => o.r.show);
  630. return lit_element_1.html `
  631. <style>${this.customStyle}</style>
  632. <div class="jc-timeline-header">
  633. <div class="jc-timeline-rows-title" style=${style_map_1.styleMap({ minWidth: this.ressourceWidth + "px", width: this.ressourceWidth + "px" })}>${this.rowsTitle}</div>
  634. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  635. <div class="jc-timeline-grid-title-container">
  636. <table @mousedown="${this._grabHeader}" style="width:${nCol * this.slotWidth}px;">
  637. <colgroup>${columns.map(_o => lit_element_1.html `<col style="min-width:${this.slotWidth}px">`)}</colgroup>
  638. <tbody>
  639. ${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>`)}
  640. </tbody>
  641. </table>
  642. </div>
  643. </div>
  644. <div class="jc-timeline-content">
  645. <table class="jc-timeline-rows"
  646. style="${style_map_1.styleMap({ "--width": this.ressourceWidth + "px" })}"
  647. @dragover="${(e) => e.preventDefault()}"
  648. @dragenter="${this._onRessourceDragEnter}"
  649. @dragleave="${this._onRessourceDragLeave}"
  650. @drop="${this._onRessourceDrop}">
  651. ${this.rows.length > 0 ? displayedRows.map(o => this.renderRessource(o.r)) : lit_element_1.html `<tr class="empty"><td>No ressource</td></tr>`}
  652. </table>
  653. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  654. <div class="jc-timeline-grid-container">
  655. <table style="width:${nCol * this.slotWidth}px;">
  656. <colgroup>${columns.map(_o => lit_element_1.html `<col style="min-width:${this.slotWidth}px">`)}</colgroup>
  657. <tbody>
  658. ${this.rows.length > 0 ? displayedRows.map(o => this.renderGridRow(columns, o.i, o.r.height)) : this.renderGridRow(columns)}
  659. </tbody>
  660. </table>
  661. <div class="jc-timeslots" style="width:${nCol * this.slotWidth}px;">
  662. ${this.items.map(slot => this.renderTimeslot(slot))}
  663. </div>
  664. </div>
  665. </div>
  666. `;
  667. }
  668. };
  669. __decorate([
  670. lit_element_1.internalProperty()
  671. ], Timeline.prototype, "rows", void 0);
  672. __decorate([
  673. lit_element_1.internalProperty()
  674. ], Timeline.prototype, "items", void 0);
  675. __decorate([
  676. lit_element_1.property({ type: Number })
  677. ], Timeline.prototype, "ressourceWidth", void 0);
  678. __decorate([
  679. lit_element_1.property({ type: Object })
  680. ], Timeline.prototype, "_start", void 0);
  681. __decorate([
  682. lit_element_1.property({ type: String })
  683. ], Timeline.prototype, "start", null);
  684. __decorate([
  685. lit_element_1.property({ type: String })
  686. ], Timeline.prototype, "end", null);
  687. __decorate([
  688. lit_element_1.property({ type: Number })
  689. ], Timeline.prototype, "slotDuration", null);
  690. __decorate([
  691. lit_element_1.property({ type: Number })
  692. ], Timeline.prototype, "legendSpan", null);
  693. __decorate([
  694. lit_element_1.property({ type: Number })
  695. ], Timeline.prototype, "rowHeight", void 0);
  696. __decorate([
  697. lit_element_1.property({ type: Number })
  698. ], Timeline.prototype, "slotWidth", void 0);
  699. __decorate([
  700. lit_element_1.property({ type: String })
  701. ], Timeline.prototype, "rowsTitle", void 0);
  702. __decorate([
  703. lit_element_1.property({ type: Array })
  704. ], Timeline.prototype, "legend", void 0);
  705. __decorate([
  706. lit_element_1.property({ type: String })
  707. ], Timeline.prototype, "defaultBackground", null);
  708. Timeline = __decorate([
  709. lit_element_1.customElement('jc-timeline')
  710. ], Timeline);
  711. exports.default = Timeline;
  712. //# sourceMappingURL=Timeline.js.map