Subject: | add periodic autosave feature |
The attached patch provides automatic saving of response/comment draft. The autosave period can be configured globally (in RT_SiteConfig.d/) or by user (default to 300s).
Subject: | add-periodic-autosave-feature.patch |
From d6f343816f2fe876dedb2975515cbedabc8e2a0d Mon Sep 17 00:00:00 2001
From: gibus <gibus@easter-eggs.com>
Date: Tue, 23 Jan 2018 16:47:22 +0100
Subject: [PATCH] #57165 add periodic autosave feature to RT-Extension-Drafts
---
.../Drafts/Ticket/Update.html/AfterMessageBox | 15 ++++++++++++
html/Helpers/SaveDraft | 27 ++++++++++++++++++++++
lib/RT/Extension/Drafts.pm | 11 +++++++++
3 files changed, 53 insertions(+)
create mode 100644 html/Helpers/SaveDraft
diff --git a/html/Callbacks/Drafts/Ticket/Update.html/AfterMessageBox b/html/Callbacks/Drafts/Ticket/Update.html/AfterMessageBox
index 2a92f3c..fb9e389 100644
--- a/html/Callbacks/Drafts/Ticket/Update.html/AfterMessageBox
+++ b/html/Callbacks/Drafts/Ticket/Update.html/AfterMessageBox
@@ -2,6 +2,21 @@
<input type="submit" class="button" name="LoadDraft" value="<&|/l&>Load draft</&>" />
% }
<input type="submit" class="button" name="SaveDraft" value="<% ($Draft && $Draft->id) ? loc('Update draft') : loc('Save draft')%>" />
+
+<script type="text/javascript">
+jQuery(function() {
+ timeout_save_draft = window.setTimeout(save_draft, <% RT->Config->Get('AutoSaveDraftPeriod', $session{'CurrentUser'}) || 300 %> * 1000);
+});
+
+function save_draft() {
+ jQuery.post("/Helpers/SaveDraft", {UserId: "<% $session{'CurrentUser'}->id %>", TicketId: "<% $Ticket->id %>", Content: jQuery('#UpdateContent').val(), ContentType: jQuery('#UpdateContent').hasClass('richtext') ? 'text/html' : 'text/plain'},
+ function(res) {
+ console.log(res);
+ }
+ );
+ timeout_save_draft = window.setTimeout(save_draft, <% RT->Config->Get('AutoSaveDraftPeriod', $session{'CurrentUser'}) || 300 %> * 1000);
+}
+</script>
<%init>
my $Ticket = LoadTicket($ARGS{'id'});
diff --git a/html/Helpers/SaveDraft b/html/Helpers/SaveDraft
new file mode 100644
index 0000000..c7e0ac5
--- /dev/null
+++ b/html/Helpers/SaveDraft
@@ -0,0 +1,27 @@
+<%ARGS>
+$UserId => undef;
+$TicketId => undef;
+$Content => undef;
+$ContentType => undef;
+</%ARGS>
+
+<%INIT>
+my $Ticket = LoadTicket($TicketId);
+my $Draft = RT::Attribute->new($session{'CurrentUser'});
+$Draft->LoadByNameAndObject(Object => $session{'CurrentUser'}->UserObj, Name => 'Draft-' . $Ticket->id);
+my ($ok, $msg) = (0, '');
+if ($Content) {
+ if ($Draft && $Draft->Id) {
+ ($ok, $msg) = $Draft->SetContent($Content);
+ $Draft->SetContentType($ContentType);
+ } else {
+ ($ok, $msg) = $Draft->Create(Name => 'Draft-' . $Ticket->id,
+ Object => $session{'CurrentUser'}->UserObj,
+ Content => $Content,
+ ContentType => $ContentType,
+ );
+ }
+}
+$m->out($msg);
+$m->abort;
+</%INIT>
diff --git a/lib/RT/Extension/Drafts.pm b/lib/RT/Extension/Drafts.pm
index 90f0581..818a3ca 100644
--- a/lib/RT/Extension/Drafts.pm
+++ b/lib/RT/Extension/Drafts.pm
@@ -78,4 +78,15 @@ Request Tracker (RT) is Copyright Best Practical Solutions, LLC.
=cut
+# User overridable options
+$RT::Config::META{AutoSaveDraftPeriod} = {
+ Section => 'Ticket composition',
+ Overridable => 1,
+ SortOrder => 20,
+ Widget => '/Widgets/Form/Integer',
+ WidgetArguments => {
+ Description => 'Period (in seconds) to automatically save a response/comment draft'
+ },
+};
+
1; # End of RT::Extension::Drafts
--
2.11.0