1
1
/* eslint-disable ember/no-classic-components, ember/no-classic-classes, ember/require-tagless-components, prettier/prettier, ember/no-get, ember/no-component-lifecycle-hooks, ember/require-super-in-lifecycle-hooks */
2
- import Component from '@ember/component' ;
3
- import layout from 'ember-styleguide/templates/components/es-header-navbar-link' ;
4
- import { computed } from '@ember/object' ;
5
- import { equal } from '@ember/object/computed' ;
2
+ import Component from '@glimmer/component' ;
6
3
import { inject as service } from '@ember/service' ;
7
4
import { schedule , next } from '@ember/runloop' ;
8
5
import { action } from '@ember/object' ;
6
+ import { tracked } from '@glimmer/tracking' ;
9
7
10
- export default Component . extend ( {
11
- layout,
12
- tagName : 'li' ,
13
- classNames : [ 'navbar-list-item' ] ,
14
- classNameBindings : [ 'isDropdown:dropdown' ] ,
15
- isDropdown : equal ( 'link.type' , 'dropdown' ) ,
16
- isDropdownOpen : false ,
8
+ export default class EsHeaderNavbarLink extends Component {
9
+ @service navbar ;
17
10
18
- // because aria-expanded requires a string value instead of a boolean
19
- isExpanded : computed ( 'isDropdownOpen' , function ( ) {
20
- return this . isDropdownOpen ? 'true' : 'false' ;
21
- } ) ,
11
+ @tracked element = null ;
12
+ @tracked linkType = this . args . link . type ;
13
+ @tracked isDropdownOpen = false ;
22
14
23
- navbar : service ( ) ,
15
+ get isDropdown ( ) {
16
+ return this . linkType === 'dropdown' ;
17
+ }
24
18
25
- init ( ) {
26
- this . _super ( ...arguments ) ;
27
- this . get ( 'navbar' ) . register ( this ) ;
28
- } ,
29
-
30
- didInsertElement ( ) {
31
- let button = this . element . querySelector ( '.navbar-list-item-dropdown-toggle' ) ;
19
+ // because aria-expanded requires a string value instead of a boolean
20
+ get isExpanded ( ) {
21
+ return this . isDropdownOpen ? 'true' : 'false' ;
22
+ }
32
23
33
- if ( button ) {
34
- button . addEventListener ( 'blur' , ( ) => this . processBlur ( ) ) ;
35
- }
36
- } ,
24
+ constructor ( ) {
25
+ super ( ... arguments ) ;
26
+ this . navbar . register ( this ) ;
27
+ }
37
28
38
29
setupLinkBlurs ( ) {
39
30
if ( this . linkBlursActive ) {
40
31
return ;
41
32
}
42
33
43
- this . set ( 'linkBlursActive' , true ) ;
44
-
34
+ this . linkBlursActive = true ;
45
35
let links = Array . from ( this . element . querySelectorAll ( '.navbar-dropdown-list-item-link' ) ) ;
46
36
47
- links . forEach ( ancor => {
48
- ancor . addEventListener ( 'blur' , ( ) => this . processBlur ( ) ) ;
37
+ links . forEach ( anchor => {
38
+ anchor . addEventListener ( 'blur' , ( ) => this . processBlur ( ) ) ;
49
39
} ) ;
50
- } ,
40
+ }
41
+
42
+ @action
43
+ setElement ( element ) {
44
+ this . element = element ;
45
+ }
51
46
52
47
@action
53
48
toggleDropdown ( event ) {
54
- this . get ( ' navbar' ) . closePopupMenu ( this ) ;
55
- this . toggleProperty ( ' isDropdownOpen' ) ;
49
+ this . navbar . closePopupMenu ( this ) ;
50
+ this . isDropdownOpen = ! this . isDropdownOpen ;
56
51
57
52
if ( this . isDropdownOpen ) {
58
53
// if it's open, let's make sure it can do some things
@@ -68,20 +63,9 @@ export default Component.extend({
68
63
this . processKeyPress ( ) ;
69
64
} ) ;
70
65
}
71
- } ,
72
-
73
- closeDropdown ( ) {
74
- // set the isDropdownOpen to false, which will make the dropdown go away
75
- this . set ( 'isDropdownOpen' , false ) ;
76
- } ,
77
-
78
- openDropdown ( ) {
79
- //might not need this
80
- // open the dropdown and set the focus to the first item inside
81
- this . set ( 'isDropdownOpen' , true ) ;
82
- this . processFirstElementFocus ( ) ;
83
- } ,
66
+ }
84
67
68
+ @action
85
69
processBlur ( ) {
86
70
next ( this , function ( ) {
87
71
let subItems = Array . from ( this . element . querySelectorAll ( '.navbar-dropdown-list li' ) ) ;
@@ -92,17 +76,35 @@ export default Component.extend({
92
76
this . closeDropdown ( ) ;
93
77
}
94
78
} ) ;
95
- } ,
79
+ }
80
+
81
+ @action
82
+ unregisterListener ( element ) {
83
+ element . removeEventListener ( 'keydown' , this . triggerDropdown ) ;
84
+ element . removeEventListener ( 'click' , this . triggerDropdown ) ;
85
+ }
86
+
87
+ closeDropdown ( ) {
88
+ // set the isDropdownOpen to false, which will make the dropdown go away
89
+ this . isDropdownOpen = false ;
90
+ }
91
+
92
+ openDropdown ( ) {
93
+ //might not need this
94
+ // open the dropdown and set the focus to the first item inside
95
+ this . isDropdownOpen = true ;
96
+ this . processFirstElementFocus ( ) ;
97
+ }
96
98
97
99
processClick ( ) {
98
100
// TODO handle mouseclick outside the current dropdown
99
- } ,
101
+ }
100
102
101
103
processFirstElementFocus ( ) {
102
104
// Identify the first item in the dropdown list & set focus on it
103
105
let firstFocusable = this . element . querySelector ( '.navbar-dropdown-list li:first-of-type a' ) ;
104
106
firstFocusable . focus ( ) ;
105
- } ,
107
+ }
106
108
107
109
processKeyPress ( ) {
108
110
// add event listeners
@@ -122,18 +124,13 @@ export default Component.extend({
122
124
return ;
123
125
}
124
126
} ) ;
125
- } ,
127
+ }
126
128
127
129
returnFocus ( ) {
128
130
// after that rendering bit happens, we need to return the focus to the trigger
129
131
schedule ( 'afterRender' , this , function ( ) {
130
132
let dropdownTrigger = this . element . querySelector ( '.navbar-list-item-dropdown-toggle' ) ;
131
133
dropdownTrigger . focus ( ) ;
132
134
} ) ;
133
- } ,
134
-
135
- willDestroyElement ( ) {
136
- document . removeEventListener ( 'keydown' , this . triggerDropdown ) ;
137
- // document.removeEventListener('click', this.triggerDropdown);
138
- } ,
139
- } ) ;
135
+ }
136
+ }
0 commit comments