More attachment handling code de-duplication

This commit is contained in:
Aleksander Machniak 2018-03-19 18:08:56 +00:00
parent 5b242b8e26
commit fa9d09939c
8 changed files with 172 additions and 244 deletions

View file

@ -1957,7 +1957,8 @@ class calendar extends rcube_plugin
*/ */
public function attachment_upload() public function attachment_upload()
{ {
$this->lib->attachment_upload(self::SESSION_KEY, 'cal-'); $handler = new kolab_attachments_handler();
$handler->attachment_upload(self::SESSION_KEY, 'cal-');
} }
/** /**
@ -1965,9 +1966,11 @@ class calendar extends rcube_plugin
*/ */
public function attachment_get() public function attachment_get()
{ {
$handler = new kolab_attachments_handler();
// show loading page // show loading page
if (!empty($_GET['_preload'])) { if (!empty($_GET['_preload'])) {
return $this->lib->attachment_loading_page(); return $handler->attachment_loading_page();
} }
$event_id = rcube_utils::get_input_value('_event', rcube_utils::INPUT_GPC); $event_id = rcube_utils::get_input_value('_event', rcube_utils::INPUT_GPC);
@ -1992,12 +1995,7 @@ class calendar extends rcube_plugin
// show part page // show part page
if (!empty($_GET['_frame'])) { if (!empty($_GET['_frame'])) {
$this->lib->attachment = $attachment; $handler->attachment_page($attachment);
$this->register_handler('plugin.attachmentframe', array($this->lib, 'attachment_frame'));
$this->register_handler('plugin.attachmentcontrols', array($this->lib, 'attachment_header'));
$this->rc->output->set_env('filename', $attachment['name']);
$this->rc->output->set_env('mimetype', $attachment['mimetype']);
$this->rc->output->send('calendar.attachment');
} }
// deliver attachment content // deliver attachment content
else if ($attachment) { else if ($attachment) {
@ -2005,7 +2003,7 @@ class calendar extends rcube_plugin
$attachment['body'] = $this->driver->get_attachment_body($id, $event); $attachment['body'] = $this->driver->get_attachment_body($id, $event);
} }
$this->lib->attachment_get($attachment); $handler->attachment_get($attachment);
} }
// if we arrive here, the requested part was not found // if we arrive here, the requested part was not found

View file

@ -284,86 +284,32 @@ function rcube_calendar_ui(settings)
return date.getHours() >= settings['work_start'] && date.getHours() < settings['work_end']; return date.getHours() >= settings['work_start'] && date.getHours() < settings['work_end'];
}; };
var load_attachment = function(event, att) var load_attachment = function(data)
{ {
var query = { _id: att.id, _event: event.recurrence_id || event.id, _cal:event.calendar, _frame: 1 }; var event = data.record,
query = {_id: data.attachment.id, _event: event.recurrence_id || event.id, _cal: event.calendar};
if (event.rev) if (event.rev)
query._rev = event.rev; query._rev = event.rev;
if (event.calendar == "--invitation--itip") if (event.calendar == "--invitation--itip")
$.extend(query, {_uid: event._uid, _part: event._part, _mbox: event._mbox}); $.extend(query, {_uid: event._uid, _part: event._part, _mbox: event._mbox});
// open attachment in frame if it's of a supported mimetype libkolab.load_attachment(query, data.attachment);
if (id && att.mimetype && $.inArray(att.mimetype, settings.mimetypes)>=0) {
if (rcmail.open_window(rcmail.url('get-attachment', query), true, true)) {
return;
}
}
query._frame = null;
query._download = 1;
rcmail.goto_url('get-attachment', query, false);
}; };
// build event attachments list // build event attachments list
var event_show_attachments = function(list, container, event, edit) var event_show_attachments = function(list, container, event, edit)
{ {
var i, id, len, img, content, li, elem, libkolab.list_attachments(list, container, edit, event,
ul = document.createElement('UL'); function(id) { remove_attachment(id); },
ul.className = 'attachmentslist'; function(data) { load_attachment(data); }
);
for (i=0, len=list.length; i<len; i++) {
elem = list[i];
li = document.createElement('LI');
li.className = elem.classname;
if (edit) {
rcmail.env.attachments[elem.id] = elem;
// delete icon
content = $('<a href="#delete" />')
.attr('title', rcmail.gettext('delete'))
.attr('aria-label', rcmail.gettext('delete') + ' ' + Q(elem.name))
.addClass('delete')
.click({id: elem.id}, function(e) { remove_attachment(this, e.data.id); return false; });
if (!rcmail.env.deleteicon)
content.html(rcmail.gettext('delete'));
else {
img = document.createElement('IMG');
img.src = rcmail.env.deleteicon;
img.alt = rcmail.gettext('delete');
content.append(img);
}
content.appendTo(li);
}
// name/link
content = $('<a href="#load" />')
.html(Q(elem.name))
.addClass('file')
.click({event: event, att: elem}, function(e) {
load_attachment(e.data.event, e.data.att);
return false;
})
.appendTo(li);
ul.appendChild(li);
}
if (edit && rcmail.gui_objects.attachmentlist) {
ul.id = rcmail.gui_objects.attachmentlist.id;
rcmail.gui_objects.attachmentlist = ul;
}
container.empty().append(ul);
}; };
var remove_attachment = function(elem, id) var remove_attachment = function(id)
{ {
$(elem.parentNode).hide();
rcmail.env.deleted_attachments.push(id); rcmail.env.deleted_attachments.push(id);
delete rcmail.env.attachments[id];
}; };
// event details dialog (show only) // event details dialog (show only)

