1- /**
2- ** File: modules.js
3- ** Version: 1.0
4- ** Author: en0ndev
5- This file is part of Custom Profile Avatar.
6-
7- Custom Profile Avatar is free software: you can redistribute it and/or modify
8- it under the terms of the GNU General Public License as published by
9- the Free Software Foundation, either version 3 of the License, or
10- (at your option) any later version.
11-
12- Custom Profile Avatar is distributed in the hope that it will be useful,
13- but WITHOUT ANY WARRANTY; without even the implied warranty of
14- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15- GNU General Public License for more details.
16-
17- You should have received a copy of the GNU General Public License
18- along with Custom Profile Avatar. If not, see <https://www.gnu.org/licenses/>.
19- */
20-
21- jQuery ( document ) . ready ( function ( $ ) {
22- var mediaUploader ;
23- $ ( '#change' ) . click ( function ( e ) {
24- e . preventDefault ( ) ;
25- // If the uploader object has already been created, reopen the dialog
26- if ( mediaUploader ) {
27- mediaUploader . open ( ) ;
28- return ;
29- }
30- // Extend the wp.media object
31- mediaUploader = wp . media . frames . file_frame = wp . media ( {
32- title : 'Choose Your Custom Profile Avatar' ,
33- button : {
34- text : 'Choose'
35- } ,
36- multiple : false ,
37- library : {
38- type : [ 'image' ]
39- } ,
40- } ) ;
41- // When a file is selected, grab the URL and set it as the text field's value
42- mediaUploader . on ( 'select' , function ( ) {
43- attachment = mediaUploader . state ( ) . get ( 'selection' ) . first ( ) . toJSON ( ) ;
44- $ ( '.user__avatar > .avatar' ) . attr ( 'src' , attachment . url ) ;
45- $ ( 'input[name="avatar__val"]' ) . attr ( 'value' , attachment . url ) ;
46- if ( ! $ ( '.user__avatar' ) . children ( ) . hasClass ( 'remove' ) ) { $ ( '.user__avatar > .avatar' ) . after ( '<div class="remove"></div>' ) } ;
47- } ) ;
48- // Open the uploader dialog
49- mediaUploader . open ( ) ;
50- } ) ;
51- $ ( document ) . on ( 'click' , '.remove' , function ( ) {
52- var pull = $ ( '#pull > img' ) . attr ( 'src' ) ;
53- $ ( '.user__avatar .avatar' ) . attr ( 'src' , pull ) ;
54- $ ( 'input[name="avatar__val"]' ) . removeAttr ( 'value' ) ;
55- $ ( this ) . remove ( ) ;
56- } ) ;
1+ /**
2+ ** File: modules.js
3+ ** Version: 1.0
4+ ** Author: en0ndev
5+ This file is part of Custom Profile Avatar.
6+
7+ Custom Profile Avatar is free software: you can redistribute it and/or modify
8+ it under the terms of the GNU General Public License as published by
9+ the Free Software Foundation, either version 3 of the License, or
10+ (at your option) any later version.
11+
12+ Custom Profile Avatar is distributed in the hope that it will be useful,
13+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ GNU General Public License for more details.
16+
17+ You should have received a copy of the GNU General Public License
18+ along with Custom Profile Avatar. If not, see <https://www.gnu.org/licenses/>.
19+ */
20+
21+ jQuery ( document ) . ready ( function ( $ ) {
22+ var mediaUploader ;
23+ $ ( '#change' ) . click ( function ( e ) {
24+ e . preventDefault ( ) ;
25+ // If the uploader object has already been created, reopen the dialog
26+ if ( mediaUploader ) {
27+ mediaUploader . open ( ) ;
28+ return ;
29+ }
30+ // Extend the wp.media object
31+ mediaUploader = wp . media . frames . file_frame = wp . media ( {
32+ title : 'Choose Your Custom Profile Avatar' ,
33+ button : {
34+ text : 'Choose'
35+ } ,
36+ multiple : false ,
37+ library : {
38+ type : [ 'image' ]
39+ } ,
40+ } ) ;
41+ // When a file is selected, grab the URL and set it as the text field's value
42+ mediaUploader . on ( 'select' , function ( ) {
43+ attachment = mediaUploader . state ( ) . get ( 'selection' ) . first ( ) . toJSON ( ) ;
44+ $ ( '.user__avatar > .avatar' ) . attr ( 'src' , attachment . url ) ;
45+ $ ( 'input[name="avatar__val"]' ) . attr ( 'value' , attachment . url ) ;
46+ if ( ! $ ( '.user__avatar' ) . children ( ) . hasClass ( 'remove' ) ) { $ ( '.user__avatar > .avatar' ) . after ( '<div class="remove"></div>' ) } ;
47+ } ) ;
48+ // Open the uploader dialog
49+ mediaUploader . open ( ) ;
50+ } ) ;
51+ $ ( document ) . on ( 'click' , '.remove' , function ( ) {
52+ var pull = $ ( '#pull > img' ) . attr ( 'src' ) ;
53+ $ ( '.user__avatar .avatar' ) . attr ( 'src' , pull ) ;
54+ $ ( 'input[name="avatar__val"]' ) . removeAttr ( 'value' ) ;
55+ $ ( this ) . remove ( ) ;
56+ } ) ;
5757} ) ;
0 commit comments