티스토리 수익 글 보기
function SaveFollow(){
var self=this;
};
SaveFollow.prototype.toggleSaveDesign = function (event){
var self = this,
button=event.currentTarget,
action=$(button).hasClass(‘sf-checked’)?false:true,
product_id=$(button).data(‘sf-product-id’),
url = ‘/like/287/’ + product_id + ‘/’;
event.preventDefault();
$(button).children(“div”).html(“Loading…”);
$.ajax({
type: ‘POST’,
data: {‘active’: action},
url: url,
traditional: ‘true’,
dataType: ‘json’,
cache: false,
beforeSend: function(xhr) {
xhr.setRequestHeader(‘X-Requested-With’, ‘XMLHttpRequest’);
xhr.setRequestHeader(‘X-CSRFToken’, $.cookie(‘csrftoken3’));
}
}).error( function(jqXHR, textStatus, errorThrown) {
$(button).children(“div”).html(“Error Saving”);
}).success( function(data, textStatus, jqXHR) {
// this will mark all follow buttons accurately if there is more than one follow button on the page for the same artist
if(action){
$(“.sf-toggle[data-sf-product-id='”+product_id+”‘]”).removeClass(‘sf-unchecked’).addClass(‘sf-checked’).children(“div”).html(“You Saved This”);
}else{
$(“.sf-toggle[data-sf-product-id='”+product_id+”‘]”).removeClass(‘sf-checked’).addClass(‘sf-unchecked’).children(“div”).html(“Save Design”);
}
});
};
SaveFollow.prototype.toggleFollowArtist = function (event){
var self = this,
button=event.currentTarget,
action=$(button).hasClass(‘sf-checked’)?’unfollow’:’follow’,
artist_username=$(button).data(‘sf-username’),
url = ‘/profile/’ + action;
event.preventDefault();
$(button).children(“div”).html(“Loading…”);
$.ajax({
type: ‘POST’,
data: {‘username’: artist_username},
url: url,
traditional: ‘true’,
dataType: ‘json’,
cache: false,
beforeSend: function(xhr) {
xhr.setRequestHeader(‘X-Requested-With’, ‘XMLHttpRequest’);
xhr.setRequestHeader(‘X-CSRFToken’, $.cookie(‘csrftoken3’));
}
}).error( function(jqXHR, textStatus, errorThrown) {
$(button).children(“div”).html(“Error Following”);
}).success( function(data, textStatus, jqXHR) {
// this will mark all follow buttons accurately if there is more than one follow button on the page for the same artist
if(action == ‘follow’){
$(“.sf-toggle[data-sf-username='”+artist_username+”‘]”).removeClass(‘sf-unchecked’).addClass(‘sf-checked’).children(“div”).html(“You Follow ” + artist_username);
}else{
$(“.sf-toggle[data-sf-username='”+artist_username+”‘]”).removeClass(‘sf-checked’).addClass(‘sf-unchecked’).children(“div”).html(“Follow ” + artist_username);
}
});
};
SaveFollow.prototype.events = function() {
var self = this;
$(document).off(‘click’, ‘.fa-heart.sf-toggle’).on(‘click’, ‘.fa-heart.sf-toggle’, function(event){
event.preventDefault();
self.toggleSaveDesign(event);
});
$(document).off(‘click’, ‘.fa-plus.sf-toggle’).on(‘click’, ‘.fa-plus.sf-toggle’, function(event){
event.preventDefault();
self.toggleFollowArtist(event);
});
};
var sfRunning = false;
function findSaveFollowButtons(){
var d = []
var a = []
if($(“.sf-loading”).length){
$(“.sf-loading[data-sf-product-id]”).each(function() {
d.push(parseInt($(this).attr(“data-sf-product-id”)));
});
$(“.sf-loading[data-sf-username]”).each(function() {
a.push($(this).attr(“data-sf-username”));
});
if(d && a){
data = {
d,
a
}
if(!sfRunning){
sfRunning = true;
$.ajax({
url: ‘/save-follow-ajax/’,
type: ‘GET’,
data: data,
dataType: ‘json’,
traditional: true,
success: function(data){
// loop through designs and set save states
$.each(d, function( key, product_id ) {
$(“.sf-loading[data-sf-product-id='”+product_id+”‘]”).removeClass(“sf-loading”).addClass(“sf-unchecked sf-toggle”).children(“div”).html(“Save Design”);
});
$.each(data.liked_designs, function( key, product_id ) {
$(“.sf-unchecked[data-sf-product-id='”+product_id+”‘]”).removeClass(“sf-unchecked”).addClass(“sf-checked”).children(“div”).html(“You Saved This”);;
});
// loop through artists and set follow states
$.each(a, function( key, artist_username ) {
$(“.sf-loading[data-sf-username='”+artist_username+”‘]”).removeClass(“sf-loading”).addClass(“sf-unchecked sf-toggle”).children(“div”).html(“Follow ” + artist_username);
});
$.each(data.followed_artists, function( key, artist_username ) {
$(“.sf-unchecked[data-sf-username='”+artist_username+”‘]”).removeClass(“sf-unchecked”).addClass(“sf-checked”).children(“div”).html(“You Follow ” + artist_username);
});
sfRunning = false;
},
error: function(jqXHR, textStatus, errorThrown){
// ignore 400’s
if (jqXHR.statusCode().status != 400){
$(“.sf-loading”).hide();
console.log(“Error getting save and follow button states”);
}
sfRunning = false;
}
});
}
}
}
}
function activateSaveFollowButtons(sfObserver){
// set up actions after getting button states
$.when( findSaveFollowButtons() ).done(function() {
$.when ( save_follow.events() ).done(function() {
// begin observing for new buttons
sfObserver.observe(document, {attributes: false, childList: true, characterData: false, subtree:true});
});
});
}
var save_follow;
jQuery(document).ready(function($) {
save_follow = new SaveFollow();
/* run actions when new buttons are found on the page */
var sfObserver = new MutationObserver(function(mutations) {
var sfElement = $(“.sf-loading”).length;
if (sfElement) {
sfObserver.disconnect();
activateSaveFollowButtons(sfObserver);
}
});
activateSaveFollowButtons(sfObserver);
});