Skip to content

Instantly share code, notes, and snippets.

@Ratko-Solaja
Last active November 8, 2016 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ratko-Solaja/5061633731a897d92235c845d8993c0f to your computer and use it in GitHub Desktop.
Save Ratko-Solaja/5061633731a897d92235c845d8993c0f to your computer and use it in GitHub Desktop.
Our public JS.
jQuery(document).ready(function($) {
'use strict';
/**
* Create our notification
*/
function toptalAddNotification( text, url ) {
var $appendTo = $('body'),
$content = '<div class="toptal-save-notification"><span class="notification-icon"></span><a href="'+url+'" class="notification-link">'+text+'</a></div>';
$appendTo.append($content);
setTimeout(function() {
$('.toptal-save-notification').addClass('active');
}, 250);
setTimeout(function() {
$('.toptal-save-notification').removeClass('active');
setTimeout(function() {
$('.toptal-save-notification').remove();
}, 250);
}, 3250);
}
/**
* Save/unsave item.
*/
if ( $('.toptal-save-button').length ) {
// ^ this was to check if the button exists, now, let's do something with it
$('.toptal-save-button').on('click', function(event) {
// Prevents the default behaviour of the button.
event.preventDefault();
// Make sure that the user can't click the button multiple times unless
// AJAX call is finished.
var anchor = $(this);
if ( anchor.data('disabled') ) {
return false;
}
anchor.data('disabled', 'disabled');
// Let's get some basic variables here that we're going to need.
var $this = $(this),
item_id = $this.data('item-id'),
nonce = $this.data('nonce');
// Let's do some AJAX
$.ajax({
type: 'post',
url: toptal_save_ajax.ajax_url,
data: {
'nonce': nonce,
'item_id': item_id,
'action': 'save_unsave_item'
},
success: function(data) {
// If true, remove from saved, else, add to saved.
if ( data.is_saved == true ) {
$this.removeClass('saved');
$this.find('span.toptal-save-text').text(toptal_save_ajax.item_save_text);
} else {
$this.addClass('saved');
$this.find('span.toptal-save-text').text(toptal_save_ajax.item_unsave_text);
// Show our notification
toptalAddNotification( toptal_save_ajax.item_saved_text, toptal_save_ajax.saved_page_url );
}
anchor.removeData('disabled');
},
error: function(error) {
console.log(error);
}
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment