1- window . addEventListener ( 'DOMContentLoaded' , function ( ) {
2-
3- if ( ! document . querySelector ( '.profile-form' ) ) return ;
4-
51"use strict" ;
62
73function rewriteURL ( link ) {
@@ -11,12 +7,6 @@ function rewriteURL(link) {
117 return true ;
128}
139
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-
2010function removeDiv ( div ) {
2111 new Promise ( resolve => {
2212 div . addEventListener ( "animationend" , resolve ) ;
@@ -30,19 +20,12 @@ function removeField(e) {
3020 e . preventDefault ( ) ;
3121 removeDiv ( this . closest ( '.field-container' ) ) ;
3222}
33- for ( const remove_field of document . querySelectorAll ( '.account-settings .remove-field' ) ) {
34- remove_field . addEventListener ( 'click' , removeField ) ;
35- }
3623
3724function removeProfile ( e ) {
3825 e . preventDefault ( ) ;
3926 removeDiv ( this . closest ( '.profile-container' ) ) ;
4027}
4128
42- for ( const remove_profile of document . querySelectorAll ( '.account-settings .remove-profile' ) ) {
43- remove_profile . addEventListener ( 'click' , removeProfile ) ;
44- }
45-
4629function addProfile ( container , id , title , formatUrl ) {
4730 const profileNode = document . importNode ( document . querySelector ( '#profile-tmpl' ) . content , true ) ;
4831 profileNode . querySelector ( '.remove-profile' ) . addEventListener ( 'click' , removeProfile ) ;
@@ -52,44 +35,25 @@ function addProfile(container, id, title, formatUrl) {
5235 const profile_name = profileNode . querySelector ( 'input[name="profile.name"]' ) ;
5336 profile_name . value = id ;
5437 profile_name . type = 'hidden' ;
55- profileNode . querySelector ( 'a.check-url' ) . dataset . urlTemplate ( formatUrl ) ;
5638
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+ } ) ;
6246 }
6347 container . append ( profileNode ) ;
6448}
6549
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-
7850function addField ( container , id ) {
7951 const fieldNode = document . importNode ( document . querySelector ( 'template#field-tmpl' ) . content , true ) ;
8052 fieldNode . querySelector ( 'input' ) . name = id ;
8153 fieldNode . querySelector ( '.remove-field' ) . addEventListener ( 'click' , removeField ) ;
8254 container . append ( fieldNode ) ;
8355}
8456
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-
9357function validateJSON ( input ) {
9458 try {
9559 input . value && JSON . parse ( input . value ) ;
@@ -99,12 +63,6 @@ function validateJSON(input) {
9963 }
10064}
10165
102- const extra = document . querySelector ( '.account-settings textarea[name="extra"]' )
103- extra . addEventListener ( 'keyup' , function ( e ) {
104- validateJSON ( this ) ;
105- } ) ;
106- validateJSON ( extra ) ;
107-
10866function fillLocation ( ) {
10967 navigator . geolocation . getCurrentPosition ( ( pos ) => {
11068 document . querySelector ( 'input[name="latitude"]' ) . value = pos . coords . latitude ;
@@ -114,20 +72,64 @@ function fillLocation() {
11472 return false ;
11573}
11674
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+ }
12188
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 ) ;
126103 }
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+ } ) ;
129109 }
130- donation_box . classList . toggle ( "slide-down" ) ;
131- } ) ;
132110
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