-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemojiMap2.js
217 lines (175 loc) · 7.37 KB
/
emojiMap2.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var activeEmojis = [];
var dashID = "emoji-dashboard";
var totalOccurrences;
var sizeOfContainer;
/*
--------------------------------------------------------------------------------
Emoji Map.js
--------------------------------------------------------------------------------
Author: J. Patrick Taggart
================================================================================
*/
/*
--------------------------------------------------------------------------------
Emoji Creation Functions
--------------------------------------------------------------------------------
*/
// Create a div to contain elements related to a particular emoji
// *** NO ERROR HANDLING CURRENTLY IMPLEMENTED
function createEmojiDiv(emojiData) {
console.log(emojiData);
var emojiDiv = document.createElement('div');
emojiDiv.setAttribute('class','emoji-container');
// Add the emoji image element as a child
emojiDiv.appendChild(createEmojiImage(emojiData));
// Keep track of emojis being displayed
activeEmojis.push(emojiData.emojiName);
return emojiDiv;
};
// *** NO ERROR HANDLING CURRENTLY IMPLEMENTED
function createEmojiImage(emojiData) {
// Assume file path is img/emojiName
console.log(emojiData);
var img = document.createElement('img');
img.setAttribute('id', emojiData.emojiName);
img.setAttribute('src', 'emoji_resources/' + emojiData.emojiName + '.svg');
var size = sizeOfContainer * emojiData.occurrences / totalOccurrences;
console.log("size of con: " + sizeOfContainer + ", occurenecs: " + emojiData.occurrences + ", total: " + totalOccurrences);
console.log(size);
img.style.height = size + "px";
img.style.width = size + "px";
return img;
};
// Make the emoji dashboard with a banner at top (and bottom?)
function createDashboard() {
// Could maybe make a call to refresh?
// get data from database
var dashboard = document.getElementById(dashID);
var data = JSON.parse(pullEmojiData());
console.log("total count: " + data.totalOccurrences);
totalOccurrences = parseInt(data.totalOccurrences);
sizeOfContainer = Math.min(dashboard.clientHeight, dashboard.clientWidth);
// Populate dashboard with emoji divs (could maybe place them in a table later)
var numEmojis = data.emojis.length;
for (var i = 0; i < numEmojis; ++i) {
//console.log(data.emojis[i].emojiName);
dashboard.appendChild(createEmojiDiv(data.emojis[i]));
}
setTimeout(refresh, 5000);
console.log(activeEmojis);
};
/*
--------------------------------------------------------------------------------
Dashboard functions
--------------------------------------------------------------------------------
*/
// Updates the sizes of emojis on screen
// *** NO ERROR HANDLING CURRENTLY IMPLEMENTED
function reorder(data) {
// Reorder emojis on screen and play a notification noise when emoji resized
activeEmojis.sort(function(a,b) {
console.log("sorting")
var a1 = parseInt(document.getElementById(a).style.width.substring(0, document.getElementById(a).style.width.length - 2));
var b1 = parseInt(document.getElementById(b).style.width.substring(0, document.getElementById(b).style.width.length - 2));
console.log('a1: ' + a1 + ', b1: ' + b1);
console.log(a1 > b1 ? -1 : a1 < b1 ? 1 : 0)
return a1 > b1 ? 1 : a1 < b1 ? -1 : 0;
});
activeEmojis.reverse();
var length = activeEmojis.length;
for (var i = 0; i < length; ++i) {
placeEmoji(activeEmojis[i], i);
}
// In future could add function to allow emojis to float
};
// Places an emoji on the display area
function placeEmoji(emojiName, index) {
};
function resize(data) {
// Resize all elements and notify when changes occur
console.log(data);
var numEmojis = data.emojis.length;
var dashboard = document.getElementById(dashID);
sizeOfContainer = Math.min(dashboard.clientHeight, dashboard.clientWidth);
totalOccurrences = data.totalOccurrences;
// Loop through all elements passed from database
var oldEmojis = activeEmojis;
activeEmojis = [];
for (var i = 0; i < numEmojis; ++i) {
var element = document.getElementById(data.emojis[i].emojiName);
if ( element !== null) {
var size = sizeOfContainer * data.emojis[i].occurrences / totalOccurrences; //Might need to redefine the globals?
// Check if element has changed size between iterations
// If they havent changed size then they don't need to be adjusted
if (Math.round(size) !== element.width) {
notify(data.emojis[i], size, element.size);
console.log("---------------------------------------------");
console.log('Changing size of: ' + data.emojis[i].emojiName);
console.log(size + ", " + element.width);
element.style.width = size + 'px';
element.style.height = size + 'px';
console.log(size + ", " + element.width);
console.log("---------------------------------------------");
activeEmojis.push(element.getAttribute('id'));
}
oldEmojis.splice(oldEmojis.indexOf(data.emojis[i].emojiName), 1);
} else {
// Add a new element to the document
emojiDiv = createEmojiDiv(data.emojis[i]);
dashboard.appendChild(emojiDiv);
notify(data.emojis[i], emojiDiv.style.height, 0);
}
}
// clear old elements
var oldLength = oldEmojis.length;
for (var i = 0; i < oldLength; ++i) {
var element = document.getElementById(oldEmojis[i]);
// Need to remove image and div
element.parentNode.parentNode.removeChild(element.parentNode);
}
};
function notify(emojiData, newSize, oldSize) {
//Make some noise and play an animation
};
// Function to refresh data pulled from database
// *** NO ERROR HANDLING CURRENTLY IMPLEMENTED
function refresh() {
console.log("You're about to fuck up");
var data = JSON.parse(pullEmojiData());
resize(data);
reorder(data);
setTimeout(refresh, 1000);
};
/*
--------------------------------------------------------------------------------
Database interactivity functions
--------------------------------------------------------------------------------
*/
function pullEmojiData() {
// *** NO ERROR HANDLING CURRENTLY IMPLEMENTED
// Access database and pull information
// Return data
if (dummyCounter >= 2) {
dummyCounter = 0;
} else {
++dummyCounter;
}
console.log(dummyCounter);
return dummyDataSet[dummyCounter];
};
// *** NO ERROR HANDLING CURRENTLY IMPLEMENTED
function playSound(filename) {
document.getElementById("sound").innerHTML='<audio autoplay="autoplay"><source src="' + filename + '.mp3" type="audio/mpeg" /><source src="' + filename + '.ogg" type="audio/ogg" /><embed hidden="true" autostart="true" loop="false" src="' + filename +'.mp3" /></audio>';
};
window.onload = function() { refresh(); };
window.onresize = function(event) {}; //refresh(); };
/*
--------------------------------------------------------------------------------
Functions and data for testing
--------------------------------------------------------------------------------
*/
var dummyCounter = 0;
var dummyData = '{ "totalOccurrences": 200, ' + '"emojis": [{ "emojiName": "happy", "occurrences": 190 }, { "emojiName": "angry", "occurrences": 10 }]' + '}';
var dummyData2 = '{ "totalOccurrences": 200, ' + '"emojis": [{ "emojiName": "happy", "occurrences": 195 }, { "emojiName": "angry", "occurrences": 5 }]' + '}';
var dummyData3 = '{ "totalOccurrences": 200, ' + '"emojis": [{ "emojiName": "happy", "occurrences": 200 }]' + '}';
var dummyDataSet = [dummyData, dummyData2, dummyData3];