From 340f983249f25d3c092d81b3ddb38ceff23d545a Mon Sep 17 00:00:00 2001 From: Russell Beattie Date: Tue, 12 Mar 2013 17:22:48 -0700 Subject: [PATCH] Added sort and fixed shuffle --- app.js | 57 ++++++++++++++++++++++++++++++++++++++++-------------- index.html | 3 ++- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/app.js b/app.js index d9419b8..e7a7b9b 100644 --- a/app.js +++ b/app.js @@ -265,6 +265,7 @@ App.PuzzleView = Backbone.View.extend({ if(App.touch){ events = { 'touchend .shuffle' : 'doShuffle', + 'touchend .sort' : 'doSort', 'touchend .hint' : 'doHint', 'touchend .square' : 'doSquare', 'touchend .reset': 'doReset', @@ -275,6 +276,7 @@ App.PuzzleView = Backbone.View.extend({ } else { events = { 'click .shuffle' : 'doShuffle', + 'click .sort' : 'doSort', 'click .hint' : 'doHint', 'click .square' : 'doSquare', 'click .reset': 'doReset', @@ -328,8 +330,46 @@ App.PuzzleView = Backbone.View.extend({ }, doShuffle: function(e){ - shuffleItems('tr'); - shuffleItems('tbody'); + App.puzzleView.redrawSquares('shuffle'); + }, + + doSort: function(e){ + App.puzzleView.redrawSquares('sort'); + }, + + redrawSquares: function(order){ + + App.puzzleView.doReset(); + + var squares = puzzles[App.puzzleView.puzNum]['squares']; + + if(order == 'sort'){ + squares.sort(); + } else { + squares = shuffle(squares); + } + + var hidden = []; + $('.square:hidden').each(function(){ + hidden.push($(this).text()); + }); + hidden.sort(); + + for(var i = 0; i < $('.square').length; i++){ + + $('.square').eq(i).text(squares[i]); + + var h = hidden.indexOf(squares[i]); + if(h !== -1){ + delete hidden[h]; + $('.square').eq(i).hide(); + } else { + $('.square').eq(i).show(); + } + + } + + }, doHint: function(e){ @@ -388,7 +428,7 @@ App.PuzzleView = Backbone.View.extend({ $('.answertext').html(a); - if(a.length > 9){ + if(a.length > 10){ App.puzzleView.doReset(e); } else { App.puzzleView.doGuess(a); @@ -545,17 +585,6 @@ function rot13(str) { return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26); }); } - - -function shuffleItems(sel) { - var $el = $(sel); - $el.each(function(){ - var items = $(this).children().clone(true); - var ret = (items.length) ? $(this).html(shuffle(items)) : this; - $el.replaceWith(ret); - }); -} - function shuffle(arr) { for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x); diff --git a/index.html b/index.html index 0b70968..5ff1421 100644 --- a/index.html +++ b/index.html @@ -88,7 +88,7 @@ for(var i = 0; i < clues.length; i++){ var clue = clues[i] %> -
  • +
  • <%=clue['word'].length%> letters: <%=clue['def']%>
  • @@ -102,6 +102,7 @@ Home Restart Hint + Sort Shuffle