changed Asset and ProgressBar into constructors

This commit is contained in:
bill bittner 2017-11-29 21:36:30 -08:00
parent d619281908
commit 0f55da1349
6 changed files with 89 additions and 86 deletions

View file

@ -1,9 +1,9 @@
const showFunctions = { const Asset = function () {
state: {}, this.state = {};
setState: function(key, value) { this.setState = function(key, value) {
this.state[key] = value; this.state[key] = value;
}, };
addPlayPauseToVideo: function () { this.addPlayPauseToVideo = function () {
const that = this; const that = this;
const video = document.getElementById('video-asset'); const video = document.getElementById('video-asset');
if (video) { if (video) {
@ -18,16 +18,16 @@ const showFunctions = {
} }
}; };
} }
}, };
playOrPause: function(video){ this.playOrPause = function(video){
if (video.paused == true) { if (video.paused == true) {
video.play(); video.play();
} }
else{ else{
video.pause(); video.pause();
} }
}, };
showAsset: function () { this.showAsset = function () {
this.hideAssetStatus(); this.hideAssetStatus();
this.showAssetHolder(); this.showAssetHolder();
if (!this.state.src) { if (!this.state.src) {
@ -41,33 +41,33 @@ const showFunctions = {
} else { } else {
this.showImage(); this.showImage();
} }
}, };
showVideo: function () { this.showVideo = function () {
console.log('showing video', this.state.src); console.log('showing video', this.state.src);
const video = document.getElementById('video-asset'); const video = document.getElementById('video-asset');
const source = document.createElement('source'); const source = document.createElement('source');
source.setAttribute('src', this.state.src); source.setAttribute('src', this.state.src);
video.appendChild(source); video.appendChild(source);
video.play(); video.play();
}, };
showImage: function () { this.showImage = function () {
console.log('showing image', this.state.src); console.log('showing image', this.state.src);
const asset = document.getElementById('image-asset'); const asset = document.getElementById('image-asset');
asset.setAttribute('src', this.state.src); asset.setAttribute('src', this.state.src);
}, };
hideAssetStatus: function () { this.hideAssetStatus = function () {
const assetStatus = document.getElementById('asset-status'); const assetStatus = document.getElementById('asset-status');
assetStatus.hidden = true; assetStatus.hidden = true;
}, };
showAssetHolder: function () { this.showAssetHolder =function () {
const assetHolder = document.getElementById('asset-holder'); const assetHolder = document.getElementById('asset-holder');
assetHolder.hidden = false; assetHolder.hidden = false;
}, };
showSearchMessage: function () { this.showSearchMessage = function () {
const searchMessage = document.getElementById('searching-message'); const searchMessage = document.getElementById('searching-message');
searchMessage.hidden = false; searchMessage.hidden = false;
}, };
showFailureMessage: function (msg) { this.showFailureMessage = function (msg) {
console.log(msg); console.log(msg);
const searchMessage = document.getElementById('searching-message'); const searchMessage = document.getElementById('searching-message');
const failureMessage = document.getElementById('failure-message'); const failureMessage = document.getElementById('failure-message');
@ -75,8 +75,8 @@ const showFunctions = {
searchMessage.hidden = true; searchMessage.hidden = true;
failureMessage.hidden = false; failureMessage.hidden = false;
errorMessage.innerText = msg; errorMessage.innerText = msg;
}, };
checkClaimAvailability: function () { this.checkClaimAvailability = function () {
const that = this; const that = this;
const uri = `/api/local-file-available/${this.state.claimName}/${this.state.claimId}`; const uri = `/api/local-file-available/${this.state.claimName}/${this.state.claimId}`;
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
@ -100,8 +100,8 @@ const showFunctions = {
} }
}; };
xhr.send(); xhr.send();
}, };
getAsset: function() { this.getAsset = function() {
const that = this; const that = this;
const uri = `/api/get-claim/${this.state.claimName}/${this.state.claimId}`; const uri = `/api/get-claim/${this.state.claimName}/${this.state.claimId}`;
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
@ -120,5 +120,5 @@ const showFunctions = {
} }
}; };
xhr.send(); xhr.send();
}, };
}; };

View file

@ -0,0 +1,48 @@
const ProgressBar = function() {
this.state = {
x: 0,
adder: 1,
bars: [],
};
this.setState = function (key, value) {
this.state[key] = value;
};
this.barHolder = document.getElementById('bar-holder');
this.createProgressBar = function (size) {
this.setState('size', size);
for (var i = 0; i < size; i++) {
const bar = document.createElement('span');
bar.innerText = '| ';
bar.setAttribute('class', 'progress-bar progress-bar--inactive');
this.barHolder.appendChild(bar);
this.state.bars.push(bar);
}
};
this.startProgressBar = function () {
this.updateInterval = setInterval(this.updateProgressBar.bind(this), 300);
};
this.updateProgressBar = function () {
const x = this.state.x;
const adder = this.state.adder;
const size = this.state.size;
// update the appropriate bar
if (x > -1 && x < size){
if (adder === 1){
this.state.bars[x].setAttribute('class', 'progress-bar progress-bar--active');
} else {
this.state.bars[x].setAttribute('class', 'progress-bar progress-bar--inactive');
}
}
// update adder
if (x === size){
this.setState('adder', -1);
} else if ( x === -1){
this.setState('adder', 1);
}
// update x
this.setState('x', x + adder);
};
this.stopProgressBar = function () {
clearInterval(this.updateInterval);
};
};

