|  | 
|  | 1 | +// Copyright (c) Jupyter Development Team. | 
|  | 2 | +// Distributed under the terms of the Modified BSD License. | 
|  | 3 | + | 
|  | 4 | +import { | 
|  | 5 | +    DOMWidgetView, | 
|  | 6 | +    unpack_models, | 
|  | 7 | +    ViewList, | 
|  | 8 | +    JupyterLuminoPanelWidget, | 
|  | 9 | +    reject, | 
|  | 10 | +    WidgetModel, | 
|  | 11 | +    WidgetView, | 
|  | 12 | +} from '@jupyter-widgets/base'; | 
|  | 13 | + | 
|  | 14 | +import { CoreDOMWidgetModel } from './widget_core'; | 
|  | 15 | + | 
|  | 16 | +import { ArrayExt } from '@lumino/algorithm'; | 
|  | 17 | + | 
|  | 18 | +import { MessageLoop } from '@lumino/messaging'; | 
|  | 19 | + | 
|  | 20 | +import { Widget } from '@lumino/widgets'; | 
|  | 21 | + | 
|  | 22 | +import $ from 'jquery'; | 
|  | 23 | + | 
|  | 24 | +export class FieldsetModel extends CoreDOMWidgetModel { | 
|  | 25 | +    defaults(): Backbone.ObjectHash { | 
|  | 26 | +        return { | 
|  | 27 | +            ...super.defaults(), | 
|  | 28 | +            _view_name: 'FieldsetView', | 
|  | 29 | +            _model_name: 'FieldsetModel', | 
|  | 30 | +            children: [], | 
|  | 31 | +            box_style: '', | 
|  | 32 | +        }; | 
|  | 33 | +    } | 
|  | 34 | + | 
|  | 35 | +    static serializers = { | 
|  | 36 | +        ...CoreDOMWidgetModel.serializers, | 
|  | 37 | +        children: { deserialize: unpack_models }, | 
|  | 38 | +    }; | 
|  | 39 | +} | 
|  | 40 | + | 
|  | 41 | + | 
|  | 42 | +export class FieldsetView extends DOMWidgetView { | 
|  | 43 | +    _createElement(tagName: string): HTMLElement { | 
|  | 44 | +        this.luminoWidget = new JupyterLuminoPanelWidget({ view: this }); | 
|  | 45 | +        return this.luminoWidget.node; | 
|  | 46 | +    } | 
|  | 47 | + | 
|  | 48 | +    _setElement(el: HTMLElement): void { | 
|  | 49 | +        if (this.el || el !== this.luminoWidget.node) { | 
|  | 50 | +            // Boxes don't allow setting the element beyond the initial creation. | 
|  | 51 | +            throw new Error('Cannot reset the DOM element.'); | 
|  | 52 | +        } | 
|  | 53 | + | 
|  | 54 | +        this.el = this.luminoWidget.node; | 
|  | 55 | +        this.$el = $(this.luminoWidget.node); | 
|  | 56 | +    } | 
|  | 57 | + | 
|  | 58 | +    initialize(parameters: WidgetView.IInitializeParameters): void { | 
|  | 59 | +        super.initialize(parameters); | 
|  | 60 | +        this.children_views = new ViewList(this.add_child_model, null, this); | 
|  | 61 | +        this.listenTo(this.model, 'change:children', this.update_children); | 
|  | 62 | +        this.listenTo(this.model, 'change:fieldset_style', this.update_fieldset_style); | 
|  | 63 | + | 
|  | 64 | +        this.luminoWidget.addClass('jupyter-widgets'); | 
|  | 65 | +        this.luminoWidget.addClass('widget-container'); | 
|  | 66 | +    } | 
|  | 67 | + | 
|  | 68 | +    render(): void { | 
|  | 69 | +        super.render(); | 
|  | 70 | +        this.update_children(); | 
|  | 71 | +        this.set_fieldset_style(); | 
|  | 72 | +    } | 
|  | 73 | + | 
|  | 74 | +    update_children(): void { | 
|  | 75 | +        this.children_views | 
|  | 76 | +            ?.update(this.model.get('children')) | 
|  | 77 | +            .then((views: DOMWidgetView[]) => { | 
|  | 78 | +                // Notify all children that their sizes may have changed. | 
|  | 79 | +                views.forEach((view) => { | 
|  | 80 | +                    MessageLoop.postMessage( | 
|  | 81 | +                        view.luminoWidget, | 
|  | 82 | +                        Widget.ResizeMessage.UnknownSize | 
|  | 83 | +                    ); | 
|  | 84 | +                }); | 
|  | 85 | +            }); | 
|  | 86 | +    } | 
|  | 87 | + | 
|  | 88 | +    update_fieldset_style(): void { | 
|  | 89 | +        this.update_mapped_classes(FieldsetView.class_map, 'fieldset_style'); | 
|  | 90 | +    } | 
|  | 91 | + | 
|  | 92 | +    set_fieldset_style(): void { | 
|  | 93 | +        this.set_mapped_classes(FieldsetView.class_map, 'fieldset_style'); | 
|  | 94 | +    } | 
|  | 95 | + | 
|  | 96 | +    add_child_model(model: WidgetModel): Promise<DOMWidgetView> { | 
|  | 97 | +        // we insert a dummy element so the order is preserved when we add | 
|  | 98 | +        // the rendered content later. | 
|  | 99 | +        const dummy = new Widget(); | 
|  | 100 | +        this.luminoWidget.addWidget(dummy); | 
|  | 101 | + | 
|  | 102 | +        return this.create_child_view(model) | 
|  | 103 | +            .then((view: DOMWidgetView) => { | 
|  | 104 | +                // replace the dummy widget with the new one. | 
|  | 105 | +                const i = ArrayExt.firstIndexOf(this.luminoWidget.widgets, dummy); | 
|  | 106 | +                this.luminoWidget.insertWidget(i, view.luminoWidget); | 
|  | 107 | +                dummy.dispose(); | 
|  | 108 | +                return view; | 
|  | 109 | +            }) | 
|  | 110 | +            .catch(reject('Could not add child view to fieldset', true)); | 
|  | 111 | +    } | 
|  | 112 | + | 
|  | 113 | +    remove(): void { | 
|  | 114 | +        this.children_views = null; | 
|  | 115 | +        super.remove(); | 
|  | 116 | +    } | 
|  | 117 | + | 
|  | 118 | +    children_views: ViewList<DOMWidgetView> | null; | 
|  | 119 | +    luminoWidget: JupyterLuminoPanelWidget; | 
|  | 120 | + | 
|  | 121 | +    static class_map = { | 
|  | 122 | +        success: ['alert', 'alert-success'], | 
|  | 123 | +        info: ['alert', 'alert-info'], | 
|  | 124 | +        warning: ['alert', 'alert-warning'], | 
|  | 125 | +        danger: ['alert', 'alert-danger'], | 
|  | 126 | +    }; | 
|  | 127 | +} | 
0 commit comments