Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0][RFC] Custom Element: Joomla! Toolbar button #19635

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5ff1aef
joomla-toolbar-button for standard button
Fedik Feb 10, 2018
4122250
JoomlaToolbarButton custom element
Fedik Feb 10, 2018
761209b
clean up
Fedik Feb 10, 2018
09e572d
move to webcomponents
Fedik Feb 10, 2018
db79853
Merge branch '4.0-dev' into toolbar-button
Fedik Feb 11, 2018
a16fbe6
make a button to support button behavior
Fedik Feb 11, 2018
7afd80e
Disable the button if list selection are required
Fedik Feb 11, 2018
23b8847
Update Apply toolbar button
Fedik Feb 11, 2018
ef55a80
Add minified version
Fedik Feb 11, 2018
b15353e
disabled:true
Fedik Feb 11, 2018
bcaeab4
Make sure the id of toolbar buttons are unique
Fedik Feb 11, 2018
abc913d
Better handling of list selection
Fedik Feb 11, 2018
74a18fc
code style
Fedik Feb 11, 2018
2cf919c
Link and Help buttons to <joomla-toolbar-button/>
Fedik Feb 11, 2018
629a762
joomla.toolbar.batch to <joomla-toolbar-button/>
Fedik Feb 11, 2018
cbcaf79
listConfirmation > listSelection
Fedik Feb 11, 2018
0be7461
Fix position of Help button
Fedik Feb 11, 2018
28db80d
Confirm button to <joomla-toolbar-button/>
Fedik Feb 11, 2018
0346c3d
Sync. Merge branch '4.0-dev' into toolbar-button
Fedik Feb 18, 2018
7509dc7
Use a real button
Fedik Feb 18, 2018
55c045d
Update minified version
Fedik Feb 18, 2018
a415aa6
restore control+s
Fedik Feb 18, 2018
0757815
Merge branch '4.0-dev' into toolbar-button
Fedik Mar 3, 2018
6017c0f
update core.min.js after conflict
Fedik Mar 3, 2018
318b686
revert control+s for now
Fedik Mar 3, 2018
5ee2fb2
c.s.
Fedik Mar 3, 2018
5c9e73b
fix karma core.js test of isChecked
Fedik Mar 3, 2018
8523793
fix typo
Fedik Mar 3, 2018
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
89 changes: 89 additions & 0 deletions build/webcomponents/js/toolbar-button/toolbar-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

;(function(customElements) {
"use strict";

class JoomlaToolbarButton extends HTMLElement {

// Attribute getters
get task() { return this.getAttribute('task'); }
get execute() { return this.getAttribute('execute'); }
get listSelection() { return this.hasAttribute('list-selection'); }
get form() { return this.getAttribute('form'); }
get formValidation() { return this.hasAttribute('form-validation'); }
get confirmMessage() { return this.getAttribute('confirm-message'); }

constructor() {
super();

// We need a button to support button behavior, because we cannot currently extend HTMLButtonElement
this.buttonElement = this.querySelector('button');
this.disabled = false;

// If list selection are required, set button to disabled by default
if (this.listSelection) {
this.setDisabled(true);
}

this.addEventListener('click', e => this.executeTask());
}

connectedCallback() {
// Check whether we have a form
let formSelector = this.form || '#adminForm';
this.formElement = document.querySelector(formSelector);

if (this.listSelection) {
if (!this.formElement) {
throw new Error('The form "' + formSelector + '" is required to perform the task, but the form not found on the page.');
}

// Watch on list selection
this.formElement.boxchecked.addEventListener('change', (event) => {
// Check whether we have selected something
this.setDisabled(event.target.value == 0);
});
}
}

setDisabled(disabled) {
// Make sure we have a boolean value
this.disabled = !!disabled;

if (this.buttonElement) {
if (this.disabled) {
this.buttonElement.setAttribute('disabled', true);
} else {
this.buttonElement.removeAttribute('disabled');
}
}
}

executeTask() {
if (this.disabled) {
return;
}

if (this.confirmMessage && !confirm(this.confirmMessage)) {
return;
}

if (this.task) {
Joomla.submitbutton(this.task, this.form, this.formValidation);
} else if (this.execute) {
let method = new Function(this.execute);
method.call({});
} else {
throw new Error('Either "task" or "execute" attribute must be preset to perform an action.');
}

}

}

customElements.define('joomla-toolbar-button', JoomlaToolbarButton);

})(customElements);
38 changes: 24 additions & 14 deletions layouts/joomla/toolbar/apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,34 @@
use Joomla\CMS\HTML\HTMLHelper;

