1
- window . addEventListener ( 'DOMContentLoaded' , function ( ) {
2
-
3
- if ( ! document . querySelector ( '.profile-form' ) ) return ;
4
-
5
1
"use strict" ;
6
2
7
3
function rewriteURL ( link ) {
@@ -11,12 +7,6 @@ function rewriteURL(link) {
11
7
return true ;
12
8
}
13
9
14
- for ( const check_url of document . querySelectorAll ( '.account-settings .check-url' ) ) {
15
- check_url . addEventListener ( 'click' , function ( e ) {
16
- return rewriteURL ( this ) ;
17
- } ) ;
18
- }
19
-
20
10
function removeDiv ( div ) {
21
11
new Promise ( resolve => {
22
12
div . addEventListener ( "animationend" , resolve ) ;
@@ -30,19 +20,12 @@ function removeField(e) {
30
20
e . preventDefault ( ) ;
31
21
removeDiv ( this . closest ( '.field-container' ) ) ;
32
22
}
33
- for ( const remove_field of document . querySelectorAll ( '.account-settings .remove-field' ) ) {
34
- remove_field . addEventListener ( 'click' , removeField ) ;
35
- }
36
23
37
24
function removeProfile ( e ) {
38
25
e . preventDefault ( ) ;
39
26
removeDiv ( this . closest ( '.profile-container' ) ) ;
40
27
}
41
28
42
- for ( const remove_profile of document . querySelectorAll ( '.account-settings .remove-profile' ) ) {
43
- remove_profile . addEventListener ( 'click' , removeProfile ) ;
44
- }
45
-
46
29
function addProfile ( container , id , title , formatUrl ) {
47
30
const profileNode = document . importNode ( document . querySelector ( '#profile-tmpl' ) . content , true ) ;
48
31
profileNode . querySelector ( '.remove-profile' ) . addEventListener ( 'click' , removeProfile ) ;
@@ -52,44 +35,25 @@ function addProfile(container, id, title, formatUrl) {
52
35
const profile_name = profileNode . querySelector ( 'input[name="profile.name"]' ) ;
53
36
profile_name . value = id ;
54
37
profile_name . type = 'hidden' ;
55
- profileNode . querySelector ( 'a.check-url' ) . dataset . urlTemplate ( formatUrl ) ;
56
38
57
- for ( const check_url of profileNode . querySelectorAll ( ':scope .check-url' ) ) {
58
- check_url . addEventListener ( 'click' , function ( e ) {
59
- return rewriteURL ( this ) ;
60
- } ) ;
61
- }
39
+ const check_button = profileNode . querySelector ( 'a.check-url' ) ;
40
+
41
+ check_button . dataset . urlTemplate = formatUrl ;
42
+
43
+ check_button . addEventListener ( 'click' , function ( e ) {
44
+ rewriteURL ( this ) ;
45
+ } ) ;
62
46
}
63
47
container . append ( profileNode ) ;
64
48
}
65
49
66
- document . querySelector ( '.account-settings .add-profile' ) . addEventListener ( 'change' , function ( e ) {
67
- e . preventDefault ( ) ;
68
- const option = this . selectedOptions [ 0 ] ;
69
- addProfile (
70
- document . querySelector ( '#metacpan_profiles' ) ,
71
- this . value ,
72
- option . dataset . title ,
73
- option . dataset . urlFormat ,
74
- ) ;
75
- this . selectedIndex = 0 ;
76
- } ) ;
77
-
78
50
function addField ( container , id ) {
79
51
const fieldNode = document . importNode ( document . querySelector ( 'template#field-tmpl' ) . content , true ) ;
80
52
fieldNode . querySelector ( 'input' ) . name = id ;
81
53
fieldNode . querySelector ( '.remove-field' ) . addEventListener ( 'click' , removeField ) ;
82
54
container . append ( fieldNode ) ;
83
55
}
84
56
85
- for ( const btn of document . querySelectorAll ( '.account-settings button.add-field' ) ) {
86
- btn . addEventListener ( 'click' , function ( e ) {
87
- e . preventDefault ( ) ;
88
- addField ( this . closest ( '.field-container' ) . parentNode , this . dataset . fieldType ) ;
89
- } ) ;
90
- }
91
-
92
-
93
57
function validateJSON ( input ) {
94
58
try {
95
59
input . value && JSON . parse ( input . value ) ;
@@ -99,12 +63,6 @@ function validateJSON(input) {
99
63
}
100
64
}
101
65
102
- const extra = document . querySelector ( '.account-settings textarea[name="extra"]' )
103
- extra . addEventListener ( 'keyup' , function ( e ) {
104
- validateJSON ( this ) ;
105
- } ) ;
106
- validateJSON ( extra ) ;
107
-
108
66
function fillLocation ( ) {
109
67
navigator . geolocation . getCurrentPosition ( ( pos ) => {
110
68
document . querySelector ( 'input[name="latitude"]' ) . value = pos . coords . latitude ;
@@ -114,20 +72,64 @@ function fillLocation() {
114
72
return false ;
115
73
}
116
74
117
- document . querySelector ( '.account-settings button.fill-location' ) . addEventListener ( 'click' , function ( e ) {
118
- e . preventDefault ( ) ;
119
- fillLocation ( ) ;
120
- } ) ;
75
+ const profileForm = document . querySelector ( '.profile-form' ) ;
76
+
77
+ if ( profileForm ) {
78
+ for ( const btn of profileForm . querySelectorAll ( ':scope .add-field' ) ) {
79
+ btn . addEventListener ( 'click' , function ( e ) {
80
+ e . preventDefault ( ) ;
81
+ addField ( this . closest ( '.field-container' ) . parentNode , this . dataset . fieldType ) ;
82
+ } ) ;
83
+ }
84
+
85
+ for ( const remove_field of profileForm . querySelectorAll ( ':scope .remove-field' ) ) {
86
+ remove_field . addEventListener ( 'click' , removeField ) ;
87
+ }
121
88
122
- const donation_box = document . querySelector ( '#metacpan_donations' ) ;
123
- document . querySelector ( '.profile-form input[name="donations"]' ) . addEventListener ( 'change' , ( e ) => {
124
- if ( donation_box . classList . contains ( "slide-out-hidden" ) ) {
125
- donation_box . classList . toggle ( "slide-out-hidden" ) ;
89
+ profileForm . querySelector ( '.add-profile' ) . addEventListener ( 'change' , function ( e ) {
90
+ e . preventDefault ( ) ;
91
+ const option = this . selectedOptions [ 0 ] ;
92
+ addProfile (
93
+ document . querySelector ( '#metacpan_profiles' ) ,
94
+ this . value ,
95
+ option . dataset . title ,
96
+ option . dataset . urlFormat ,
97
+ ) ;
98
+ this . selectedIndex = 0 ;
99
+ } ) ;
100
+
101
+ for ( const remove_profile of profileForm . querySelectorAll ( ':scope .remove-profile' ) ) {
102
+ remove_profile . addEventListener ( 'click' , removeProfile ) ;
126
103
}
127
- else {
128
- donation_box . classList . toggle ( "slide-up" ) ;
104
+
105
+ for ( const check_url of profileForm . querySelectorAll ( ':scope .check-url' ) ) {
106
+ check_url . addEventListener ( 'click' , function ( e ) {
107
+ rewriteURL ( this ) ;
108
+ } ) ;
129
109
}
130
- donation_box . classList . toggle ( "slide-down" ) ;
131
- } ) ;
132
110
133
- } ) ;
111
+ const extra = profileForm . querySelector ( 'textarea[name="extra"]' )
112
+ extra . addEventListener ( 'keyup' , function ( e ) {
113
+ validateJSON ( this ) ;
114
+ } ) ;
115
+ validateJSON ( extra ) ;
116
+
117
+ profileForm . querySelector ( '.fill-location' ) . addEventListener ( 'click' , function ( e ) {
118
+ e . preventDefault ( ) ;
119
+ fillLocation ( ) ;
120
+ } ) ;
121
+
122
+ const donation_box = document . querySelector ( '#metacpan_donations' ) ;
123
+ profileForm . querySelector ( 'input[name="donations"]' ) . addEventListener ( 'change' , ( e ) => {
124
+ donation_box . classList . remove ( "slide-out-hidden" ) ;
125
+
126
+ if ( this . value ) {
127
+ donation_box . classList . add ( "slide-down" ) ;
128
+ donation_box . classList . remove ( "slide-up" ) ;
129
+ }
130
+ else {
131
+ donation_box . classList . remove ( "slide-down" ) ;
132
+ donation_box . classList . add ( "slide-up" ) ;
133
+ }
134
+ } ) ;
135
+ }
0 commit comments