2017-11-30 06:36:30 +01:00
|
|
|
const Asset = function () {
|
|
|
|
this.state = {};
|
|
|
|
this.setState = function(key, value) {
|
2017-11-30 02:13:49 +01:00
|
|
|
this.state[key] = value;
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
|
|
|
this.addPlayPauseToVideo = function () {
|
2017-11-30 01:02:40 +01:00
|
|
|
const that = this;
|
|
|
|
const video = document.getElementById('video-asset');
|
|
|
|
if (video) {
|
|
|
|
// add event listener for click
|
|
|
|
video.addEventListener('click', ()=> {
|
|
|
|
that.playOrPause(video);
|
|
|
|
});
|
|
|
|
// add event listener for space bar
|
|
|
|
document.body.onkeyup = (event) => {
|
|
|
|
if (event.keyCode == 32) {
|
|
|
|
that.playOrPause(video);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
|
|
|
this.playOrPause = function(video){
|
2017-11-30 01:02:40 +01:00
|
|
|
if (video.paused == true) {
|
|
|
|
video.play();
|
2017-10-17 19:10:26 +02:00
|
|
|
}
|
2017-11-30 01:02:40 +01:00
|
|
|
else{
|
|
|
|
video.pause();
|
|
|
|
}
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
|
|
|
this.showAsset = function () {
|
2017-11-30 02:13:49 +01:00
|
|
|
this.hideAssetStatus();
|
|
|
|
this.showAssetHolder();
|
|
|
|
if (!this.state.src) {
|
|
|
|
return console.log('error: src is not set')
|
|
|
|
}
|
|
|
|
if (!this.state.contentType) {
|
|
|
|
return console.log('error: contentType is not set')
|
|
|
|
}
|
|
|
|
if (this.state.contentType === 'video/mp4') {
|
|
|
|
this.showVideo();
|
|
|
|
} else {
|
|
|
|
this.showImage();
|
|
|
|
}
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
|
|
|
this.showVideo = function () {
|
2017-11-30 02:13:49 +01:00
|
|
|
console.log('showing video', this.state.src);
|
|
|
|
const video = document.getElementById('video-asset');
|
|
|
|
const source = document.createElement('source');
|
|
|
|
source.setAttribute('src', this.state.src);
|
|
|
|
video.appendChild(source);
|
|
|
|
video.play();
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
|
|
|
this.showImage = function () {
|
2017-11-30 02:13:49 +01:00
|
|
|
console.log('showing image', this.state.src);
|
|
|
|
const asset = document.getElementById('image-asset');
|
|
|
|
asset.setAttribute('src', this.state.src);
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
|
|
|
this.hideAssetStatus = function () {
|
2017-11-30 02:13:49 +01:00
|
|
|
const assetStatus = document.getElementById('asset-status');
|
|
|
|
assetStatus.hidden = true;
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
|
|
|
this.showAssetHolder =function () {
|
2017-11-30 02:13:49 +01:00
|
|
|
const assetHolder = document.getElementById('asset-holder');
|
|
|
|
assetHolder.hidden = false;
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
|
|
|
this.showSearchMessage = function () {
|
2017-11-30 02:13:49 +01:00
|
|
|
const searchMessage = document.getElementById('searching-message');
|
|
|
|
searchMessage.hidden = false;
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
|
|
|
this.showFailureMessage = function (msg) {
|
2017-11-30 02:13:49 +01:00
|
|
|
console.log(msg);
|
|
|
|
const searchMessage = document.getElementById('searching-message');
|
|
|
|
const failureMessage = document.getElementById('failure-message');
|
|
|
|
const errorMessage = document.getElementById('error-message');
|
|
|
|
searchMessage.hidden = true;
|
|
|
|
failureMessage.hidden = false;
|
|
|
|
errorMessage.innerText = msg;
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
|
|
|
this.checkClaimAvailability = function () {
|
2017-11-30 02:13:49 +01:00
|
|
|
const that = this;
|
2017-12-05 19:18:49 +01:00
|
|
|
const uri = `/api/file-is-available/${this.state.claimName}/${this.state.claimId}`;
|
2017-11-30 02:13:49 +01:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
console.log(`checking local availability for ${this.state.claimName}#${this.state.claimId}`)
|
|
|
|
xhr.open("GET", uri, true);
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState == 4) {
|
|
|
|
const response = JSON.parse(xhr.response);
|
|
|
|
if (xhr.status == 200) {
|
|
|
|
if (response.message === true) {
|
|
|
|
console.log('local asset is available on spee.ch')
|
|
|
|
that.showAsset();
|
|
|
|
} else {
|
|
|
|
that.showSearchMessage();
|
|
|
|
that.getAsset()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('get failed:', response);
|
|
|
|
that.showFailureMessage('Well this sucks, but we can\'t seem to phone home');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send();
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
|
|
|
this.getAsset = function() {
|
2017-11-30 02:13:49 +01:00
|
|
|
const that = this;
|
2017-12-05 19:18:49 +01:00
|
|
|
const uri = `/api/claim-get/${this.state.claimName}/${this.state.claimId}`;
|
2017-11-30 02:13:49 +01:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
console.log(`getting ${this.state.claimName}#${this.state.claimId}`)
|
|
|
|
xhr.open("GET", uri, true);
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState == 4) {
|
|
|
|
const response = JSON.parse(xhr.response);
|
|
|
|
if (xhr.status == 200) {
|
|
|
|
console.log('get returned successfully', response);
|
|
|
|
that.showAsset();
|
|
|
|
} else {
|
|
|
|
console.log('get failed:', response);
|
|
|
|
that.showFailureMessage(`${response.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send();
|
2017-11-30 06:36:30 +01:00
|
|
|
};
|
2017-11-30 02:13:49 +01:00
|
|
|
};
|