]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/element.js
Scroll to active version on click in breadcrumbs
[rails.git] / app / assets / javascripts / index / element.js
1 (function () {
2   $(document).on("click", "a[href='#versions-navigation-active-page-item']", function (e) {
3     scrollToActiveVersion();
4     $("#versions-navigation-active-page-item a.page-link").trigger("focus");
5     e.preventDefault();
6   });
7
8   OSM.Element = function (map, type) {
9     const page = {};
10
11     page.pushstate = page.popstate = function (path, id, version) {
12       OSM.loadSidebarContent(path, function () {
13         scrollToActiveVersion();
14         page._addObject(type, id, version);
15       });
16     };
17
18     page.load = function (path, id, version) {
19       scrollToActiveVersion();
20       page._addObject(type, id, version, true);
21     };
22
23     page.unload = function () {
24       page._removeObject();
25     };
26
27     page._addObject = function () {};
28     page._removeObject = function () {};
29
30     return page;
31   };
32
33   OSM.MappedElement = function (map, type) {
34     const page = OSM.Element(map, type);
35
36     page._addObject = function (type, id, version, center) {
37       const hashParams = OSM.parseHash();
38       map.addObject({ type: type, id: parseInt(id, 10), version: version && parseInt(version, 10) }, function (bounds) {
39         if (!hashParams.center && bounds.isValid() &&
40             (center || !map.getBounds().contains(bounds))) {
41           OSM.router.withoutMoveListener(function () {
42             map.fitBounds(bounds);
43           });
44         }
45       });
46     };
47
48     page._removeObject = function () {
49       map.removeObject();
50     };
51
52     return page;
53   };
54
55   function scrollToActiveVersion() {
56     const [scrollableList] = $("#versions-navigation-list-middle");
57
58     if (!scrollableList) return;
59
60     const [activeStartItem] = $("#versions-navigation-list-start #versions-navigation-active-page-item");
61     const [activeScrollableItem] = $("#versions-navigation-list-middle #versions-navigation-active-page-item");
62
63     if (activeStartItem) {
64       scrollableList.scrollLeft = 0;
65     } else if (activeScrollableItem) {
66       scrollableList.scrollLeft = Math.round(activeScrollableItem.offsetLeft - (scrollableList.offsetWidth / 2) + (activeScrollableItem.offsetWidth / 2));
67     } else {
68       scrollableList.scrollLeft = scrollableList.scrollWidth - scrollableList.offsetWidth;
69     }
70   }
71 }());