Files
junisaber/src/components/search.js

93 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-07-18 20:48:45 +02:00
var algoliasearch = require('algoliasearch/lite');
2018-07-20 09:34:52 +02:00
var bindEvent = require('aframe-event-decorators').bindEvent;
2018-07-18 20:48:45 +02:00
var client = algoliasearch('QULTOY3ZWU', 'be07164192471df7e97e6fa70c1d041d');
2018-07-22 11:54:26 +02:00
var algolia = client.initIndex('supersaber');
2018-07-18 20:48:45 +02:00
/**
* Search (including the initial list of popular searches).
2018-07-20 15:42:47 +02:00
* Attached to super-keyboard.
2018-07-18 20:48:45 +02:00
*/
AFRAME.registerComponent('search', {
2018-10-13 17:19:36 -07:00
init: function () {
2018-07-18 20:48:45 +02:00
this.eventDetail = {results: []};
2018-09-19 05:54:05 -07:00
this.popularHits = null;
this.queryObject = {hitsPerPage: 100, query: ''};
2018-07-18 20:48:45 +02:00
// Populate popular.
this.search('');
2018-09-19 05:54:05 -07:00
// Less hits on normal searches.
this.queryObject.hitsPerPage = 30;
2018-07-18 20:48:45 +02:00
},
2018-07-20 15:42:47 +02:00
superkeyboardchange: bindEvent(function (evt) {
this.search(evt.detail.value);
}),
2018-07-18 20:48:45 +02:00
search: function (query) {
2018-09-19 05:54:05 -07:00
// Use cached for popular hits.
if (!query && this.popularHits) {
2018-10-04 01:01:31 -07:00
this.eventDetail.results = this.popularHits;
2018-09-19 05:54:05 -07:00
this.el.sceneEl.emit('searchresults', this.eventDetail);
return;
}
2018-07-18 20:48:45 +02:00
this.queryObject.query = query;
2018-07-22 11:54:26 +02:00
algolia.search(this.queryObject, (err, content) => {
2018-09-19 05:54:05 -07:00
// Cache popular hits.
if (err) {
this.el.sceneEl.emit('searcherror', null, false);
console.error(err);
return;
}
2018-09-19 05:54:05 -07:00
if (!query) { this.popularHits = content.hits; }
2018-07-18 20:48:45 +02:00
this.eventDetail.results = content.hits;
this.el.sceneEl.emit('searchresults', this.eventDetail);
});
}
});
/**
* Click listener for search result.
*/
2018-07-20 19:58:09 +02:00
AFRAME.registerComponent('search-result-list', {
init: function () {
const obv = new MutationObserver(mutations => {
for (let i = 0; i < mutations.length; i++) {
if (mutations[i].attributeName === 'data-index') {
this.refreshLayout();
}
}
});
obv.observe(this.el, {attributes: true, childList: false, subtree: true});
},
refreshLayout: function () {
this.el.emit('layoutrefresh', null, false);
},
2018-07-20 19:58:09 +02:00
click: bindEvent(function (evt) {
2018-07-20 20:47:42 +02:00
this.el.sceneEl.emit('menuchallengeselect',
evt.target.closest('.searchResult').dataset.id,
2018-07-20 19:58:09 +02:00
false);
2018-10-13 17:19:36 -07:00
})
2018-07-18 20:48:45 +02:00
});
2018-09-19 05:54:05 -07:00
AFRAME.registerComponent('search-song-name-selected', {
schema: {
anchor: {default: 0},
index: {default: 0},
offset: {default: 0},
selectedChallengeId: {default: ''}
},
update: function () {
const data = this.data;
const el = this.el;
el.object3D.visible = !!data.selectedChallengeId && data.index !== -1;
el.object3D.position.y = data.index * data.offset + data.anchor;
}
});