@@ -2,49 +2,59 @@ import FetchUtil from "../utils/fetchUtil";
2
2
3
3
export default class MajorProjectForm {
4
4
5
- tags_written = false ;
6
-
7
- constructor ( form ) {
8
- this . form = form ;
9
- this . endpoint = '/major_project/submit' ;
10
- this . render ( ) ;
11
- }
12
-
13
- render ( ) {
14
- this . form . querySelector ( 'input[type=submit]' )
15
- . addEventListener ( 'click' , e => this . _submitForm ( e ) ) ;
16
- this . form . querySelector ( 'input[id=skill-input]' )
17
- . addEventListener ( 'focusout' , e => this . onWriteSkill ( e ) ) ;
18
- }
19
-
20
- onWriteSkill ( e ) {
21
- let input = document . getElementById ( "skill-input" )
22
- if ( ! this . tags_written ) {
23
- this . tags_written = true
24
- document . getElementsByClassName ( "placeholder" ) . item ( 0 ) . remove ( )
5
+
6
+ constructor ( form ) {
7
+ this . form = form ;
8
+ this . endpoint = '/major_project/submit' ;
9
+ this . tags_written = false ;
10
+ this . tag_keys = [ "Enter" , "Comma" , "Tab" ] ;
11
+ this . render ( ) ;
12
+ }
13
+
14
+ render ( ) {
15
+ this . form . querySelector ( 'input[type=submit]' )
16
+ . addEventListener ( 'click' , e => this . _submitForm ( e ) ) ;
17
+ this . form . querySelector ( 'input[id=skill-input]' )
18
+ . addEventListener ( 'focusout' , e => this . onWriteSkill ( e ) ) ;
19
+ this . form . querySelector ( 'input[id=skill-input]' )
20
+ . addEventListener ( 'keyup' , e => this . onKeyPress ( e ) ) ;
21
+ }
22
+
23
+ onKeyPress ( e ) {
24
+ if ( this . tag_keys . includes ( e . code ) ) {
25
+ e . preventDefault ( ) ;
26
+ this . onWriteSkill ( e ) ;
27
+ }
25
28
}
29
+
30
+ onWriteSkill ( e ) {
31
+ let input = document . getElementById ( "skill-input" )
32
+ if ( ! this . tags_written ) {
33
+ this . tags_written = true
34
+ document . getElementsByClassName ( "placeholder" ) . item ( 0 ) . remove ( )
35
+ }
26
36
let txt = input . value . replace ( / [ ^ a - z A - Z 0 - 9 \+ \- \. \# ] / g, '' ) ; // allowed characters list
27
- if ( txt ) input . insertAdjacentHTML ( "beforebegin" , '<span class="skill-tag">' + txt + '</span>' ) ;
28
- input . value = "" ;
29
- input . focus ( ) ;
30
-
31
- }
32
-
33
- _submitForm ( e ) {
34
- e . preventDefault ( ) ;
35
-
36
- let payload = {
37
- projectName : this . form . querySelector ( 'input[name=name]' ) . value ,
38
- projectTldr : this . form . querySelector ( 'input[name=tldr]' ) . value ,
39
- projectTimeSpent : this . form . querySelector ( 'textarea[name=time-commitment]' ) . value ,
40
- projectDescription :
41
- this . form . querySelector ( 'textarea[name=description]' ) . value
42
- } ;
43
-
44
- FetchUtil . postWithWarning ( this . endpoint , payload , {
45
- warningText : "You will not be able to edit your " +
46
- "project once it has been submitted." ,
47
- successText : "Your project has been submitted."
48
- } ) ;
49
- }
37
+ if ( txt ) input . insertAdjacentHTML ( "beforebegin" , '<span class="skill-tag">' + txt + '</span>' ) ;
38
+ input . value = "" ;
39
+ input . focus ( ) ;
40
+
41
+ }
42
+
43
+ _submitForm ( e ) {
44
+ e . preventDefault ( ) ;
45
+
46
+ let payload = {
47
+ projectName : this . form . querySelector ( 'input[name=name]' ) . value ,
48
+ projectTldr : this . form . querySelector ( 'input[name=tldr]' ) . value ,
49
+ projectTimeSpent : this . form . querySelector ( 'textarea[name=time-commitment]' ) . value ,
50
+ projectDescription :
51
+ this . form . querySelector ( 'textarea[name=description]' ) . value
52
+ } ;
53
+
54
+ FetchUtil . postWithWarning ( this . endpoint , payload , {
55
+ warningText : "You will not be able to edit your " +
56
+ "project once it has been submitted." ,
57
+ successText : "Your project has been submitted."
58
+ } ) ;
59
+ }
50
60
}
0 commit comments