/
show-hide-when.ts
174 lines (155 loc) · 4.29 KB
/
show-hide-when.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { Attribute, Directive, NgZone } from '@angular/core';
import { Platform } from '../../platform/platform';
/**
* @private
*/
export class DisplayWhen {
isMatch: boolean = false;
conditions: string[];
constructor(conditions: string, public platform: Platform, public zone: NgZone) {
this.platform = platform;
if (!conditions) return;
this.conditions = conditions.replace(/\s/g, '').split(',');
// check if its one of the matching platforms first
// a platform does not change during the life of an app
for (let i = 0; i < this.conditions.length; i++) {
if (this.conditions[i] && platform.is(this.conditions[i])) {
this.isMatch = true;
return;
}
}
if (this.orientation()) {
// add window resize listener
platform.onResize(() => {
zone.run(() => {
this.orientation();
});
});
return;
}
}
orientation(): boolean {
for (let i = 0; i < this.conditions.length; i++) {
if (this.conditions[i] === 'portrait') {
this.isMatch = this.platform.isPortrait();
return true;
}
if (this.conditions[i] === 'landscape') {
this.isMatch = this.platform.isLandscape();
return true;
}
}
}
}
/**
*
* @name ShowWhen
* @description
* The `showWhen` attribute takes a string that represents a platform or screen orientation.
* The element the attribute is added to will only be shown when that platform or screen orientation is active.
*
* Complements the [hideWhen attribute](../HideWhen). If the `showWhen` attribute is used on an
* element that also has the `hideWhen` attribute, the element will not show if `hideWhen` evaluates
* to `true` or `showWhen` evaluates to `false`. If the `hidden` attribute is also added, the element
* will not show if `hidden` evaluates to `true`.
*
* View the [Platform API docs](../../../platform/Platform) for more information on the different
* platforms you can use.
*
* @usage
* ```html
* <div showWhen="android">
* I am visible on Android!
* </div>
*
* <div showWhen="ios">
* I am visible on iOS!
* </div>
*
* <div showWhen="android,ios">
* I am visible on Android and iOS!
* </div>
*
* <div showWhen="portrait">
* I am visible on Portrait!
* </div>
*
* <div showWhen="landscape">
* I am visible on Landscape!
* </div>
* ```
* @demo /docs/v2/demos/src/show-when/
* @see {@link ../HideWhen HideWhen API Docs}
* @see {@link ../../../platform/Platform Platform API Docs}
*/
@Directive({
selector: '[showWhen]',
host: {
'[class.hidden-show-when]': '!isMatch'
}
})
export class ShowWhen extends DisplayWhen {
constructor(
@Attribute('showWhen') showWhen: string,
platform: Platform,
zone: NgZone
) {
super(showWhen, platform, zone);
}
}
/**
* @name HideWhen
* @description
* The `hideWhen` attribute takes a string that represents a plaform or screen orientation.
* The element the attribute is added to will only be hidden when that platform or screen orientation is active.
*
* Complements the [showWhen attribute](../ShowWhen). If the `hideWhen` attribute is used on an
* element that also has the `showWhen` attribute, the element will not show if `hideWhen` evaluates
* to `true` or `showWhen` evaluates to `false`. If the `hidden` attribute is also added, the element
* will not show if `hidden` evaluates to `true`.
*
* View the [Platform API docs](../../../platform/Platform) for more information on the different
* platforms you can use.
*
* @usage
* ```html
* <div hideWhen="android">
* I am hidden on Android!
* </div>
*
* <div hideWhen="ios">
* I am hidden on iOS!
* </div>
*
* <div hideWhen="android,ios">
* I am hidden on Android and iOS!
* </div>
*
* <div hideWhen="portrait">
* I am hidden on Portrait!
* </div>
*
* <div hideWhen="landscape">
* I am hidden on Landscape!
* </div>
* ```
*
* @demo /docs/v2/demos/src/hide-when/
* @see {@link ../ShowWhen ShowWhen API Docs}
* @see {@link ../../../platform/Platform Platform API Docs}
*/
@Directive({
selector: '[hideWhen]',
host: {
'[class.hidden-hide-when]': 'isMatch'
}
})
export class HideWhen extends DisplayWhen {
constructor(
@Attribute('hideWhen') hideWhen: string,
platform: Platform,
zone: NgZone
) {
super(hideWhen, platform, zone);
}
}