initial checkin
This commit is contained in:
123
app/js/index.js
Normal file
123
app/js/index.js
Normal file
@@ -0,0 +1,123 @@
|
||||
if (typeof(sw) == 'undefined') {
|
||||
var sw = {};
|
||||
sw.apiPrefix = '/';
|
||||
}
|
||||
|
||||
|
||||
(function($) {
|
||||
|
||||
|
||||
//helper functions
|
||||
var pulse = function pulse(elem, duration, easing, props_to, props_from, until) {
|
||||
elem.animate(props_to, duration, easing, function() {
|
||||
if (until() == false) {
|
||||
pulse(elem, duration, easing, props_from, props_to, until);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var shuffle = function(obj) {
|
||||
return obj.sort( function() { return 0.5 - Math.random(); } );
|
||||
};
|
||||
var getKey = function(obj) {
|
||||
var word = [];
|
||||
for(var k in obj) word.push(k);
|
||||
return word[0];
|
||||
};
|
||||
|
||||
var selectWordsForGame = function() {
|
||||
//select a random word from the list
|
||||
var words = sw.words[Math.round(Math.random() * sw.words.length)];
|
||||
|
||||
var word = getKey(words);
|
||||
var similar = words[word];
|
||||
//i really should include _
|
||||
var filtered = [];
|
||||
for(var i=0; i<similar.length; i++) {
|
||||
if(similar[i] != word) {
|
||||
filtered.push(similar[i]);
|
||||
}
|
||||
}
|
||||
filtered = shuffle(filtered);
|
||||
filtered = filtered.slice(0, 3);
|
||||
filtered.push(word);
|
||||
filtered = shuffle(filtered);
|
||||
|
||||
sw.currentWord = word;
|
||||
return {word: word, similar: filtered};
|
||||
};
|
||||
|
||||
var startGame = function() {
|
||||
$("#main").empty().html('views/index.ejs', selectWordsForGame());
|
||||
};
|
||||
|
||||
//start app
|
||||
var w = $.ajax({
|
||||
url: sw.apiPrefix + "words.json",
|
||||
dataType: 'json'
|
||||
});
|
||||
|
||||
|
||||
w.done(function(words) {
|
||||
var start = $('.start');
|
||||
start.removeClass('disabled');
|
||||
start.html(' Start ');
|
||||
start.animate({
|
||||
opacity : 1
|
||||
}, 1000, 'linear');
|
||||
|
||||
sw.words = words.words;
|
||||
});
|
||||
|
||||
//events
|
||||
$(document).on('click', '.start', function() {
|
||||
startGame();
|
||||
});
|
||||
|
||||
$(document).on('click', '.word-button', function(ev, el) {
|
||||
el = $(ev.currentTarget);
|
||||
if(sw.currentWord == el.html()) {
|
||||
el.addClass('btn-success');
|
||||
} else {
|
||||
el.addClass('btn-danger');
|
||||
}
|
||||
|
||||
setTimeout(function() {
|
||||
$('.word-button[data-word="' + sw.currentWord + '"]').addClass('btn-success');
|
||||
|
||||
setTimeout(function() {
|
||||
startGame();
|
||||
}, 1000);
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
|
||||
//globals
|
||||
sw.words = [];
|
||||
sw.currentWord;
|
||||
})(jQuery);
|
||||
|
||||
|
||||
|
||||
//utils
|
||||
(function() {
|
||||
window.Utils = {};
|
||||
|
||||
Utils.ellipsis = function(text, maxLength){
|
||||
if(typeof maxLength === 'undefined'){
|
||||
maxLength = 9000; //a large number
|
||||
}
|
||||
|
||||
if (text.length <= maxLength)
|
||||
return text;
|
||||
|
||||
var xMaxFit = maxLength - 1;
|
||||
var xTruncateAt = text.lastIndexOf(' ', xMaxFit);
|
||||
if (xTruncateAt == -1 || xTruncateAt < maxLength / 2)
|
||||
xTruncateAt = xMaxFit;
|
||||
|
||||
return text.substr(0, xTruncateAt) + "…";
|
||||
};
|
||||
|
||||
})();
|
||||
|
Reference in New Issue
Block a user