From 03917ee526909024bb55eb076e072e0481982a75 Mon Sep 17 00:00:00 2001 From: hugolmendez Date: Fri, 28 Aug 2015 15:40:32 +0000 Subject: [PATCH 1/7] update db --- app/views/shared/_chessboard.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/shared/_chessboard.html.erb b/app/views/shared/_chessboard.html.erb index 4664c15..4bf6574 100644 --- a/app/views/shared/_chessboard.html.erb +++ b/app/views/shared/_chessboard.html.erb @@ -10,7 +10,7 @@ <% 1.upto(8) do |col| %> <%= link_to piece_path(id: @select_pc.id, x_position: col, y_position: row), method: :put do %> -
+
<% @pieces.detect do |piece| %> <% if piece.x_position == col && piece.y_position == row %> <%= link_to piece_color(piece), piece_path(piece.id), :class => hilite(piece) %> From 777ad1feee3535df28a6fe910bf0c4c13d05a1ac Mon Sep 17 00:00:00 2001 From: hugolmendez Date: Mon, 31 Aug 2015 23:08:49 +0000 Subject: [PATCH 2/7] correct syntax on _chessboard partial --- app/views/shared/_chessboard.html.erb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/views/shared/_chessboard.html.erb b/app/views/shared/_chessboard.html.erb index d8cff7c..053b4b2 100644 --- a/app/views/shared/_chessboard.html.erb +++ b/app/views/shared/_chessboard.html.erb @@ -9,8 +9,6 @@ <% 1.upto(8) do |col| %> - <%= link_to piece_path(id: @select_pc.id, x_position: col, y_position: row), method: :put do %> -
<%= link_to '', piece_path(id: @select_pc.id, x_position: col, y_position: row), :class => square_link(@select_pc), method: :put %> <% @pieces.detect do |piece| %> <% if piece.x_position == col && piece.y_position == row && piece.active == 1 %> From 10ba9a3b8ea1820e9be0b5193b2f84f2eae64e8e Mon Sep 17 00:00:00 2001 From: hugolmendez Date: Mon, 31 Aug 2015 23:23:36 +0000 Subject: [PATCH 3/7] add js from method and css --- app/assets/stylesheets/pieces.css.scss | 16 +++++++++++++--- app/views/shared/_chessboard.html.erb | 15 ++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/assets/stylesheets/pieces.css.scss b/app/assets/stylesheets/pieces.css.scss index 4ece9bb..29898c6 100644 --- a/app/assets/stylesheets/pieces.css.scss +++ b/app/assets/stylesheets/pieces.css.scss @@ -1,3 +1,13 @@ -// Place all the styles related to the pieces controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: http://sass-lang.com/ +div.piece.color_0 { +/* dark */ + color: red; +} + +div.piece.color_1 { +/* light */ + color: blue; +} + +div.piece.ui-draggable-dragging { + background-color: yellow; +} \ No newline at end of file diff --git a/app/views/shared/_chessboard.html.erb b/app/views/shared/_chessboard.html.erb index 053b4b2..33b0e10 100644 --- a/app/views/shared/_chessboard.html.erb +++ b/app/views/shared/_chessboard.html.erb @@ -8,13 +8,14 @@ <% 8.downto(1) do |row| %> <% 1.upto(8) do |col| %> - - <%= link_to '', piece_path(id: @select_pc.id, x_position: col, y_position: row), :class => square_link(@select_pc), method: :put %> - <% @pieces.detect do |piece| %> - <% if piece.x_position == col && piece.y_position == row && piece.active == 1 %> - <%= link_to piece_color(piece), piece_path(piece.id), :class => hilite(piece) %> - <% end %> - <% end %> + + <% @pieces.detect do |piece| %> + <% if piece.x_position == col && piece.y_position == row %> +
+ <%= piece_color(piece) %> +
+ <% end %> + <% end %> <% end %> From 9133fcd899235977a047186f9d4b67d0e3b8928d Mon Sep 17 00:00:00 2001 From: hugolmendez Date: Mon, 31 Aug 2015 23:28:16 +0000 Subject: [PATCH 4/7] add js --- app/assets/javascripts/move_piece.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 app/assets/javascripts/move_piece.js diff --git a/app/assets/javascripts/move_piece.js b/app/assets/javascripts/move_piece.js new file mode 100644 index 0000000..46daa0e --- /dev/null +++ b/app/assets/javascripts/move_piece.js @@ -0,0 +1,27 @@ +
+
+
+

Have Fun

