| 1 |
/* eslint-disable @typescript-eslint/no-empty-function */ |
| 2 |
// Copyright (c) Jupyter Development Team. |
| 3 |
// Distributed under the terms of the Modified BSD License. |
| 4 |
|
| 5 |
const CommandRegistry = lumino_commands.CommandRegistry; |
| 6 |
const BoxPanel = lumino_widgets.BoxPanel; |
| 7 |
const CommandPalette = lumino_widgets.CommandPalette; |
| 8 |
const ContextMenu = lumino_widgets.ContextMenu; |
| 9 |
const DockPanel = lumino_widgets.DockPanel; |
| 10 |
const Menu = lumino_widgets.Menu; |
| 11 |
const MenuBar = lumino_widgets.MenuBar; |
| 12 |
const Widget = lumino_widgets.Widget; |
| 13 |
|
| 14 |
const commands = new CommandRegistry(); |
| 15 |
|
| 16 |
function createMenu() { |
| 17 |
let sub1 = new Menu({ commands: commands }); |
| 18 |
sub1.title.label = 'More...'; |
| 19 |
sub1.title.mnemonic = 0; |
| 20 |
sub1.addItem({ command: 'example:one' }); |
| 21 |
sub1.addItem({ command: 'example:two' }); |
| 22 |
sub1.addItem({ command: 'example:three' }); |
| 23 |
sub1.addItem({ command: 'example:four' }); |
| 24 |
|
| 25 |
let sub2 = new Menu({ commands: commands }); |
| 26 |
sub2.title.label = 'More...'; |
| 27 |
sub2.title.mnemonic = 0; |
| 28 |
sub2.addItem({ command: 'example:one' }); |
| 29 |
sub2.addItem({ command: 'example:two' }); |
| 30 |
sub2.addItem({ command: 'example:three' }); |
| 31 |
sub2.addItem({ command: 'example:four' }); |
| 32 |
sub2.addItem({ type: 'submenu', submenu: sub1 }); |
| 33 |
|
| 34 |
let root = new Menu({ commands: commands }); |
| 35 |
root.addItem({ command: 'example:copy' }); |
| 36 |
root.addItem({ command: 'example:cut' }); |
| 37 |
root.addItem({ command: 'example:paste' }); |
| 38 |
root.addItem({ type: 'separator' }); |
| 39 |
root.addItem({ command: 'example:new-tab' }); |
| 40 |
root.addItem({ command: 'example:close-tab' }); |
| 41 |
root.addItem({ command: 'example:save-on-exit' }); |
| 42 |
root.addItem({ type: 'separator' }); |
| 43 |
root.addItem({ command: 'example:open-task-manager' }); |
| 44 |
root.addItem({ type: 'separator' }); |
| 45 |
root.addItem({ type: 'submenu', submenu: sub2 }); |
| 46 |
root.addItem({ type: 'separator' }); |
| 47 |
root.addItem({ command: 'example:close' }); |
| 48 |
|
| 49 |
return root; |
| 50 |
} |
| 51 |
|
| 52 |
class ContentWidget extends Widget { |
| 53 |
constructor(name) { |
| 54 |
super({ node: ContentWidget.prototype.createNode() }); |
| 55 |
this.setFlag(Widget.Flag.DisallowLayout); |
| 56 |
this.addClass('content'); |
| 57 |
this.addClass(name.toLowerCase()); |
| 58 |
this.title.label = name; |
| 59 |
this.title.closable = true; |
| 60 |
this.title.caption = 'Long description for: ' + name; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
ContentWidget.prototype = Object.create(Widget.prototype); |
| 65 |
|
| 66 |
ContentWidget.prototype.createNode = function () { |
| 67 |
let node = document.createElement('div'); |
| 68 |
let content = document.createElement('div'); |
| 69 |
let input = document.createElement('input'); |
| 70 |
input.placeholder = 'Placeholder...'; |
| 71 |
content.appendChild(input); |
| 72 |
node.appendChild(content); |
| 73 |
return node; |
| 74 |
}; |
| 75 |
|
| 76 |
ContentWidget.prototype.inputNode = function () { |
| 77 |
return this.node.getElementsByTagName('input')[0]; |
| 78 |
}; |
| 79 |
|
| 80 |
ContentWidget.prototype.onActivateRequest = function (msg) { |
| 81 |
if (this.isAttached) { |
| 82 |
this.inputNode().focus(); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
function main() { |
| 87 |
commands.addCommand('example:cut', { |
| 88 |
label: 'Cut', |
| 89 |
mnemonic: 1, |
| 90 |
iconClass: 'fa fa-cut', |
| 91 |
execute: function () { |
| 92 |
console.log('Cut'); |
| 93 |
} |
| 94 |
}); |
| 95 |
|
| 96 |
commands.addCommand('example:copy', { |
| 97 |
label: 'Copy File', |
| 98 |
mnemonic: 0, |
| 99 |
iconClass: 'fa fa-copy', |
| 100 |
execute: function () { |
| 101 |
console.log('Copy'); |
| 102 |
} |
| 103 |
}); |
| 104 |
|
| 105 |
commands.addCommand('example:paste', { |
| 106 |
label: 'Paste', |
| 107 |
mnemonic: 0, |
| 108 |
iconClass: 'fa fa-paste', |
| 109 |
execute: function () { |
| 110 |
console.log('Paste'); |
| 111 |
} |
| 112 |
}); |
| 113 |
|
| 114 |
commands.addCommand('example:new-tab', { |
| 115 |
label: 'New Tab', |
| 116 |
mnemonic: 0, |
| 117 |
caption: 'Open a new tab', |
| 118 |
execute: function () { |
| 119 |
console.log('New Tab'); |
| 120 |
} |
| 121 |
}); |
| 122 |
|
| 123 |
commands.addCommand('example:close-tab', { |
| 124 |
label: 'Close Tab', |
| 125 |
mnemonic: 2, |
| 126 |
caption: 'Close the current tab', |
| 127 |
execute: function () { |
| 128 |
console.log('Close Tab'); |
| 129 |
} |
| 130 |
}); |
| 131 |
|
| 132 |
commands.addCommand('example:save-on-exit', { |
| 133 |
label: 'Save on Exit', |
| 134 |
mnemonic: 0, |
| 135 |
caption: 'Toggle the save on exit flag', |
| 136 |
execute: function () { |
| 137 |
console.log('Save on Exit'); |
| 138 |
} |
| 139 |
}); |
| 140 |
|
| 141 |
commands.addCommand('example:open-task-manager', { |
| 142 |
label: 'Task Manager', |
| 143 |
mnemonic: 5, |
| 144 |
isEnabled: function () { |
| 145 |
return false; |
| 146 |
}, |
| 147 |
execute: function () {} |
| 148 |
}); |
| 149 |
|
| 150 |
commands.addCommand('example:close', { |
| 151 |
label: 'Close', |
| 152 |
mnemonic: 0, |
| 153 |
iconClass: 'fa fa-close', |
| 154 |
execute: function () { |
| 155 |
console.log('Close'); |
| 156 |
} |
| 157 |
}); |
| 158 |
|
| 159 |
commands.addCommand('example:one', { |
| 160 |
label: 'One', |
| 161 |
execute: function () { |
| 162 |
console.log('One'); |
| 163 |
} |
| 164 |
}); |
| 165 |
|
| 166 |
commands.addCommand('example:two', { |
| 167 |
label: 'Two', |
| 168 |
execute: function () { |
| 169 |
console.log('Two'); |
| 170 |
} |
| 171 |
}); |
| 172 |
|
| 173 |
commands.addCommand('example:three', { |
| 174 |
label: 'Three', |
| 175 |
execute: function () { |
| 176 |
console.log('Three'); |
| 177 |
} |
| 178 |
}); |
| 179 |
|
| 180 |
commands.addCommand('example:four', { |
| 181 |
label: 'Four', |
| 182 |
execute: function () { |
| 183 |
console.log('Four'); |
| 184 |
} |
| 185 |
}); |
| 186 |
|
| 187 |
commands.addCommand('example:black', { |
| 188 |
label: 'Black', |
| 189 |
execute: function () { |
| 190 |
console.log('Black'); |
| 191 |
} |
| 192 |
}); |
| 193 |
|
| 194 |
commands.addCommand('example:clear-cell', { |
| 195 |
label: 'Clear Cell', |
| 196 |
execute: function () { |
| 197 |
console.log('Clear Cell'); |
| 198 |
} |
| 199 |
}); |
| 200 |
|
| 201 |
commands.addCommand('example:cut-cells', { |
| 202 |
label: 'Cut Cell(s)', |
| 203 |
execute: function () { |
| 204 |
console.log('Cut Cell(s)'); |
| 205 |
} |
| 206 |
}); |
| 207 |
|
| 208 |
commands.addCommand('example:run-cell', { |
| 209 |
label: 'Run Cell', |
| 210 |
execute: function () { |
| 211 |
console.log('Run Cell'); |
| 212 |
} |
| 213 |
}); |
| 214 |
|
| 215 |
commands.addCommand('example:cell-test', { |
| 216 |
label: 'Cell Test', |
| 217 |
execute: function () { |
| 218 |
console.log('Cell Test'); |
| 219 |
} |
| 220 |
}); |
| 221 |
|
| 222 |
commands.addCommand('notebook:new', { |
| 223 |
label: 'New Notebook', |
| 224 |
execute: function () { |
| 225 |
console.log('New Notebook'); |
| 226 |
} |
| 227 |
}); |
| 228 |
|
| 229 |
commands.addKeyBinding({ |
| 230 |
keys: ['Accel X'], |
| 231 |
selector: 'body', |
| 232 |
command: 'example:cut' |
| 233 |
}); |
| 234 |
|
| 235 |
commands.addKeyBinding({ |
| 236 |
keys: ['Accel C'], |
| 237 |
selector: 'body', |
| 238 |
command: 'example:copy' |
| 239 |
}); |
| 240 |
|
| 241 |
commands.addKeyBinding({ |
| 242 |
keys: ['Accel V'], |
| 243 |
selector: 'body', |
| 244 |
command: 'example:paste' |
| 245 |
}); |
| 246 |
|
| 247 |
commands.addKeyBinding({ |
| 248 |
keys: ['Accel J', 'Accel J'], |
| 249 |
selector: 'body', |
| 250 |
command: 'example:new-tab' |
| 251 |
}); |
| 252 |
|
| 253 |
commands.addKeyBinding({ |
| 254 |
keys: ['Accel M'], |
| 255 |
selector: 'body', |
| 256 |
command: 'example:open-task-manager' |
| 257 |
}); |
| 258 |
|
| 259 |
let menu1 = createMenu(); |
| 260 |
menu1.title.label = 'File'; |
| 261 |
menu1.title.mnemonic = 0; |
| 262 |
|
| 263 |
let menu2 = createMenu(); |
| 264 |
menu2.title.label = 'Edit'; |
| 265 |
menu2.title.mnemonic = 0; |
| 266 |
|
| 267 |
let menu3 = createMenu(); |
| 268 |
menu3.title.label = 'View'; |
| 269 |
menu3.title.mnemonic = 0; |
| 270 |
|
| 271 |
let bar = new MenuBar(); |
| 272 |
bar.addMenu(menu1); |
| 273 |
bar.addMenu(menu2); |
| 274 |
bar.addMenu(menu3); |
| 275 |
bar.id = 'menuBar'; |
| 276 |
|
| 277 |
let palette = new CommandPalette({ commands: commands }); |
| 278 |
palette.addItem({ command: 'example:cut', category: 'Edit' }); |
| 279 |
palette.addItem({ command: 'example:copy', category: 'Edit' }); |
| 280 |
palette.addItem({ command: 'example:paste', category: 'Edit' }); |
| 281 |
palette.addItem({ command: 'example:one', category: 'Number' }); |
| 282 |
palette.addItem({ command: 'example:two', category: 'Number' }); |
| 283 |
palette.addItem({ command: 'example:three', category: 'Number' }); |
| 284 |
palette.addItem({ command: 'example:four', category: 'Number' }); |
| 285 |
palette.addItem({ command: 'example:black', category: 'Number' }); |
| 286 |
palette.addItem({ command: 'example:new-tab', category: 'File' }); |
| 287 |
palette.addItem({ command: 'example:close-tab', category: 'File' }); |
| 288 |
palette.addItem({ command: 'example:save-on-exit', category: 'File' }); |
| 289 |
palette.addItem({ command: 'example:open-task-manager', category: 'File' }); |
| 290 |
palette.addItem({ command: 'example:close', category: 'File' }); |
| 291 |
palette.addItem({ |
| 292 |
command: 'example:clear-cell', |
| 293 |
category: 'Notebook Cell Operations' |
| 294 |
}); |
| 295 |
palette.addItem({ |
| 296 |
command: 'example:cut-cells', |
| 297 |
category: 'Notebook Cell Operations' |
| 298 |
}); |
| 299 |
palette.addItem({ |
| 300 |
command: 'example:run-cell', |
| 301 |
category: 'Notebook Cell Operations' |
| 302 |
}); |
| 303 |
palette.addItem({ command: 'example:cell-test', category: 'Console' }); |
| 304 |
palette.addItem({ command: 'notebook:new', category: 'Notebook' }); |
| 305 |
palette.id = 'palette'; |
| 306 |
|
| 307 |
let contextMenu = new ContextMenu({ commands: commands }); |
| 308 |
|
| 309 |
document.addEventListener('contextmenu', function (event) { |
| 310 |
if (contextMenu.open(event)) { |
| 311 |
event.preventDefault(); |
| 312 |
} |
| 313 |
}); |
| 314 |
|
| 315 |
contextMenu.addItem({ command: 'example:cut', selector: '.content' }); |
| 316 |
contextMenu.addItem({ command: 'example:copy', selector: '.content' }); |
| 317 |
contextMenu.addItem({ command: 'example:paste', selector: '.content' }); |
| 318 |
|
| 319 |
contextMenu.addItem({ |
| 320 |
command: 'example:one', |
| 321 |
selector: '.lm-CommandPalette' |
| 322 |
}); |
| 323 |
contextMenu.addItem({ |
| 324 |
command: 'example:two', |
| 325 |
selector: '.lm-CommandPalette' |
| 326 |
}); |
| 327 |
contextMenu.addItem({ |
| 328 |
command: 'example:three', |
| 329 |
selector: '.lm-CommandPalette' |
| 330 |
}); |
| 331 |
contextMenu.addItem({ |
| 332 |
command: 'example:four', |
| 333 |
selector: '.lm-CommandPalette' |
| 334 |
}); |
| 335 |
contextMenu.addItem({ |
| 336 |
command: 'example:black', |
| 337 |
selector: '.lm-CommandPalette' |
| 338 |
}); |
| 339 |
|
| 340 |
contextMenu.addItem({ |
| 341 |
command: 'notebook:new', |
| 342 |
selector: '.lm-CommandPalette-input' |
| 343 |
}); |
| 344 |
contextMenu.addItem({ |
| 345 |
command: 'example:save-on-exit', |
| 346 |
selector: '.lm-CommandPalette-input' |
| 347 |
}); |
| 348 |
contextMenu.addItem({ |
| 349 |
command: 'example:open-task-manager', |
| 350 |
selector: '.lm-CommandPalette-input' |
| 351 |
}); |
| 352 |
contextMenu.addItem({ |
| 353 |
type: 'separator', |
| 354 |
selector: '.lm-CommandPalette-input' |
| 355 |
}); |
| 356 |
|
| 357 |
document.addEventListener('keydown', function (event) { |
| 358 |
commands.processKeydownEvent(event); |
| 359 |
}); |
| 360 |
|
| 361 |
let r1 = new ContentWidget('Red'); |
| 362 |
let b1 = new ContentWidget('Blue'); |
| 363 |
let g1 = new ContentWidget('Green'); |
| 364 |
let y1 = new ContentWidget('Yellow'); |
| 365 |
|
| 366 |
let r2 = new ContentWidget('Red'); |
| 367 |
let b2 = new ContentWidget('Blue'); |
| 368 |
// let g2 = new ContentWidget('Green'); |
| 369 |
// let y2 = new ContentWidget('Yellow'); |
| 370 |
|
| 371 |
let dock = new DockPanel(); |
| 372 |
dock.addWidget(r1); |
| 373 |
dock.addWidget(b1, { mode: 'split-right', ref: r1 }); |
| 374 |
dock.addWidget(y1, { mode: 'split-bottom', ref: b1 }); |
| 375 |
dock.addWidget(g1, { mode: 'split-left', ref: y1 }); |
| 376 |
dock.addWidget(r2, { ref: b1 }); |
| 377 |
dock.addWidget(b2, { mode: 'split-right', ref: y1 }); |
| 378 |
dock.id = 'dock'; |
| 379 |
|
| 380 |
let savedLayouts = []; |
| 381 |
|
| 382 |
commands.addCommand('save-dock-layout', { |
| 383 |
label: 'Save Layout', |
| 384 |
caption: 'Save the current dock layout', |
| 385 |
execute: function () { |
| 386 |
savedLayouts.push(dock.saveLayout()); |
| 387 |
palette.addItem({ |
| 388 |
command: 'restore-dock-layout', |
| 389 |
category: 'Dock Layout', |
| 390 |
args: { index: savedLayouts.length - 1 } |
| 391 |
}); |
| 392 |
} |
| 393 |
}); |
| 394 |
|
| 395 |
commands.addCommand('restore-dock-layout', { |
| 396 |
label: function (args) { |
| 397 |
return 'Restore Layout ' + args.index; |
| 398 |
}, |
| 399 |
execute: function (args) { |
| 400 |
dock.restoreLayout(savedLayouts[args.index]); |
| 401 |
} |
| 402 |
}); |
| 403 |
|
| 404 |
palette.addItem({ |
| 405 |
command: 'save-dock-layout', |
| 406 |
category: 'Dock Layout', |
| 407 |
rank: 0 |
| 408 |
}); |
| 409 |
|
| 410 |
BoxPanel.setStretch(dock, 1); |
| 411 |
|
| 412 |
let main = new BoxPanel({ direction: 'left-to-right', spacing: 0 }); |
| 413 |
main.id = 'main'; |
| 414 |
main.addWidget(palette); |
| 415 |
main.addWidget(dock); |
| 416 |
|
| 417 |
window.onresize = function () { |
| 418 |
main.update(); |
| 419 |
}; |
| 420 |
|
| 421 |
Widget.attach(bar, document.body); |
| 422 |
Widget.attach(main, document.body); |
| 423 |
} |
| 424 |
|
| 425 |
window.onload = main; |