View file

@ -21,7 +21,7 @@
<body id="show-body"> <body id="show-body">
<script src="/assets/js/generalFunctions.js"></script> <script src="/assets/js/generalFunctions.js"></script>
<script src="/assets/js/navBarFunctions.js"></script> <script src="/assets/js/navBarFunctions.js"></script>
<script src="/assets/js/showFunctions.js"></script> <script src="/assets/js/assetConstructor.js"></script>
{{> navBar}} {{> navBar}}
{{{ body }}} {{{ body }}}
</body> </body>

View file

@ -17,7 +17,7 @@
{{ googleAnalytics }} {{ googleAnalytics }}
</head> </head>
<body id="showlite-body"> <body id="showlite-body">
<script src="/assets/js/showFunctions.js"></script> <script src="/assets/js/assetConstructor.js"></script>
{{{ body }}} {{{ body }}}
</body> </body>
</html> </html>

View file

@ -3,6 +3,7 @@
<div id="searching-message" hidden="true"> <div id="searching-message" hidden="true">
<p>Sit tight, we're searching the LBRY blockchain for your asset!</p> <p>Sit tight, we're searching the LBRY blockchain for your asset!</p>
{{> progressBar}} {{> progressBar}}
<p>Curious what magic is happening here? <a class="link--primary" target="blank" href="https://lbry.io/faq/what-is-lbry">Learn more.</a></p>
</div> </div>
<div id="failure-message" hidden="true"> <div id="failure-message" hidden="true">
<p>Unfortunately, we couldn't download your asset from LBRY. You can help us out by sharing the below error message in the <a class="link--primary" href="https://discord.gg/YjYbwhS" target="_blank">LBRY discord</a>.</p> <p>Unfortunately, we couldn't download your asset from LBRY. You can help us out by sharing the below error message in the <a class="link--primary" href="https://discord.gg/YjYbwhS" target="_blank">LBRY discord</a>.</p>
@ -24,12 +25,13 @@
<script type="text/javascript"> <script type="text/javascript">
showFunctions.setState('src', '/{{claimInfo.claimId}}/{{claimInfo.name}}.{{claimInfo.fileExt}}'); const asset = new Asset();
showFunctions.setState('claimName', '{{claimInfo.name}}'); asset.setState('src', '/{{claimInfo.claimId}}/{{claimInfo.name}}.{{claimInfo.fileExt}}');
showFunctions.setState('claimId', '{{claimInfo.claimId}}'); asset.setState('claimName', '{{claimInfo.name}}');
showFunctions.setState('fileExt', '{{claimInfo.fileExt}}'); asset.setState('claimId', '{{claimInfo.claimId}}');
showFunctions.setState('contentType', '{{claimInfo.contentType}}'); asset.setState('fileExt', '{{claimInfo.fileExt}}');
console.log(showFunctions.state); asset.setState('contentType', '{{claimInfo.contentType}}');
showFunctions.checkClaimAvailability(); console.log('asset state:', asset.state);
asset.checkClaimAvailability();
</script> </script>

View file

@ -1,55 +1,8 @@
<p id="bar-holder"></p> <p id="bar-holder"></p>
<script src="/assets/js/progressBarConstructor.js"></script>
<script type="text/javascript"> <script type="text/javascript">
const progressBarFunctions = { const progressBar = new ProgressBar();
state: { progressBar.createProgressBar(10);
x: 0, progressBar.startProgressBar();
adder: 1,
bars: [],
},
setState: function (key, value) {
this.state[key] = value;
},
barHolder: document.getElementById('bar-holder'),
createProgressBar: function (size) {
this.setState('size', size);
for (var i = 0; i < size; i++) {
const bar = document.createElement('span');
bar.innerText = '| ';
bar.setAttribute('class', 'progress-bar progress-bar--inactive');
this.barHolder.appendChild(bar);
this.state.bars.push(bar);
}
},
startProgressBar: function () {
this.updateInterval = setInterval(this.updateProgressBar.bind(this), 300);
},
updateProgressBar: function () {
const x = this.state.x;
const adder = this.state.adder;
const size = this.state.size;
// update the appropriate bar
if (x > -1 && x < size){
if (adder === 1){
this.state.bars[x].setAttribute('class', 'progress-bar progress-bar--active');
} else {
this.state.bars[x].setAttribute('class', 'progress-bar progress-bar--inactive');
}
}
// update adder
if (x === size){
this.setState('adder', -1);
} else if ( x === -1){
this.setState('adder', 1);
}
// update x
this.setState('x', x + adder);
},
stopProgressBar: function () {
clearInterval(this.updateInterval);
},
}
progressBarFunctions.createProgressBar(10);
progressBarFunctions.startProgressBar();
</script> </script>