-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgraded-hexaflip-text-slides.js
439 lines (359 loc) · 11.6 KB
/
upgraded-hexaflip-text-slides.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
var hexaflipTextSlides = {
setTextSlidesInProgress: false,
threadMode: true,
disableThreadMode: function() {
hexaflipTextSlides.threadMode = false;
},
enableThreadMode: function() {
hexaflipTextSlides.threadMode = true;
},
isThreadMode: function() {
return hexaflipTextSlides.threadMode;
},
currentThread: 0,
newThread: function() {
return ++hexaflipTextSlides.currentThread;
},
isCurrentThread: function(thread) {
if (hexaflipTextSlides.isThreadMode()) {
return thread === hexaflipTextSlides.currentThread;
} else {
return true;
}
},
whetherSlidesCanBeSet: function() {
if (hexaflipTextSlides.isThreadMode()) {
return true;
} else {
return !hexaflipTextSlides.setTextSlidesInProgress;
}
},
linesOrgHTML: ''
};
hexaflipTextSlides.generateLines = function generateLines(container, rows) {
var i, lineHTML = '<div class="line"></div>\n';
container.innerHTML = '';
for (i = 0; i < rows; i++) {
container.innerHTML += lineHTML;
}
hexaflipTextSlides.linesOrgHTML = container.innerHTML;
};
hexaflipTextSlides.emptyLines = function emptyLines(number) {
if (number <= 0) {
return '';
}
var i, text = '';
for (i = 0; i < number; i++) {
text += endl;
}
return text;
};
hexaflipTextSlides.emojiStringToArray = function emojiStringToArray(str) {
var split = str.split(/([\uD800-\uDBFF][\uDC00-\uDFFF])/);
var arr = [];
for (var i = 0; i < split.length; i++) {
var char = split[i];
if (char !== "") {
arr.push(char);
}
}
return arr;
};
hexaflipTextSlides.removeEmojiFromString = function removeEmojiFromString(str) {
return str.replace(
/(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/g,
'');
};
hexaflipTextSlides.mixedStringToArray = function mixedStringToArray(str) {
var src = str.split('');
var dst = [];
var emoji = '', emojiDetected = false;
var i, j;
for (i = 0; i < src.length; i++) {
if (extendedAlphabet.indexOf(src[i]) == -1) {
emoji += src[i];
if (!emojiDetected) {
emojiDetected = true;
}
if (emojisArray.indexOf(emoji) != -1) {
dst.push(emoji);
emojiDetected = false;
emoji = '';
}
} else {
if (emojiDetected) {
dst.push(emoji);
emojiDetected = false;
emoji = '';
}
dst.push(src[i]);
}
}
if (emojiDetected) {
dst.push(emoji);
}
return dst;
};
hexaflipTextSlides.getWordElement = function getWordElement(alphabet, length, styleFn) {
var element = {};
for (var i = 0; i < length; i++) {
element['l' + i] = getLetterElement(alphabet, i, styleFn);
}
function getLetterElement(letters, letterNo, styleFn) {
var element = [], style;
for (var i = 0; i < letters.length; i++) {
element.push({
value: letters[i]
});
if (styleFn) {
style = styleFn(letterNo, i, letters[i]);
if (style) {
element[element.length - 1].style = style;
}
}
}
return element;
}
return element;
};
hexaflipTextSlides.setChosenWordInCubesLine = function setChosenWordInCubesLine(hexaTimeLine, word) {
var i, element, setsLength = 0;
word = hexaflipTextSlides.mixedStringToArray(word);
for (var key in hexaTimeLine.sets) {
if (hexaTimeLine.sets.hasOwnProperty(key)) {
setsLength++;
}
}
if (word.length > 0) {
for (i = 0; i < Math.min(setsLength, word.length); i++) {
element = {};
element['l' + i] = word[i];
hexaTimeLine.setValue(element);
}
}
};
hexaflipTextSlides.setSlides = function setSlides(slides, options, callback) {
var thread = hexaflipTextSlides.newThread();
if (hexaflipTextSlides.whetherSlidesCanBeSet()) {
hexaflipTextSlides.setTextSlidesInProgress = true;
var i, option, slideOptions;
options = options || {};
var defaults = {
duration: 1000,
textAlign: 'left',
textVAlign: 'top',
wordBreaking: true
};
for (option in defaults) {
if (defaults.hasOwnProperty(option)) {
options[option] = (options[option] !== undefined ? options[option] : defaults[option]);
}
}
i = -1;
promiseWhile(function() {
i++;
return i < slides.length && hexaflipTextSlides.isCurrentThread(thread);
}, function() {
var deferred = Q.defer();
slides[i].options = slides[i].options || {};
slideOptions = {};
for (option in options) {
if (options.hasOwnProperty(option)) {
slides[i].options[option] = (slides[i].options[option] !== undefined ? slides[i].options[option] : options[option]);
slideOptions[option] = slides[i].options[option];
}
}
slideOptions.thread = thread;
hexaflipTextSlides.setSlide(slides[i].text, slideOptions).done(function(remainText) {
if (remainText && remainText.length > 0) {
slides.splice(i + 1, 0, {
text: remainText,
options: slides[i].options
});
}
deferred.resolve();
});
return deferred.promise;
}).done(function() {
hexaflipTextSlides.setTextSlidesInProgress = false;
if (hexaflipTextSlides.isCurrentThread(thread) && callback) {
callback();
}
});
}
};
hexaflipTextSlides.setSlide = function setSlide(text, options) {
var deferred = Q.defer();
var i, j, currentHexaTimeNo = -1, maxHexaTimeNo = hexaTime.length - 1, currentHexaTime;
var emptyFields = 0, emptyLine = space, fulfilled = false;
options = options || {};
if (maxHexaTimeNo < 0) {
deferred.resolve();
return deferred.promise;
}
var setsLength = 0;
for (var key in hexaTime[0].sets) {
if (hexaTime[0].sets.hasOwnProperty(key)) {
setsLength++;
}
}
var regExp = new RegExp('[ ' + endl + ']');
text = text.split(regExp);
prepareHAlignedTexts();
prepareVAlignedTexts();
for (i = 0; i < text.length; i++) {
if (emptyFields <= 0 || emptyFields < text[i].length) {
setNextHexaTime();
if (emptyFields <= 0) {
for (j = i; j < text.length; j++) {
text[j] = text[j].trim(space);
}
setTimeout(function() {
if (hexaflipTextSlides.isCurrentThread(options.thread)) {
deferred.resolve(text.slice(i).join(space));
} else {
deferred.resolve();
}
}, options.duration);
fulfilled = true;
break;
}
}
emptyFields -= text[i].length;
hexaflipTextSlides.setChosenWordInCubesLine(currentHexaTime, text[i]);
}
if (!fulfilled) {
for (i = 0; i < setsLength; i++) {
emptyLine += space;
}
for (i = currentHexaTimeNo + 1; i <= maxHexaTimeNo; i++) {
setNextHexaTime();
hexaflipTextSlides.setChosenWordInCubesLine(currentHexaTime, emptyLine);
}
setTimeout(function() {
deferred.resolve();
}, options.duration);
}
return deferred.promise;
function setNextHexaTime() {
currentHexaTimeNo++;
if (currentHexaTimeNo <= maxHexaTimeNo) {
currentHexaTime = hexaTime[currentHexaTimeNo];
emptyFields = setsLength;
} else {
currentHexaTimeNo = -1;
currentHexaTime = undefined;
emptyFields = 0;
}
}
function prepareHAlignedTexts() {
for (i = 0; i <= text.length - 1; i++) {
if (i != 0) {
if (text[i] != '' && text[i - 1].length + text[i].length <= setsLength) {
text[i - 1] += text[i];
if (text[i - 1].length < setsLength) {
text[i - 1] += ' ';
}
text.splice(i, 1);
i--;
if (i == text.length - 1) {
text[i] = completeTheLine(text[i]);
}
continue;
} else if (text[i - 1].length < setsLength) {
text[i - 1] = completeTheLine(text[i - 1]);
}
if (text[i] == '') {
text[i] = completeTheLine('');
continue;
}
}
if (text[i].length > setsLength) {
if (options.wordBreaking) {
//if word breaking is enabled, split the word
text.splice(i + 1, 0, text[i].substring(setsLength - 1));
text[i] = text[i].substring(0, setsLength - 1) + jointChar;
} else {
//if word breaking is disabled, take up the text to max length
text[i] = text[i].substring(0, setsLength);
}
} else if (text[i].length < setsLength) {
text[i] = text[i] + space;
}
if (i == text.length - 1) {
text[i] = completeTheLine(text[i]);
}
}
}
function completeTheLine(line) {
var i, spacesBefore, completedLine = '';
line = line.replace(/^[ ]+|[ ]+$/g, ''); //remove leading and trailing white spaces only
//line = line.trim(space);
var lineLength = hexaflipTextSlides.mixedStringToArray(line).length;
if (options.textAlign == 'left') {
spacesBefore = 0;
}
if (options.textAlign == 'right') {
spacesBefore = Math.max(setsLength - lineLength, 0);
}
if (options.textAlign == 'center') {
spacesBefore = Math.max(Math.floor((setsLength - lineLength) / 2), 0);
}
for (i = 0; i < spacesBefore; i++) {
completedLine += space;
}
if (lineLength > setsLength) {
completedLine += line.substring(0, setsLength);
} else {
completedLine += line;
}
for (i = Math.max(spacesBefore + lineLength - 1, 0); i < setsLength; i++) {
completedLine += space;
}
return completedLine;
}
function prepareVAlignedTexts() {
var i, emptyLine = '', count;
if (text.length - 1 >= maxHexaTimeNo) {
//do nothing here: text is already top, center and bottom vertically aligned
return;
}
if (options.textVAlign == 'top') {
//do nothing here: text is top aligned vertically by default
}
for (i = 0; i < setsLength; i++) {
emptyLine += space;
}
if (options.textVAlign == 'bottom') {
count = maxHexaTimeNo + 1 - text.length;
} else if (options.textVAlign == 'center') {
count = Math.floor((maxHexaTimeNo + 1 - text.length) / 2);
}
for (i = 0; i < count; i++) {
text.splice(0, 0, emptyLine); //add as many empty lines as needed
}
}
};
function promiseWhile(condition, body) {
var done = Q.defer();
function loop() {
// When the result of calling `condition` is no longer true, we are
// done.
if (!condition()) return done.resolve();
// Use `when`, in case `body` does not return a promise.
// When it completes loop again otherwise, if it fails, reject the
// done promise
Q.when(body(), loop, done.reject);
}
// Start running the loop in the next tick so that this function is
// completely async. It would be unexpected if `body` was called
// synchronously the first time.
Q.nextTick(loop);
// The promise
return done.promise;
}
var extendedAlphabet = " 01234567890AĄBCĆDEĘFGHIJKLŁMNŃOÓPQRSŚTUVWXYZŹŻaąbcćdeęfghijklłmnńoópqrsśtuvwxyzźż!@#$%^^&*()-_=+[]{}:;–'\",.<>/?♥→".split('');
var emojis = "✝👍😄🐟💪✔💬🔥😊💡";
var emojisArray = hexaflipTextSlides.emojiStringToArray(emojis);
extendedAlphabet = extendedAlphabet.concat(emojisArray);
var endl = '◘', space = ' ', jointChar = '–';