+ + + <% 8.downto(1) do |row| %> + + <% 1.upto(8) do |col| %> + + <% end %> + + <% end %> + +
+ <% @pieces.detect do |piece| %> + <% if piece.x_position == col && piece.y_position == row %> +
+ <%= piece_color(piece) %> +
+ <% end %> + <% end %> +
+
+ <%= link_to 'Sign In', new_user_session_path %> +
+
\ No newline at end of file From 0f3d787ce639f43db6baebbf53105518a8326b39 Mon Sep 17 00:00:00 2001 From: hugolmendez Date: Mon, 31 Aug 2015 23:32:54 +0000 Subject: [PATCH 5/7] js does not work --- app/assets/javascripts/move_piece.js | 66 ++++++++++++++++------------ 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/app/assets/javascripts/move_piece.js b/app/assets/javascripts/move_piece.js index 46daa0e..58c920b 100644 --- a/app/assets/javascripts/move_piece.js +++ b/app/assets/javascripts/move_piece.js @@ -1,27 +1,39 @@ -
-
-
-

Have Fun

- - - <% 8.downto(1) do |row| %> - - <% 1.upto(8) do |col| %> - - <% end %> - - <% end %> - -
- <% @pieces.detect do |piece| %> - <% if piece.x_position == col && piece.y_position == row %> -
- <%= piece_color(piece) %> -
- <% end %> - <% end %> -
-
- <%= link_to 'Sign In', new_user_session_path %> -
-
\ No newline at end of file +$(document).ready(function() { + if ($(".chess-board").length == 0) { + return; + } + + $( ".draggable" ).draggable(); + + + $( "td" ).droppable({ + drop: function( event, ui ) { + //these are where the piece is moving to + var startRow = $(this).data("square-row"); + var startCol = $(this).data("square-col"); + + //these are the draggable pieces + var pieceId = $(ui.draggable).data("piece-id"); + var newRow = $(ui.draggable).data("piece-start-row"); + var newCol = $(ui.draggable).data("piece-start-col"); + var piece = $(ui.draggable); + var newSquare = $(this); + + console.debug("The piece to move has an id of: "+pieceId+"."); + console.debug("The square to move to is on row: "+startRow+" and col: "+startCol+"."); + + $.ajax({ + method: 'put', + url: "/pieces/" + pieceId, + data: { y_position: startRow, x_position: startCol } + }).done(function() { + console.debug("it worked! valid move") + newSquare.append(piece.css('position','static')); + }).fail(function() { + // console.debug("the world really really hates me") + // move piece back - invalid move + }); + + } //drop + }); //droppable +}); \ No newline at end of file From 25b11fe8949d8c7e5c3fd8503b82d118e3c69035 Mon Sep 17 00:00:00 2001 From: hugolmendez Date: Mon, 31 Aug 2015 23:38:52 +0000 Subject: [PATCH 6/7] add js query gem --- Gemfile | 2 ++ Gemfile.lock | 3 +++ app/assets/javascripts/application.js | 2 ++ app/assets/stylesheets/application.scss | 3 +++ 4 files changed, 10 insertions(+) diff --git a/Gemfile b/Gemfile index 12296c9..cc3499d 100644 --- a/Gemfile +++ b/Gemfile @@ -42,6 +42,8 @@ gem 'pry' # FactoryGirl for testing gem 'factory_girl_rails', '~> 4.0' +gem 'jquery-ui-rails' + group :doc do # bundle exec rake doc:rails generates the API under doc/api. diff --git a/Gemfile.lock b/Gemfile.lock index 3b91a00..f69eeee 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -67,6 +67,8 @@ GEM jquery-rails (3.1.3) railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) + jquery-ui-rails (5.0.5) + railties (>= 3.2.16) json (1.8.3) mail (2.5.4) mime-types (~> 1.16) @@ -161,6 +163,7 @@ DEPENDENCIES factory_girl_rails (~> 4.0) jbuilder (~> 1.2) jquery-rails + jquery-ui-rails pg pry rails (= 4.0.1) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index b3de8c8..15f8281 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -15,3 +15,5 @@ //= require jquery_ujs //= require turbolinks //= require_tree . + +//= require jquery-ui \ No newline at end of file diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 3c9f313..3995306 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -10,10 +10,13 @@ * *= require_self *= require_tree . + *= require jquery-ui */ @import "bootstrap-sprockets"; @import "bootstrap"; + + .hilite { background: rgba(255, 230, 0, 0.5); padding: 3px 2px; From 95ef45f868649bd325bc896e60a67803b94c5ae0 Mon Sep 17 00:00:00 2001 From: hugolmendez Date: Wed, 2 Sep 2015 23:59:06 +0000 Subject: [PATCH 7/7] update --- app/assets/stylesheets/application.scss | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 3995306..e916cb0 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -10,7 +10,7 @@ * *= require_self *= require_tree . - *= require jquery-ui + *= require jquery-ui */ @import "bootstrap-sprockets"; @import "bootstrap";