Skip to content

Commit

Permalink
WIP: Use jQuery to attach event-handlers based on attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
pabzm committed Sep 5, 2024
1 parent 775ab62 commit 72cfd49
Show file tree
Hide file tree
Showing 38 changed files with 497 additions and 311 deletions.
18 changes: 16 additions & 2 deletions plugins/enigma/enigma.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,15 @@ rcube_webmail.prototype.enigma_compose_handler = function (props) {
};

// Import attached keys/certs file
rcube_webmail.prototype.enigma_import_attachment = function (mime_id) {
$('[data-event-handle="enigma_import_attachment"]').on('click', function (event) {
var mime_id = event.target.dataset.part;
var lock = this.set_busy(true, 'loading'),
post = { _uid: this.env.uid, _mbox: this.env.mailbox, _part: mime_id };

this.http_post('plugin.enigmaimport', post, lock);

return false;
};
});

// password request popup
rcube_webmail.prototype.enigma_password_request = function (data) {
Expand Down Expand Up @@ -745,3 +746,16 @@ rcube_webmail.prototype.enigma_find_publickey = function (email) {
}
);
};

// Some event handlers.
$('[data-event-handle="enigma_import_upload"]').on('click', function (event) {
return window.rcmail.command('plugin.enigma-import', '', event.target, event);
});

$('[data-event-handle="enigma_import_search"]').on('click', function (event) {
return window.rcmail.command('plugin.enigma-import-search', '', event.target, event);
});

$('[data-event-handle="enigma_import_search"]').on('click', function (event) {
window.rcmail.command('menu-open', 'enigmamenu', event.target, event);
});
9 changes: 5 additions & 4 deletions plugins/enigma/lib/enigma_ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ public function tpl_key_import_form($attrib)
$max_filesize = rcmail_action::upload_init();
$upload_button = new html_button([
'class' => 'button import',
'data-onclick' => ['command', 'plugin.enigma-import', '', '__THIS__', '__EVENT__'],
'data-event-handle' => 'enigma_import_upload',
]);