View file

@ -80,9 +80,6 @@ class calendar_ui
$this->cal->register_handler('plugin.sensitivity_select', array($this, 'sensitivity_select')); $this->cal->register_handler('plugin.sensitivity_select', array($this, 'sensitivity_select'));
$this->cal->register_handler('plugin.alarm_select', array($this, 'alarm_select')); $this->cal->register_handler('plugin.alarm_select', array($this, 'alarm_select'));
$this->cal->register_handler('plugin.recurrence_form', array($this->cal->lib, 'recurrence_form')); $this->cal->register_handler('plugin.recurrence_form', array($this->cal->lib, 'recurrence_form'));
$this->cal->register_handler('plugin.attachments_form', array($this, 'attachments_form'));
$this->cal->register_handler('plugin.attachments_list', array($this, 'attachments_list'));
$this->cal->register_handler('plugin.filedroparea', array($this, 'file_drop_area'));
$this->cal->register_handler('plugin.attendees_list', array($this, 'attendees_list')); $this->cal->register_handler('plugin.attendees_list', array($this, 'attendees_list'));
$this->cal->register_handler('plugin.attendees_form', array($this, 'attendees_form')); $this->cal->register_handler('plugin.attendees_form', array($this, 'attendees_form'));
$this->cal->register_handler('plugin.resources_form', array($this, 'resources_form')); $this->cal->register_handler('plugin.resources_form', array($this, 'resources_form'));
@ -100,6 +97,8 @@ class calendar_ui
$this->cal->register_handler('plugin.events_export_form', array($this, 'events_export_form')); $this->cal->register_handler('plugin.events_export_form', array($this, 'events_export_form'));
$this->cal->register_handler('plugin.object_changelog_table', array('libkolab', 'object_changelog_table')); $this->cal->register_handler('plugin.object_changelog_table', array('libkolab', 'object_changelog_table'));
$this->cal->register_handler('plugin.searchform', array($this->rc->output, 'search_form')); // use generic method from rcube_template $this->cal->register_handler('plugin.searchform', array($this->rc->output, 'search_form')); // use generic method from rcube_template
kolab_attachments_handler::ui();
} }
/** /**
@ -610,54 +609,6 @@ class calendar_ui
); );
} }
/**
* Generate the form for event attachments upload
*/
function attachments_form($attrib = array())
{
// add ID if not given
if (!$attrib['id']) {
$attrib['id'] = 'rcmUploadForm';
}
return $this->rc->upload_form($attrib, 'uploadform', 'upload-file', array('multiple' => true));
}
/**
* Register UI object for HTML5 drag & drop file upload
*/
function file_drop_area($attrib = array())
{
if ($attrib['id']) {
$this->rc->output->add_gui_object('filedrop', $attrib['id']);
$this->rc->output->set_env('filedrop', array('action' => 'upload', 'fieldname' => '_attachments'));
}
}
/**
* Generate HTML element for attachments list
*/
function attachments_list($attrib = array())
{
if (!$attrib['id'])
$attrib['id'] = 'rcmAttachmentList';
$skin_path = $this->cal->local_skin_path();
if ($attrib['deleteicon']) {
$_SESSION[calendar::SESSION_KEY . '_deleteicon'] = $skin_path . $attrib['deleteicon'];
$this->rc->output->set_env('deleteicon', $skin_path . $attrib['deleteicon']);
}
if ($attrib['cancelicon'])
$this->rc->output->set_env('cancelicon', $skin_path . $attrib['cancelicon']);
if ($attrib['loadingicon'])
$this->rc->output->set_env('loadingicon', $skin_path . $attrib['loadingicon']);
$this->rc->output->add_gui_object('attachmentlist', $attrib['id']);
$this->attachmentlist_id = $attrib['id'];
return html::tag('ul', $attrib, '', html::$common_attrib);
}
/** /**
* Handler for calendar form template. * Handler for calendar form template.
* The form content could be overriden by the driver * The form content could be overriden by the driver

View file

@ -250,10 +250,6 @@ class libcalendaring extends rcube_plugin
); );
$settings['today'] = $this->rc->gettext('today'); $settings['today'] = $this->rc->gettext('today');
// define list of file types which can be displayed inline
// same as in program/steps/mail/show.inc
$settings['mimetypes'] = (array)$this->rc->config->get('client_mimetypes');
return $settings; return $settings;
} }

View file

@ -33,6 +33,61 @@ class kolab_attachments_handler
$this->rc = rcmail::get_instance(); $this->rc = rcmail::get_instance();
} }
public static function ui()
{
$rcmail = rcmail::get_instance();
$self = new self;
$rcmail->output->add_handler('plugin.attachments_form', array($self, 'files_form'));
$rcmail->output->add_handler('plugin.attachments_list', array($self, 'files_list'));
$rcmail->output->add_handler('plugin.filedroparea', array($self, 'files_drop_area'));
}
/**
* Generate HTML element for attachments list
*/
public function files_list($attrib = array())
{
if (!$attrib['id']) {
$attrib['id'] = 'kolabattachmentlist';
}
// define list of file types which can be displayed inline
// same as in program/steps/mail/show.inc
$this->rc->output->set_env('mimetypes', (array)$this->rc->config->get('client_mimetypes'));
$this->rc->output->add_gui_object('attachmentlist', $attrib['id']);
return html::tag('ul', $attrib, '', html::$common_attrib);
}
/**
* Generate the form for event attachments upload
*/
public function files_form($attrib = array())
{
// add ID if not given
if (!$attrib['id']) {
$attrib['id'] = 'kolabuploadform';
}
return $this->rc->upload_form($attrib, 'uploadform', 'upload-file', array('multiple' => true));
}
/**
* Register UI object for HTML5 drag & drop file upload
*/
public function files_drop_area($attrib = array())
{
// add ID if not given
if (!$attrib['id']) {
$attrib['id'] = 'kolabfiledroparea';
}
$this->rc->output->add_gui_object('filedrop', $attrib['id']);
$this->rc->output->set_env('filedrop', array('action' => 'upload', 'fieldname' => '_attachments'));
}
/** /**
* Displays attachment preview page * Displays attachment preview page
*/ */
@ -40,6 +95,8 @@ class kolab_attachments_handler
{ {
$this->attachment = $attachment; $this->attachment = $attachment;
$this->rc->plugins->include_script('libkolab/libkolab.js');
$this->rc->output->add_handler('plugin.attachmentframe', array($this, 'attachment_frame')); $this->rc->output->add_handler('plugin.attachmentframe', array($this, 'attachment_frame'));
$this->rc->output->add_handler('plugin.attachmentcontrols', array($this, 'attachment_header')); $this->rc->output->add_handler('plugin.attachmentcontrols', array($this, 'attachment_header'));
$this->rc->output->set_env('filename', $attachment['name']); $this->rc->output->set_env('filename', $attachment['name']);

View file

@ -25,7 +25,7 @@
* for the JavaScript code in this file. * for the JavaScript code in this file.
*/ */
var libkolab_audittrail = {} var libkolab_audittrail = {}, libkolab = {};
libkolab_audittrail.quote_html = function(str) libkolab_audittrail.quote_html = function(str)
{ {
@ -221,6 +221,73 @@ libkolab_audittrail.dialog_resize = function(id, height, width)
$(id).dialog('option', { height: Math.min(h-20, height+130), width: Math.min(w-20, width+50) }); $(id).dialog('option', { height: Math.min(h-20, height+130), width: Math.min(w-20, width+50) });
}; };
/**
* Open an attachment either in a browser window for inline view or download it
*/
libkolab.load_attachment = function(query, attachment)
{
query._frame = 1;
// open attachment in frame if it's of a supported mimetype similar as in app.js
if (attachment.id && attachment.mimetype && $.inArray(attachment.mimetype, rcmail.env.mimetypes) >= 0) {
if (rcmail.open_window(rcmail.url('get-attachment', query), true, true)) {
return;
}
}
query._frame = null;
query._download = 1;
rcmail.goto_url('get-attachment', query, false);
};
/**
* Build attachments list element
*/
libkolab.list_attachments = function(list, container, edit, data, ondelete, onload)
{
var ul = $('<ul>').addClass('attachmentslist');
$.each(list || [], function(i, elem) {
var li = $('<li>').addClass(elem.classname);
// name/link
$('<a>').attr({href: '#load', 'class': 'filename'})
.append($('<span class="attachment-name">').text(elem.name))
.click({record: data, attachment: elem}, function(e) {
if (onload) {
onload(e.data);
}
return false;
})
.appendTo(li);
if (edit) {
rcmail.env.attachments[elem.id] = elem;
// delete link
$('<a>').attr({href: '#delete', title: rcmail.gettext('delete'), 'class': 'delete'})
.click({id: elem.id}, function(e) {
$(this.parentNode).hide();
delete rcmail.env.attachments[e.data.id];
if (ondelete) {
ondelete(e.data.id);
}
return false;
})
.appendTo(li);
}
ul.append(li);
});
if (edit && rcmail.gui_objects.attachmentlist) {
ul.id = rcmail.gui_objects.attachmentlist.id;
rcmail.gui_objects.attachmentlist = ul.get(0);
}
container.empty().append(ul);
};
function kolab_folderlist(node, p) function kolab_folderlist(node, p)
{ {
// extends treelist.js // extends treelist.js
@ -552,7 +619,9 @@ function kolab_folderlist(node, p)
} }
// link prototype from base class // link prototype from base class
kolab_folderlist.prototype = rcube_treelist_widget.prototype; if (window.rcube_treelist_widget) {
kolab_folderlist.prototype = rcube_treelist_widget.prototype;
}
window.rcmail && rcmail.addEventListener('init', function(e) { window.rcmail && rcmail.addEventListener('init', function(e) {
@ -599,7 +668,7 @@ window.rcmail && rcmail.addEventListener('init', function(e) {
} }
if (rcmail.env.action == 'get-attachment') { if (rcmail.env.action == 'get-attachment') {
if (rcmail.rcmail.gui_objects.attachmentframe) { if (rcmail.gui_objects.attachmentframe) {
rcmail.gui_objects.messagepartframe = rcmail.gui_objects.attachmentframe; rcmail.gui_objects.messagepartframe = rcmail.gui_objects.attachmentframe;
rcmail.enable_command('image-scale', 'image-rotate', !!/^image\//.test(rcmail.env.mimetype)); rcmail.enable_command('image-scale', 'image-rotate', !!/^image\//.test(rcmail.env.mimetype));
rcmail.register_command('print-attachment', function() { rcmail.register_command('print-attachment', function() {

View file

@ -2611,89 +2611,39 @@ function rcube_tasklist_ui(settings)
/** /**
* Open a task attachment either in a browser window for inline view or download it * Open a task attachment either in a browser window for inline view or download it
*/ */
function load_attachment(rec, att) function load_attachment(data)
{ {
var rec = data.record;
// can't open temp attachments // can't open temp attachments
if (!rec.id || rec.id < 0) if (!rec.id || rec.id < 0)
return false; return false;
var query = { _id: att.id, _t: rec.recurrence_id||rec.id, _list:rec.list, _frame: 1 }; var query = {_id: data.attachment.id, _t: rec.recurrence_id || rec.id, _list: rec.list};
if (rec.rev) if (rec.rev)
query._rev = rec.rev; query._rev = rec.rev;
// open attachment in frame if it's of a supported mimetype libkolab.load_attachment(query, data.attachment);
// similar as in app.js and calendar_ui.js
if (att.id && att.mimetype && $.inArray(att.mimetype, settings.mimetypes)>=0) {
if (rcmail.open_window(rcmail.url('get-attachment', query), true, true)) {
return;
}
}
query._frame = null;
query._download = 1;
rcmail.goto_url('get-attachment', query, false);
}; };
/** /**
* Build task attachments list * Build task attachments list
*/ */
function task_show_attachments(list, container, rec, edit) function task_show_attachments(list, container, task, edit)
{ {
var i, id, len, content, li, elem, libkolab.list_attachments(list, container, edit, task,
ul = $('<ul>').addClass('attachmentslist'); function(id) { remove_attachment(id); },
function(data) { load_attachment(data); }
for (i=0, len=list.length; i<len; i++) { );
elem = list[i];
li = $('<li>').addClass(elem.classname);
if (edit) {
rcmail.env.attachments[elem.id] = elem;
// delete icon
content = $('<a>').attr({href: '#delete', title: rcmail.gettext('delete'), 'class': 'delete'})
.click({ id:elem.id }, function(e) {
remove_attachment(this, e.data.id);
return false;
});
if (!rcmail.env.deleteicon) {
// content.html(rcmail.gettext('delete'));
}
else {
$('<img>').attr('src', rcmail.env.deleteicon).attr('alt', rcmail.gettext('delete')).appendTo(content);
}
li.append(content);
}
// name/link
$('<a>').attr({href: '#load', 'class': 'filename'})
.append($('<span class="attachment-name">').text(elem.name))
.click({ task:rec, att:elem }, function(e) {
load_attachment(e.data.task, e.data.att);
return false;
})
.appendTo(li);
ul.append(li);
}
if (edit && rcmail.gui_objects.attachmentlist) {
ul.id = rcmail.gui_objects.attachmentlist.id;
rcmail.gui_objects.attachmentlist = ul.get(0);
}
container.empty().append(ul);
}; };
/** /**
* *
*/ */
function remove_attachment(elem, id) function remove_attachment(id)
{ {
$(elem.parentNode).hide();
me.selected_task.deleted_attachments.push(id); me.selected_task.deleted_attachments.push(id);
delete rcmail.env.attachments[id];
} }
/** /**

View file

@ -146,9 +146,6 @@ class tasklist_ui
$this->plugin->register_handler('plugin.tags_editline', array($this, 'tags_editline')); $this->plugin->register_handler('plugin.tags_editline', array($this, 'tags_editline'));
$this->plugin->register_handler('plugin.alarm_select', array($this, 'alarm_select')); $this->plugin->register_handler('plugin.alarm_select', array($this, 'alarm_select'));
$this->plugin->register_handler('plugin.recurrence_form', array($this->plugin->lib, 'recurrence_form')); $this->plugin->register_handler('plugin.recurrence_form', array($this->plugin->lib, 'recurrence_form'));
$this->plugin->register_handler('plugin.attachments_form', array($this, 'attachments_form'));
$this->plugin->register_handler('plugin.attachments_list', array($this, 'attachments_list'));
$this->plugin->register_handler('plugin.filedroparea', array($this, 'file_drop_area'));
$this->plugin->register_handler('plugin.attendees_list', array($this, 'attendees_list')); $this->plugin->register_handler('plugin.attendees_list', array($this, 'attendees_list'));
$this->plugin->register_handler('plugin.attendees_form', array($this, 'attendees_form')); $this->plugin->register_handler('plugin.attendees_form', array($this, 'attendees_form'));
$this->plugin->register_handler('plugin.identity_select', array($this, 'identity_select')); $this->plugin->register_handler('plugin.identity_select', array($this, 'identity_select'));
@ -158,6 +155,8 @@ class tasklist_ui
$this->plugin->register_handler('plugin.tasks_export_form', array($this, 'tasks_export_form')); $this->plugin->register_handler('plugin.tasks_export_form', array($this, 'tasks_export_form'));
$this->plugin->register_handler('plugin.tasks_import_form', array($this, 'tasks_import_form')); $this->plugin->register_handler('plugin.tasks_import_form', array($this, 'tasks_import_form'));
kolab_attachments_handler::ui();
$this->plugin->include_script('tasklist.js'); $this->plugin->include_script('tasklist.js');
$this->rc->output->include_script('treelist.js'); $this->rc->output->include_script('treelist.js');
$this->plugin->api->include_script('libkolab/libkolab.js'); $this->plugin->api->include_script('libkolab/libkolab.js');
@ -412,44 +411,6 @@ class tasklist_ui
return html::div($attrib, $input->show('')); return html::div($attrib, $input->show(''));
} }
/**
* Generate HTML element for attachments list
*/
function attachments_list($attrib = array())
{
if (!$attrib['id']) {
$attrib['id'] = 'rcmtaskattachmentlist';
}
$this->register_gui_object('attachmentlist', $attrib['id']);
return html::tag('ul', $attrib, '', html::$common_attrib);
}
/**
* Generate the form for event attachments upload
*/
function attachments_form($attrib = array())
{
// add ID if not given
if (!$attrib['id']) {
$attrib['id'] = 'rcmtaskuploadform';
}
return $this->rc->upload_form($attrib, 'uploadform', 'upload-file', array('multiple' => true));
}
/**
* Register UI object for HTML5 drag & drop file upload
*/
function file_drop_area($attrib = array())
{
if ($attrib['id']) {
$this->register_gui_object('filedrop', $attrib['id']);
$this->rc->output->set_env('filedrop', array('action' => 'upload', 'fieldname' => '_attachments'));
}
}
/** /**
* *
*/ */