-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowsersize-tooltip.js
123 lines (103 loc) · 2.58 KB
/
browsersize-tooltip.js
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
'use strict';
!function() {
class BrowserSizeTooltip {
constructor() {
var oldEl = document.getElementById(this._getID()); // Old element
if(oldEl == null) {
this._el = document.createElement('span');
this._setId();
} else {
this._el = oldEl;
}
this._init(); // Initialize component
}
/**
* Component initialization
*
*/
_init() {
this._applyStyles({
'position': 'fixed',
'bottom': 0,
'right': 0,
'zIndex': 9999999,
'padding': '8px',
'backgroundColor': '#000000',
'color': '#FFFFFF'
});
this._updateDimension(); // Calculate the dimension
this._events(); // Events
this._addEl(); // Append element
}
/**
* The ID string of the element.
*
* @return {String] - The ID of the element.
*/
_getID() {
return 'browsersize-tooltip-' + Date.now();
}
/**
* Apply element styles.
*
* @param {Array} styles [] - The CSS to apply, supplied in JSON format.
*/
_applyStyles(styles = []) {
var newStyle = this._el.getAttribute('style');
if(newStyle == null) {
newStyle = '';
}
for(var key in styles) {
newStyle += this._dashed(key) + ':' + styles[key] + ';';
}
this._el.setAttribute('style', newStyle);
}
/**
* Convert camelCase string to kebab-case.
*
* @param {String} property - The string to convert.
*/
_dashed(property) {
var newProperty = '';
for(var i = 0; i < property.length; i++) {
var char = property.charAt(i);
if(char === char.toUpperCase()) { // character is uppercase
if(property.indexOf(char) > 0) {
newProperty += '-';
}
}
newProperty += char;
}
return newProperty.toLowerCase();
}
/**
* Set the ID attribute of the element.
*/
_setId() {
this._el.setAttribute('id', this._getID());
}
/**
* Determine the dimension of the browser window.
*/
_updateDimension() {
this._el.innerHTML = window.innerWidth + ' x ' + window.innerHeight + ' pixels';
}
/**
* Append the element to the body.
*/
_addEl() {
document.body.appendChild(this._el);
}
/**
* Event listeners.
* Specifically listening to the resize event of the window.
*/
_events() {
var that = this;
window.addEventListener('resize', function() {
that._updateDimension();
})
}
}
new BrowserSizeTooltip();
}();