$form = html::div(null, html::p(null, rcube::Q($this->enigma->gettext('keyimporttext'), 'show'))
Expand Down Expand Up @@ -639,7 +639,7 @@ public function tpl_key_import_form($attrib)

$search_button = new html_button([
'class' => 'button search',
'data-onclick' => ['command', 'plugin.enigma-import-search', '', '__THIS__', '__EVENT__'],
'data-event-handle' => 'enigma_import_search',
]);

$form = html::div(null,
Expand Down Expand Up @@ -848,7 +848,7 @@ private function compose_ui()
$this->enigma->add_button([
'type' => 'link',
'command' => 'plugin.enigma',
'data-onclick' => ['command', 'menu-open', 'enigmamenu', '__THIS__', '__EVENT__'],
'data-event-handle' => 'enigma_menu_open',
'class' => 'button enigma',
'title' => 'encryptionoptions',
'label' => 'encryption',
Expand Down Expand Up @@ -1107,7 +1107,8 @@ public function message_output($p)
$p['content'] = html::p(['class' => 'enigmaattachment boxinformation aligned-buttons'],
html::span(null, rcube::Q($this->enigma->gettext('keyattfound'))) .
html::tag('button', [
'data-onclick' => ['enigma_import_attachment', $part],
'data-event-handle' => 'enigma_import_attachment',
'data-part' => $part,
'title' => $this->enigma->gettext('keyattimport'),
'class' => 'import btn-sm',
], rcube::Q($this->rc->gettext('import'))
Expand Down
3 changes: 2 additions & 1 deletion plugins/help/help.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public function tablink($attrib)
// so button() will translate it correctly
$attrib['title'] = $attrib['label'];

$attrib['data-onclick'] = ['show_help_content', $attrib['action'], '__EVENT__'];
$attrib['data-action'] = $attrib['action'];
$attrib['data-event-handle'] = 'call_show_help_content';

return $rcmail->output->button($attrib);
}
Expand Down
26 changes: 16 additions & 10 deletions plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,7 @@ public function filter_form($attrib)
// any, allof, anyof radio buttons
$field_id = '_allof';
$input_join = new html_radiobutton(['name' => '_join', 'id' => $field_id, 'value' => 'allof',
'data-onclick' => ['managesieve_rule_join_radio', 'allof'], 'class' => 'radio']);
'data-event-handle' => 'managesieve_rule_join_radio', 'data-value' => 'allof', 'class' => 'radio']);

if (isset($scr) && !$any) {
$input_join = $input_join->show($scr['join'] ? 'allof' : '');
Expand All @@ -1604,7 +1604,7 @@ public function filter_form($attrib)

$field_id = '_anyof';
$input_join = new html_radiobutton(['name' => '_join', 'id' => $field_id, 'value' => 'anyof',
'data-onclick' => ['managesieve_rule_join_radio', 'anyof'], 'class' => 'radio']);
'data-event-handle' => 'managesieve_rule_join_radio', 'data-value' => 'anyof', 'class' => 'radio']);

if (isset($scr) && !$any) {
$input_join = $input_join->show($scr['join'] ? '' : 'anyof');
Expand All @@ -1616,7 +1616,7 @@ public function filter_form($attrib)

$field_id = '_any';
$input_join = new html_radiobutton(['name' => '_join', 'id' => $field_id, 'value' => 'any',
'data-onclick' => ['managesieve_rule_join_radio', 'any'], 'class' => 'radio']);
'data-event-handle' => 'managesieve_rule_join_radio', 'data-value' => 'any', 'class' => 'radio']);

$input_join = $input_join->show($any ? 'any' : '');

Expand Down Expand Up @@ -2113,7 +2113,8 @@ public function rule_div($fid, $id, $div = true, $compact = false)
'href' => '#',
'id' => "ruleadv{$id}",
'title' => $adv_title,
'data-onclick' => ['rule_adv_switch', $id, '__THIS__', ['preventDefault' => true]],
'data-id' => $id,
'data-event-handle' => 'managesieve_rule_adv_switch',
'class' => 'show',
],
html::span(['class' => 'inner'], $adv_title)
Expand All @@ -2140,7 +2141,8 @@ public function rule_div($fid, $id, $div = true, $compact = false)
'href' => '#',
'id' => "ruleadv{$id}",
'title' => $adv_title,
'data-onclick' => ['rule_adv_switch', $id, '__THIS__', ['preventDefault' => true]],
'data-id' => $id,
'data-event-handle' => 'managesieve_rule_adv_switch',
'class' => 'advanced show',
],
html::span(['class' => 'inner'], $adv_title)
Expand All @@ -2150,7 +2152,8 @@ public function rule_div($fid, $id, $div = true, $compact = false)
'href' => '#',
'id' => "ruleadd{$id}",
'title' => $add_title,
'data-onclick' => ['managesieve_ruleadd', $id, ['preventDefault' => true]],
'data-id' => $id,
'data-event-handle' => 'managesieve_ruleadd',
'class' => 'button create add',
],
html::span(['class' => 'inner'], $add_title)
Expand All @@ -2159,7 +2162,8 @@ public function rule_div($fid, $id, $div = true, $compact = false)
'href' => '#',
'id' => "ruledel{$id}",
'title' => $del_title,
'data-onclick' => ['managesieve_ruledel', $id, ['preventDefault' => true]],
'data-id' => $id,
'data-event-handle' => 'managesieve_ruledel',
'class' => 'button delete del ' . $rows_num < 2 ? 'disabled' : '',
],
html::span(['class' => 'inner'], $del_title)
Expand Down Expand Up @@ -2410,7 +2414,7 @@ public function action_div($fid, $id, $div = true)
$out .= $this->list_input($id, 'action_addresses', $action['addresses'] ?? null,
30, false, ['class' => $this->error_class($id, 'action', 'addresses', 'action_addresses')]
)
. html::a(['href' => '#', 'data-onclick' => ['managesieve_vacation_addresses', $id]],
. html::a(['href' => '#', 'data-event-handle' => 'managesieve_vacation_addresses', 'data-id' => $id],
rcube::Q($this->plugin->gettext('filladdresses')));
$out .= '<br><span class="label">' . rcube::Q($this->plugin->gettext('vacationinterval')) . '</span><br>';
$out .= '<div class="input-group">' . html::tag('input', [
Expand Down Expand Up @@ -2714,7 +2718,8 @@ public function action_div($fid, $id, $div = true)
'href' => '#',
'id' => "actionadd{$id}",
'title' => $add_label,
'data-onclick' => ['managesieve_actionadd', $id],
'data-id' => $id,
'data-event-handle' => 'managesieve_actionadd',
'class' => 'button create add',
],
html::span(['class' => 'inner'], $add_label),
Expand All @@ -2724,7 +2729,8 @@ public function action_div($fid, $id, $div = true)
'href' => '#',
'id' => "actiondel{$id}",
'title' => $del_label,
'data-onclick' => ['managesieve_actiondel', $id],
'data-id' => $id,
'data-event-handle' => ['managesieve_actiondel',
'class' => 'button delete del ' . ($rows_num < 2 ? 'disabled' : ''),
],
html::span(['class' => 'inner'], $del_label),
Expand Down
2 changes: 1 addition & 1 deletion plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public function vacation_form($attrib)
'type' => 'button',
'href' => '#',
'class' => 'button',
'data-onclick' => ['managesieve_vacation_addresses'],
'data-event-handle' => 'managesieve_vacation_addresses',
]);

