| 1 |
import { DataGrid } from '@lumino/datagrid'; |
| 2 |
|
| 3 |
import { DataModel } from '@lumino/datagrid'; |
| 4 |
|
| 5 |
import { CommandRegistry } from '@lumino/commands'; |
| 6 |
|
| 7 |
import { Menu, Widget } from '@lumino/widgets'; |
| 8 |
|
| 9 |
/** |
| 10 |
* An object which provides context menus for the data grid. |
| 11 |
* |
| 12 |
* #### Notes |
| 13 |
* This item creates one Menu widget, then changes it's items based on |
| 14 |
* the `cellClick` signal from `DataGrid`. |
| 15 |
*/ |
| 16 |
export abstract class GridContextMenu { |
| 17 |
/** |
| 18 |
* Construct a new grid context menu. |
| 19 |
* |
| 20 |
* @param options - The options for initializing the context menu. |
| 21 |
*/ |
| 22 |
constructor(options: GridContextMenu.IOptions) { |
| 23 |
this._menu = new Menu({ commands: options.commands }); |
| 24 |
this._menu.addClass('ipydatagrid-context-menu'); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Opens the context menu in response to the `cellClick` signal of the |
| 29 |
* data grid. |
| 30 |
* |
| 31 |
* @param grid - The "sender" of the signal. |
| 32 |
* |
| 33 |
* @param hit - The "value" of the signal. |
| 34 |
*/ |
| 35 |
abstract open(grid: DataGrid, hit: DataGrid.HitTestResult): void; |
| 36 |
|
| 37 |
/** |
| 38 |
* The menu widget which displays the relevant context items. |
| 39 |
*/ |
| 40 |
protected readonly _menu: Menu; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* The namespace for the `GridContextMenu` class statics. |
| 45 |
*/ |
| 46 |
export namespace GridContextMenu { |
| 47 |
/** |
| 48 |
* An options object for initializing a data grid. |
| 49 |
*/ |
| 50 |
export interface IOptions { |
| 51 |
/** |
| 52 |
* The data grid the context menu should be attached to. |
| 53 |
*/ |
| 54 |
grid: DataGrid; |
| 55 |
|
| 56 |
/** |
| 57 |
* The command registry to use with the context menu. |
| 58 |
*/ |
| 59 |
commands: CommandRegistry; |
| 60 |
|
| 61 |
/** |
| 62 |
* The custom renderer to use to render menu items. |
| 63 |
*/ |
| 64 |
renderer?: Menu.Renderer; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* An WIP object which provides context menus for the data grid. |
| 70 |
* |
| 71 |
* #### Notes |
| 72 |
* This is primarily here for demo purposes to demonstrate how we may want to |
| 73 |
* manage context menus. |
| 74 |
*/ |
| 75 |
export class FeatherGridContextMenu extends GridContextMenu { |
| 76 |
/** |
| 77 |
* Opens the context menu in response to the `cellClick` signal of the |
| 78 |
* data grid. |
| 79 |
* |
| 80 |
* @param grid - The "sender" of the signal. |
| 81 |
* |
| 82 |
* @param hit - The "value" of the signal. |
| 83 |
*/ |
| 84 |
open(grid: DataGrid, hit: DataGrid.HitTestResult): void { |
| 85 |
// Discard the current menu items. |
| 86 |
this._menu.clearItems(); |
| 87 |
|
| 88 |
// Create the args that will be provided to the commands' .execute() method |
| 89 |
const args: FeatherGridContextMenu.CommandArgs = { |
| 90 |
region: hit.region as DataModel.CellRegion, |
| 91 |
rowIndex: hit.row, |
| 92 |
columnIndex: hit.column, |
| 93 |
clientX: hit.x, |
| 94 |
clientY: hit.y, |
| 95 |
}; |
| 96 |
|
| 97 |
// Add menu items based on the region of the grid that was clicked on. |
| 98 |
switch (hit.region) { |
| 99 |
case 'column-header': |
| 100 |
this._menu.addItem({ |
| 101 |
command: FeatherGridContextMenu.CommandID.SortAscending, |
| 102 |
args: args, |
| 103 |
}); |
| 104 |
this._menu.addItem({ |
| 105 |
command: FeatherGridContextMenu.CommandID.SortDescending, |
| 106 |
args: args, |
| 107 |
}); |
| 108 |
this._menu.addItem({ |
| 109 |
command: FeatherGridContextMenu.CommandID.SortClear, |
| 110 |
args: args, |
| 111 |
}); |
| 112 |
this._menu.addItem({ |
| 113 |
type: 'separator', |
| 114 |
}); |
| 115 |
this._menu.addItem({ |
| 116 |
command: FeatherGridContextMenu.CommandID.OpenFilterByConditionDialog, |
| 117 |
args: args, |
| 118 |
}); |
| 119 |
this._menu.addItem({ |
| 120 |
command: FeatherGridContextMenu.CommandID.OpenFilterByValueDialog, |
| 121 |
args: args, |
| 122 |
}); |
| 123 |
this._menu.addItem({ |
| 124 |
type: 'separator', |
| 125 |
}); |
| 126 |
this._menu.addItem({ |
| 127 |
command: FeatherGridContextMenu.CommandID.ClearSelection, |
| 128 |
args: args, |
| 129 |
}); |
| 130 |
this._menu.addItem({ |
| 131 |
command: FeatherGridContextMenu.CommandID.ClearThisFilter, |
| 132 |
args: args, |
| 133 |
}); |
| 134 |
this._menu.addItem({ |
| 135 |
command: FeatherGridContextMenu.CommandID.ClearFiltersInAllColumns, |
| 136 |
args: args, |
| 137 |
}); |
| 138 |
this._menu.addItem({ |
| 139 |
command: FeatherGridContextMenu.CommandID.CopyToClipboard, |
| 140 |
args: args, |
| 141 |
}); |
| 142 |
break; |
| 143 |
case 'corner-header': |
| 144 |
this._menu.addItem({ |
| 145 |
command: FeatherGridContextMenu.CommandID.SortAscending, |
| 146 |
args: args, |
| 147 |
}); |
| 148 |
this._menu.addItem({ |
| 149 |
command: FeatherGridContextMenu.CommandID.SortDescending, |
| 150 |
args: args, |
| 151 |
}); |
| 152 |
this._menu.addItem({ |
| 153 |
command: FeatherGridContextMenu.CommandID.SortClear, |
| 154 |
args: args, |
| 155 |
}); |
| 156 |
this._menu.addItem({ |
| 157 |
type: 'separator', |
| 158 |
}); |
| 159 |
this._menu.addItem({ |
| 160 |
command: FeatherGridContextMenu.CommandID.OpenFilterByConditionDialog, |
| 161 |
args: args, |
| 162 |
}); |
| 163 |
this._menu.addItem({ |
| 164 |
command: FeatherGridContextMenu.CommandID.OpenFilterByValueDialog, |
| 165 |
args: args, |
| 166 |
}); |
| 167 |
this._menu.addItem({ |
| 168 |
type: 'separator', |
| 169 |
}); |
| 170 |
this._menu.addItem({ |
| 171 |
command: FeatherGridContextMenu.CommandID.CopySelectionToClipboard, |
| 172 |
args: args, |
| 173 |
}); |
| 174 |
this._menu.addItem({ |
| 175 |
command: FeatherGridContextMenu.CommandID.SaveSelectionAsCsv, |
| 176 |
args: args, |
| 177 |
}); |
| 178 |
this._menu.addItem({ |
| 179 |
command: FeatherGridContextMenu.CommandID.SaveAllAsCsv, |
| 180 |
args: args, |
| 181 |
}); |
| 182 |
this._menu.addItem({ |
| 183 |
type: 'separator', |
| 184 |
}); |
| 185 |
this._menu.addItem({ |
| 186 |
command: FeatherGridContextMenu.CommandID.ClearSelection, |
| 187 |
args: args, |
| 188 |
}); |
| 189 |
this._menu.addItem({ |
| 190 |
command: FeatherGridContextMenu.CommandID.ClearThisFilter, |
| 191 |
args: args, |
| 192 |
}); |
| 193 |
this._menu.addItem({ |
| 194 |
command: FeatherGridContextMenu.CommandID.ClearFiltersInAllColumns, |
| 195 |
args: args, |
| 196 |
}); |
| 197 |
break; |
| 198 |
case 'body': |
| 199 |
this._menu.addItem({ |
| 200 |
command: FeatherGridContextMenu.CommandID.RevertGrid, |
| 201 |
args: args, |
| 202 |
}); |
| 203 |
break; |
| 204 |
default: |
| 205 |
throw 'unreachable'; |
| 206 |
} |
| 207 |
|
| 208 |
// Open context menu at location of the click event |
| 209 |
this._menu.open(hit.x, hit.y); |
| 210 |
|
| 211 |
// Issue 422: menu should be first child of document.body not last child to work on all of |
| 212 |
// Jupyter Lab, Notebook < 7, NbClassic and Voila. Until this is available in lumino/widgets, |
| 213 |
// detach and reattach the menu here. |
| 214 |
const bodyFirstChild = document.body.firstElementChild; |
| 215 |
if ( |
| 216 |
this._menu.node.parentElement == document.body && |
| 217 |
bodyFirstChild != this._menu.node |
| 218 |
) { |
| 219 |
Widget.detach(this._menu); |
| 220 |
Widget.attach(this._menu, document.body, bodyFirstChild as HTMLElement); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* The namespace for the `IPyDataGridContextMenu` class statics. |
| 227 |
*/ |
| 228 |
export namespace FeatherGridContextMenu { |
| 229 |
/** |
| 230 |
* An options object for initializing a context menu. |
| 231 |
*/ |
| 232 |
export interface IOptions { |
| 233 |
/** |
| 234 |
* The data grid to listen to clicks on. |
| 235 |
*/ |
| 236 |
grid: DataGrid; |
| 237 |
|
| 238 |
/** |
| 239 |
* The command registry used by the menu. |
| 240 |
*/ |
| 241 |
commands: CommandRegistry; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Command ID strings for the IpyDataGridContextMenu. |
| 246 |
*/ |
| 247 |
export enum CommandID { |
| 248 |
SortAscending = 'sort:Asc', |
| 249 |
SortDescending = 'sort:Desc', |
| 250 |
SortClear = 'sort:Clear', |
| 251 |
OpenFilterByConditionDialog = 'filterCondition:openDialog', |
| 252 |
OpenFilterByValueDialog = 'filterValue:openDialog', |
| 253 |
RevertGrid = 'grid:reset', |
| 254 |
ClearThisFilter = 'filter:clearCurrentColumn', |
| 255 |
ClearFiltersInAllColumns = 'filter:clearAllColumns', |
| 256 |
CopyToClipboard = 'copyToClipboard', |
| 257 |
CopySelectionToClipboard = 'copySelectionToClipboard', |
| 258 |
SaveSelectionAsCsv = 'saveSelectionAsCsv', |
| 259 |
SaveAllAsCsv = 'saveAllAsCsv', |
| 260 |
ClearSelection = 'clearSelection', |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Arguments to be provided to a command for execution. |
| 265 |
*/ |
| 266 |
export type CommandArgs = { |
| 267 |
region: DataModel.CellRegion; |
| 268 |
rowIndex: number; |
| 269 |
columnIndex: number; |
| 270 |
clientX: number; |
| 271 |
clientY: number; |
| 272 |
}; |
| 273 |
} |