Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions lib/GADS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1893,22 +1893,18 @@ get '/chronology/:id?' => require_login sub {
user => $user,
schema => schema,
);
$record->find_chronology_id($id);
$record->find_current_id($id);

my $layout = $record->layout;
my $base_url = request->base;
my $table_short = $layout->identifier;
my $record_id = $record->current_id;


template 'chronology' => {
record => $record,
record_id => $id,
page => 'chronology',
content_block_custom_classes => 'content-block--record',
header_type => "table_tabs",
header_back_url => "${base_url}${table_short}/record/${record_id}",
layout => $layout,
layout_obj => $layout,
header_back_url => "${base_url}${table_short}/record/${id}",
};
};

Expand Down
34 changes: 32 additions & 2 deletions lib/GADS/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ post '/api/settings/logo' => require_login sub {
my $filecheck = GADS::Filecheck->instance;
error __x"Files of mimetype {mimetype} are not allowed", mimetype => $filecheck->get_filetype($file)
unless $filecheck->is_image($file);

$site->update({ site_logo => $file->content });

content_type 'application/json';
Expand Down Expand Up @@ -668,7 +668,7 @@ sub _put_dashboard_widget_edit {
my $widget = _get_widget_write(route_parameters->get('id'), route_parameters->get('dashboard_id'), $layout, $user);

my $body = from_json(request->body);

$widget->title($body->{'title'});
$widget->static(query_parameters->get('static') ? 1 : 0)
if $widget->dashboard->is_shared;
Expand Down Expand Up @@ -1516,6 +1516,36 @@ post '/api/script_error' => require_login sub {
_success("Script error logged successfully");
};

get '/api/chronology/:id' => require_login sub {
my $user = logged_in_user;
my $current_id = route_parameters->get('id');
my $page = query_parameters->get('page');

my $record = GADS::Record->new(
user => $user,
schema => schema,
);

my %params = (
chronology_page => $page,
chronology_rows => 10,
);

$record->find_chronology_id($current_id, %params);

my $layout = $record->layout;
my $base_url = request->base;

return template 'chronology/chronology.tt' => {
record => $record,
layout_obj => $layout,
page => $page,
last_page => $record->last_page,
}, {
layout => undef # do not render page header, footer etc
}
};

sub _success
{ my $msg = shift;
send_as JSON => {
Expand Down
42 changes: 42 additions & 0 deletions lib/GADS/Helper/Pager.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=pod
GADS - Globally Accessible Data Store
Copyright (C) 2014 Ctrl O Ltd

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=cut
package GADS::Helper::Pager;

use strict;
use warnings;

use Exporter qw/import/;

our @EXPORT_OK = qw(get_page);
our %EXPORT_TAGS = ( all => \@EXPORT_OK );

=head1 FUNCTIONS
=head2 get_page
Return items for a page - we start from the end and return the last $count+1 items
This is for Chronology paging - but I felt it may be useful elsewhere too.
=cut
sub get_page {
my ($page, $size, @items) = @_;

my $start = -(($page * $size) + 1);
my $end = -((($page - 1) * $size) + 1);
my @result = @items[ $start..$end ];
return grep { defined } @result;
}

1;
37 changes: 32 additions & 5 deletions lib/GADS/Record.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use GADS::Datum::Rag;
use GADS::Datum::Serial;
use GADS::Datum::String;
use GADS::Datum::Tree;
use GADS::Helper::Pager qw/get_page/;
use GADS::Hooks;
use GADS::Layout;
use Log::Report 'linkspace';
Expand Down Expand Up @@ -753,7 +754,16 @@ sub find_chronology_id
or error __x"Record ID {id} not found", id => $current_id;
my $instance_id = $current->instance_id;
$self->_set_instance_id($current->instance_id);
$self->_find(current_id => $current_id, chronology => 1, current => $current);
my $page = $options{chronology_page} // 1;
my $rows = $options{chronology_rows} // 10;
my %params = (
current_id => $current_id,
chronology => 1,
current => $current,
chronology_page => $page,
chronology_rows => $rows
);
$self->_find(%params);
}

sub find_draftuser_id
Expand Down Expand Up @@ -861,6 +871,10 @@ sub _find
error __"You do not have access to this deleted record"
if $find{deleted} && !$self->layout->user_can("purge") && !$GADS::Schema::IGNORE_PERMISSIONS;

my $last_record_id = $find{last_record_id};
my $chronology_page = $find{chronology_page};
my $chronology_rows = $find{chronology_rows};

my %params = (
user => $self->user,
layout => $self->layout,
Expand Down Expand Up @@ -1041,16 +1055,19 @@ sub _find
# limits. It could be that they can't see the actual latest version though,
# and that the latest version here is an old version. Check now whether
# they are allowed to see this record at all.
error __"Requested record not found"
if $find{chronology} && $find{current}->current_version_id != $record_ids[-1];
error __"Requested record not found"
if $find{chronology} && $find{current}->current_version_id != $record_ids[-1];

$self->clear_is_draft;

my @chronology_record_ids = get_page($chronology_page, $chronology_rows, @record_ids)
if $find{chronology};

# Fetch and add multi-values
my @record_objects;
if ($find{chronology})
{
foreach my $record_id (@record_ids)
foreach my $record_id (@chronology_record_ids)
{
my $record_hash = $record->{$record_id};
push @record_objects, GADS::Record->new(
Expand Down Expand Up @@ -1176,7 +1193,13 @@ sub _find
} if @changed; # There may have been changes, but not ones the user has access to
$last_record = $record;
}
@chronology = grep { defined } @chronology;
my $count = scalar @chronology;
my $start = $count > $chronology_rows ? 1 : 0;
@chronology = grep { defined } @chronology[$start .. $chronology_rows];
@chronology = reverse @chronology;
$self->_set_chronology(\@chronology);
$self->_set_last_page($start==0);
}

$self; # Allow chaining
Expand All @@ -1186,6 +1209,10 @@ has chronology => (
is => 'rwp',
);

has last_page => (
is => 'rwp',
);

sub clone
{ my $self = shift;
my $cloned = GADS::Record->new(
Expand Down Expand Up @@ -2440,7 +2467,7 @@ sub _field_write
# child records. This is used in chronology view to
# ensure the correct versions at the time of record
# edit are used
version_datetime => $created,
version_datetime => $created,
);
push @{$self->_records_to_write_after}, $record
if $record->is_edited;
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/components/button/_button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,7 @@ button.close {
color: lighten($brand-primary, 0.1);
}
}

.btn-js-load-chronology {
margin-top: $spacer;
}
6 changes: 6 additions & 0 deletions src/frontend/components/button/lib/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ class ButtonComponent extends Component {
createCancelButton(el);
});
});
map.set("btn-js-load-chronology", (el)=> {
import(/* webpackChunkName: "load-more-chronology-button" */ "./load-more-chronology-button")
.then(({default: createLoadMoreChronologyButton}) => {
createLoadMoreChronologyButton(el);
});
});
ButtonComponent.staticButtonsMap = map;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */
import { describe, it, expect } from '@jest/globals';
import {LoadMoreChronologyButton, default as createLoadMoreChronologyButton} from './load-more-chronology-button';

describe('LoadMoreChronologyButton', () => {
it('should create a button', () => {
// Mock jQuery elements
const $button = $('<button data-record-id="123">Load More</button>');
const $target = $('<div class="chronology"></div>');
const $spinner = $('<div class="chronology_spinner" style="display:none;"></div>');
$('body').append($button, $target, $spinner);

const btn = createLoadMoreChronologyButton($button);

expect(btn).toBeInstanceOf(LoadMoreChronologyButton);
});
});
93 changes: 93 additions & 0 deletions src/frontend/components/button/lib/load-more-chronology-button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { initializeRegisteredComponents } from "component";
import ErrorHandler from "util/errorHandler";

/**
* Button to load more chronology items
*/
export class LoadMoreChronologyButton {
private page: number = 1;
private $target: JQuery<HTMLElement>;
private $spinner: JQuery<HTMLElement>;
private errorHandler: ErrorHandler;

/**
* Create a new LoadMoreChronologyButton instance
* @param $el The element to use as the button
*/
constructor(private $el: JQuery<HTMLElement>) {
// Where to put the chronology entries
this.$target = $(".chronology");
// Loading spinner
this.$spinner = $(".chronology_spinner");
// Error handler - attached to the chronology container
this.errorHandler = new ErrorHandler(this.$target[0]);
// Initialize the button
this.init();
}

/**
* Initialize the button and set up event handlers
*/
private init() {
// Set up the click event handler for the button
this.$el.on("click", async () => {
// When the button is clicked, fetch the next page of chronology data
await this.fetchChronology();
});
// Fetch the first page of chronology data on initialization
this.fetchChronology();
}

/**
* Fetch the next page of chronology data and append it to the chronology list.
* @returns {Promise<void>} A promise that resolves when the chronology data has been fetched and appended.
*/
private async fetchChronology(): Promise<void> {
// Disable the button
this.$el.prop("disabled", true);
// Get the record ID from the button's data attribute
const recordId = this.$el.data("record-id");
// Get the current page number
const page = this.page;
// Download the chronology data for the next page
const url = `/api/chronology/${recordId}?page=${page}`;
try {
// Show the spinner while loading
this.$spinner.show();
// Fetch the data
const response = await fetch(url);
// Check if the response is successful
if (!response.ok) {
// display an error message using the error handler
if(response.status !== 400) {
this.errorHandler.addError(`Failed to load chronology data. Server responded with status: ${response.status}`);
}
return;
}
// If successful, get the response data as text
const html = await response.text();
// Append the new data to the chronology list
this.$target.append(html);
// Initialize any new components in the newly loaded data (e.g., tooltips, popovers)
initializeRegisteredComponents(this.$target[0]);
// Increment the page number for the next fetch
this.page += 1;
} catch (error) {
// If there is an error, use the error handler to display an error message
this.errorHandler.addError(`An error occurred while loading chronology data. Please try again. Error: ${error}`);
} finally {
// Hide the spinner
this.$spinner.hide();
// Re-enable the button
if(!$("[data-last-page]").length) {
this.$el.prop("disabled", false);
}
}
}
}

const createLoadMoreChronologyButton = (el: JQuery<HTMLElement>) => {
return new LoadMoreChronologyButton(el);
};

export default createLoadMoreChronologyButton;
7 changes: 7 additions & 0 deletions src/frontend/components/list/_list.scss
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@

.list__value {
flex: 1;
display: flex;
flex-wrap: wrap;
justify-content: flex-start;

& > * {
margin-left: $spacer * 0.25;
}
}
}
}
Expand Down
Loading
Loading