spee.ch/public/assets/js/generalFunctions.js

212 lines
6.8 KiB
JavaScript
Raw Normal View History

2017-09-29 00:47:55 +02:00
function getRequest (url) {
return new Promise((resolve, reject) => {
let xhttp = new XMLHttpRequest();
xhttp.open('GET', url, true);
xhttp.responseType = 'json';
xhttp.onreadystatechange = () => {
2018-01-17 21:54:26 +01:00
if (xhttp.readyState === 4 ) {
if ( xhttp.status === 200) {
2017-09-29 00:47:55 +02:00
resolve(xhttp.response);
2018-01-17 21:54:26 +01:00
} else if (xhttp.status === 403) {
2017-11-13 20:20:37 +01:00
reject('Wrong channel name or password');
2017-09-29 00:47:55 +02:00
} else {
reject('request failed with status:' + xhttp.status);
};
}
};
xhttp.send();
})
}
function postRequest (url, params) {
return new Promise((resolve, reject) => {
let xhttp = new XMLHttpRequest();
xhttp.open('POST', url, true);
xhttp.responseType = 'json';
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.onreadystatechange = () => {
2018-01-17 21:54:26 +01:00
if (xhttp.readyState === 4 ) {
if ( xhttp.status === 200) {
2017-09-29 00:47:55 +02:00
resolve(xhttp.response);
2018-01-17 21:54:26 +01:00
} else if (xhttp.status === 401) {
2017-11-13 20:20:37 +01:00
reject( new AuthenticationError('Wrong channel name or password'));
2017-09-29 00:47:55 +02:00
} else {
reject('request failed with status:' + xhttp.status);
};
}
};
xhttp.send(params);
})
}
function toggleSection(event){
event.preventDefault();
var dataSet = event.target.dataset;
var status = dataSet.open;
var masterElement = document.getElementById(event.target.id||event.srcElement.id);
var slaveElement = document.getElementById(dataSet.slaveelementid);
var closedLabel = dataSet.closedlabel;
var openLabel = dataSet.openlabel;
if (status === "false") {
slaveElement.hidden = false;
masterElement.innerText = openLabel;
masterElement.dataset.open = "true";
} else {
slaveElement.hidden = true;
masterElement.innerText = closedLabel;
masterElement.dataset.open = "false";
}
}
2017-10-24 02:03:28 +02:00
function createProgressBar(element, size){
var x = 0;
var adder = 1;
2017-10-24 02:03:28 +02:00
// create the bar holder & place it
var barHolder = document.createElement('p');
for (var i = 0; i < size; i++) {
const bar = document.createElement('span');
bar.innerText = '| ';
bar.setAttribute('class', 'progress-bar progress-bar--inactive');
barHolder.appendChild(bar);
}
element.appendChild(barHolder);
// get the bars
const bars = document.getElementsByClassName('progress-bar');
// function to update the bars' classes
function updateOneBar(){
// update the appropriate bar
if (x > -1 && x < size){
if (adder === 1){
bars[x].setAttribute('class', 'progress-bar progress-bar--active');
} else {
bars[x].setAttribute('class', 'progress-bar progress-bar--inactive');
}
}
// set x
if (x === size){
adder = -1;
} else if ( x === -1){
adder = 1;
}
// update the adder
x += adder;
};
2017-10-24 02:03:28 +02:00
// start updater
setInterval(updateOneBar, 300);
}
function setCookie(key, value) {
document.cookie = `${key}=${value}`;
}
function getCookie(cname) {
const name = cname + "=";
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
const channelName = getCookie("channel_name");
if (channelName != "") {
console.log(`cookie found for ${channelName}`);
} else {
console.log('no channel_name cookie found');
}
}
2017-10-11 01:51:07 +02:00
function clearCookie(name) {
document.cookie = `${name}=; expires=Thu, 01-Jan-1970 00:00:01 GMT;`;
}
2018-01-10 20:26:01 +01:00
function setUserCookies(channelName, shortChannelId, channelClaimId) {
2018-01-18 21:43:02 +01:00
setCookie('CHANNEL_NAME', channelName)
setCookie('CHANNEL_SHORT_ID', shortChannelId);
setCookie('CHANNEL_LONG_ID', channelClaimId);
2017-10-11 01:51:07 +02:00
}
2017-10-11 01:51:07 +02:00
function clearUserCookies() {
2018-01-18 21:43:02 +01:00
clearCookie('CHANNEL_NAME')
clearCookie('CHANNEL_SHORT_ID');
clearCookie('CHANNEL_LONG_ID');
}
2017-10-10 19:14:43 +02:00
function copyToClipboard(event){
var elementToCopy = event.target.dataset.elementtocopy;
var element = document.getElementById(elementToCopy);
var errorElement = 'input-error-copy-text' + elementToCopy;
element.select();
try {
document.execCommand('copy');
} catch (err) {
2017-11-10 02:46:50 +01:00
validationFunctions.showError(errorElement, 'Oops, unable to copy');
2017-10-10 19:14:43 +02:00
}
}
// Create new error objects, that prototypically inherit from the Error constructor
function FileError(message) {
this.name = 'FileError';
this.message = message || 'Default Message';
this.stack = (new Error()).stack;
}
FileError.prototype = Object.create(Error.prototype);
FileError.prototype.constructor = FileError;
function NameError(message) {
this.name = 'NameError';
this.message = message || 'Default Message';
this.stack = (new Error()).stack;
}
NameError.prototype = Object.create(Error.prototype);
2017-09-20 00:39:54 +02:00
NameError.prototype.constructor = NameError;
2017-09-20 18:49:05 +02:00
function ChannelNameError(message) {
this.name = 'ChannelNameError';
2017-09-20 00:39:54 +02:00
this.message = message || 'Default Message';
this.stack = (new Error()).stack;
}
2017-09-20 18:49:05 +02:00
ChannelNameError.prototype = Object.create(Error.prototype);
ChannelNameError.prototype.constructor = ChannelNameError;
function ChannelPasswordError(message) {
this.name = 'ChannelPasswordError';
this.message = message || 'Default Message';
this.stack = (new Error()).stack;
}
ChannelPasswordError.prototype = Object.create(Error.prototype);
2017-10-12 23:37:25 +02:00
ChannelPasswordError.prototype.constructor = ChannelPasswordError;
function AuthenticationError(message) {
this.name = 'AuthenticationError';
this.message = message || 'Default Message';
this.stack = (new Error()).stack;
}
AuthenticationError.prototype = Object.create(Error.prototype);
2017-10-25 20:22:15 +02:00
AuthenticationError.prototype.constructor = AuthenticationError;
function showAssetDetails(event) {
var thisAssetHolder = document.getElementById(event.target.id);
var thisAssetImage = thisAssetHolder.firstElementChild;
var thisAssetDetails = thisAssetHolder.lastElementChild;
thisAssetImage.style.opacity = 0.2;
2017-10-30 18:07:23 +01:00
thisAssetDetails.setAttribute('class', 'grid-item-details flex-container--column flex-container--center-center');
2017-10-25 20:22:15 +02:00
}
function hideAssetDetails(event) {
var thisAssetHolder = document.getElementById(event.target.id);
var thisAssetImage = thisAssetHolder.firstElementChild;
var thisAssetDetails = thisAssetHolder.lastElementChild;
thisAssetImage.style.opacity = 1;
thisAssetDetails.setAttribute('class', 'hidden');
}