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) {
2016-03-13 17:00:32 +00:00
d = parseInt(d, 10);
2016-03-06 07:44:38 +00:00
2016-03-16 03:51:07 +00:00
let s = d % 60;
2015-12-13 04:42:28 +00:00
if (s < 10) {
s = '0' + s;
}
2016-03-16 03:51:07 +00:00
let i = Math.floor((d / 60) % 60);
2015-12-13 04:42:28 +00:00
if (i < 10) {
i = '0' + i;
}
2016-03-16 03:51:07 +00:00
let h = Math.floor(d / 3600);
2015-12-13 04:42:28 +00:00
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-16 03:51:07 +00:00
let AudioContext = (window.AudioContext ||
2016-03-06 07:44:38 +00:00
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) {
2016-03-13 17:00:32 +00:00
return str.replace(/<br\s*[\/]?>/gi, '\n');
2016-03-06 07:44:38 +00:00
}
2015-12-13 04:42:28 +00:00
};