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