HTMLHelper::_('behavior.core');
HTMLHelper::_('webcomponent', ['joomla-toolbar-button' => 'system/webcomponents/joomla-toolbar-button.min.js'], ['relative' => true, 'version' => 'auto', 'detectBrowser' => false, 'detectDebug' => true]);

if (preg_match('/Joomla.submitbutton/', $displayData['doTask']))
{
$ctrls = str_replace("Joomla.submitbutton('", '', $displayData['doTask']);
$ctrls = str_replace("')", '', $ctrls);
$ctrls = str_replace(";", '', $ctrls);

$options = array('task' => $ctrls);
Factory::getDocument()->addScriptOptions('keySave', $options);
}
$options = array('task' => $displayData['task']);
Factory::getDocument()->addScriptOptions('keySave', $options);

$id = isset($displayData['id']) ? $displayData['id'] : '';
$doTask = $displayData['doTask'];
$class = $displayData['class'];
$text = $displayData['text'];
$btnClass = $displayData['btnClass'];
$group = $displayData['group'];
$task = '';
$list = !empty($displayData['list']) ? ' list-selection' : '';
$form = !empty($displayData['form']) ? ' form="' . $displayData['form'] . '"' : '';
$validate = !empty($displayData['validate']) ? ' form-validation' : '';

if (!empty($displayData['task']))
{
$task = ' task="' . $displayData['task'] . '"';
}
elseif (!empty($displayData['doTask']))
{
$task = ' execute="' . $displayData['doTask'] . '"';
}
?>
<button<?php echo $id; ?> onclick="<?php echo $doTask; ?>" class="<?php echo $btnClass; ?>">
<span class="<?php echo trim($class); ?>"></span>
<?php echo $text; ?>
</button>

<joomla-toolbar-button <?php echo $id.$task.$list.$form.$validate; ?>>
<button type="button" class="<?php echo $btnClass; ?>">
<span class="<?php echo trim($class); ?>" aria-hidden="true"></span>
<?php echo $text; ?>
</button>
</joomla-toolbar-button>
16 changes: 8 additions & 8 deletions layouts/joomla/toolbar/batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
use Joomla\CMS\Language\Text;

HTMLHelper::_('behavior.core');
HTMLHelper::_('webcomponent', ['joomla-toolbar-button' => 'system/webcomponents/joomla-toolbar-button.min.js'], ['relative' => true, 'version' => 'auto', 'detectBrowser' => false, 'detectDebug' => true]);

$id = isset($displayData['id']) ? $displayData['id'] : '';
$title = $displayData['title'];
Text::script('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST');
Text::script('ERROR');
$message = "{'error': [Joomla.JText._('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST')]}";
$alert = "Joomla.renderMessages(" . $message . ")";

?>
<button<?php echo $id; ?> data-toggle="modal" onclick="if (document.adminForm.boxchecked.value==0){<?php echo $alert; ?>}else{jQuery( '#collapseModal' ).modal('show'); return true;}" class="btn btn-outline-primary btn-sm">
<span class="fa fa-square" aria-hidden="true" title="<?php echo $title; ?>"></span>
<?php echo $title; ?>
</button>
<joomla-toolbar-button <?php echo $id; ?> execute="jQuery( '#collapseModal' ).modal('show');" list-selection>
<button type="button" class="btn btn-outline-primary btn-sm">
<span class="fa fa-square" aria-hidden="true"></span>
<?php echo $title; ?>
</button>
</joomla-toolbar-button>
26 changes: 22 additions & 4 deletions layouts/joomla/toolbar/confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,31 @@
use Joomla\CMS\HTML\HTMLHelper;

HTMLHelper::_('behavior.core');
HTMLHelper::_('webcomponent', ['joomla-toolbar-button' => 'system/webcomponents/joomla-toolbar-button.min.js'], ['relative' => true, 'version' => 'auto', 'detectBrowser' => false, 'detectDebug' => true]);

