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

Add actions column to grids #14806

Merged
merged 2 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions _build/templates/default/sass/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,21 @@ a.x-grid-link:focus {
text-decoration: underline;
}

.x-grid-buttons {
text-align: center;
}
.x-grid-buttons li {
cursor: pointer;
display: inline-block;
font-size: 1.1em;
line-height: .7;
margin-right: 10px;
}

.x-grid-buttons li:last-child {
margin-right: 0;
}

/* panel stylings */
.modx-page-header,
.modx-page-header div {
Expand Down
1 change: 1 addition & 0 deletions core/lexicon/en/default.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@
$_lang['newest'] = 'Newest';
$_lang['oldest'] = 'Oldest';
$_lang['constraints'] = 'Constraints';
$_lang['context_menu'] = 'Context Menu';

$_lang['january'] = 'January';
$_lang['february'] = 'February';
Expand Down
149 changes: 149 additions & 0 deletions manager/assets/modext/widgets/core/modx.grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ MODx.grid.Grid = function(config) {
,preventSaveRefresh: true
,showPerPage: true
,stateful: false
,showActionsColumn: true
,actionsColumnWidth: 50
,disableContextMenuAction: false
,menuConfig: {
defaultAlign: 'tl-b?'
,enableScrolling: false
Expand Down Expand Up @@ -91,6 +94,23 @@ MODx.grid.Grid = function(config) {
if (!itm.scope) { itm.scope = this; }
}
}

if (config.showActionsColumn) {
if (config.columns && Array.isArray(config.columns)) {
config.columns.push({
width: 50
,renderer: this.actionsColumnRenderer.bind(this)
});
}

if (config.cm && config.cm.columns && Array.isArray(config.cm.columns)) {
config.cm.columns.push({
width: 50
,renderer: this.actionsColumnRenderer.bind(this)
});
}
}

MODx.grid.Grid.superclass.constructor.call(this,config);
this._loadMenu(config);
this.addEvents('beforeRemoveRow','afterRemoveRow','afterAutoSave');
Expand Down Expand Up @@ -574,6 +594,63 @@ Ext.extend(MODx.grid.Grid,Ext.grid.EditorGridPanel,{

return plugins[index];
}

,_getActionsColumnTpl: function () {
return new Ext.XTemplate('<tpl for=".">'
+ '<tpl if="actions !== null">'
+ '<ul class="x-grid-buttons">'
+ '<tpl for="actions">'
+ '<li><i class="x-grid-action icon icon-{icon:htmlEncode}" title="{text:htmlEncode}" data-action="{action:htmlEncode}"></i></li>'
+ '</tpl>'
+ '</ul>'
+ '</tpl>'
+ '</tpl>', {
compiled: true
});
}

,actionsColumnRenderer: function(value, metaData, record, rowIndex, colIndex, store) {
var actions = this.getActions.apply(this, [record, rowIndex, colIndex, store]);
if (this.config.disableContextMenuAction !== true) {
actions.push({
text: _('context_menu'),
action: 'contextMenu',
icon: 'gear'
});
}

return this._getActionsColumnTpl().apply({
actions: actions
});
}

,getActions: function(record, rowIndex, colIndex, store) {
return [];
}

,onClick: function(e) {
var target = e.getTarget();
if (!target.classList.contains('x-grid-action')) return;
if (!target.dataset.action) return;

var actionHandler = 'action' + target.dataset.action.charAt(0).toUpperCase() + target.dataset.action.slice(1);
if (!this[actionHandler] || (typeof this[actionHandler] !== 'function')) {
actionHandler = target.dataset.action;
if (!this[actionHandler] || (typeof this[actionHandler] !== 'function')) {
return;
}
}

var record = this.getSelectionModel().getSelected();
var recordIndex = this.store.indexOf(record);
this.menu.record = record.data;

this[actionHandler](record, recordIndex, e);
},

actionContextMenu: function(record, recordIndex, e) {
this._showMenu(this, recordIndex, e);
}
});

/* local grid */
Expand Down Expand Up @@ -611,6 +688,9 @@ MODx.grid.LocalGrid = function(config) {
,enableColumnMove: true
,header: false
,cls: 'modx-grid'
,showActionsColumn: true
,actionsColumnWidth: 50
,disableContextMenuAction: false
,viewConfig: {
forceFit: true
,enableRowBody: true
Expand All @@ -625,6 +705,17 @@ MODx.grid.LocalGrid = function(config) {
this.menu = new Ext.menu.Menu(config.menuConfig);
this.config = config;
this._loadColumnModel();

if (config.showActionsColumn && config.columns && Array.isArray(config.columns)) {
config.columns.push({
width: 50
,renderer: {
fn: this.actionsColumnRenderer,
scope: this
}
});
}

MODx.grid.LocalGrid.superclass.constructor.call(this,config);
this.addEvents({
beforeRemoveRow: true
Expand Down Expand Up @@ -910,6 +1001,64 @@ Ext.extend(MODx.grid.LocalGrid,Ext.grid.EditorGridPanel,{
}
return z;
}

,_getActionsColumnTpl: function () {
return new Ext.XTemplate('<tpl for=".">'
+ '<tpl if="actions !== null">'
+ '<ul class="x-grid-buttons">'
+ '<tpl for="actions">'
+ '<li><i class="x-grid-action icon icon-{icon:htmlEncode}" title="{text:htmlEncode}" data-action="{action:htmlEncode}"></i></li>'
+ '</tpl>'
+ '</ul>'
+ '</tpl>'
+ '</tpl>', {
compiled: true
});
}

,actionsColumnRenderer: function(value, metaData, record, rowIndex, colIndex, store) {
var actions = this.getActions.apply(this, arguments);

if (this.config.disableContextMenuAction !== true) {
actions.push({
text: _('context_menu'),
action: 'contextMenu',
icon: 'gear'
});
}

return this._getActionsColumnTpl().apply({
actions: actions
});
}

,getActions: function(value, metaData, record, rowIndex, colIndex, store) {
return [];
}

,onClick: function(e) {
var target = e.getTarget();
if (!target.classList.contains('x-grid-action')) return;
if (!target.dataset.action) return;

var actionHandler = 'action' + target.dataset.action.charAt(0).toUpperCase() + target.dataset.action.slice(1);
if (!this[actionHandler] || (typeof this[actionHandler] !== 'function')) {
actionHandler = target.dataset.action;
if (!this[actionHandler] || (typeof this[actionHandler] !== 'function')) {
return;
}
}

var record = this.getSelectionModel().getSelected();
var recordIndex = this.store.indexOf(record);
this.menu.record = record.data;

this[actionHandler](record, recordIndex, e);
},

actionContextMenu: function(record, recordIndex, e) {
this._showMenu(this, recordIndex, e);
}
});
Ext.reg('grid-local',MODx.grid.LocalGrid);
Ext.reg('modx-grid-local',MODx.grid.LocalGrid);
Expand Down
25 changes: 25 additions & 0 deletions manager/assets/modext/widgets/system/modx.grid.context.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ Ext.extend(MODx.grid.Context,MODx.grid.Grid,{
,scope: this
});
}

if (p.indexOf('pedit') != -1) {
m.push({
text: _('context_update')
,handler: this.updateContext
});
}

if (p.indexOf('premove') != -1) {
m.push('-');
m.push({
Expand Down Expand Up @@ -236,6 +238,29 @@ Ext.extend(MODx.grid.Context,MODx.grid.Grid,{
this.getSelectionModel().clearSelections(true);
this.refresh();
}

,getActions: function(record, rowIndex, colIndex, store) {
var permissions = record.data.perm;
var actions = [];

if (~permissions.indexOf('pedit')) {
actions.push({
action: 'updateContext',
icon: 'pencil-square-o',
text: _('context_update')
});
}

if (~permissions.indexOf('premove')) {
actions.push({
action: 'remove',
icon: 'trash-o',
text: _('context_remove')
});
}

return actions;
}
});
Ext.reg('modx-grid-contexts',MODx.grid.Context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ MODx.grid.SystemEvent = function(config) {
,groupBy: 'groupname'
,singleText: _('system_event')
,pluralText: _('system_events')
,showActionsColumn: false
,columns: [{
header: _('name')
,dataIndex: 'name'
Expand Down