Merge pull request #274 from AyuntamientoMadrid/refactor-selects

Refactor selects
This commit is contained in:
Juanjo Bazán
2015-08-29 11:36:38 +02:00
19 changed files with 44 additions and 105 deletions

View File

@@ -21,7 +21,6 @@
//= require d3
//= require c3
//= require c3ext
//= require qp
//= require app
//= require_tree .
@@ -31,9 +30,7 @@ var initialize_modules = function() {
App.Votes.initialize();
App.Tags.initialize();
App.Stats.initialize();
App.LocaleSwitcher.initialize();
App.DebatesOrderSelector.initialize();
App.DebatesTagFilter.initialize();
App.LocationChanger.initialize();
};
$(function(){

View File

@@ -1,17 +0,0 @@
App.DebatesOrderSelector =
href_with_params: (query_params) ->
loc = window.location
loc.protocol + "//" + loc.hostname +
(if loc.port then ':' + loc.port else '') +
loc.pathname +
loc.hash +
'?' + $.param(query_params)
initialize: ->
$('.js-order-selector').on 'change', ->
query_params = window.getQueryParameters()
query_params['order'] = $(this).val()
window.location.assign(App.DebatesOrderSelector.href_with_params(query_params))

View File

@@ -1,17 +0,0 @@
App.DebatesTagFilter =
href_with_params: (query_params) ->
loc = window.location
loc.protocol + "//" + loc.hostname +
(if loc.port then ':' + loc.port else '') +
loc.pathname +
loc.hash +
'?' + $.param(query_params)
initialize: ->
$('.js-tag-filter').on 'change', ->
query_params = window.getQueryParameters()
query_params['tag'] = $(this).val()
window.location.assign(App.DebatesTagFilter.href_with_params(query_params))

View File

@@ -1,19 +0,0 @@
App.LocaleSwitcher =
href_with_params: (query_params) ->
loc = window.location
loc.protocol + "//" + loc.hostname +
(if loc.port then ':' + loc.port else '') +
loc.pathname +
loc.hash +
'?' + $.param(query_params)
initialize: ->
$('.js-locale-switcher').on 'change', ->
query_params = window.getQueryParameters()
query_params['locale'] = $(this).val()
window.location.assign(App.LocaleSwitcher.href_with_params(query_params))

View File

@@ -0,0 +1,8 @@
App.LocationChanger =
initialize: ->
$('.js-location-changer').on 'change', ->
window.location.assign($(this).val())

View File

@@ -1,12 +0,0 @@
/*
* getQueryParameters.js
* Copyright (c) 2014 Nicholas Ortenzio
* The MIT License (MIT)
*
*/
window.getQueryParameters = function(str) {
str = (str || document.location.search).replace(/(^\?)/,'');
if(!str) { return {}; }
return str.split("&").reduce(function(o,n){n=n.split('=');o[n[0]]=n[1];return o},{});
};

View File

@@ -641,7 +641,7 @@ form.locale-form {
select {
background-image: image-url("language_select.png");
&.js-locale-switcher {
&.locale-switcher {
background-color: transparent;
border: 0;
color: white;

View File

@@ -7,6 +7,7 @@ class DebatesController < ApplicationController
def index
@debates = Debate.search(params).page(params[:page]).for_render.send("sort_by_#{@order}")
@tags = ActsAsTaggableOn::Tag.all
set_debate_votes(@debates)
end

View File

@@ -14,13 +14,10 @@ module ApplicationHelper
home_page? ? '' : 'results'
end
def available_locale_options_for_select
options_for_select(available_locales_array, I18n.locale)
# if current path is /debates current_path_with_query_params(foo: 'bar') returns /debates?foo=bar
# notice: if query_params have a param which also exist in current path, it "overrides" (query_params is merged last)
def current_path_with_query_params(query_parameters)
url_for(request.query_parameters.merge(query_parameters))
end
private
def available_locales_array
I18n.available_locales.map { |loc| [I18n.t('locale', locale: loc), loc] }
end
end

View File

@@ -1,12 +0,0 @@
module DebatesHelper
def available_options_for_order_selector(valid_orders, current_order)
options_for_select(available_order_filters_array(valid_orders), current_order)
end
private
def available_order_filters_array(orders)
orders.map { |f| [t("debates.index.orders.#{f}"), f] }
end
end

View File

@@ -8,7 +8,7 @@
<dd class="active"><%= t("admin.comments.index.filters.#{filter}") %></dd>
<% else %>
<dd><%= link_to t("admin.comments.index.filters.#{filter}"),
admin_comments_path(filter: filter) %></dd>
current_path_with_query_params(filter: filter) %></dd>
<% end %>
<% end %>
</dl>

View File

@@ -8,7 +8,7 @@
<dd class="active"><%= t("admin.debates.index.filters.#{filter}") %></dd>
<% else %>
<dd><%= link_to t("admin.debates.index.filters.#{filter}"),
admin_debates_path(filter: filter) %></dd>
current_path_with_query_params(filter: filter) %></dd>
<% end %>
<% end %>
</dl>

View File

@@ -21,7 +21,7 @@
<dd class="active"><%= t("admin.organizations.index.filters.#{filter}") %></dd>
<% else %>
<dd><%= link_to t("admin.organizations.index.filters.#{filter}"),
admin_organizations_path(filter: filter) %></dd>
current_path_with_query_params(filter: filter) %></dd>
<% end %>
<% end %>
</dl>

View File

@@ -8,7 +8,7 @@
<dd class="active"><%= t("admin.users.index.filters.#{filter}") %></dd>
<% else %>
<dd><%= link_to t("admin.users.index.filters.#{filter}"),
admin_users_path(filter: filter) %></dd>
current_path_with_query_params(filter: filter) %></dd>
<% end %>
<% end %>
</dl>

View File

@@ -8,14 +8,18 @@
<h2>
<%= t("debates.index.filter_topic",
number: @debates.size,
topic: @tag_filter).html_safe %>
topic: @tag_filter) %>
</h2>
<% else %>
<h2><%= t("debates.index.select_topic") %></h2>
<form class="inline-block">
<select class="js-tag-filter" name="tag-filter">
<option value="all" selected="selected"><%= t("debates.index.all") %></option>
<%= options_from_collection_for_select(ActsAsTaggableOn::Tag.all, :name, :name) %>
<select class="js-location-changer" name="tag-filter">
<option value="/" selected="selected"><%= t("debates.index.all") %></option>
<% @tags.each do |tag| %>
<option value='<%= current_path_with_query_params(tag: tag.name) %>'>
<%= tag.name %>
</option>
<% end %>
</select>
</form>
<% end %>
@@ -24,12 +28,16 @@
<div class="inline-block right">
<h6 class="inline-block"><%= t("debates.index.select_order") %></h6>
<form class="inline-block">
<select class="js-order-selector" name="order-selector">
<%= available_options_for_order_selector(@valid_orders, @order) %>
<select class="js-location-changer" name="order-selector">
<% @valid_orders.each do |order| %>
<option <%= 'selected' if order == @order %>
value='<%= current_path_with_query_params(order: order) %>'>
<%= t("debates.index.orders.#{order}") %>
</option>
<% end %>
</select>
</form>
</div>
</div>
</div>

View File

@@ -8,7 +8,7 @@
<dd class="active"><%= t("moderation.comments.index.filters.#{filter}") %></dd>
<% else %>
<dd><%= link_to t("moderation.comments.index.filters.#{filter}"),
moderation_comments_path(filter: filter) %></dd>
current_path_with_query_params(filter: filter) %></dd>
<% end %>
<% end %>
</dl>

View File

@@ -8,7 +8,7 @@
<dd class="active"><%= t("moderation.debates.index.filters.#{filter}") %></dd>
<% else %>
<dd><%= link_to t("moderation.debates.index.filters.#{filter}"),
moderation_debates_path(filter: filter) %></dd>
current_path_with_query_params(filter: filter) %></dd>
<% end %>
<% end %>
</dl>

View File

@@ -1,8 +1,13 @@
<div class="locale">
<span class="inline-block"><%= t("layouts.header.locale") %></span>
<form class="locale-form">
<select class="js-locale-switcher" name="locale-switcher">
<%= available_locale_options_for_select %>
<select class="js-location-changer locale-switcher" name="locale-switcher">
<% I18n.available_locales.map do |loc| %>
<option <%= 'selected' if loc == I18n.locale %>
value='<%= current_path_with_query_params(locale: loc) %>'>
<%= I18n.t('locale', locale: loc) %>
</option>
<% end %>
</select>
</form>
</div>

View File

@@ -12,7 +12,7 @@ feature 'Localization' do
scenario 'Available locales appear in the locale switcher' do
visit '/'
within('.js-locale-switcher') do
within('.locale-form .js-location-changer') do
expect(page).to have_content 'Español'
expect(page).to have_content 'English'
end