Skip to content

Commit

Permalink
Merge pull request #3522 from meeting-room-booking-system/localised_s…
Browse files Browse the repository at this point in the history
…orting

Localised sorting
  • Loading branch information
campbell-m authored Sep 27, 2023
2 parents f13cbe2 + fbb7368 commit 2b2324b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
7 changes: 3 additions & 4 deletions web/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ function generate_new_room_form()
echo "<thead>\n";
echo "<tr>\n";

echo "<th>" . get_vocab("name") . "</th>\n";
echo '<th><span data-type="string">' . get_vocab("name") . "</span></th>\n";
if (is_admin())
{
// Don't show ordinary users the disabled status: they are only going to see enabled rooms
Expand Down Expand Up @@ -450,10 +450,9 @@ function generate_new_room_form()

$html_name = htmlspecialchars($r['room_name']);
$href = multisite('edit_room.php?room=' . $r['id']);
// We insert an invisible span containing the sort key so that the rooms will
// We insert a data attribute containing the sort key so that the rooms will
// be sorted properly
echo "<td><div>" .
"<span>" . htmlspecialchars($r['sort_key']) . "</span>" .
echo '<td data-order="' . htmlspecialchars($r['sort_key']) . '"><div>' .
"<a title=\"$html_name\" href=\"" . htmlspecialchars($href) . "\">$html_name</a>" .
"</div></td>\n";
if (is_admin())
Expand Down
11 changes: 6 additions & 5 deletions web/edit_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ function output_row($row)
}

$sortname = get_sortable_name($row['display_name']);
$values[] = '<span title="' . htmlspecialchars($sortname) . '"></span>' . $display_name_value;
// TODO: move the data-order attribute up into the <td> and get rid of the <span>
$values[] = '<span data-order="' . htmlspecialchars($sortname) . '"></span>' . $display_name_value;

// Then the username
$name_value = "<span class=\"normal\">" . htmlspecialchars($row['name']) . "</span>";
$values[] = '<span title="' . htmlspecialchars($row['name']) . '"></span>' . $name_value;
$values[] = '<span class="normal">' . htmlspecialchars($row['name']) . '</span>';

// Other columns
foreach ($fields as $field)
Expand Down Expand Up @@ -1370,8 +1370,8 @@ function get_fieldset_submit_buttons(?int $user_id, $delete=false, $disabled=fal
echo "<tr>";

// First two columns which are the name and display name
echo '<th><span class="normal" data-type="title-string">' . get_vocab("users.display_name") . "</span></th>\n";
echo '<th><span class="normal" data-type="title-string">' . get_vocab("users.name") . "</span></th>\n";
echo '<th><span class="normal" data-type="string">' . get_vocab("users.display_name") . "</span></th>\n";
echo '<th><span class="normal" data-type="string">' . get_vocab("users.name") . "</span></th>\n";

// Other column headers
foreach ($fields as $field)
Expand All @@ -1387,6 +1387,7 @@ function get_fieldset_submit_buttons(?int $user_id, $delete=false, $disabled=fal
case 'level':
case 'timestamp':
case 'last_login':
// TODO: Switch from using title-numeric to just numeric(?)
$heading = '<span class="normal" data-type="title-numeric">' . $heading . '</span>';
break;
default:
Expand Down
21 changes: 17 additions & 4 deletions web/jquery/datatables/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"title-string-pre": function ( a ) {
return a.match(/title="(.*?)"/)[1].toLowerCase();
},

"title-string-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},

"title-string-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );



$.fn.dataTableExt.oSort['title-numeric-asc'] = function(a,b) {
var x = a.match(/title="*(-?[0-9\.]+)/)[1];
var y = b.match(/title="*(-?[0-9\.]+)/)[1];
Expand All @@ -36,9 +35,23 @@ $.fn.dataTableExt.oSort['title-numeric-desc'] = function(a,b) {
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};

// Localised sorting
$.fn.dataTable.ext.order.intl = function ( locales, options ) {
if ( window.Intl ) {
var collator = new window.Intl.Collator( locales, options );
var types = $.fn.dataTable.ext.type;

delete types.order['string-pre'];
types.order['string-asc'] = collator.compare;
types.order['string-desc'] = function ( a, b ) {
return collator.compare( a, b ) * -1;
};
}
};


// Filtering plugins

$.fn.dataTableExt.ofnSearch['title-numeric'] = function ( sData ) {
return sData.replace(/\n/g," ").replace( /<.*?>/g, "" );
};

10 changes: 7 additions & 3 deletions web/js/admin.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
?>

$(document).on('page_ready', function() {


var tableOptions = {};
var fixedColumnsOptions = {leftColumns: 1};

<?php // Get the types and feed those into dataTables ?>
tableOptions.columnDefs = getTypes($('#rooms_table'));

<?php
// Turn the list of rooms into a dataTable
// If we're an admin, then fix the right hand column
Expand All @@ -24,7 +28,7 @@
{
fixedColumnsOptions.rightColumns = 1;
}
makeDataTable('#rooms_table', {}, fixedColumnsOptions);

makeDataTable('#rooms_table', tableOptions, fixedColumnsOptions);
});

4 changes: 4 additions & 0 deletions web/js/datatables.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ function makeDataTable(id, specificOptions, fixedColumnsOptions)
}

defaultOptions.initComplete = initCompleteActions;

<?php
// Merge the specific options with the default options. We do a deep
// merge.
Expand All @@ -348,6 +349,9 @@ function makeDataTable(id, specificOptions, fixedColumnsOptions)
};
}

<?php // Localise the sorting. See https://datatables.net/blog/2017-02-28 ?>
$.fn.dataTable.ext.order.intl($('body').data('langPrefs'));

dataTable = table.DataTable(mergedOptions);

if (fixedColumnsOptions)
Expand Down

0 comments on commit 2b2324b

Please sign in to comment.