diff --git a/app/assets/javascripts/drag.js b/app/assets/javascripts/drag.js index 0f60463..e97ec64 100644 --- a/app/assets/javascripts/drag.js +++ b/app/assets/javascripts/drag.js @@ -6,9 +6,24 @@ $(document).on('turbolinks:load', function() { }).on('drop', function(from, to, el, mode) { // Al soltar, reordenamos toda la tabla $('.post_order').val(function(i,v) { return i; }); - $('.reorder-posts-panel') - .addClass('d-block') - .removeClass('d-none'); + // Mostramos el nuevo orden también + $('.order.is').text(function(i,v) { return i; }); + // Solo mostramos el valor anterior si no coincide con el valor + // actual. Como lo mostramos entre comillas, usamos el atributo + // data-order del + $('.order.was').each(function() { + var el = $(this); + var is = el.parent().find('.order.is'); + + if (is.text() == el.data('order')) { + el.addClass('d-none'); + } else { + el.removeClass('d-none'); + } + }); + + // Muestra el panel + $('.reorder-posts-panel').addClass('d-block').removeClass('d-none'); }); // Ocultar el panel de ayuda con botón diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 86059af..72b3967 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -6,6 +6,12 @@ class PostsController < ApplicationController @site = find_site @category = session[:category] = params.dig(:category) @posts = @site.posts_for(@lang) + + if params[:sort_by].present? + @posts.sort_by! do |p| + p.send(params[:sort_by].to_s) + end + end end def show diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb index 238e48a..7ff72ac 100644 --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -43,4 +43,5 @@ class SitesController < ApplicationController redirect_to site_posts_path @site end + end diff --git a/app/views/posts/index.haml b/app/views/posts/index.haml index c9e6ec2..8df63d2 100644 --- a/app/views/posts/index.haml +++ b/app/views/posts/index.haml @@ -20,6 +20,16 @@ .col = render 'layouts/flash' - if @posts.present? + .row + .col + %small.pull-right + = t('posts.sort.by') + - %w[order date].each do |s| + = link_to t("posts.sort.#{s}"), + site_posts_path(@site, + category: @category, + lang: @lang, + sort_by: s) = form_tag site_reorder_posts_path, method: :post do = hidden_field 'posts', 'lang', value: @lang .reorder-posts-panel.alert.alert-info.alert-dismissible.fade.show{role: 'alert'} @@ -38,10 +48,15 @@ - next unless post.categories.include?(@category) -# establecer la direccion del texto - direction = post.get_front_matter(:dir) + - order = post.order || i %tr %td = fa_icon 'arrows-v', class: 'handle' - = hidden_field 'posts[order]', i, value: post.order || i, class: 'post_order' + = hidden_field 'posts[order]', i, value: order, class: 'post_order' + %small + %br + %span.order.is= order + %span.order.was.d-none{data: { order: order }}= "(#{order})" %td{class: direction} = link_to post.title, site_post_path(@site, post, lang: @lang) diff --git a/config/locales/en.yml b/config/locales/en.yml index 82398bc..b719635 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -137,6 +137,11 @@ en: ar: 'Arabic' posts: reorder_posts: 'Reorder posts' + sort: + by: 'Sort by' + order: 'order' + date: 'date' + order: 'Order' new: 'New post' index: 'Posts' edit: 'Edit' diff --git a/config/locales/es.yml b/config/locales/es.yml index 9d47c5a..d5743cc 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -139,6 +139,11 @@ es: ar: 'árabe' posts: reorder_posts: 'Reordenar artículos' + sort: + by: 'Ordenar por' + order: 'posición' + date: 'fecha' + order: 'Posición' new: 'Empezar un artículo nuevo' index: 'Artículos' edit: 'Editar'