2013-02-27 04:34:44 +00:00
|
|
|
/**
|
|
|
|
|
* Helpers for Pinry
|
|
|
|
|
* Descrip: A hodgepodge of useful things to help clean up Pinry's JavaScript.
|
|
|
|
|
* Authors: Pinry Contributors
|
|
|
|
|
* Updated: Feb 26th, 2013
|
|
|
|
|
* Require: jQuery
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2019-02-21 13:22:28 +08:00
|
|
|
function _getCookie(name) {
|
|
|
|
|
var cookieValue = null;
|
|
|
|
|
if (document.cookie && document.cookie !== '') {
|
|
|
|
|
var cookies = document.cookie.split(';');
|
|
|
|
|
for (var i = 0; i < cookies.length; i++) {
|
|
|
|
|
var cookie = jQuery.trim(cookies[i]);
|
|
|
|
|
// Does this cookie string begin with the name we want?
|
|
|
|
|
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
|
|
|
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return cookieValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getCSRFToken() {
|
|
|
|
|
return _getCookie('csrftoken');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function csrfSafeMethod(method) {
|
|
|
|
|
// these HTTP methods do not require CSRF protection
|
|
|
|
|
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
|
|
|
|
|
}
|
|
|
|
|
$.ajaxSetup({
|
|
|
|
|
beforeSend: function(xhr, settings) {
|
|
|
|
|
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
|
|
|
|
|
xhr.setRequestHeader("X-CSRFToken", getCSRFToken());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2013-02-27 04:34:44 +00:00
|
|
|
function renderTemplate(templateId, context) {
|
|
|
|
|
var template = Handlebars.compile($(templateId).html());
|
|
|
|
|
return template(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function cleanTags(tags) {
|
2013-03-03 13:45:27 +00:00
|
|
|
if (typeof tags === 'string' && tags.length > 0) {
|
|
|
|
|
tags = tags.split(/[\s,]+/);
|
|
|
|
|
for (var i in tags) {
|
|
|
|
|
tags[i] = tags[i].trim();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2013-03-03 13:55:35 +00:00
|
|
|
return [];
|
2013-02-27 04:34:44 +00:00
|
|
|
}
|
2013-02-27 07:38:38 +00:00
|
|
|
return tags;
|
2013-02-27 04:34:44 +00:00
|
|
|
}
|
2013-02-27 06:03:05 +00:00
|
|
|
|
|
|
|
|
|
2013-02-27 22:44:56 +00:00
|
|
|
function getImageData(imageId) {
|
|
|
|
|
var apiUrl = '/api/v1/image/'+imageId+'/?format=json';
|
|
|
|
|
return $.get(apiUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-02-27 06:03:05 +00:00
|
|
|
function getPinData(pinId) {
|
|
|
|
|
var apiUrl = '/api/v1/pin/'+pinId+'/?format=json';
|
|
|
|
|
return $.get(apiUrl);
|
|
|
|
|
}
|
2013-02-27 07:38:38 +00:00
|
|
|
|
2013-03-03 14:30:06 +00:00
|
|
|
|
|
|
|
|
function deletePinData(pinId) {
|
|
|
|
|
var apiUrl = '/api/v1/pin/'+pinId+'/?format=json';
|
|
|
|
|
return $.ajax(apiUrl, {
|
|
|
|
|
type: 'DELETE'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-27 07:38:38 +00:00
|
|
|
function postPinData(data) {
|
|
|
|
|
return $.ajax({
|
|
|
|
|
type: "post",
|
|
|
|
|
url: "/api/v1/pin/",
|
|
|
|
|
contentType: 'application/json',
|
|
|
|
|
data: JSON.stringify(data)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getUrlParameter(name) {
|
2014-05-28 16:09:22 +02:00
|
|
|
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
|
2013-02-27 07:38:38 +00:00
|
|
|
}
|
2014-11-22 22:15:33 +01:00
|
|
|
|
|
|
|
|
Handlebars.registerHelper('niceLinks', (function () {
|
|
|
|
|
var reNL = /\r?\n/g,
|
|
|
|
|
reURL = /https?:[/][/](?:www[.])?([^/]+)(?:[/]([.]?[^\s,.])+)?/g;
|
|
|
|
|
return function (text) {
|
|
|
|
|
var t = Handlebars.Utils.escapeExpression(text);
|
|
|
|
|
t = t.replace(reURL, '<a href="$&" target="_blank">$1</a>');
|
|
|
|
|
t = t.replace(reNL, '<br>');
|
|
|
|
|
return new Handlebars.SafeString(t);
|
|
|
|
|
};
|
|
|
|
|
})());
|
|
|
|
|
|