| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
import { DOMWidgetView, ViewList, reject, } from '@jupyter-widgets/base'; |
| 4 |
import { BoxModel, BoxView } from './widget_box'; |
| 5 |
import { TabPanel } from './lumino/tabpanel'; |
| 6 |
import { Accordion } from './lumino/accordion'; |
| 7 |
import { Widget } from '@lumino/widgets'; |
| 8 |
import { each, ArrayExt } from '@lumino/algorithm'; |
| 9 |
import { MessageLoop } from '@lumino/messaging'; |
| 10 |
import $ from 'jquery'; |
| 11 |
export class SelectionContainerModel extends BoxModel { |
| 12 |
defaults() { |
| 13 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'SelectionContainerModel', selected_index: null, titles: [] }); |
| 14 |
} |
| 15 |
} |
| 16 |
export class AccordionModel extends SelectionContainerModel { |
| 17 |
defaults() { |
| 18 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'AccordionModel', _view_name: 'AccordionView' }); |
| 19 |
} |
| 20 |
} |
| 21 |
// We implement our own tab widget since Phoshpor's TabPanel uses an absolute |
| 22 |
// positioning BoxLayout, but we want a more an html/css-based Panel layout. |
| 23 |
export class JupyterLuminoAccordionWidget extends Accordion { |
| 24 |
constructor(options) { |
| 25 |
const view = options.view; |
| 26 |
delete options.view; |
| 27 |
super(options); |
| 28 |
this._view = view; |
| 29 |
} |
| 30 |
/** |
| 31 |
* Process the Lumino message. |
| 32 |
* |
| 33 |
* Any custom Lumino widget used inside a Jupyter widget should override |
| 34 |
* the processMessage function like this. |
| 35 |
*/ |
| 36 |
processMessage(msg) { |
| 37 |
var _a; |
| 38 |
super.processMessage(msg); |
| 39 |
(_a = this._view) === null || _a === void 0 ? void 0 : _a.processLuminoMessage(msg); |
| 40 |
} |
| 41 |
/** |
| 42 |
* Dispose the widget. |
| 43 |
* |
| 44 |
* This causes the view to be destroyed as well with 'remove' |
| 45 |
*/ |
| 46 |
dispose() { |
| 47 |
if (this.isDisposed) { |
| 48 |
return; |
| 49 |
} |
| 50 |
super.dispose(); |
| 51 |
this._view.remove(); |
| 52 |
this._view = null; |
| 53 |
} |
| 54 |
} |
| 55 |
export class AccordionView extends DOMWidgetView { |
| 56 |
_createElement(tagName) { |
| 57 |
this.luminoWidget = new JupyterLuminoAccordionWidget({ view: this }); |
| 58 |
return this.luminoWidget.node; |
| 59 |
} |
| 60 |
_setElement(el) { |
| 61 |
if (this.el || el !== this.luminoWidget.node) { |
| 62 |
// Accordions don't allow setting the element beyond the initial creation. |
| 63 |
throw new Error('Cannot reset the DOM element.'); |
| 64 |
} |
| 65 |
this.el = this.luminoWidget.node; |
| 66 |
this.$el = $(this.luminoWidget.node); |
| 67 |
} |
| 68 |
initialize(parameters) { |
| 69 |
super.initialize(parameters); |
| 70 |
this.children_views = new ViewList(this.add_child_view, this.remove_child_view, this); |
| 71 |
this.listenTo(this.model, 'change:children', () => this.updateChildren()); |
| 72 |
this.listenTo(this.model, 'change:selected_index', () => this.update_selected_index()); |
| 73 |
this.listenTo(this.model, 'change:titles', () => this.update_titles()); |
| 74 |
} |
| 75 |
/** |
| 76 |
* Called when view is rendered. |
| 77 |
*/ |
| 78 |
render() { |
| 79 |
var _a; |
| 80 |
super.render(); |
| 81 |
const accordion = this.luminoWidget; |
| 82 |
accordion.addClass('jupyter-widgets'); |
| 83 |
accordion.addClass('widget-accordion'); |
| 84 |
accordion.addClass('widget-container'); |
| 85 |
accordion.selection.selectionChanged.connect((sender) => { |
| 86 |
if (!this.updatingChildren) { |
| 87 |
this.model.set('selected_index', accordion.selection.index); |
| 88 |
this.touch(); |
| 89 |
} |
| 90 |
}); |
| 91 |
(_a = this.children_views) === null || _a === void 0 ? void 0 : _a.update(this.model.get('children')); |
| 92 |
this.update_titles(); |
| 93 |
this.update_selected_index(); |
| 94 |
} |
| 95 |
/** |
| 96 |
* Update children |
| 97 |
*/ |
| 98 |
updateChildren() { |
| 99 |
var _a; |
| 100 |
// While we are updating, the index may not be valid, so deselect the |
| 101 |
// tabs before updating so we don't get spurious changes in the index, |
| 102 |
// which would then set off another sync cycle. |
| 103 |
this.updatingChildren = true; |
| 104 |
this.luminoWidget.selection.index = null; |
| 105 |
(_a = this.children_views) === null || _a === void 0 ? void 0 : _a.update(this.model.get('children')); |
| 106 |
this.update_selected_index(); |
| 107 |
this.updatingChildren = false; |
| 108 |
} |
| 109 |
/** |
| 110 |
* Set header titles |
| 111 |
*/ |
| 112 |
update_titles() { |
| 113 |
const collapsed = this.luminoWidget.collapseWidgets; |
| 114 |
const titles = this.model.get('titles'); |
| 115 |
for (let i = 0; i < collapsed.length; i++) { |
| 116 |
if (titles[i] !== void 0) { |
| 117 |
collapsed[i].widget.title.label = titles[i]; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
/** |
| 122 |
* Make the rendering and selected index consistent. |
| 123 |
*/ |
| 124 |
update_selected_index() { |
| 125 |
this.luminoWidget.selection.index = this.model.get('selected_index'); |
| 126 |
} |
| 127 |
/** |
| 128 |
* Called when a child is removed from children list. |
| 129 |
*/ |
| 130 |
remove_child_view(view) { |
| 131 |
this.luminoWidget.removeWidget(view.luminoWidget); |
| 132 |
view.remove(); |
| 133 |
} |
| 134 |
/** |
| 135 |
* Called when a child is added to children list. |
| 136 |
*/ |
| 137 |
add_child_view(model, index) { |
| 138 |
// Placeholder widget to keep our position in the tab panel while we create the view. |
| 139 |
const accordion = this.luminoWidget; |
| 140 |
const placeholder = new Widget(); |
| 141 |
placeholder.title.label = this.model.get('titles')[index] || ''; |
| 142 |
accordion.addWidget(placeholder); |
| 143 |
return this.create_child_view(model) |
| 144 |
.then((view) => { |
| 145 |
const widget = view.luminoWidget; |
| 146 |
widget.title.label = placeholder.title.label; |
| 147 |
const collapse = accordion.collapseWidgets[accordion.indexOf(placeholder)]; |
| 148 |
collapse.widget = widget; |
| 149 |
placeholder.dispose(); |
| 150 |
return view; |
| 151 |
}) |
| 152 |
.catch(reject('Could not add child view to box', true)); |
| 153 |
} |
| 154 |
remove() { |
| 155 |
this.children_views = null; |
| 156 |
super.remove(); |
| 157 |
} |
| 158 |
} |
| 159 |
export class TabModel extends SelectionContainerModel { |
| 160 |
defaults() { |
| 161 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'TabModel', _view_name: 'TabView' }); |
| 162 |
} |
| 163 |
} |
| 164 |
// We implement our own tab widget since Phoshpor's TabPanel uses an absolute |
| 165 |
// positioning BoxLayout, but we want a more an html/css-based Panel layout. |
| 166 |
export class JupyterLuminoTabPanelWidget extends TabPanel { |
| 167 |
constructor(options) { |
| 168 |
const view = options.view; |
| 169 |
delete options.view; |
| 170 |
super(options); |
| 171 |
this._view = view; |
| 172 |
// We want the view's messages to be the messages the tabContents panel |
| 173 |
// gets. |
| 174 |
MessageLoop.installMessageHook(this.tabContents, (handler, msg) => { |
| 175 |
// There may be times when we want the view's handler to be called |
| 176 |
// *after* the message has been processed by the widget, in which |
| 177 |
// case we'll need to revisit using a message hook. |
| 178 |
this._view.processLuminoMessage(msg); |
| 179 |
return true; |
| 180 |
}); |
| 181 |
} |
| 182 |
/** |
| 183 |
* Dispose the widget. |
| 184 |
* |
| 185 |
* This causes the view to be destroyed as well with 'remove' |
| 186 |
*/ |
| 187 |
dispose() { |
| 188 |
if (this.isDisposed) { |
| 189 |
return; |
| 190 |
} |
| 191 |
super.dispose(); |
| 192 |
this._view.remove(); |
| 193 |
this._view = null; |
| 194 |
} |
| 195 |
} |
| 196 |
export class TabView extends DOMWidgetView { |
| 197 |
constructor() { |
| 198 |
super(...arguments); |
| 199 |
this.updatingTabs = false; |
| 200 |
} |
| 201 |
_createElement(tagName) { |
| 202 |
this.luminoWidget = new JupyterLuminoTabPanelWidget({ |
| 203 |
view: this, |
| 204 |
}); |
| 205 |
return this.luminoWidget.node; |
| 206 |
} |
| 207 |
_setElement(el) { |
| 208 |
if (this.el || el !== this.luminoWidget.node) { |
| 209 |
// TabViews don't allow setting the element beyond the initial creation. |
| 210 |
throw new Error('Cannot reset the DOM element.'); |
| 211 |
} |
| 212 |
this.el = this.luminoWidget.node; |
| 213 |
this.$el = $(this.luminoWidget.node); |
| 214 |
} |
| 215 |
/** |
| 216 |
* Public constructor. |
| 217 |
*/ |
| 218 |
initialize(parameters) { |
| 219 |
super.initialize(parameters); |
| 220 |
this.childrenViews = new ViewList(this.addChildView, (view) => { |
| 221 |
view.remove(); |
| 222 |
}, this); |
| 223 |
this.listenTo(this.model, 'change:children', () => this.updateTabs()); |
| 224 |
this.listenTo(this.model, 'change:titles', () => this.updateTitles()); |
| 225 |
} |
| 226 |
/** |
| 227 |
* Called when view is rendered. |
| 228 |
*/ |
| 229 |
render() { |
| 230 |
super.render(); |
| 231 |
const tabs = this.luminoWidget; |
| 232 |
tabs.addClass('jupyter-widgets'); |
| 233 |
tabs.addClass('widget-container'); |
| 234 |
tabs.addClass('jupyter-widget-tab'); |
| 235 |
tabs.addClass('widget-tab'); |
| 236 |
tabs.tabsMovable = true; |
| 237 |
tabs.tabBar.insertBehavior = 'none'; // needed for insert behavior, see below. |
| 238 |
tabs.tabBar.currentChanged.connect(this._onTabChanged, this); |
| 239 |
tabs.tabBar.tabMoved.connect(this._onTabMoved, this); |
| 240 |
tabs.tabBar.addClass('widget-tab-bar'); |
| 241 |
tabs.tabContents.addClass('widget-tab-contents'); |
| 242 |
// TODO: expose this option in python |
| 243 |
tabs.tabBar.tabsMovable = false; |
| 244 |
this.updateTabs(); |
| 245 |
this.update(); |
| 246 |
} |
| 247 |
/** |
| 248 |
* Render tab views based on the current model's children. |
| 249 |
*/ |
| 250 |
updateTabs() { |
| 251 |
var _a; |
| 252 |
// While we are updating, the index may not be valid, so deselect the |
| 253 |
// tabs before updating so we don't get spurious changes in the index, |
| 254 |
// which would then set off another sync cycle. |
| 255 |
this.updatingTabs = true; |
| 256 |
this.luminoWidget.currentIndex = null; |
| 257 |
(_a = this.childrenViews) === null || _a === void 0 ? void 0 : _a.update(this.model.get('children')); |
| 258 |
this.luminoWidget.currentIndex = this.model.get('selected_index'); |
| 259 |
this.updatingTabs = false; |
| 260 |
} |
| 261 |
/** |
| 262 |
* Called when a child is added to children list. |
| 263 |
*/ |
| 264 |
addChildView(model, index) { |
| 265 |
// Placeholder widget to keep our position in the tab panel while we create the view. |
| 266 |
const label = this.model.get('titles')[index] || ''; |
| 267 |
const tabs = this.luminoWidget; |
| 268 |
const placeholder = new Widget(); |
| 269 |
placeholder.title.label = label; |
| 270 |
tabs.addWidget(placeholder); |
| 271 |
return this.create_child_view(model) |
| 272 |
.then((view) => { |
| 273 |
const widget = view.luminoWidget; |
| 274 |
widget.title.label = placeholder.title.label; |
| 275 |
widget.title.closable = false; |
| 276 |
const i = ArrayExt.firstIndexOf(tabs.widgets, placeholder); |
| 277 |
// insert after placeholder so that if placeholder is selected, the |
| 278 |
// real widget will be selected now (this depends on the tab bar |
| 279 |
// insert behavior) |
| 280 |
tabs.insertWidget(i + 1, widget); |
| 281 |
placeholder.dispose(); |
| 282 |
return view; |
| 283 |
}) |
| 284 |
.catch(reject('Could not add child view to box', true)); |
| 285 |
} |
| 286 |
/** |
| 287 |
* Update the contents of this view |
| 288 |
* |
| 289 |
* Called when the model is changed. The model may have been |
| 290 |
* changed by another view or by a state update from the back-end. |
| 291 |
*/ |
| 292 |
update() { |
| 293 |
// Update the selected index in the overall update method because it |
| 294 |
// should be run after the tabs have been updated. Otherwise the |
| 295 |
// selected index may not be a valid tab in the tab bar. |
| 296 |
this.updateSelectedIndex(); |
| 297 |
return super.update(); |
| 298 |
} |
| 299 |
/** |
| 300 |
* Updates the tab page titles. |
| 301 |
*/ |
| 302 |
updateTitles() { |
| 303 |
const titles = this.model.get('titles') || []; |
| 304 |
each(this.luminoWidget.widgets, (widget, i) => { |
| 305 |
widget.title.label = titles[i] || ''; |
| 306 |
}); |
| 307 |
} |
| 308 |
/** |
| 309 |
* Updates the selected index. |
| 310 |
*/ |
| 311 |
updateSelectedIndex() { |
| 312 |
this.luminoWidget.currentIndex = this.model.get('selected_index'); |
| 313 |
} |
| 314 |
remove() { |
| 315 |
this.childrenViews = null; |
| 316 |
super.remove(); |
| 317 |
} |
| 318 |
_onTabChanged(sender, args) { |
| 319 |
if (!this.updatingTabs) { |
| 320 |
const i = args.currentIndex; |
| 321 |
this.model.set('selected_index', i === -1 ? null : i); |
| 322 |
this.touch(); |
| 323 |
} |
| 324 |
} |
| 325 |
/** |
| 326 |
* Handle the `tabMoved` signal from the tab bar. |
| 327 |
*/ |
| 328 |
_onTabMoved(sender, args) { |
| 329 |
const children = this.model.get('children').slice(); |
| 330 |
ArrayExt.move(children, args.fromIndex, args.toIndex); |
| 331 |
this.model.set('children', children); |
| 332 |
this.touch(); |
| 333 |
} |
| 334 |
} |
| 335 |
export class StackModel extends SelectionContainerModel { |
| 336 |
defaults() { |
| 337 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'StackModel', _view_name: 'StackView' }); |
| 338 |
} |
| 339 |
} |
| 340 |
export class StackView extends BoxView { |
| 341 |
initialize(parameters) { |
| 342 |
super.initialize(parameters); |
| 343 |
this.listenTo(this.model, 'change:selected_index', this.update_children); |
| 344 |
} |
| 345 |
update_children() { |
| 346 |
var _a; |
| 347 |
let child; |
| 348 |
if (this.model.get('selected_index') === null) { |
| 349 |
child = []; |
| 350 |
} |
| 351 |
else { |
| 352 |
child = [this.model.get('children')[this.model.get('selected_index')]]; |
| 353 |
} |
| 354 |
(_a = this.children_views) === null || _a === void 0 ? void 0 : _a.update(child).then((views) => { |
| 355 |
// Notify all children that their sizes may have changed. |
| 356 |
views.forEach((view) => { |
| 357 |
MessageLoop.postMessage(view.luminoWidget, Widget.ResizeMessage.UnknownSize); |
| 358 |
}); |
| 359 |
}); |
| 360 |
} |
| 361 |
} |
| 362 |
//# sourceMappingURL=widget_selectioncontainer.js.map |