$id = isset($displayData['id']) ? $displayData['id'] : '';
$doTask = $displayData['doTask'];
$class = $displayData['class'];
$text = $displayData['text'];
$task = '';
$list = !empty($displayData['list']) ? ' list-selection' : '';
$form = !empty($displayData['form']) ? ' form="' . $displayData['form'] . '"' : '';
$validate = !empty($displayData['validate']) ? ' form-validation' : '';
$msg = !empty($displayData['msg']) ? ' confirm-message="' . $this->escape($displayData['msg']) . '"' : '';

if (!empty($displayData['task']))
{
$task = ' task="' . $displayData['task'] . '"';
}
elseif (!empty($displayData['doTask']))
{
$task = ' execute="' . $displayData['doTask'] . '"';
}

?>
<button<?php echo $id; ?> onclick="<?php echo $doTask; ?>" class="btn btn-sm btn-outline-danger">
<span class="<?php echo $class; ?>" aria-hidden="true"></span>
<?php echo $text; ?>
</button>
<joomla-toolbar-button <?php echo $id.$task.$list.$form.$validate.$msg; ?>>
<button type="button" class="btn btn-sm btn-outline-danger">
<span class="<?php echo trim($class); ?>" aria-hidden="true"></span>
<?php echo $text; ?>
</button>
</joomla-toolbar-button>
11 changes: 7 additions & 4 deletions layouts/joomla/toolbar/help.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
use Joomla\CMS\HTML\HTMLHelper;

HTMLHelper::_('behavior.core');
HTMLHelper::_('webcomponent', ['joomla-toolbar-button' => 'system/webcomponents/joomla-toolbar-button.min.js'], ['relative' => true, 'version' => 'auto', 'detectBrowser' => false, 'detectDebug' => true]);

$id = $displayData['id'] ?? '';
$doTask = $displayData['doTask'];
$text = $displayData['text'];
?>
<button<?php echo $id; ?> onclick="<?php echo $doTask; ?>" rel="help" class="btn btn-outline-info btn-sm">
<span class="fa fa-question" aria-hidden="true"></span>
<?php echo $text; ?>
</button>
<joomla-toolbar-button <?php echo $id; ?> execute="<?php echo $doTask; ?>">
<button type="button" class="btn btn-outline-info btn-sm" rel="help">
<span class="fa fa-question" aria-hidden="true"></span>
<?php echo $text; ?>
</button>
</joomla-toolbar-button>
33 changes: 27 additions & 6 deletions layouts/joomla/toolbar/link.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,34 @@

defined('JPATH_BASE') or die;

use Joomla\CMS\HTML\HTMLHelper;

HTMLHelper::_('behavior.core');
HTMLHelper::_('webcomponent', ['joomla-toolbar-button' => 'system/webcomponents/joomla-toolbar-button.min.js'], ['relative' => true, 'version' => 'auto', 'detectBrowser' => false, 'detectDebug' => true]);

$id = isset($displayData['id']) ? $displayData['id'] : '';
$doTask = $displayData['doTask'];
$task = '';
$class = $displayData['class'];
$text = $displayData['text'];
$margin = (strpos($doTask, 'index.php?option=com_config') === false) ? '' : ' ml-auto';

if (!empty($displayData['task']))
{
$task = ' task="' . $displayData['task'] . '"';
}
elseif (!empty($displayData['doTask']))
{
$task = ' execute="location.href=\'' . $displayData['doTask'] . '\';"';
}

$margin = (strpos($task, 'index.php?option=com_config') === false) ? '' : 'ml-auto';

if ($margin):
?>
<button<?php echo $id; ?> class="btn btn-outline-danger btn-sm<?php echo $margin; ?>" onclick="location.href='<?php echo $doTask; ?>';">
<span class="<?php echo $class; ?>" aria-hidden="true"></span>
<?php echo $text; ?>
</button>
<div class="<?php echo $margin; ?>"></div>
<?php endif; ?>
<joomla-toolbar-button <?php echo $id.$task; ?>>
<button type="button" class="btn btn-outline-danger btn-sm">
<span class="<?php echo trim($class); ?>" aria-hidden="true"></span>
<?php echo $text; ?>
</button>
</joomla-toolbar-button>
35 changes: 22 additions & 13 deletions layouts/joomla/toolbar/standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,32 @@
use Joomla\CMS\HTML\HTMLHelper;