$redirect = !empty($this->vacation['action'])
Expand Down
130 changes: 73 additions & 57 deletions plugins/managesieve/managesieve.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,6 @@ rcube_webmail.prototype.managesieve_save = function () {
};

// Operations on filters form
rcube_webmail.prototype.managesieve_ruleadd = function (id) {
this.http_post('plugin.managesieve-action', '_act=ruleadd&_rid=' + id);
};

rcube_webmail.prototype.managesieve_rulefill = function (content, id, after) {
if (content != '') {
// create new element
Expand All @@ -559,22 +555,6 @@ rcube_webmail.prototype.managesieve_rulefill = function (content, id, after) {
}
};

rcube_webmail.prototype.managesieve_ruledel = function (id) {
if ($('#ruledel' + id).hasClass('disabled')) {
return;
}

this.confirm_dialog(this.get_label('managesieve.ruledeleteconfirm'), 'delete', function (e, ref) {
var row = document.getElementById('rulerow' + id);
row.parentNode.removeChild(row);
ref.managesieve_formbuttons(document.getElementById('rules'));
});
};

rcube_webmail.prototype.managesieve_actionadd = function (id) {
this.http_post('plugin.managesieve-action', '_act=actionadd&_aid=' + id);
};

rcube_webmail.prototype.managesieve_actionfill = function (content, id, after) {
if (content != '') {
var div = $('#actions')[0],
Expand All @@ -592,18 +572,6 @@ rcube_webmail.prototype.managesieve_actionfill = function (content, id, after) {
}
};

rcube_webmail.prototype.managesieve_actiondel = function (id) {
if ($('#actiondel' + id).hasClass('disabled')) {
return;
}

this.confirm_dialog(this.get_label('managesieve.actiondeleteconfirm'), 'delete', function (e, ref) {
var row = document.getElementById('actionrow' + id);
row.parentNode.removeChild(row);
ref.managesieve_formbuttons(document.getElementById('actions'));
});
};

// insert rule/action row in specified place on the list
rcube_webmail.prototype.managesieve_insertrow = function (div, row, after) {
var node = $('#' + ($(div).attr('id') == 'rules' ? 'rulerow' : 'actionrow') + after)[0];
Expand All @@ -627,12 +595,6 @@ rcube_webmail.prototype.managesieve_formbuttons = function (div) {
}
};

// update vacation addresses field with user identities
rcube_webmail.prototype.managesieve_vacation_addresses = function (id) {
var lock = this.set_busy(true, 'loading');
this.http_post('plugin.managesieve-action', { _act: 'addresses', _aid: id }, lock);
};

// update vacation addresses field with user identities
rcube_webmail.prototype.managesieve_vacation_addresses_update = function (id, addresses) {
var field = $('#vacation_addresses,#action_addresses' + (id || ''));
Expand Down Expand Up @@ -789,22 +751,6 @@ function rule_spamtest_select(id) {
$(obj)[obj.value ? 'removeClass' : 'addClass']('rounded-right');
}

function rule_join_radio(value) {
$('#rules').css('display', value == 'any' ? 'none' : 'block');
}

function rule_adv_switch(id, elem) {
var elem = $(elem), enabled = elem.hasClass('hide'), adv = $('#rule_advanced' + id);

if (enabled) {
adv.get(0).style.display = 'none';
elem.removeClass('hide').addClass('show');
} else {
adv.get(0).style.display = '';
elem.removeClass('show').addClass('hide');
}
}

function rule_mime_select(id) {
var elem = $('#rule_mime_type' + id),
param_elem = $('#rule_mime_param' + id + '_list');
Expand Down Expand Up @@ -1288,6 +1234,76 @@ rcube_webmail.prototype.managesieve_dialog_resize = function (o) {
dialog.dialog('option', { height: Math.min(h - 20, height + 120), width: Math.min(w - 20, width + 65) });
};

rcube_webmail.prototype.ui_switch_nav_list = function (elem) {
UI.switch_nav_list(elem);
};
// Some event handlers.
window.rcmail.addEventListener('init', function () {

$('[data-event-handle="managesieve_switch_nav_list"]').on('click', function (event) {
UI.switch_nav_list(event.target);
});

$('[data-event-handle="managesieve_ruleadd"]').on('click', function (event) {
const id = event.target.dataset.id;
this.http_post('plugin.managesieve-action', '_act=ruleadd&_rid=' + id);
return false;
});

$('[data-event-handle="managesieve_ruledel"]').on('click', function (event) {
const id = event.target.dataset.id;
if ($('#ruledel' + id).hasClass('disabled')) {
return;
}

this.confirm_dialog(this.get_label('managesieve.ruledeleteconfirm'), 'delete', function (e, ref) {
var row = document.getElementById('rulerow' + id);
row.parentNode.removeChild(row);
ref.managesieve_formbuttons(document.getElementById('rules'));
});
});

$('[data-event-handle="managesieve_actionadd"]').on('click', function (event) {
const id = event.target.dataset.id;
this.http_post('plugin.managesieve-action', '_act=actionadd&_aid=' + id);
});

$('[data-event-handle="managesieve_actiondel"]').on('click', function (event) {
const id = event.target.dataset.id;
if ($('#actiondel' + id).hasClass('disabled')) {
return;
}

this.confirm_dialog(this.get_label('managesieve.actiondeleteconfirm'), 'delete', function (e, ref) {
var row = document.getElementById('actionrow' + id);
row.parentNode.removeChild(row);
ref.managesieve_formbuttons(document.getElementById('actions'));
});
});

// update vacation addresses field with user identities
$('[data-event-handle="managesieve_vacation_addresses"]').on('click', function (event) {
const id = event.target.dataset.id;
var lock = window.rcmail.set_busy(true, 'loading');
window.rcmail.http_post('plugin.managesieve-action', { _act: 'addresses', _aid: id }, lock);
});

$('data-event-handle="managesieve_rule_join_radio"]').on('click', function (event) {
const value = event.target.dataset.value;
$('#rules').css('display', value == 'any' ? 'none' : 'block');
});

$('data-event-handle="managesieve_rule_adv_switch"]').on('click', function (event) {
var elem = event.target;
var id = elem.dataset.id;
var elem = $(elem), enabled = elem.hasClass('hide'), adv = $('#rule_advanced' + id);

if (enabled) {
adv.get(0).style.display = 'none';
elem.removeClass('hide').addClass('show');
} else {
adv.get(0).style.display = '';
elem.removeClass('show').addClass('hide');
}

return false;
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h1 class="voice"><roundcube:label name="settings" /> : <roundcube:label name="m
<a class="button icon toolbar-menu-button" href="#list-menu"><span class="inner"><roundcube:label name="menu" /></span></a>
</div>
<roundcube:if condition="!env:managesieve_no_set_list" />
<div class="pagenav pagenav-list menu" data-onclick='["ui_switch_nav_list", "__THIS__"]'>
<div class="pagenav pagenav-list menu" data-event-handle="managesieve_switch_nav_list">
<a class="expand"><span class="inner"></span></a>
<span id="filterset-name" class="pagenav-text"></span>
<h2 class="voice" id="aria-label-filtersets"><roundcube:label name="managesieve.filtersets" /></h2>
Expand Down
4 changes: 2 additions & 2 deletions plugins/vcard_attachments/vcard_attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public function message_objects($p)

if (count($contacts) == 1) {
$display = array_first($contacts);
$attr['data-onclick'] = ['vcard_attachments_plugin_vcard_import', key($contacts)];
$attr['data-onclick'] = ['vcard_attachments_plugin_vcard_import', key($contacts), ['returnResult' => true]];
} else {
$display = $this->gettext(['name' => 'contactsattached', 'vars' => ['num' => count($contacts)]]);
$attr['data-onclick'] = ['vcard_attachments_plugin_vcard_import'];
$attr['data-onclick'] = ['vcard_attachments_plugin_vcard_import', ['returnResult' => true]];

$rcmail->output->set_env('vcards', $contacts);
$rcmail->output->add_label('vcard_attachments.addvcardmsg', 'import');
Expand Down
2 changes: 1 addition & 1 deletion program/actions/contacts/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function run($args = [])
$_SESSION['contact_undo']['ts'] = time();
$msg = html::span(null, $rcmail->gettext('contactdeleted'))
. ' ' . html::a(
['data-onclick' => ['command', 'undo', '', '__THIS__']],
['data-event-handle' => 'contacts_delete_undo'],
$rcmail->gettext('undo')
);

Expand Down
Loading

0 comments on commit 72cfd49

Please sign in to comment.