-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhubs-slide-show.js
192 lines (153 loc) · 7.6 KB
/
hubs-slide-show.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
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
function inject_slideshow_Media() {
AFRAME.registerComponent("slidemenu-pager", {
schema: {
index: { default: 0 },
maxIndex: { default: 0 }
},
init() {
this.onNext = this.onNext.bind(this);
this.onPrev = this.onPrev.bind(this);
this.update = this.update.bind(this);
this.content = slideconfig.slides;
this.data.maxIndex = this.content.length - 1;
this.el.setAttribute("hover-menu__pager", { template: "#slidepager-hover-menu", isFlat: true });
this.el.components["hover-menu__pager"].getHoverMenu().then(menu => {
// If we got removed while waiting, do nothing.
if (!this.el.parentNode) return;
this.hoverMenu = menu;
this.nextButton = this.el.querySelector(".next-button [text-button]");
this.prevButton = this.el.querySelector(".prev-button [text-button]");
this.pageLabel = this.el.querySelector(".page-label");
this.nextButton.object3D.addEventListener("interact", this.onNext);
this.prevButton.object3D.addEventListener("interact", this.onPrev);
this.update();
//this.el.emit("pager-loaded");
});
NAF.utils
.getNetworkedEntity(this.el)
.then(networkedEl => {
this.networkedEl = networkedEl;
this.networkedEl.addEventListener("pinned", this.update);
this.networkedEl.addEventListener("unpinned", this.update);
window.APP.hubChannel.addEventListener("permissions_updated", this.update);
this.data.index = this.networkedEl.getAttribute("slide-element").index;
})
.catch(() => {}); //ignore exception, entity might not be networked
},
async update(oldData) {
if (this.networkedEl && NAF.utils.isMine(this.networkedEl)) {
if (oldData && typeof oldData.index === "number" && oldData.index !== this.data.index) {
this.el.emit("owned-pager-page-changed");
}
}
if (this.pageLabel) {
this.pageLabel.setAttribute("text", "value", `${this.data.index + 1}/${this.data.maxIndex + 1}`);
}
if (this.prevButton && this.nextButton) {
const pinnableElement = this.el.components["media-loader"].data.linkedEl || this.el;
const isPinned = pinnableElement.components.pinnable && pinnableElement.components.pinnable.data.pinned;
this.prevButton.object3D.visible = this.nextButton.object3D.visible =
!isPinned || window.APP.hubChannel.can("pin_objects");
}
},
onNext() {
if (this.networkedEl && !NAF.utils.isMine(this.networkedEl) && !NAF.utils.takeOwnership(this.networkedEl)) return;
const newIndex = Math.min(this.data.index + 1, this.data.maxIndex);
this.el.setAttribute("slide-element", "index", newIndex);
this.el.setAttribute("slidemenu-pager", "index", newIndex);
},
onPrev() {
if (this.networkedEl && !NAF.utils.isMine(this.networkedEl) && !NAF.utils.takeOwnership(this.networkedEl)) return;
const newIndex = Math.max(this.data.index - 1, 0);
this.el.setAttribute("slide-element", "index", newIndex);
this.el.setAttribute("slidemenu-pager", "index", newIndex);
},
remove() {
if (this.networkedEl) {
this.networkedEl.removeEventListener("pinned", this.update);
this.networkedEl.removeEventListener("unpinned", this.update);
}
window.APP.hubChannel.removeEventListener("permissions_updated", this.update);
}
});
//Query assets in order to setup template
let assets = document.querySelector("a-assets");
// create a new template variable
let pageHoverTemplate = document.createElement("template");
// create template id
pageHoverTemplate.id = "slidepager-hover-menu";
// create a new entity for the template so we can append it to the assets later
// normally this is done in the Hubs.html "bootstrap" file
let menuEntity = document.createElement("a-entity");
menuEntity.setAttribute("class", "ui interactable-ui hover-container");
menuEntity.setAttribute("visible", "false");
menuEntity.innerHTML = "<a-entity class='prev-button' position='-0.50 0 0'><a-entity is-remote-hover-target tags='singleActionButton:true; isHoverMenuChild: true;' mixin='rounded-text-button' slice9='width: 0.2'><a-entity sprite icon-button='image: prev.png; hoverImage: prev.png;' scale='0.070 0.070 0.070' position='0 0 0.005' ></a-entity></a-entity></a-entity><a-entity class='page-label' position='0 -0.2 0' text='value:.; width:2; align:center;' text-raycast-hack></a-entity><a-entity class='next-button' position='0.50 0 0'><a-entity is-remote-hover-target tags='singleActionButton:true; isHoverMenuChild: true;' mixin='rounded-text-button' slice9='width: 0.2'><a-entity sprite icon-button='image: next.png; hoverImage: next.png;' scale='0.070 0.070 0.070' position='0 0 0.005' ></a-entity></a-entity></a-entity>";
pageHoverTemplate.content.appendChild(menuEntity);
// once the template is created you append it to the assets
assets.appendChild(pageHoverTemplate);
AFRAME.registerComponent("slide-element", {
schema: {
index: { default: 0 },
slideScale: {default: 5}
},
init() {
//this.onNext = this.onNext.bind(this);
this.update = this.update.bind(this);
this.setupSlides = this.setupSlides.bind(this);
//if you want to disable the menu and make the slide clickable and loopable
//then uncomment the line below and remove the slidemenu-pager component from the object
//this.el.object3D.addEventListener("interact", this.onNext);
//get our content from the variable in the script injected above.
this.content = slideconfig.slides;
this.max = this.content.length;
NAF.utils
.getNetworkedEntity(this.el)
.then(networkedEl => {
this.networkedEl = networkedEl;
this.networkedEl.addEventListener("pinned", this.update);
this.networkedEl.addEventListener("unpinned", this.update);
window.APP.hubChannel.addEventListener("permissions_updated", this.update);
this.networkedEl.object3D.scale.setScalar(this.data.slideScale);
this.currentSlide = this.networkedEl.getAttribute("slide-element").index;
this.setupSlides();
})
.catch(() => {}); //ignore exception, entity might not be networked
},
async update(oldData) {
this.currentSlide = this.data.index;
this.el.setAttribute("media-loader", {src: this.content[this.currentSlide], fitToBox: true, resolve: false});
if (this.networkedEl && NAF.utils.isMine(this.networkedEl)) {
if (oldData && typeof oldData.index === "number" && oldData.index !== this.data.index) {
this.networkedEl.setAttribute("slide-element", {index: this.currentSlide});
}
}
},
remove() {
if (this.networkedEl) {
this.networkedEl.removeEventListener("pinned", this.update);
this.networkedEl.removeEventListener("unpinned", this.update);
}
window.APP.hubChannel.removeEventListener("permissions_updated", this.update);
},
setupSlides(){
this.currentSlide = this.networkedEl.getAttribute("slide-element").index;
this.el.setAttribute("media-loader", {src: this.content[this.currentSlide], fitToBox: true, resolve: false})
}
});
}
// we add the prefix inject_ to our utility functions to isolate them from the global namespace
inject_slideshow_Media();
// we add the prefix mod_ to this function to allow it to be targeted by the chat interface
function mod_addSlides(){
//only perform this once if the slideshow does not exist already.
if(document.querySelector("a-entity[slide-element]") == null){
var el = document.createElement("a-entity")
el.setAttribute("id", "slideshow")
el.setAttribute("networked", { template: "#slideshow-media" } )
el.setAttribute("media-loader", {animate: false, fileIsOwned: true})
el.object3D.position.y = 2;
AFRAME.scenes[0].appendChild(el)
}else{
console.log("a slideshow already exists");
}
}