2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-07-24 13:44:28 +02:00
|
|
|
import Q from 'q';
|
2015-11-09 17:52:09 +01:00
|
|
|
import moment from 'moment';
|
2015-07-24 13:44:28 +02:00
|
|
|
|
2015-07-16 18:27:34 +02:00
|
|
|
import AppConstants from '../constants/application_constants';
|
2015-05-19 17:01:28 +02:00
|
|
|
|
|
|
|
// TODO: Create Unittests that test all functions
|
2015-05-19 13:45:19 +02:00
|
|
|
|
2015-06-02 13:48:01 +02:00
|
|
|
export function status(response) {
|
|
|
|
if (response.status >= 200 && response.status < 300) {
|
2015-06-05 11:06:36 +02:00
|
|
|
return response;
|
2015-05-19 13:45:19 +02:00
|
|
|
}
|
2015-06-05 11:06:36 +02:00
|
|
|
throw new Error(response.json());
|
|
|
|
}
|
2015-06-16 14:01:53 +02:00
|
|
|
|
|
|
|
export function getCookie(name) {
|
2015-07-16 17:04:37 +02:00
|
|
|
let parts = document.cookie.split(';');
|
2015-10-30 17:43:20 +01:00
|
|
|
|
2015-07-16 17:04:37 +02:00
|
|
|
for(let i = 0; i < parts.length; i++) {
|
2015-11-09 17:52:09 +01:00
|
|
|
if(parts[i].indexOf(name + '=') > -1) {
|
2015-07-16 17:04:37 +02:00
|
|
|
return parts[i].split('=').pop();
|
|
|
|
}
|
2015-06-16 14:01:53 +02:00
|
|
|
}
|
2015-06-30 15:41:39 +02:00
|
|
|
}
|
|
|
|
|
2015-11-09 17:52:09 +01:00
|
|
|
export function setCookie(key, value, days) {
|
|
|
|
const exdate = moment();
|
|
|
|
exdate.add(days, 'days');
|
|
|
|
value = window.escape(value) + ((days === null) ? '' : `; expires= ${exdate.utc()}`);
|
|
|
|
document.cookie = `${key}=${value}`;
|
|
|
|
}
|
|
|
|
|
2015-06-30 15:41:39 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Given a url for an image, this method fetches it and returns a promise that resolves to
|
|
|
|
a blob object.
|
|
|
|
It can be used to create a 64base encoded data url.
|
|
|
|
|
|
|
|
Taken from: http://jsfiddle.net/jan_miksovsky/yy7zs/
|
|
|
|
|
2015-07-14 17:12:32 +02:00
|
|
|
CURRENTLY NOT USED...
|
|
|
|
|
2015-06-30 15:41:39 +02:00
|
|
|
*/
|
|
|
|
export function fetchImageAsBlob(url) {
|
2015-07-24 13:44:28 +02:00
|
|
|
return Q.Promise((resolve, reject) => {
|
2015-06-30 15:41:39 +02:00
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.open('GET', url, true);
|
|
|
|
|
|
|
|
// Ask for the result as an ArrayBuffer.
|
|
|
|
xhr.responseType = 'arraybuffer';
|
|
|
|
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if(xhr.readyState === 4 && xhr.status >= 400) {
|
|
|
|
reject(xhr.statusText);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onload = function() {
|
|
|
|
// Obtain a blob: URL for the image data.
|
|
|
|
let arrayBufferView = new Uint8Array(this.response);
|
|
|
|
let blob = new Blob([arrayBufferView], {type: 'image/jpeg'});
|
|
|
|
resolve(blob);
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
});
|
2015-10-30 17:43:20 +01:00
|
|
|
}
|