-
Notifications
You must be signed in to change notification settings - Fork 12
/
func.js
325 lines (299 loc) · 8.8 KB
/
func.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var clips = $(document).data('clips');
var names = $(document).data('names');
var sentences = $(document).data('sentences');
var categories = $(document).data('categories');
var playing = null;
var previous = null;
var interval = null;
function renderButtons(objs) {
$(Object.keys(objs)).each(function(i, key) {
var obj = $(objs[key]);
if (obj.length > 0) {
var path = obj[0];
var button = $('<button data-token="' + key + '">' + names[key] + '</button>');
button.on('mousedown', function() {
try {
var sound = new Howl({
urls: genPath(path),
onend: function() {this.unload();}
});
sound.play();
} catch(e) {
console.log('Howl is not found');
}
return false;
})
button.holdEvent({handler: tokenUpdater(), time: 600});
if (0 === $('#buttons div[data-category="' + categories[key] + '"]').length) {
var div = $('<div data-category="' + categories[key] + '"><h2>' + categories[key] + '</h2></div>');
$('#buttons').append(div);
}
$('#buttons div[data-category="' + categories[key] + '"]').append(button);
}
});
}
function tokenMake(key) {
return $('<button data-token="' + key + '">' + names[key] + '</button>').on('dblclick', function() {$(this).remove()});
}
function tokenUpdater() {
return function(elementClicked) {
tokenMake(elementClicked.data('token')).appendTo('#tokens');
$('#tokens').sortable({cancel: ''});
}
}
function sayTokens() {
stopPlaying();
updateHash();
var tokens = $('#tokens button');
if (tokens.length > 0) {
tokens.removeClass('talking');
sayToken(tokens);
} else {
$('#lucky').click();
}
}
function feelLucky() {
var sentence = sentences[Math.floor(Math.random()*sentences.length)];
appendTokensByString(sentence);
}
function genPath(key) {
var filename = 'convert/' + key;
return [filename + '.ogg', filename + '.mp3'];
}
function convertTokToKey(key) {
var tokenSet = clips[key];
if ('undefined' !== typeof(tokenSet)) {
return tokenSet[Math.floor(Math.random()*tokenSet.length)];
}
return null;
}
function sayToken(tokens) {
var tokQue = tokens.not('.talking');
if (0 === tokQue.length) {
return;
}
var currentTok = tokQue.first();
playing = new Howl({
urls: genPath(convertTokToKey(currentTok.data('token'))),
onplay: function() {
currentTok.addClass('talking');
var pos = currentTok.position().top - $('#tokens').position().top - 10;
if (pos > 0) {
$('#tokens').animate({scrollTop: $('#tokens').scrollTop() + pos}, 'fast');
}
},
onloaderror: function() {
this.stop();
this.unload();
},
onend: function() {
if (null !== previous) {
previous.unload();
previous = null;
}
if (tokQue.length > 1) {
previous = this;
sayToken(tokens);
} else {
playing = null;
this.unload();
tokens.removeClass('talking');
}
}
});
playing.play();
}
$(document).ready(function() {
renderButtons(clips);
var str = window.location.hash.substr(1);
if (str.length > 0) {
appendTokensByString(str);
setTimeout(function() {
sayTokens();
}, 1000);
}
setTimeout(function() {
$('#fb_like iframe:first').attr('src', $('#fb_like iframe:first').data('src'));
}, 1000);
$('#say').on('click', function() {
sayTokens();
return false;
});
$('#time').on('click', function() {
$('#tokens').empty();
appendTokensByString(getTimeString());
sayTokens();
return false;
});
$('#ip').on('click', function() {
$('#tokens').empty();
appendTokensByString(getIPString());
sayTokens();
return false;
});
$('#like').on('click', function() {
$('#tokens').empty();
var url = prompt('URL? (cancel for stop looping)', 'https://www.facebook.com/IsShow/');
speakFacebookLikeCount(url);
return false;
});
$('#clear').on('click', function() {
$('#tokens').empty();
stopPlaying();
return false;
});
$('#stop').on('click', function() {
stopPlaying();
$('#tokens button').removeClass('talking');
return false;
});
$('#lucky').on('click', function() {
$('#tokens').empty();
feelLucky();
sayTokens();
return false;
});
$('#getlink').on('click', function() {
return prompt('分享連結', getLink());
});
return false;
});
function stopPlaying() {
if (null !== playing) {
try {
playing.stop();
playing.unload();
} catch (e) {
playing = null;
}
}
$('#tokens').animate({scrollTop: 0}, 'fast');
playing = null;
previous = null;
}
function appendTokensByString(str) {
if (str.length > 0) {
var tokens = str.replace(/_/g, ' ').trim().split(' ');
$(tokens).each(function(i, key) {
if ("[TIME]" === key) {
appendTokensByString(getTimeString());
}
if ("[IP]" === key) {
appendTokensByString(getIPString());
}
if (key.match(/^\[FBLIKE/)) {
url = key.match(/^\[FBLIKE=(.*)\]/);
stopPlaying();
speakFacebookLikeCount(url[1]);
}
if ('undefined' !== typeof(names[key])) {
$('#tokens').append(tokenMake(key));
}
});
$('#tokens').sortable({cancel: ''});
}
}
function updateHash() {
var tokens = $('#tokens button');
if (tokens.length > 0) {
var hash = '';
tokens.each(function (i, obj) {
hash += $(obj).data('token') + ' ';
});
window.location.hash = hash.trim().replace(/ /g, '_');
}
return false;
}
function getTimeString() {
var date = new Date();
var hour = date.getHours();
var minute = date.getMinutes();
if (0 === minute) {
return convertNumToTok(hour) + ' dian whole';
} else {
return convertNumToTok(hour) + ' dian ' + convertNumToTok(minute) + ' fen';
}
}
function getIPString() {
var ip = client_ip;
var retStr = '';
for (var pos = 0; pos < ip.length; pos++) {
if('.' == ip[pos]) {
retStr += ' dian';
} else {
retStr += ' ' + ip[pos];
}
}
return retStr;
}
function convertNumToTok(num) {
if (0 == num) {
return '0';
} else if (2 == num) {
return '2s';
} else if (10 == num) {
return '10';
} else {
if (num < 10) {
return num;
} else if (num > 10 && num < 20) {
return '10 ' + (num%10);
} else if (num >= 20) {
var retStr = parseInt(num/10) + ' 10 ';
if (num % 10 != 0) {
retStr += num % 10;
}
return retStr;
}
}
}
function getLink() {
updateHash();
return window.location;
}
// from http://jsfiddle.net/Takazudo/RHnzM/
function fetchLikeCount(url) {
return $.Deferred(function(defer){
$.ajax({
dataType: 'jsonp',
url: 'https://api.facebook.com/method/fql.query?callback=callback',
data: {
query: 'SELECT like_count FROM link_stat WHERE url="' + url + '"',
format: 'JSON'
}
}).then(function(res){
try {
defer.resolve(res[0].like_count);
} catch(e) {
reject();
}
}, reject);
function reject(){
defer.reject(';(');
};
}).promise();
}
function speakFacebookLikeCount(url) {
var callback = function () {
// dirty way for cleaning input text
stopPlaying();
url = $('<div>' + url + '<div>').text();
fetchLikeCount(url).always(function (count) {
$('#tokens').empty();
$('#tokens').append('<div>Facebook like count of [' + url + ']</div>');
appendTokensByString(count.toString().replace(/\B(?=(\d){1})/g, " "));
sayTokens();
})
};
// stops interval that has been set
if (null !== interval) {
clearInterval(interval);
}
if (url) {
$('#like').addClass('talking');
callback();
interval = setInterval(callback, 3000);
} else {
$('#like').removeClass('talking');
}
}