This repository has been archived on 2025-06-22. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
perl-dam/Dam/Components/Actions/Sort.pm

216 lines
5.5 KiB
Perl

=head1 NAME
Dam::Components::Actions::Sort
=head1 SYNOPSIS
my $orderby = Component__Get(ACTION_SORT);
=head1 DESCRIPTION
Action to order a list by columns.
=head1 ARGUMENTS
(
TYPE => 'Sort',
COLUMNS => { 'col1' => 'Column 1', 'col2' => 'Column 2' } (default),
DEFAULT => 'col1' (default valus is undef)
)
=cut
use strict;
use warnings;
use utf8;
package Dam::Components::Actions::Sort;
use Exporter qw(import);
our @EXPORT = qw(
Action__html Action__js
);
use Dam::Util;
use Dam::DamLogic;
my $ID_DEFAULT = 'sort';
sub __arguments {
my $arg_ref = shift;
check_arguments($arg_ref,
TYPE => [ ARG_REQUIRED ],
ID => [ ARG_DEFAULT, $ID_DEFAULT ],
COLUMNS => [ ARG_DEFAULT, { 'col1' => 'Column 1', 'col2' => 'Column 2' } ],
DEFAULT => [ ARG_OPTIONAL ]
);
}
sub Action__html {
my ($self, $arg_ref, $info_ref) = @_;
__arguments($arg_ref);
my $default = undef;
my %list_columns = %{$$arg_ref{COLUMNS}};
if (!is_empty($$arg_ref{DEFAULT})) {
my @default = split(' ', $$arg_ref{DEFAULT});
if (defined($list_columns{$default[0]}) && !defined($default[2])) {
my $dir = defined($default[1]) && in_array(uc($default[1]), 'ASC', 'DESC') ? uc($default[1]) : 'ASC';
$default = strval($default[0], ' ', $dir);
}
}
# Order value:
my $orderby_value = cgiapp_param('orderby');
$orderby_value = $default if is_empty($orderby_value) && defined($default);
if (defined($orderby_value)) {
my @orderby = split(' ', $orderby_value);
if (defined($orderby[0]) && defined($list_columns{$orderby[0]})) {
my $orderby = $list_columns{$orderby[0]};
if (defined($orderby[1])) {
$orderby = strval($orderby, is_eq($orderby[1], 'ASC') ? strval(' (', _t('ascendant'), ')') : is_eq($orderby[1], 'DESC') ? strval(' (', _t('descendent'), ')') : '');
}
push(@{$info_ref}, { DATA => _t('Order by'), VALUE => $orderby });
}
else {
$orderby_value = '';
}
}
else {
$orderby_value = '';
}
return strval('
<input type="hidden" name="orderby" id="orderby" value="', $orderby_value, '" />
');
}
sub Action__js {
my ($self, $arg_ref) = @_;
__arguments($arg_ref);
my $default = undef;
my %list_columns = %{$$arg_ref{COLUMNS}};
if (!is_empty($$arg_ref{DEFAULT})) {
my @default = split(' ', $$arg_ref{DEFAULT});
if (defined($list_columns{$default[0]}) && !defined($default[2])) {
my $dir = defined($default[1]) && in_array(uc($default[1]), 'ASC', 'DESC') ? uc($default[1]) : 'ASC';
$default = strval($default[0], ' ', $dir);
}
}
my $columns_id = '';
my $columns_name = '';
foreach my $column (keys(%list_columns)) {
$columns_id .= strval('#', $column, ',');
$columns_name .= strval($list_columns{$column}, ',');
}
chop($columns_id);
chop($columns_name);
return strval('
$(function(){
var current_orderby = $("#orderby").val().split(" ");
var columns_id = ["', strval_join('","', split(',', $columns_id)), '"];
var columns_name = ["', strval_join('","', split(',', $columns_name)), '"];
columns_id.forEach(function(value,index,array){
var glypho = "sort";
if (current_orderby[0] == value.substring(1)) {
if (current_orderby[1] == "ASC") {
glypho = "triangle-bottom";
}
else if (current_orderby[1] == "DESC") {
glypho = "triangle-top";
}
}
$(value).css("white-space","nowrap");
$(value).prepend("<span style=\"font-size: medium; color: #999;\" class=\"minitip glyphicon glyphicon-" + glypho + " hidden-print\" data-toggle=\"tooltip\" title=\"', _t('Sort by'), ' " + columns_name[index] + "\"></span>");
$(value).hover(function(){
$(this).css("cursor","pointer");
});
$(value).click(function(){
var current_column = $(this).attr("id");
if (current_column == current_orderby[0]) {
if (current_orderby[1] == "ASC") {
current_column += " DESC";
}
else if (current_orderby[1] != "DESC") {
current_column += " ASC";
}
else {
current_column = "', $default, '";
}
}
else {
current_column += " ASC";
}
$("#orderby").val(current_column);
$("#submit").click();
});
});
$(".minitip").tooltip();
});
');
}
sub Get {
my $orderby_value = cgiapp_param('orderby');
return undef if is_empty($orderby_value);
return $orderby_value;
}
sub Set {
my ($self, $value, $id) = @_;
cgiapp_param('orderby', $value);
}
1;
=head1 AUTHOR
Manuel Cillero C<< <manuel@cillero.es> >>
=head1 COPYRIGHT
The MIT License (MIT)
Copyright (c) 2004-2020 Manuel Cillero. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
=cut