initial commit
This commit is contained in:
3
js/ejs-utils.js
Normal file
3
js/ejs-utils.js
Normal file
@@ -0,0 +1,3 @@
|
||||
ejs.rtfe = function(templateElement, data) {
|
||||
return ejs.render($('#template-' + templateElement).html(), data);
|
||||
};
|
18
js/index.js
Normal file
18
js/index.js
Normal file
@@ -0,0 +1,18 @@
|
||||
(function($) {
|
||||
if (typeof(n3wz) == 'undefined') {
|
||||
var n3wz = {};
|
||||
n3wz.apiPrefix = '/api/';
|
||||
}
|
||||
|
||||
$.when(
|
||||
$.get('templates.ejs'),
|
||||
$.getJSON(n3wz.apiPrefix + 'newsgroup/it.comp.console/threads')
|
||||
)
|
||||
.then(function(templates, threads) {
|
||||
$('body').append(templates[0]);
|
||||
|
||||
$('.js-articles').html(ejs.rtfe('articles', {
|
||||
threads: threads[0].threads
|
||||
}));
|
||||
});
|
||||
})(jQuery);
|
91
js/utils.js
Normal file
91
js/utils.js
Normal file
@@ -0,0 +1,91 @@
|
||||
//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');
|
||||
};
|
||||
})();
|
Reference in New Issue
Block a user