-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathol3-sidebar.js
137 lines (114 loc) · 4.22 KB
/
ol3-sidebar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// detect jsdom environment
// borrowed from: https://github.com/jsdom/jsdom/issues/1537
var ol;
if (navigator.userAgent.includes("Node.js") || navigator.userAgent.includes("jsdom")) {
ol = require('openlayers');
}
ol.control.Sidebar = function (settings) {
var defaults = {
element: null,
position: 'left'
}, i, child;
this._options = Object.assign({}, defaults, settings);
ol.control.Control.call(this, {
element: document.getElementById(this._options.element),
target: this._options.target
});
// Attach .sidebar-left/right class
this.element.classList.add('sidebar-' + this._options.position);
// Find sidebar > div.sidebar-content
for (i = this.element.children.length - 1; i >= 0; i--) {
child = this.element.children[i];
if (child.tagName === 'DIV' &&
child.classList.contains('sidebar-content')) {
this._container = child;
}
}
// Find sidebar ul.sidebar-tabs > li, sidebar .sidebar-tabs > ul > li
this._tabitems = this.element.querySelectorAll('ul.sidebar-tabs > li, .sidebar-tabs > ul > li');
for (i = this._tabitems.length - 1; i >= 0; i--) {
this._tabitems[i]._sidebar = this;
}
// Find sidebar > div.sidebar-content > div.sidebar-pane
this._panes = [];
this._closeButtons = [];
for (i = this._container.children.length - 1; i >= 0; i--) {
child = this._container.children[i];
if (child.tagName == 'DIV' &&
child.classList.contains('sidebar-pane')) {
this._panes.push(child);
var closeButtons = child.querySelectorAll('.sidebar-close');
for (var j = 0, len = closeButtons.length; j < len; j++) {
this._closeButtons.push(closeButtons[j]);
}
}
}
};
if ('inherits' in ol) {
ol.inherits(ol.control.Sidebar, ol.control.Control);
} else {
ol.control.Sidebar.prototype = Object.create(ol.control.Control.prototype);
ol.control.Sidebar.prototype.constructor = ol.control.Sidebar;
}
ol.control.Sidebar.prototype.setMap = function(map) {
var i, child;
for (i = this._tabitems.length - 1; i >= 0; i--) {
child = this._tabitems[i];
var sub = child.querySelector('a');
if (sub.hasAttribute('href') && sub.getAttribute('href').slice(0,1) == '#') {
sub.onclick = this._onClick.bind(child);
}
}
for (i = this._closeButtons.length - 1; i >= 0; i--) {
child = this._closeButtons[i];
child.onclick = this._onCloseClick.bind(this);
}
};
ol.control.Sidebar.prototype.open = function(id) {
var i, child;
// hide old active contents and show new content
for (i = this._panes.length - 1; i >= 0; i--) {
child = this._panes[i];
if (child.id == id)
child.classList.add('active');
else if (child.classList.contains('active'))
child.classList.remove('active');
}
// remove old active highlights and set new highlight
for (i = this._tabitems.length - 1; i >= 0; i--) {
child = this._tabitems[i];
if (child.querySelector('a').hash == '#' + id)
child.classList.add('active');
else if (child.classList.contains('active'))
child.classList.remove('active');
}
// open sidebar (if necessary)
if (this.element.classList.contains('collapsed')) {
this.element.classList.remove('collapsed');
}
return this;
};
ol.control.Sidebar.prototype.close = function() {
// remove old active highlights
for (var i = this._tabitems.length - 1; i >= 0; i--) {
var child = this._tabitems[i];
if (child.classList.contains('active'))
child.classList.remove('active');
}
// close sidebar
if (!this.element.classList.contains('collapsed')) {
this.element.classList.add('collapsed');
}
return this;
};
ol.control.Sidebar.prototype._onClick = function(evt) {
evt.preventDefault();
if (this.classList.contains('active')) {
this._sidebar.close();
} else if (!this.classList.contains('disabled')) {
this._sidebar.open(this.querySelector('a').hash.slice(1));
}
};
ol.control.Sidebar.prototype._onCloseClick = function() {
this.close();
};