From 335adc0e9306027e9a4e2e21890356d628deea73 Mon Sep 17 00:00:00 2001 From: Robin Boutros Date: Wed, 21 May 2014 00:43:46 -0400 Subject: [PATCH] Good progress on pagination, but still a big bug with cocoon, mofo --- Gemfile.lock | 2 +- .../javascripts/application/item.js.coffee | 1 + .../application/number_formater.js.coffee | 7 +- .../application/online_status.js.coffee | 2 +- .../application/search_form_ajax.js.coffee | 89 +- .../stylesheets/application/_facets.css.scss | 2 +- .../stylesheets/application/_header.css.scss | 4 +- app/assets/stylesheets/exiles/index.sass | 2 +- app/controllers/armour_searches_controller.rb | 4 + app/controllers/misc_searches_controller.rb | 4 + app/controllers/searches_controller.rb | 26 +- app/controllers/weapon_searches_controller.rb | 4 + app/models/elastic/base_search.rb | 5 +- app/views/application/_sub_header.html.erb | 2 +- .../layouts/layout/header/_admin.html.erb | 2 +- .../header/_authentication_header.html.erb | 2 +- .../layouts/layout/header/_header.html.erb | 4 +- app/views/searches/_form.html.erb | 9 +- app/views/searches/_search.html.erb | 4 + app/views/searches/_tab_nav.html.erb | 2 - .../searches/handlebars/_show_more.html.erb | 3 + app/views/users/show.html.erb | 3 + config/environments/production.rb | 2 +- .../application_vendor/jquery.history.js | 2122 +++++++++++++++++ 24 files changed, 2272 insertions(+), 35 deletions(-) create mode 100644 app/views/searches/handlebars/_show_more.html.erb create mode 100644 vendor/assets/javascripts/application_vendor/jquery.history.js diff --git a/Gemfile.lock b/Gemfile.lock index d10d020..ed4a954 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -75,7 +75,7 @@ GEM rack-test (>= 0.5.4) xpath (~> 2.0) chronic (0.10.2) - cocoon (1.2.5) + cocoon (1.2.6) coffee-rails (4.0.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) diff --git a/app/assets/javascripts/application/item.js.coffee b/app/assets/javascripts/application/item.js.coffee index 2040a5c..c258839 100644 --- a/app/assets/javascripts/application/item.js.coffee +++ b/app/assets/javascripts/application/item.js.coffee @@ -144,6 +144,7 @@ class Item Item.templates["requirements"] = Handlebars.compile($("#requirements-template").html()) Item.templates["account"] = Handlebars.compile($("#account-template").html()) Item.templates["no-results"] = Handlebars.compile($("#no-results-template").html()) + Item.templates["show-more"] = Handlebars.compile($("#show-more-template").html()) @create: (item, layoutSize = "large") -> new Item(item, layoutSize) diff --git a/app/assets/javascripts/application/number_formater.js.coffee b/app/assets/javascripts/application/number_formater.js.coffee index 8b4d49e..1030005 100644 --- a/app/assets/javascripts/application/number_formater.js.coffee +++ b/app/assets/javascripts/application/number_formater.js.coffee @@ -5,8 +5,11 @@ class NumberFormatter setup: -> @numbers.html @format - format: (i, oldHtml) -> - nb = parseInt(oldHtml) + format: (i, oldHtml) => + @constructor.format(oldHtml) + + @format: (html) -> + nb = parseInt(html) return nb if nb < 1000 (nb / 1000).toFixed(1) + "k" diff --git a/app/assets/javascripts/application/online_status.js.coffee b/app/assets/javascripts/application/online_status.js.coffee index 993882b..d99cf78 100644 --- a/app/assets/javascripts/application/online_status.js.coffee +++ b/app/assets/javascripts/application/online_status.js.coffee @@ -27,7 +27,7 @@ class OnlineStatus players - updateAccountStatuses: (accountNames = nil) -> + updateAccountStatuses: (accountNames = null) -> self = @ accountNames = accountNames || @accountNames() diff --git a/app/assets/javascripts/application/search_form_ajax.js.coffee b/app/assets/javascripts/application/search_form_ajax.js.coffee index 9ebd102..71641d7 100644 --- a/app/assets/javascripts/application/search_form_ajax.js.coffee +++ b/app/assets/javascripts/application/search_form_ajax.js.coffee @@ -17,11 +17,27 @@ class AjaxForm @setupFacets() + @setupPagination() + setupEvents: -> self = @ + @$form.on "itemLoaded", (e) => + @updateFormAction(e.page) + @updateURL(e.page) + @enhanceItems(e) + @updateTotalCount(e.page) + @setPageNb(e.page.current) + + @$form.on "click", "input[type=submit]", (e) => + e.preventDefault() + @$form.trigger({ type: "submit", page: @$form.find("#search-page").val() }) + $("body").on "submit", "#search-form", (e) -> e.preventDefault() + return false if self.working + + self.setPageNb(e.page) self.beforeSubmit() @@ -32,28 +48,40 @@ class AjaxForm dataType: 'json' success: (data) -> - self.$results.html("") - self.$sidebar.html("") + page = data.page + + self.resetPageHtml() layoutSize = self.layoutSize() - self.renderItems(data.results) + self.renderItems(data.results, data.page) self.renderFacets(data.facets) self.$form.trigger( type: "itemLoaded", - results: data.results + results: data.results, + page: data.page ) - self.updatePagination(data.pagination) - error: -> alert("An error occured. If the problem persists, contact niuage[at]gmail.com.") complete: -> self.complete() - @$form.on "itemLoaded", (e) -> - self.enhanceItems() + resetPageHtml: -> + @$results.html("") + @$sidebar.html("") + + updateTotalCount: (page) -> + @$innerHeader ||= $("#sub-header") + @$innerHeader.find("span.tag span").text( + App.NumberFormatter.format(page.results.totalCount) + ) + + updateFormAction: (page) -> + @$form.attr + action: page.formPath + method: if page.persisted then "PUT" else "POST" setupFacets: -> @$sidebar.on "click", ".facet li a", (e) => @@ -62,34 +90,73 @@ class AjaxForm @$sidebar.on "click", ".facet h3 a", (e) => App.FacetHandler.reset(e, @$form) + setupPagination: -> + @$results.on "click", "#show-more", (e) => + e.preventDefault() + $a = $(e.currentTarget) + page = $a.data("page") + @$form.find("#search-page").val(page) + console.log "trigger submit with ", page + @$form.trigger({ type: "submit", page: page }) + $a.remove() + $('html, body').animate( + scrollTop: @$results.offset().top - 93, + 500 + ) + renderFacets: (facets) -> return if !facets || $.isEmptyObject(facets) $.each facets, (facetName, facet) => @$sidebar.append @facetTemplate(App.Facet.create(facetName, facet).toJson()) - renderItems: (results) -> + renderItems: (results, page) -> if results && results.length > 0 $.each results, (i, result) => @$results.append @resultTemplate(App.Item.create(result).toJson()) + @renderPagination(page) else @$results.append App.Item.templates["no-results"]() + renderPagination: (page) -> + @$results.append( + App.Item.templates["show-more"]({ currentPage: page.current + 1 }) + ) + beforeSubmit: -> + @working = true @$results.addClass("loading") @$sidebar.addClass("loading") @$submitButton.attr("disabled", "disabled") complete: -> + @working = false @$results.removeClass("loading") @$sidebar.removeClass("loading") @$form.find("input[type=submit]").removeAttr("disabled") + $removedStats = @$form.find(".nested-fields [id$=_destroy][value=1]") + .closest(".nested-fields") + $removedStats.next("input").remove() + $removedStats.remove() + layoutSize: -> @$results.data("size") - updatePagination: (pagination) -> - @currentPage = pagination.currentPage + setPageNb: (nb) -> + if nb = parseInt(nb) + console.log "set page nb ", nb + @$form.find("#search-page").val(nb) + else + console.log "erase pg nb" + @$form.find("#search-page").val("") + + updateURL: (page) -> + History.pushState( + page: page.current, + page.title, + page.path + ) enhanceItems: -> App.ItemRenderer.setup("#results .result") diff --git a/app/assets/stylesheets/application/_facets.css.scss b/app/assets/stylesheets/application/_facets.css.scss index f1652c1..930bad8 100644 --- a/app/assets/stylesheets/application/_facets.css.scss +++ b/app/assets/stylesheets/application/_facets.css.scss @@ -39,7 +39,7 @@ @extend .right; @extend .small; margin-right: 6px; - line-height: 19px; + line-height: 13px; color: #da4f49; display: none; diff --git a/app/assets/stylesheets/application/_header.css.scss b/app/assets/stylesheets/application/_header.css.scss index 7b15c0e..b206793 100644 --- a/app/assets/stylesheets/application/_header.css.scss +++ b/app/assets/stylesheets/application/_header.css.scss @@ -16,7 +16,7 @@ z-index: 10001; } - #inner-header { + .inner-header { border-bottom: 1px solid $border-bottom-color; background: $header-color; // @include box-shadow(0 1px 3px -2px rgba(0, 0, 0, 0.7)); @@ -190,7 +190,7 @@ } } - #inner-header { + .inner-header { background: $sub-header-color image-url("subbg.png") center center repeat-y; border-bottom: 1px solid $sub-header-bottom-border-color; diff --git a/app/assets/stylesheets/exiles/index.sass b/app/assets/stylesheets/exiles/index.sass index dbe6195..26e7e74 100644 --- a/app/assets/stylesheets/exiles/index.sass +++ b/app/assets/stylesheets/exiles/index.sass @@ -101,7 +101,7 @@ form .input.string input, .btn .main-menu box-shadow: none - #inner-header + .inner-header background: #23416B [data-vote=button] diff --git a/app/controllers/armour_searches_controller.rb b/app/controllers/armour_searches_controller.rb index 5f30876..13e1496 100644 --- a/app/controllers/armour_searches_controller.rb +++ b/app/controllers/armour_searches_controller.rb @@ -3,6 +3,10 @@ def typed_search ArmourSearch end + def new_polymorphic_search_path + new_armour_search_path + end + def search_params params[:armour_search] end diff --git a/app/controllers/misc_searches_controller.rb b/app/controllers/misc_searches_controller.rb index a61d1de..d62aa30 100644 --- a/app/controllers/misc_searches_controller.rb +++ b/app/controllers/misc_searches_controller.rb @@ -3,6 +3,10 @@ def typed_search MiscSearch end + def new_polymorphic_search_path + new_misc_search_path + end + def search_params params[:misc_search] end diff --git a/app/controllers/searches_controller.rb b/app/controllers/searches_controller.rb index 0a62434..fbac14e 100644 --- a/app/controllers/searches_controller.rb +++ b/app/controllers/searches_controller.rb @@ -23,8 +23,8 @@ def new def create @search = typed_search.new(search_params) - @search.save - search if !@search.valid? + @search.save unless first_request? + search unless @search.valid? respond_with @search, location: location do |format| format.html @@ -34,7 +34,7 @@ def create def update @search.update_attributes(search_params) - search if !@search.valid? + search unless @search.valid? respond_with @search, location: location do |format| format.html @@ -44,15 +44,29 @@ def update private + def first_request? + request.xhr? && params[:first] + end + def ajax_search search + page = params[:page].to_i + search_path = polymorphic_path(@search, page: page > 1 ? page : nil) + render json: { results: @tire_search.results, facets: @results.facets, - pagination: { - total_pages: @results.total_pages, - current_page: @results.current_page + page: { + path: @search.persisted? ? search_path : new_polymorphic_search_path, + formPath: polymorphic_path(@search), + current: @results.current_page, + title: @search.to_s, + persisted: @search.persisted?, + results: { + totalPages: @results.total_pages, + totalCount: @results.total_count, + } } } end diff --git a/app/controllers/weapon_searches_controller.rb b/app/controllers/weapon_searches_controller.rb index e85057f..18f9642 100644 --- a/app/controllers/weapon_searches_controller.rb +++ b/app/controllers/weapon_searches_controller.rb @@ -3,6 +3,10 @@ def typed_search WeaponSearch end + def new_polymorphic_search_path + new_weapon_search_path + end + def search_params params[:weapon_search] end diff --git a/app/models/elastic/base_search.rb b/app/models/elastic/base_search.rb index 1fa4bb2..fd0d0b1 100644 --- a/app/models/elastic/base_search.rb +++ b/app/models/elastic/base_search.rb @@ -7,7 +7,10 @@ class BaseSearch def initialize(search, options) @search = search - @page = (options[:page] || 1).to_i + + @page = (options[:page].presence || 1).to_i + @page = 1 if @page < 1 + self.per_page = options[:per_page] @context = nil diff --git a/app/views/application/_sub_header.html.erb b/app/views/application/_sub_header.html.erb index 0e6d3a7..27dac55 100644 --- a/app/views/application/_sub_header.html.erb +++ b/app/views/application/_sub_header.html.erb @@ -1,5 +1,5 @@
-
+
diff --git a/app/views/layouts/layout/header/_admin.html.erb b/app/views/layouts/layout/header/_admin.html.erb index b70646a..99e0306 100644 --- a/app/views/layouts/layout/header/_admin.html.erb +++ b/app/views/layouts/layout/header/_admin.html.erb @@ -1,5 +1,5 @@