forked from mistic100/Photo-Sphere-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavbar.js
More file actions
246 lines (214 loc) · 5.97 KB
/
Copy pathNavbar.js
File metadata and controls
246 lines (214 loc) · 5.97 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { AutorotateButton } from '../buttons/AutorotateButton';
import { CustomButton } from '../buttons/CustomButton';
import { DownloadButton } from '../buttons/DownloadButton';
import { FullscreenButton } from '../buttons/FullscreenButton';
import { MenuButton } from '../buttons/MenuButton';
import { MoveDownButton } from '../buttons/MoveDownButton';
import { MoveLeftButton } from '../buttons/MoveLeftButton';
import { MoveRightButton } from '../buttons/MoveRightButton';
import { MoveUpButton } from '../buttons/MoveUpButton';
import { ZoomInButton } from '../buttons/ZoomInButton';
import { ZoomOutButton } from '../buttons/ZoomOutButton';
import { ZoomRangeButton } from '../buttons/ZoomRangeButton';
import { DEFAULTS } from '../data/config';
import { PSVError } from '../PSVError';
import { clone, logWarn } from '../utils';
import { AbstractComponent } from './AbstractComponent';
import { NavbarCaption } from './NavbarCaption';
/**
* @summary List of available buttons
* @type {Object<string, Class<PSV.buttons.AbstractButton>>}
* @private
*/
const AVAILABLE_BUTTONS = {};
/**
* @summary Register a new button available for all viewers
* @param {Class<PSV.buttons.AbstractButton>} button
* @memberOf PSV
*/
export function registerButton(button) {
if (!button.id) {
throw new PSVError('Button ID is required');
}
AVAILABLE_BUTTONS[button.id] = button;
}
[
AutorotateButton,
ZoomInButton,
ZoomRangeButton,
ZoomOutButton,
DownloadButton,
FullscreenButton,
MoveRightButton,
MoveLeftButton,
MoveUpButton,
MoveDownButton,
].forEach(registerButton);
/**
* @summary Navigation bar class
* @extends PSV.components.AbstractComponent
* @memberof PSV.components
*/
export class Navbar extends AbstractComponent {
/**
* @param {PSV.Viewer} psv
*/
constructor(psv) {
super(psv, 'psv-navbar');
/**
* @summary List of buttons of the navbar
* @member {PSV.buttons.AbstractButton[]}
* @override
*/
this.children = [];
/**
* @summary List of collapsed buttons
* @member {PSV.buttons.AbstractButton[]}
* @private
*/
this.collapsed = [];
}
/**
* @summary Change the buttons visible on the navbar
* @param {string|Array<string|PSV.NavbarCustomButton>} buttons
*/
setButtons(buttons) {
this.children.slice().forEach(item => item.destroy());
this.children.length = 0;
/* eslint-disable no-new */
this.__cleanButtons(buttons).forEach((button) => {
if (typeof button === 'object') {
new CustomButton(this, button);
}
else if (AVAILABLE_BUTTONS[button]) {
new AVAILABLE_BUTTONS[button](this);
}
else if (button === 'caption') {
new NavbarCaption(this, this.psv.config.caption);
}
else if (button === 'zoom') {
new ZoomOutButton(this);
new ZoomRangeButton(this);
new ZoomInButton(this);
}
else if (button === 'move') {
new MoveLeftButton(this);
new MoveRightButton(this);
new MoveUpButton(this);
new MoveDownButton(this);
}
else {
throw new PSVError('Unknown button ' + button);
}
});
new MenuButton(this);
/* eslint-enable no-new */
this.children.forEach((item) => {
if (typeof item.checkSupported === 'function') {
item.checkSupported();
}
});
}
/**
* @summary Sets the bar caption
* @param {string} html
*/
setCaption(html) {
const caption = this.getButton('caption', false);
if (!caption) {
throw new PSVError('Cannot set caption, the navbar caption container is not initialized.');
}
caption.setCaption(html);
}
/**
* @summary Returns a button by its identifier
* @param {string} id
* @param {boolean} [warnNotFound=true]
* @returns {PSV.buttons.AbstractButton}
*/
getButton(id, warnNotFound = true) {
let button = null;
this.children.some((item) => {
if (item.prop.id === id) {
button = item;
return true;
}
else {
return false;
}
});
if (!button && warnNotFound) {
logWarn(`button "${id}" not found in the navbar`);
}
return button;
}
/**
* @summary Shows the navbar
*/
show() {
this.container.classList.add('psv-navbar--open');
this.prop.visible = true;
}
/**
* @summary Hides the navbar
*/
hide() {
this.container.classList.remove('psv-navbar--open');
this.prop.visible = false;
}
/**
* @override
*/
refreshUi() {
super.refreshUi();
if (this.psv.prop.uiRefresh === true) {
const availableWidth = this.container.offsetWidth;
let totalWidth = 0;
const visibleButtons = [];
const collapsableButtons = [];
this.children.forEach((item) => {
if (item.prop.visible) {
totalWidth += item.prop.width;
visibleButtons.push(item);
if (item.prop.collapsable) {
collapsableButtons.push(item);
}
}
});
if (!visibleButtons.length) {
return;
}
if (availableWidth < totalWidth && collapsableButtons.length > 0) {
collapsableButtons.forEach(item => item.collapse());
this.collapsed = collapsableButtons;
this.getButton(MenuButton.id).show(false);
}
else if (availableWidth >= totalWidth && this.collapsed.length > 0) {
this.collapsed.forEach(item => item.uncollapse());
this.collapsed = [];
this.getButton(MenuButton.id).hide(false);
}
const caption = this.getButton(NavbarCaption.id, false);
if (caption) {
caption.refreshUi();
}
}
}
/**
* @summary Ensure the buttons configuration is correct
* @private
*/
__cleanButtons(buttons) {
// true becomes the default array
if (buttons === true) {
return clone(DEFAULTS.navbar);
}
// can be a space or coma separated list
else if (typeof buttons === 'string') {
return buttons.split(/[ ,]/);
}
else {
return buttons || [];
}
}
}