-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
pannellum_photo.js
160 lines (130 loc) · 3.98 KB
/
pannellum_photo.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { select as d3_select } from 'd3-selection';
import { dispatch as d3_dispatch } from 'd3-dispatch';
import { utilRebind } from '../util';
const pannellumViewerCSS = 'pannellum/pannellum.css';
const pannellumViewerJS = 'pannellum/pannellum.js';
const dispatch = d3_dispatch('viewerChanged');
let _currScenes = [];
let _pannellumViewer;
export default {
init: async function(context, selection) {
selection
.append('div')
.attr('class', 'photo-frame pannellum-frame')
.attr('id', 'ideditor-pannellum-viewer')
.classed('hide', true)
.on('keydown', function(e) { e.stopPropagation(); });
if (!window.pannellum) {
await this.loadPannellum(context);
}
const options = {
'default': { firstScene: '' },
scenes: {},
minHfov: 20
};
_pannellumViewer = window.pannellum.viewer('ideditor-pannellum-viewer', options);
_pannellumViewer
.on('mousedown', () => {
d3_select(window)
.on('pointermove.pannellum mousemove.pannellum', () => {
dispatch.call('viewerChanged');
});
})
.on('mouseup', () => {
d3_select(window)
.on('pointermove.pannellum mousemove.pannellum', null);
})
.on('animatefinished', () => {
dispatch.call('viewerChanged');
});
context.ui().photoviewer.on('resize.pannellum', () => {
_pannellumViewer.resize();
});
this.event = utilRebind(this, dispatch, 'on');
return this;
},
loadPannellum: function(context) {
const head = d3_select('head');
return Promise.all([
new Promise((resolve, reject) => {
// load pannellum viewer css
head
.selectAll('#ideditor-pannellum-viewercss')
.data([0])
.enter()
.append('link')
.attr('id', 'ideditor-pannellum-viewercss')
.attr('rel', 'stylesheet')
.attr('crossorigin', 'anonymous')
.attr('href', context.asset(pannellumViewerCSS))
.on('load.pannellum', resolve)
.on('error.pannellum', reject);
}),
new Promise((resolve, reject) => {
// load pannellum viewer js
head
.selectAll('#ideditor-pannellum-viewerjs')
.data([0])
.enter()
.append('script')
.attr('id', 'ideditor-pannellum-viewerjs')
.attr('crossorigin', 'anonymous')
.attr('src', context.asset(pannellumViewerJS))
.on('load.pannellum', resolve)
.on('error.pannellum', reject);
})
]);
},
showPhotoFrame: function (context) {
const isHidden = context.selectAll('.photo-frame.pannellum-frame.hide').size();
if (isHidden) {
context
.selectAll('.photo-frame:not(.pannellum-frame)')
.classed('hide', true);
context
.selectAll('.photo-frame.pannellum-frame')
.classed('hide', false);
}
return this;
},
hidePhotoFrame: function (viewerContext) {
viewerContext
.select('photo-frame.pannellum-frame')
.classed('hide', false);
return this;
},
selectPhoto: function (data, keepOrientation) {
const {key} = data;
if ( !(key in _currScenes) ) {
let newSceneOptions = {
showFullscreenCtrl: false,
autoLoad: false,
compass: false,
yaw: 0,
type: 'equirectangular',
preview: data.preview_path,
panorama: data.image_path,
northOffset: data.ca
};
_currScenes.push(key);
_pannellumViewer.addScene(key, newSceneOptions);
}
let yaw = 0;
let pitch = 0;
if (keepOrientation) {
yaw = this.getYaw();
pitch = _pannellumViewer.getPitch();
}
_pannellumViewer.loadScene(key, pitch, yaw);
dispatch.call('viewerChanged');
if (_currScenes.length > 3) {
const old_key = _currScenes.shift();
_pannellumViewer.removeScene(old_key);
}
_pannellumViewer.resize();
return this;
},
getYaw: function() {
return _pannellumViewer.getYaw();
}
};