Skip to content

Instantly share code, notes, and snippets.

@xamedow
Created March 19, 2014 13:05
Show Gist options
  • Save xamedow/9641219 to your computer and use it in GitHub Desktop.
Save xamedow/9641219 to your computer and use it in GitHub Desktop.
AJAX sender to refactor
// Fields check
$('[name="group_name"]').blur(function() {
var value = $(this).val();
var target = $(this).next();
target.html('<img>');
function getWarn(target, text) {
text = text || 'Поле не заполнено';
target.find('img').attr('src', '/images/del_icon.png');
target.html(target.html() + text);
}
if(!value) {
getWarn(target);
return;
}
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
action: 'query',
table: 'group'
},
success: function(data) {
if(value && (data.toLowerCase().indexOf( value.toLowerCase() + '/' )) >= 0 ) {
getWarn(target, 'Название уже существует');
}
}
});
});
// Friends remover.
$('.delete_friend').click(function(e) {
e.preventDefault();
if(confirm('Удалить пользователя из списка друзей?')) {
var id = $(this).data('id');
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
person_id: $(this).data('person_id'),
friend_id: $(this).data('friend_id'),
action: 'delete',
table: 'friends'
},
success: function() {
$('#card' + id).remove();
$('.friend_action').text('Удален из друзей');
$('.delete_friend').remove();
}
});
}
});
// Banned remover.
$('.delete_banned').click(function(e) {
if(confirm('Удалить пользователя из черного списка?')) {
e.preventDefault();
var id = $(this).data('id');
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
id: id,
action: 'delete',
table: 'blacklist'
},
success: function() {
$('#card' + id).remove();
}
});
}
});
// Group remover.
$('.delete_group').click(function(e) {
e.preventDefault();
if(confirm('Удалить группу?')) {
var id = $(this).data('id');
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
id: id,
action: 'delete',
table: 'group'
},
success: function() {
$('#group_card' + id).remove();
}
});
}
});
$('.quit_group').click(function(e) {
e.preventDefault();
if(confirm('Выйти из группы?')) {
var id = $(this).data('id');
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
id: id,
action: 'quit',
table: 'group'
},
success: function() {
$('#group_card' + id).remove();
}
});
}
});
// Invites adding.
$('.add_invite').click(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
init_id: $(this).data('init_id'),
person_id: $(this).data('person_id'),
action: 'add',
table: 'invitations'
},
success: function() {
$('.add_invite').text('Отменить приглашение');
$('.friend_action').text('Приглашение отправлено');
}
});
});
// BlackList adding.
$('.add_blacklist').click(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
banned_id: $(this).data('banned_id'),
person_id: $(this).data('person_id'),
action: 'add',
table: 'blacklist'
},
success: function() {
$('.add_blacklist').remove();
$('.friend_action').text('Добавлен в черный список');
}
});
});
// BlackList removing.
$('.delete_blacklist').click(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
id: $(this).data('id'),
action: 'delete',
table: 'blacklist'
},
success: function() {
$('.delete_blacklist').remove();
$('.friend_action').text('Удален из черного списка');
}
});
});
// Invites rejecting
$('.reject_invite').click(function(e) {
e.preventDefault();
var id = $(this).data('id');
var counter = $('i.counter');
var counterText = + counter.text();
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
id: id,
action: 'rejected',
table: 'invitations'
},
success: function() {
if(counterText > 1) {
$('i.counter').text(--counterText);
$('#profile_menu_vert span').text(--counterText);
} else {
counter.parent().removeClass('with_counter');
counter.remove();
$('#profile_menu_vert span div.counter').remove();
}
$('#card' + id).remove();
}
});
});
// Invites accepting
$('.accept_invite').click(function(e) {
e.preventDefault();
var id = $(this).data('id');
var counter = $('i.counter');
var counterText = + counter.text();
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
id: $(this).data('id'),
init_id: $(this).data('init_id'),
person_id: $(this).data('person_id'),
action: 'accepted',
table: 'invitations'
},
success: function() {
if(counterText > 1) {
$('i.counter').text(--counterText);
$('#profile_menu_vert span').text(--counterText);
} else {
counter.parent().removeClass('with_counter');
counter.remove();
$('#profile_menu_vert span div.counter').remove();
}
$('#card' + id).remove();
}
});
});
// Group invites rejecting
$('.reject_group_invite').click(function(e) {
e.preventDefault();
var id = $(this).data('id');
var counter = $('i.counter');
var counterText = + counter.text();
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
id: id,
action: 'rejected',
table: 'group_invitations'
},
success: function() {
if(counterText > 1) {
$('i.counter').text(--counterText);
$('#profile_menu_vert span').text(--counterText);
} else {
counter.parent().removeClass('with_counter');
counter.remove();
$('#profile_menu_vert span div.counter').remove();
}
$('#card' + id).remove();
}
});
});
// Group invites accepting
$('.accept_group_invite').click(function(e) {
e.preventDefault();
var id = $(this).data('id');
var counter = $('i.counter');
var counterText = + counter.text();
$.ajax({
type: 'POST',
url: '/inc/ajax_xamedow.php',
data: {
id: $(this).data('id'),
init_id: $(this).data('init_id'),
person_id: $(this).data('person_id'),
action: 'accepted',
table: 'group_invitations'
},
success: function() {
if(counterText > 1) {
$('i.counter').text(--counterText);
$('#profile_menu_vert span').text(--counterText);
} else {
counter.parent().removeClass('with_counter');
counter.remove();
$('#profile_menu_vert span div.counter').remove()
}
$('#card' + id).remove();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment