2015-02-19 21:21:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* Service which exposes various utility methods.
|
|
|
|
|
*/
|
2017-07-31 16:40:37 +00:00
|
|
|
|
angular.module('quay').factory('UtilService', ['$sanitize', 'markdownConverter',
|
|
|
|
|
function($sanitize, markdownConverter) {
|
2017-05-16 20:12:03 +00:00
|
|
|
|
var utilService = {};
|
|
|
|
|
|
|
|
|
|
var adBlockEnabled = null;
|
|
|
|
|
|
|
|
|
|
utilService.isAdBlockEnabled = function(callback) {
|
|
|
|
|
if (adBlockEnabled !== null) {
|
|
|
|
|
callback(adBlockEnabled);
|
|
|
|
|
return;
|
2015-02-23 21:43:59 +00:00
|
|
|
|
}
|
2015-02-19 21:21:54 +00:00
|
|
|
|
|
2017-05-16 20:12:03 +00:00
|
|
|
|
if(typeof blockAdBlock === 'undefined') {
|
|
|
|
|
callback(true);
|
|
|
|
|
return;
|
2015-02-19 21:21:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 20:12:03 +00:00
|
|
|
|
var bab = new BlockAdBlock({
|
|
|
|
|
checkOnLoad: false,
|
|
|
|
|
resetOnEnd: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
bab.onDetected(function() { adBlockEnabled = true; callback(true); });
|
|
|
|
|
bab.onNotDetected(function() { adBlockEnabled = false; callback(false); });
|
|
|
|
|
bab.check();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
utilService.isEmailAddress = function(val) {
|
|
|
|
|
var emailRegex = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
|
|
|
|
|
return emailRegex.test(val);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
utilService.getMarkedDown = function(string) {
|
2017-07-31 16:40:37 +00:00
|
|
|
|
return markdownConverter.makeHtml(string || '');
|
2017-05-16 20:12:03 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
utilService.getFirstMarkdownLineAsText = function(commentString, placeholderNeeded) {
|
|
|
|
|
if (!commentString) {
|
|
|
|
|
if (placeholderNeeded) {
|
|
|
|
|
return '<p style="visibility:hidden">placeholder</p>';
|
|
|
|
|
}
|
|
|
|
|
return '';
|
2015-02-19 21:21:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 20:12:03 +00:00
|
|
|
|
var lines = commentString.split('\n');
|
|
|
|
|
var MARKDOWN_CHARS = {
|
|
|
|
|
'#': true,
|
|
|
|
|
'-': true,
|
|
|
|
|
'>': true,
|
|
|
|
|
'`': true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < lines.length; ++i) {
|
|
|
|
|
// Skip code lines.
|
|
|
|
|
if (lines[i].indexOf(' ') == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip empty lines.
|
|
|
|
|
if ($.trim(lines[i]).length == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip control lines.
|
|
|
|
|
if (MARKDOWN_CHARS[$.trim(lines[i])[0]]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilService.getMarkedDown(lines[i]);
|
2015-02-19 21:21:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 20:12:03 +00:00
|
|
|
|
return '';
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-13 14:12:46 +00:00
|
|
|
|
utilService.getFirstMarkdownLineAsString = function(commentString) {
|
|
|
|
|
return utilService.getFirstMarkdownLineAsText(commentString, false).replace('</p>', '')
|
|
|
|
|
.replace('<p>', '');
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-16 20:12:03 +00:00
|
|
|
|
utilService.escapeHtmlString = function(text) {
|
|
|
|
|
var textStr = (text || '').toString();
|
|
|
|
|
var adjusted = textStr.replace(/&/g, "&")
|
|
|
|
|
.replace(/</g, "<")
|
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
|
.replace(/"/g, """)
|
|
|
|
|
.replace(/'/g, "'");
|
|
|
|
|
|
|
|
|
|
return adjusted;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
utilService.stringToHTML = function(text) {
|
|
|
|
|
text = utilService.escapeHtmlString(text);
|
|
|
|
|
text = text.replace(/\n/g, '<br>');
|
|
|
|
|
return text;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
utilService.getRestUrl = function(args) {
|
|
|
|
|
var url = '';
|
|
|
|
|
for (var i = 0; i < arguments.length; ++i) {
|
|
|
|
|
if (i > 0) {
|
|
|
|
|
url += '/';
|
|
|
|
|
}
|
|
|
|
|
url += encodeURI(arguments[i])
|
2015-02-19 21:21:54 +00:00
|
|
|
|
}
|
2017-05-16 20:12:03 +00:00
|
|
|
|
return url;
|
|
|
|
|
};
|
2015-02-19 21:21:54 +00:00
|
|
|
|
|
2017-05-16 20:12:03 +00:00
|
|
|
|
utilService.textToSafeHtml = function(text) {
|
|
|
|
|
return $sanitize(utilService.escapeHtmlString(text));
|
|
|
|
|
};
|
2015-02-19 21:21:54 +00:00
|
|
|
|
|
2017-05-16 20:12:03 +00:00
|
|
|
|
return utilService;
|
2015-02-19 21:21:54 +00:00
|
|
|
|
}]);
|