92 lines
2.3 KiB
JavaScript
92 lines
2.3 KiB
JavaScript
//utils
|
|
(function() {
|
|
window.Utils = {};
|
|
|
|
Utils.uuidv4 = function() {
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
var r = Math.random() * 16 | 0;
|
|
var v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
return v.toString(16);
|
|
});
|
|
};
|
|
|
|
Utils.ellipsis = function(text, maxLength) {
|
|
if (typeof maxLength === 'undefined') {
|
|
maxLength = 9000; //a large number
|
|
}
|
|
if (typeof text === 'undefined') {
|
|
return '';
|
|
}
|
|
if (text.length <= maxLength) {
|
|
return text;
|
|
}
|
|
|
|
var xMaxFit = maxLength - 1;
|
|
var xTruncateAt = text.lastIndexOf(' ', xMaxFit);
|
|
if (xTruncateAt == -1 || xTruncateAt < maxLength / 2) {
|
|
xTruncateAt = xMaxFit;
|
|
}
|
|
|
|
return text.substr(0, xTruncateAt) + '…';
|
|
};
|
|
|
|
Utils.getSelectionText = function() {
|
|
var text = '';
|
|
if (window.getSelection) {
|
|
text = window.getSelection().toString();
|
|
} else if (document.selection && document.selection.type != 'Control') {
|
|
text = document.selection.createRange().text;
|
|
}
|
|
return text;
|
|
};
|
|
|
|
Utils.getParameterByName = function(name) {
|
|
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
|
|
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
|
|
var results = regex.exec(location.search);
|
|
return results == null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
|
|
};
|
|
|
|
Utils.isElementInViewport = function(el) {
|
|
//special bonus for those using jQuery
|
|
if (typeof jQuery === 'function' && el instanceof jQuery) {
|
|
el = el[0];
|
|
}
|
|
|
|
var rect = el.getBoundingClientRect();
|
|
|
|
return (
|
|
rect.top >= 0 &&
|
|
rect.left >= 0 &&
|
|
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
|
|
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
|
|
);
|
|
};
|
|
|
|
Utils.getUserColor = function(userstring) {
|
|
var hashCode = function(str) {
|
|
var hash = 0;
|
|
for (var i = 0; i < str.length; i++) {
|
|
hash = str.charCodeAt(i) + ((hash << 5) - hash);
|
|
}
|
|
return hash;
|
|
};
|
|
|
|
var intToRGB = function(i) {
|
|
var c = (i & 0x00FFFFFF)
|
|
.toString(16)
|
|
.toUpperCase();
|
|
|
|
return '#00000'.substring(0, 6 - c.length) + c;
|
|
};
|
|
|
|
var intToHSL = function(i) {
|
|
var shortened = Math.abs(i) % 360;
|
|
var l = 40 - (Math.abs(i) % 20);
|
|
return 'hsl(' + shortened + ',100%,' + l + '%)';
|
|
};
|
|
|
|
return (userstring ? intToHSL(hashCode(userstring)) : '3a87ad');
|
|
};
|
|
})();
|