HTMLHelper::_('behavior.core');
HTMLHelper::_('webcomponent', ['joomla-toolbar-button' => 'system/webcomponents/joomla-toolbar-button.min.js'], ['relative' => true, 'version' => 'auto', 'detectBrowser' => false, 'detectDebug' => true]);

$id = isset($displayData['id']) ? $displayData['id'] : '';
$doTask = $displayData['doTask'];
$class = $displayData['class'];
$text = $displayData['text'];
$btnClass = $displayData['btnClass'];
$group = $displayData['group'];
?>
$task = '';
$list = !empty($displayData['list']) ? ' list-selection' : '';
$form = !empty($displayData['form']) ? ' form="' . $displayData['form'] . '"' : '';
$validate = !empty($displayData['validate']) ? ' form-validation' : '';
$msg = !empty($displayData['msg']) ? ' confirm-message="' . $this->escape($displayData['msg']) . '"' : '';

if (!empty($displayData['task']))
{
$task = ' task="' . $displayData['task'] . '"';
}
elseif (!empty($displayData['doTask']))
{
$task = ' execute="' . $displayData['doTask'] . '"';
}

<?php if ($group) : ?>
<a<?php echo $id; ?> href="#" onclick="<?php echo $doTask; ?>" class="dropdown-item">
<span class="<?php echo trim($class); ?>"></span>
<?php echo $text; ?>
</a>
<?php else : ?>
<button<?php echo $id; ?> onclick="<?php echo $doTask; ?>" class="<?php echo $btnClass; ?>">
<span class="<?php echo trim($class); ?>" aria-hidden="true"></span>
<?php echo $text; ?>
</button>
<?php endif; ?>
?>
<joomla-toolbar-button <?php echo $id.$task.$list.$form.$validate.$msg; ?>>
<button type="button" class="<?php echo $btnClass; ?>">
<span class="<?php echo trim($class); ?>" aria-hidden="true"></span>
<?php echo $text; ?>
</button>
</joomla-toolbar-button>
33 changes: 22 additions & 11 deletions libraries/src/Toolbar/Button/ApplyButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,35 @@ class ApplyButton extends ToolbarButton
/**
* Fetch the HTML for the button
*
* @param string $type Unused string.
* @param string $name The name of the button icon class.
* @param string $text Button text.
* @param string $task Task associated with the button.
* @param boolean $list True to allow lists
* @param string $type Unused string.
* @param string $name The name of the button icon class.
* @param string $text Button text.
* @param string $task Task associated with the button.
* @param boolean $list True to allow lists
* @param boolean $group Does the button belong to a group?
* @param string $form The form CSS selector eg '#adminForm'
* @param bool $formValidation Whether should be called the form validation before task call
*
* @return string HTML string for the button
*
* @since 4.0.0
*/
public function fetchButton($type = 'Apply', $name = '', $text = '', $task = '', $list = true)
public function fetchButton($type = 'Apply', $name = '', $text = '', $task = '', $list = true,
$group = false, $form = '', $formValidation = false)
{
// Store all data to the options array for use with JLayout
$options = array();
$options['text'] = \JText::_($text);
$options['class'] = $this->fetchIconClass($name);
$options['doTask'] = $this->_getCommand($options['text'], $task, $list);
$options['id'] = $this->fetchId('Apply', $name);
$options['text'] = \JText::_($text);
$options['class'] = $this->fetchIconClass($name);
$options['id'] = $this->fetchId('Apply', $name);
$options['task'] = $task;
$options['list'] = $list;
$options['group'] = $group;
$options['form'] = $form;
$options['validate'] = $formValidation;

// The 'doTask' option stays for B/C
$options['doTask'] = $this->_getCommand($options['text'], $task, $list);

if ($options['id'])
{
Expand Down Expand Up @@ -87,7 +98,7 @@ public function fetchButton($type = 'Apply', $name = '', $text = '', $task = '',
*/
public function fetchId($type = 'Apply', $name = '', $text = '', $task = '', $list = true, $hideMenu = false)
{
return $this->_parent->getName() . '-' . $name;
return $this->ensureUniqueId($this->_parent->getName() . '-' . $name);
}

/**
Expand Down
Loading