2016-01-13 13:27:42 +00:00
|
|
|
import isMobile from 'ismobilejs';
|
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
/**
|
|
|
|
* Convert a duration in seconds into H:i:s format.
|
|
|
|
* If H is 0, it will be ommited.
|
|
|
|
*/
|
|
|
|
export function secondsToHis(d) {
|
|
|
|
d = parseInt(d, 10);
|
2016-03-06 07:44:38 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
let s = d % 60;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
if (s < 10) {
|
|
|
|
s = '0' + s;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
let i = Math.floor((d / 60) % 60);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
if (i < 10) {
|
|
|
|
i = '0' + i;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
let h = Math.floor(d / 3600);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
if (h < 10) {
|
|
|
|
h = '0' + h;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
return (h === '00' ? '' : h + ':') + i + ':' + s;
|
|
|
|
};
|
2015-12-15 16:28:54 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
/**
|
|
|
|
* Parse the validation error from the server into a flattened array of messages.
|
|
|
|
*
|
|
|
|
* @param {Object} error The error object in JSON format.
|
|
|
|
*
|
|
|
|
* @return {Array.<String>}
|
|
|
|
*/
|
|
|
|
export function parseValidationError(error) {
|
|
|
|
return Object.keys(error).reduce((messages, field) => messages.concat(error[field]), []);
|
|
|
|
};
|
2016-01-11 15:25:58 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
/**
|
|
|
|
* Check if AudioContext is supported by the current browser.
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
export function isAudioContextSupported() {
|
|
|
|
// Apple device just doesn't love AudioContext that much.
|
|
|
|
if (isMobile.apple.device) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-13 13:27:42 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
const AudioContext = (window.AudioContext ||
|
|
|
|
window.webkitAudioContext ||
|
|
|
|
window.mozAudioContext ||
|
|
|
|
window.oAudioContext ||
|
|
|
|
window.msAudioContext);
|
2016-01-11 15:25:58 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
if (!AudioContext) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-11 15:25:58 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
// Safari (MacOS & iOS alike) has webkitAudioContext, but is buggy.
|
|
|
|
// @link http://caniuse.com/#search=audiocontext
|
|
|
|
if (!(new AudioContext()).createMediaElementSource) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-11 15:25:58 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
return true;
|
|
|
|
};
|
2016-03-06 07:44:38 +00:00
|
|
|
|
2016-04-10 09:51:06 +00:00
|
|
|
/**
|
|
|
|
* Turn <br> into new line characters.
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
*
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
export function br2nl(str) {
|
|
|
|
return str.replace(/<br\s*[\/]?>/gi, '\n');
|
2015-12-13 04:42:28 +00:00
|
|
|
};
|