2015-04-22 07:50:11 +09:00
|
|
|
/**
|
|
|
|
* The global namespace for Telescope utils.
|
|
|
|
* @namespace Telescope.utils
|
|
|
|
*/
|
|
|
|
Telescope.utils = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a camelCase string to dash-separated string
|
|
|
|
* @param {String} str
|
|
|
|
*/
|
2015-06-19 11:52:57 +09:00
|
|
|
Telescope.utils.camelToDash = function (str) {
|
2015-04-22 07:50:11 +09:00
|
|
|
return str.replace(/\W+/g, '-').replace(/([a-z\d])([A-Z])/g, '$1-$2').toLowerCase();
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2015-05-18 10:30:08 +09:00
|
|
|
/**
|
|
|
|
* Convert an underscore-separated string to dash-separated string
|
|
|
|
* @param {String} str
|
|
|
|
*/
|
2015-06-19 11:52:57 +09:00
|
|
|
Telescope.utils.underscoreToDash = function (str) {
|
2015-05-18 10:30:08 +09:00
|
|
|
return str.replace('_', '-');
|
|
|
|
};
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
/**
|
|
|
|
* Convert a dash separated string to camelCase.
|
|
|
|
* @param {String} str
|
|
|
|
*/
|
2015-06-19 11:52:57 +09:00
|
|
|
Telescope.utils.dashToCamel = function (str) {
|
2015-04-22 07:50:11 +09:00
|
|
|
return str.replace(/(\-[a-z])/g, function($1){return $1.toUpperCase().replace('-','');});
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a string to camelCase and remove spaces.
|
|
|
|
* @param {String} str
|
|
|
|
*/
|
|
|
|
Telescope.utils.camelCaseify = function(str) {
|
2015-08-24 11:40:00 +09:00
|
|
|
str = this.dashToCamel(str.replace(' ', '-'));
|
|
|
|
str = str.slice(0,1).toLowerCase() + str.slice(1);
|
|
|
|
return str;
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trim a sentence to a specified amount of words and append an ellipsis.
|
|
|
|
* @param {String} s - Sentence to trim.
|
|
|
|
* @param {Number} numWords - Number of words to trim sentence to.
|
|
|
|
*/
|
|
|
|
Telescope.utils.trimWords = function(s, numWords) {
|
2015-07-14 12:01:32 +09:00
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return s;
|
|
|
|
|
2015-05-01 18:22:00 +02:00
|
|
|
var expString = s.split(/\s+/,numWords);
|
2015-04-22 07:50:11 +09:00
|
|
|
if(expString.length >= numWords)
|
|
|
|
return expString.join(" ")+"…";
|
|
|
|
return s;
|
|
|
|
};
|
|
|
|
|
2015-08-15 12:28:22 +09:00
|
|
|
/**
|
|
|
|
* Trim a block of HTML code to get a clean text excerpt
|
|
|
|
* @param {String} html - HTML to trim.
|
|
|
|
*/
|
|
|
|
Telescope.utils.trimHTML = function (html, numWords) {
|
|
|
|
var text = Telescope.utils.stripHTML(html);
|
|
|
|
return Telescope.utils.trimWords(text, numWords);
|
|
|
|
};
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
/**
|
|
|
|
* Capitalize a string.
|
|
|
|
* @param {String} str
|
|
|
|
*/
|
|
|
|
Telescope.utils.capitalise = function(str) {
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
Telescope.utils.t = function(message) {
|
|
|
|
var d = new Date();
|
|
|
|
console.log("### "+message+" rendered at "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
|
|
|
|
};
|
|
|
|
|
|
|
|
Telescope.utils.nl2br = function(str) {
|
|
|
|
var breakTag = '<br />';
|
|
|
|
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
|
|
|
|
};
|
|
|
|
|
|
|
|
Telescope.utils.scrollPageTo = function(selector) {
|
|
|
|
$('body').scrollTop($(selector).offset().top);
|
|
|
|
};
|
|
|
|
|
|
|
|
Telescope.utils.getDateRange = function(pageNumber) {
|
|
|
|
var now = moment(new Date());
|
|
|
|
var dayToDisplay=now.subtract(pageNumber-1, 'days');
|
|
|
|
var range={};
|
|
|
|
range.start = dayToDisplay.startOf('day').valueOf();
|
|
|
|
range.end = dayToDisplay.endOf('day').valueOf();
|
|
|
|
// console.log("after: ", dayToDisplay.startOf('day').format("dddd, MMMM Do YYYY, h:mm:ss a"));
|
|
|
|
// console.log("before: ", dayToDisplay.endOf('day').format("dddd, MMMM Do YYYY, h:mm:ss a"));
|
|
|
|
return range;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////
|
|
|
|
// URL Helper Functions //
|
|
|
|
//////////////////////////
|
|
|
|
|
2015-06-19 11:52:57 +09:00
|
|
|
/**
|
|
|
|
* Returns the user defined site URL or Meteor.absoluteUrl
|
|
|
|
*/
|
|
|
|
Telescope.utils.getSiteUrl = function () {
|
2016-02-16 15:08:30 +09:00
|
|
|
return Telescope.settings.get('siteUrl', Meteor.absoluteUrl());
|
2015-06-19 11:52:57 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The global namespace for Telescope utils.
|
|
|
|
* @param {String} url - the URL to redirect
|
|
|
|
*/
|
|
|
|
Telescope.utils.getOutgoingUrl = function (url) {
|
2015-09-17 14:51:14 +09:00
|
|
|
return Telescope.utils.getSiteUrl() + "out?url=" + encodeURIComponent(url);
|
2015-06-19 11:52:57 +09:00
|
|
|
};
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
// This function should only ever really be necessary server side
|
|
|
|
// Client side using .path() is a better option since it's relative
|
|
|
|
// and shouldn't care about the siteUrl.
|
2015-06-19 11:52:57 +09:00
|
|
|
Telescope.utils.getRouteUrl = function (routeName, params, options) {
|
2015-04-22 07:50:11 +09:00
|
|
|
options = options || {};
|
2015-09-18 16:27:59 +09:00
|
|
|
var route = FlowRouter.path(
|
2015-04-22 07:50:11 +09:00
|
|
|
routeName,
|
|
|
|
params || {},
|
|
|
|
options
|
|
|
|
);
|
|
|
|
return route;
|
|
|
|
};
|
|
|
|
|
|
|
|
Telescope.utils.getSignupUrl = function() {
|
2016-02-06 12:49:43 +09:00
|
|
|
return this.getRouteUrl('signUp');
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
|
|
|
Telescope.utils.getSigninUrl = function() {
|
2015-12-30 15:52:24 +09:00
|
|
|
return this.getRouteUrl('signIn');
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
|
|
|
|
2015-06-19 11:52:57 +09:00
|
|
|
//TODO: fix this
|
2015-04-22 07:50:11 +09:00
|
|
|
Telescope.utils.getPostCommentUrl = function(postId, commentId) {
|
|
|
|
// get link to a comment on a post page
|
|
|
|
return this.getRouteUrl('post_page_comment', {
|
|
|
|
_id: postId,
|
|
|
|
commentId: commentId
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-06-18 13:04:38 +09:00
|
|
|
Telescope.utils.slugify = function (s) {
|
2015-06-25 12:03:10 +09:00
|
|
|
var slug = getSlug(s, {
|
2015-06-18 13:04:38 +09:00
|
|
|
truncate: 60
|
|
|
|
});
|
2015-06-25 12:03:10 +09:00
|
|
|
|
|
|
|
// can't have posts with an "edit" slug
|
|
|
|
if (slug === "edit") {
|
|
|
|
slug = "edit-1";
|
|
|
|
}
|
|
|
|
|
|
|
|
return slug;
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
|
|
|
|
2015-08-20 10:30:34 +09:00
|
|
|
Telescope.utils.getUnusedSlug = function (collection, slug) {
|
|
|
|
var suffix = "";
|
|
|
|
var index = 0;
|
|
|
|
|
|
|
|
// test if slug is already in use
|
|
|
|
while (!!collection.findOne({slug: slug+suffix})) {
|
|
|
|
index++;
|
|
|
|
suffix = "-"+index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return slug+suffix;
|
|
|
|
};
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
Telescope.utils.getShortUrl = function(post) {
|
|
|
|
return post.shortUrl || post.url;
|
|
|
|
};
|
|
|
|
|
|
|
|
Telescope.utils.getDomain = function(url) {
|
|
|
|
var urlObject = Npm.require('url');
|
|
|
|
return urlObject.parse(url).hostname.replace('www.', '');
|
|
|
|
};
|
|
|
|
|
|
|
|
Telescope.utils.invitesEnabled = function() {
|
2016-02-16 15:08:30 +09:00
|
|
|
return Telescope.settings.get("requireViewInvite") || Telescope.settings.get("requirePostInvite");
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
|
|
|
|
2015-07-27 15:15:38 +09:00
|
|
|
// add http: if missing
|
|
|
|
Telescope.utils.addHttp = function (url) {
|
|
|
|
if (url.substring(0, 5) !== "http:" && url.substring(0, 6) !== "https:") {
|
|
|
|
url = "http:"+url;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
/////////////////////////////
|
|
|
|
// String Helper Functions //
|
|
|
|
/////////////////////////////
|
|
|
|
|
|
|
|
Telescope.utils.cleanUp = function(s) {
|
|
|
|
return this.stripHTML(s);
|
|
|
|
};
|
|
|
|
|
|
|
|
Telescope.utils.sanitize = function(s) {
|
|
|
|
// console.log('// before sanitization:')
|
|
|
|
// console.log(s)
|
|
|
|
if(Meteor.isServer){
|
2015-05-01 18:22:00 +02:00
|
|
|
s = sanitizeHtml(s, {
|
2015-04-22 07:50:11 +09:00
|
|
|
allowedTags: [
|
|
|
|
'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul',
|
|
|
|
'ol', 'nl', 'li', 'b', 'i', 'strong', 'em', 'strike',
|
|
|
|
'code', 'hr', 'br', 'div', 'table', 'thead', 'caption',
|
|
|
|
'tbody', 'tr', 'th', 'td', 'pre', 'img'
|
|
|
|
]
|
|
|
|
});
|
|
|
|
// console.log('// after sanitization:')
|
|
|
|
// console.log(s)
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
};
|
|
|
|
|
|
|
|
Telescope.utils.stripHTML = function(s) {
|
|
|
|
return s.replace(/<(?:.|\n)*?>/gm, '');
|
|
|
|
};
|
|
|
|
|
|
|
|
Telescope.utils.stripMarkdown = function(s) {
|
2015-08-15 12:28:22 +09:00
|
|
|
var htmlBody = marked(s);
|
|
|
|
return Telescope.utils.stripHTML(htmlBody);
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/2631001/javascript-test-for-existence-of-nested-object-key
|
|
|
|
Telescope.utils.checkNested = function(obj /*, level1, level2, ... levelN*/) {
|
2015-05-01 18:22:00 +02:00
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
obj = args.shift();
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
for (var i = 0; i < args.length; i++) {
|
|
|
|
if (!obj.hasOwnProperty(args[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
obj = obj[args[i]];
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2015-04-22 11:49:42 +09:00
|
|
|
|
2015-04-23 10:39:58 +09:00
|
|
|
Telescope.log = function (s) {
|
2016-02-16 15:08:30 +09:00
|
|
|
if(Telescope.settings.get('debug', false) || process.env.NODE_ENV === "development") {
|
2015-04-22 11:49:42 +09:00
|
|
|
console.log(s);
|
2016-01-07 18:46:17 +01:00
|
|
|
}
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-05-06 12:56:59 +09:00
|
|
|
|
|
|
|
// see http://stackoverflow.com/questions/8051975/access-object-child-properties-using-a-dot-notation-string
|
|
|
|
Telescope.getNestedProperty = function (obj, desc) {
|
|
|
|
var arr = desc.split(".");
|
|
|
|
while(arr.length && (obj = obj[arr.shift()]));
|
|
|
|
return obj;
|
2015-07-27 15:15:38 +09:00
|
|
|
};
|