-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
174 lines (147 loc) · 5.6 KB
/
scripts.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
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
window.addEventListener('DOMContentLoaded', function() {
"use strict";
var WELCOME_MSG = '*hello*';
var mainDiv = document.querySelector('.main');
var textDiv = document.querySelector('.text');
var inputField = document.querySelector('.inputbox');
var shareLinkField = document.querySelector('.js-share-link');
var charboxTemplate = document.querySelector('#charbox-template');
var defaultTitle = document.querySelector("title").innerText;
var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
function updateFragment(text) {
// Don't spam the browser history & strip query strings.
window.location.replace(location.origin + '/#' + encodeURIComponent(text));
shareLinkField.value = location.origin + '/' + location.hash;
}
function updateTitle(text) {
if (!text || text === WELCOME_MSG) {
document.title = defaultTitle;
} else {
document.title = text;
}
}
function clearChars() {
while (textDiv.firstChild) {
textDiv.removeChild(textDiv.firstChild);
}
}
function renderText() {
// Return a space as typing indicator if text is empty.
var text = decodeURIComponent(location.hash.split('#')[1] || ' ');
// var fontSize = Math.min(150 / text.length, 30);
var fontSize = 4;
clearChars();
text.split(/.*?/u).forEach(function(chr) {
var charbox = charboxTemplate.content.cloneNode(true);
var charElem = charbox.querySelector('.char');
charElem.style.fontSize = fontSize + 'vw';
if (chr !== ' ') {
charElem.textContent = chr;
} else {
charElem.innerHTML = ' ';
}
if (chr.match(/[0-9]/i)) {
charElem.className = 'number';
} else if (!chr.match(/\p{L}/iu)) {
charElem.className = 'symbol';
}
textDiv.appendChild(charbox);
});
// Ignore the placeholder space (typing indicator).
if (text === ' ') {
text = '';
}
// Don't jump the cursor to the end
if (inputField.value !== text) {
inputField.value = text;
}
updateFragment(text);
updateTitle(text);
}
function onInput(evt) {
updateFragment(evt.target.value);
}
function enterInputMode(evt) {
var defaultHash = '#' + encodeURIComponent(WELCOME_MSG);
if (location.hash === defaultHash) {
updateFragment('');
renderText();
}
inputField.focus();
}
function modalKeyHandler(sel, evt) {
// ESC to close the modal
if (evt.keyCode === 27) {
hideModal(sel);
}
}
function showModal(sel) {
window.removeEventListener('keypress', enterInputMode);
var modalDiv = document.querySelector(sel);
modalDiv.classList.add('open');
mainDiv.classList.add('blurred');
var closeBtn = modalDiv.querySelector('.js-modal-close');
// Use legacy event handling to avoid having to unregister handlers
closeBtn.onclick = hideModal.bind(null, sel);
window.onkeydown = modalKeyHandler.bind(null, sel);
// Make sure we're scrolled to the top on mobile
modalDiv.scrollTop = 0;
ga('send', 'event', 'modal-show', sel);
}
function hideModal(sel) {
var modalDiv = document.querySelector(sel);
modalDiv.classList.remove('open');
mainDiv.classList.remove('blurred');
window.onkeydown = null;
window.addEventListener('keypress', enterInputMode, false);
}
// function initAnalytics() {
// (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
// (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
// m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
// })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
//
// ga('set', 'anonymizeIp', true);
// ga('create', 'UA-37242602-2', 'auto');
// ga('send', 'pageview');
//
// window.twttr = window.twttr || {
// _e: [],
// ready: function(f) {
// this._e.push(f);
// }
// };
//
// twttr.ready(function (twttr) {
// twttr.events.bind('follow', function(event) {
// ga('send', 'event', 'twitter', 'follow');
// });
// twttr.events.bind('tweet', function(event) {
// ga('send', 'event', 'twitter', 'tweet');
// });
// });
// }
// document.querySelector('.js-help-button').addEventListener('click', function(evt) {
// evt.preventDefault();
// showModal('.js-help-modal');
// }, false);
//
// document.querySelector('.js-share-button').addEventListener('click', function(evt) {
// evt.preventDefault();
// showModal('.js-share-modal');
//
// // Don't pop up the keyboard on mobile
// if (!isMobile) {
// shareLinkField.select();
// }
// }, false);
inputField.addEventListener('input', onInput, false);
textDiv.addEventListener('click', enterInputMode, false);
window.addEventListener('keypress', enterInputMode, false);
window.addEventListener('hashchange', renderText, false);
if (!location.hash) {
updateFragment(WELCOME_MSG);
}
renderText();
// initAnalytics();
});