@@ -5,111 +5,175 @@ import { BaseComponent } from '../base-component.js';
5
5
import { urlHandler , patternName } from '../../utils' ;
6
6
import { store } from '../../store' ; // redux store
7
7
8
- import NewTabIcon from '../../../icons/new-tab.svg' ;
9
- import HelpIcon from '../../../icons/help.svg' ;
10
- import SettingsIcon from '../../../icons/settings.svg' ;
8
+ let listeningForBodyClicks = false ;
9
+
10
+ import { html } from 'lit-html' ;
11
+ import { BaseLitComponent } from '../base-component.js' ;
11
12
12
13
@define
13
- class ToolsMenu extends BaseComponent {
14
+ class ToolsMenu extends BaseLitComponent {
14
15
static is = 'pl-tools-menu' ;
15
16
17
+ static props = {
18
+ isOpen : props . boolean ,
19
+ layoutMode : props . string ,
20
+ } ;
21
+
16
22
_stateChanged ( state ) {
17
- this . currentUrl =
18
- state . app . currentUrl || urlHandler . getFileName ( patternName ) ;
23
+ if ( this . currentUrl !== state . app . currentUrl ) {
24
+ this . currentUrl =
25
+ state . app . currentUrl || urlHandler . getFileName ( patternName ) ;
26
+ }
27
+
28
+ if ( this . layoutMode !== state . app . layoutMode ) {
29
+ this . layoutMode = state . app . layoutMode || 'vertical' ;
30
+ }
19
31
}
20
32
21
33
constructor ( self ) {
22
34
self = super ( self ) ;
23
35
self . handleClick = self . handleClick . bind ( self ) ;
36
+ self . receiveIframeMessage = self . receiveIframeMessage . bind ( self ) ;
37
+ self . handleExternalClicks = self . handleExternalClicks . bind ( self ) ;
24
38
self . useShadow = false ;
25
39
return self ;
26
40
}
27
41
28
42
connecting ( ) {
43
+ super . connecting && super . connecting ( ) ;
29
44
const state = store . getState ( ) ;
30
45
const { ishControlsHide } = window . ishControls ;
31
46
this . currentUrl =
32
47
state . app . currentUrl || urlHandler . getFileName ( patternName ) ;
33
48
this . ishControlsHide = ishControlsHide ;
49
+
50
+ window . addEventListener ( 'message' , this . receiveIframeMessage , false ) ;
51
+ document . addEventListener ( 'click' , this . handleExternalClicks ) ;
52
+ }
53
+
54
+ disconnecting ( ) {
55
+ super . disconnecting && super . disconnecting ( ) ;
56
+ document . removeEventListener ( 'click' , this . handleExternalClicks ) ;
57
+ window . removeEventListener ( 'message' , this . receiveIframeMessage ) ;
58
+ }
59
+
60
+ handleExternalClicks ( e ) {
61
+ if ( window . innerWidth >= 670 && this . layoutMode === 'vertical' ) {
62
+ return ;
63
+ }
64
+
65
+ if ( ! this . contains ( e . target ) && this . isOpen === true ) {
66
+ this . isOpen = false ;
67
+ }
68
+ }
69
+
70
+ close ( ) {
71
+ this . isOpen = false ;
72
+ }
73
+
74
+ toggle ( ) {
75
+ if ( this . isOpen ) {
76
+ this . close ( ) ;
77
+ } else {
78
+ this . open ( ) ;
79
+ }
80
+ }
81
+
82
+ open ( ) {
83
+ this . isOpen = true ;
34
84
}
35
85
36
86
handleClick ( e ) {
37
- const elem = e . target . closest ( '.pl-js-acc-handle' ) ;
38
- const panel = elem . nextSibling ;
87
+ if ( window . innerWidth >= 670 && this . layoutMode === 'vertical' ) {
88
+ return ;
89
+ }
90
+
91
+ e . preventDefault ( ) ;
92
+ e . stopPropagation ( ) ;
93
+
94
+ this . toggle ( ) ;
95
+ }
96
+
97
+ receiveIframeMessage ( event ) {
98
+ const self = this ;
99
+ // does the origin sending the message match the current host? if not dev/null the request
100
+ if (
101
+ window . location . protocol !== 'file:' &&
102
+ event . origin !== window . location . protocol + '//' + window . location . host
103
+ ) {
104
+ return ;
105
+ }
106
+
107
+ let data = { } ;
108
+ try {
109
+ data =
110
+ typeof event . data !== 'string' ? event . data : JSON . parse ( event . data ) ;
111
+ } catch ( e ) {
112
+ // @todo : how do we want to handle exceptions here?
113
+ }
39
114
40
- // Activate selected panel
41
- elem . classList . toggle ( 'pl-is-active' ) ;
42
- panel . classList . toggle ( 'pl-is-active' ) ;
115
+ if ( data . event !== undefined && data . event === 'patternLab.pageClick' ) {
116
+ try {
117
+ console . log ( 'patternLab.pageClick' ) ;
118
+ self . isOpen = false ;
119
+ } catch ( error ) {
120
+ console . log ( error ) ;
121
+ }
122
+ }
43
123
}
44
124
45
125
render ( ) {
46
- return (
126
+ if ( window . innerWidth >= 670 && this . layoutMode === 'vertical' ) {
127
+ this . isOpen = true ;
128
+ }
129
+
130
+ return html `
47
131
< div class ="pl-c-tools ">
48
- < button
49
- class = "pl-c-tools__toggle pl-js-acc-handle"
50
- title = "Tools"
51
- onClick = { this . handleClick }
132
+ < pl-button
133
+ icon-only
134
+ @click ="${ this . handleClick } "
135
+ class ="pl-c-tools__toggle "
136
+ >
137
+ < pl-icon name ="settings " slot ="after "> </ pl-icon >
138
+ </ pl-button >
139
+ < ul
140
+ class ="pl-c-tools__list pl-js-acc-panel ${ this . isOpen
141
+ ? 'is-open'
142
+ : '' } "
52
143
>
53
- < SettingsIcon
54
- width = { 18 }
55
- height = { 18 }
56
- viewBox = "0 0 24 24"
57
- className = "pl-c-tools__toggle-icon"
58
- fill = "currentColor"
59
- />
60
- </ button >
61
- < ul class = "pl-c-tools__list pl-js-acc-panel" >
62
144
< li class ="pl-c-tools__item ">
63
- < pl-toggle-info / >
145
+ < pl-toggle-info > </ pl-toggle-info >
64
146
</ li >
147
+
65
148
< li class ="pl-c-tools__item ">
66
- < pl-toggle-layout text = "Switch Layout" / >
149
+ < pl-toggle-layout text ="Switch Layout "> </ pl-toggle-layout >
67
150
</ li >
68
151
< li class ="pl-c-tools__item ">
69
- < pl-toggle-theme / >
152
+ < pl-toggle-theme > </ pl-toggle-theme >
70
153
</ li >
71
154
72
- { ! this . ishControlsHide [ 'views-new' ] && (
73
- < li class = "pl-c-tools__item" >
74
- < a
75
- href = { this . currentUrl }
76
- class = "pl-c-tools__action pl-js-open-new-window"
77
- target = "_blank"
78
- >
79
- < span class = "pl-c-tools__action-text" > Open In New Tab</ span >
80
- < span class = "pl-c-tools__action-icon" >
81
- < NewTabIcon
82
- height = { 20 }
83
- width = { 20 }
84
- viewBox = "0 0 24 24"
85
- fill = "currentColor"
86
- />
87
- </ span >
88
- </ a >
89
- </ li >
90
- ) }
91
-
92
- { ! this . ishControlsHide [ 'tools-docs' ] && (
93
- < li class = "pl-c-tools__item" >
94
- < a
95
- href = "http://patternlab.io/docs/"
96
- class = "pl-c-tools__action"
97
- target = "_blank"
98
- >
99
- < span class = "pl-c-tools__action-text" > Pattern Lab Docs</ span >
100
- < span class = "pl-c-tools__action-icon" >
101
- < HelpIcon
102
- height = { 20 }
103
- width = { 20 }
104
- viewBox = "0 0 24 24"
105
- fill = "currentColor"
106
- />
107
- </ span >
108
- </ a >
109
- </ li >
110
- ) }
155
+ ${ ! this . ishControlsHide [ 'views-new' ]
156
+ ? html `
157
+ < li class ="pl-c-tools__item ">
158
+ < pl-button href ="${ this . currentUrl } " target ="_blank ">
159
+ Open In New Tab
160
+ < pl-icon name ="new-tab " slot ="after "> </ pl-icon >
161
+ </ pl-button >
162
+ </ li >
163
+ `
164
+ : '' }
165
+ ${ ! this . ishControlsHide [ 'tools-docs' ]
166
+ ? html `
167
+ < li class ="pl-c-tools__item ">
168
+ < pl-button href ="http://patternlab.io/docs/ " target ="_blank ">
169
+ Pattern Lab Docs
170
+ < pl-icon name ="help " slot ="after "> </ pl-icon >
171
+ </ pl-button >
172
+ </ li >
173
+ `
174
+ : '' }
111
175
</ ul >
112
176
</ div >
113
- ) ;
177
+ ` ;
114
178
}
115
179
}
0 commit comments