forked from vguillou/fullscreen-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullscreen-behavior.html
168 lines (154 loc) · 5.51 KB
/
fullscreen-behavior.html
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
<link rel="import" href="../polymer/polymer.html">
<script>
/**
A simple Polymer `behavior` that wraps the HTML5 full screen API.
It lets you define which element to display in full screen mode
(via the `target` attribute) and toggle normal/full screen
mode by calling the `toggleFullscreen()` method.
Note that this method MUST be triggered directly by user interaction
(typically in a native `onclick` or Polymer's `on-click` handler).
If no `target` is set, the whole page (more specifically
`document.documentElement`) will be displayed full screen.
The element also provides 2 read-only flags as attribute:
- `fullscreenAvailable`: set to `true` if the browser supports
HTML5's full screen API (Safari on iOS does not).
- `fullscreen`: set to `true` if an element is currently displayed in
full screen mode.
@homepage https://github.com/vguillou/fullscreen-api
@demo demo/index.html
@polymerBehavior
*/
Polymer.FullscreenBehavior = {
properties: {
/**
* The element to display full screen, or the selector to use to automatically
* find the element to be displayed full screen.
*
* Note that changing the target while in full screen mode will not
* have any effect, as toggling between display modes MUST be
* triggered by user interaction.
*
* If `target` is not set, the whole page (more specifically
* `document.documentElement`) will be displayed full screen.
*
* @attribute target
* @type {Object|String}
*/
target: {
type: Object,
value: undefined,
notify: true
},
/**
* Read-only flag (boolean) indicating if an element is being
* displayed full screen.
*
* @attribute fullscreen
* @type {boolean}
*/
fullscreen: {
type: Boolean,
value: false,
notify: true,
readOnly: true,
reflectToAttribute: true
},
/**
* Read-only flag (boolean) indicating if full screen mode is available
* on the browser (Safari on iOS does not support it).
*
* @attribute fullscreenAvailable
* @type {boolean}
*/
fullscreenAvailable: {
type: Boolean,
value: false,
notify: true,
readOnly: true,
reflectToAttribute: true
}
},
/**
* Toggle between full screen and normal display mode.
* MUST be triggered directly by user interaction
* (typically in a native 'onclick' or Polymer's 'on-click' handler).
*
* @method toggleFullscreen
*/
toggleFullscreen: function() {
if (this.fullscreenAvailable) {
if (!this.fullscreen) {
// We are not in full screen mode, let's request it
// But first let's grad a hold on the target
var targetElement = typeof this.target !== "string" ? this.target :
document.querySelector(this.target);
targetElement = targetElement || document.documentElement;
if (targetElement.requestFullscreen) {
targetElement.requestFullscreen();
} else if (targetElement.webkitRequestFullscreen) {
targetElement.webkitRequestFullscreen();
} else if (targetElement.mozRequestFullScreen) {
targetElement.mozRequestFullScreen();
} else if (targetElement.msRequestFullscreen) {
targetElement.msRequestFullscreen();
}
} else {
// We are in full screen mode, let's exit
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
}
},
/**
* Exit full screen mode (if toggled)
*
* @method exitFullscreen
*/
exitFullscreen: function() {
if (this.fullscreen) {
this.toggleFullscreen();
}
},
ready: function() {
this._setFullscreenAvailable(this._isFullscreenAvailable());
if (this.fullscreenAvailable) {
this._boundFullscreenChangedHandler = this._fullscreenChangedHandler.bind(this);
document.addEventListener('fullscreenchange', this._boundFullscreenChangedHandler);
document.addEventListener('webkitfullscreenchange', this._boundFullscreenChangedHandler);
document.addEventListener('mozfullscreenchange', this._boundFullscreenChangedHandler);
document.addEventListener('MSFullscreenChange', this._boundFullscreenChangedHandler);
}
this._fullscreenChangedHandler();
},
detached: function() {
if (this._boundFullscreenChangedHandler) {
document.removeEventListener('fullscreenchange', this._boundFullscreenChangedHandler);
document.removeEventListener('webkitfullscreenchange', this._boundFullscreenChangedHandler);
document.removeEventListener('mozfullscreenchange', this._boundFullscreenChangedHandler);
document.removeEventListener('MSFullscreenChange', this._boundFullscreenChangedHandler);
}
},
_fullscreenChangedHandler: function() {
this._setFullscreen(this._isFullscreenToggled());
},
_isFullscreenAvailable: function() {
return (document.fullscreenEnabled ||
document.webkitFullscreenEnabled ||
document.mozFullScreenEnabled ||
document.msFullscreenEnabled) ? true : false;
},
_isFullscreenToggled: function() {
return (document.fullscreenElement ||
document.webkitFullscreenElement ||
document.mozFullScreenElement ||
document.msFullscreenElement) ? true : false;
},
};
</script>