koel/resources/assets/js/services/utils.js

84 lines
1.9 KiB
JavaScript
Raw Normal View History

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