-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
50 lines (38 loc) · 1.33 KB
/
app.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
var container = document.getElementById('color-container');
var themeColor = document.head.querySelector('meta[name="theme-color"]');
var setter = document.getElementById('color-setter');
function stringToColor(str) {
// str to hash
for (var i = 0, hash = 0; i < str.length; hash = str.charCodeAt(i++) + ((hash << 5) - hash));
// int/hash to hex
for (var i = 0, color = "#"; i < 3; color += ("00" + ((hash >> i++ * 8) & 0xFF).toString(16)).slice(-2));
return color;
}
function greetings(event) {
localforage.setItem('savedName', event.target[0].value);
window.greet(event)
}
function alertName(e) { alert('hello, ' + e.target[0].value + '!'); }
window.greet = alertName;
function setValue() {
localforage.getItem('savedName').then(function (key) {
var name = 'tim.js';
if (key) {
name = key;
} else {
localforage.setItem('savedName', name);
}
setter.value = name;
var startValue = stringToColor(setter.value);
container.style.backgroundColor = startValue;
themeColor.content = startValue;
console.timeEnd('get value from IndexedDB')
});
}
console.time('get value from IndexedDB');
setValue();
setter.addEventListener('keyup', function(e) {
var newColor = stringToColor(e.target.value);
container.style.backgroundColor = newColor;
themeColor.content = newColor;
}, false);