-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (43 loc) · 1.74 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
50
51
52
53
54
55
(function(win, doc){
'use strict';
var $formGenerate = doc.querySelector('[data-js="formGenerator"]'),
$emojiReceiver = doc.querySelector('[data-js="emoji-receiver"]');
var app = (function(){
return {
init: function(){
this.initEvents();
},
initEvents: function initEvents() {
$formGenerate.addEventListener('submit', this.handleSubmit, false);
},
handleSubmit: function handleSubmit(event){
event.preventDefault();
//console.log('evento de formulario')
app.emojiConection();
},
emojiConection: function emojiConection(){
var ajax = new XMLHttpRequest;
ajax.open('get', 'emojis.json', true);
ajax.send();
ajax.addEventListener('readystatechange', this.getEmoji, false);
},
getEmoji: function getEmoji(){
if (!app.isReady.call(this))
return;
var data = JSON.parse(this.responseText);
var emojiCount = Object.keys(data).length;
console.log(emojiCount);
console.log(app.getRandomEmoji(emojiCount));
$emojiReceiver.textContent = data[app.getRandomEmoji(emojiCount)];
},
getRandomEmoji: function getRandomEmoji(max){
var maxNumber = max + 1;
return Math.floor(Math.random() * (maxNumber - 1)) + 1;
},
isReady: function isReady(){
return this.readyState === 4 && this.status === 200;
}
}
})();
app.init();
})(window, document);