forked from 10up/simple-local-avatars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-local-avatars.dev.js
76 lines (65 loc) · 2.53 KB
/
simple-local-avatars.dev.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var simple_local_avatar_frame, avatar_spinner, avatar_ratings, avatar_container, avatar_form_button;
var avatar_working = false;
jQuery(document).ready(function($){
$( document.getElementById('simple-local-avatar-media') ).on( 'click', function(event) {
event.preventDefault();
if ( avatar_working )
return;
if ( simple_local_avatar_frame ) {
simple_local_avatar_frame.open();
return;
}
simple_local_avatar_frame = wp.media.frames.simple_local_avatar_frame = wp.media({
title: i10n_SimpleLocalAvatars.insertMediaTitle,
button: { text: i10n_SimpleLocalAvatars.insertIntoPost },
library : { type : 'image'},
multiple: false
});
simple_local_avatar_frame.on( 'select', function() {
// We set multiple to false so only get one image from the uploader
avatar_lock('lock');
var avatar_url = simple_local_avatar_frame.state().get('selection').first().toJSON().id;
jQuery.post( ajaxurl, { action: 'assign_simple_local_avatar_media', media_id: avatar_url, user_id: i10n_SimpleLocalAvatars.user_id, _wpnonce: i10n_SimpleLocalAvatars.mediaNonce }, function(data) {
if ( data != '' ) {
avatar_container.innerHTML = data;
$( document.getElementById('simple-local-avatar-remove') ).show();
avatar_ratings.disabled = false;
avatar_lock('unlock');
}
});
});
simple_local_avatar_frame.open();
});
$( document.getElementById('simple-local-avatar-remove') ).on('click',function(event){
event.preventDefault();
if ( avatar_working )
return;
avatar_lock('lock');
$.get( ajaxurl, { action: 'remove_simple_local_avatar', user_id: i10n_SimpleLocalAvatars.user_id, _wpnonce: i10n_SimpleLocalAvatars.deleteNonce })
.done(function(data) {
if ( data != '' ) {
avatar_container.innerHTML = data;
$( document.getElementById('simple-local-avatar-remove') ).hide();
avatar_ratings.disabled = true;
avatar_lock('unlock');
}
});
});
});
function avatar_lock( lock_or_unlock ) {
if ( undefined == avatar_spinner ) {
avatar_ratings = document.getElementById('simple-local-avatar-ratings');
avatar_spinner = jQuery( document.getElementById('simple-local-avatar-spinner') );
avatar_container = document.getElementById('simple-local-avatar-photo');
avatar_form_button = jQuery(avatar_ratings).closest('form').find('input[type=submit]');
}
if ( lock_or_unlock == 'unlock' ) {
avatar_working = false;
avatar_form_button.removeAttr('disabled');
avatar_spinner.hide();
} else {
avatar_working = true;
avatar_form_button.attr('disabled','disabled');
avatar_spinner.show();
}
}