UNPKG

11.9 kBTypeScriptView Raw
1import * as utils from './utils';
2import * as Backbone from 'backbone';
3import { NativeView } from './nativeview';
4import { JSONObject } from '@lumino/coreutils';
5import { Message } from '@lumino/messaging';
6import { Widget, Panel } from '@lumino/widgets';
7import { LayoutModel } from './widget_layout';
8import { StyleModel } from './widget_style';
9import { IWidgetManager } from './manager';
10import { IClassicComm, ICallbacks } from './services-shim';
11import { BufferJSON, Dict } from './utils';
12import { KernelMessage } from '@jupyterlab/services';
13/**
14 * Replace model ids with models recursively.
15 */
16export declare function unpack_models(value: any | Dict<unknown> | string | (Dict<unknown> | string)[], manager: IWidgetManager): Promise<WidgetModel | Dict<WidgetModel> | WidgetModel[] | any>;
17/**
18 * Type declaration for general widget serializers.
19 */
20export interface ISerializers {
21 [key: string]: {
22 deserialize?: (value?: any, manager?: IWidgetManager) => any;
23 serialize?: (value?: any, widget?: WidgetModel) => any;
24 };
25}
26export interface IBackboneModelOptions extends Backbone.ModelSetOptions {
27 model_id: string;
28 comm?: any;
29 widget_manager: any;
30}
31export declare class WidgetModel extends Backbone.Model {
32 /**
33 * The default attributes.
34 */
35 defaults(): Backbone.ObjectHash;
36 /**
37 * Test to see if the model has been synced with the server.
38 *
39 * #### Notes
40 * As of backbone 1.1, backbone ignores `patch` if it thinks the
41 * model has never been pushed.
42 */
43 isNew(): boolean;
44 /**
45 * Constructor
46 *
47 * Initializes a WidgetModel instance. Called by the Backbone constructor.
48 *
49 * Parameters
50 * ----------
51 * widget_manager : WidgetManager instance
52 * model_id : string
53 * An ID unique to this model.
54 * comm : Comm instance (optional)
55 */
56 initialize(attributes: Backbone.ObjectHash, options: IBackboneModelOptions): void;
57 get comm_live(): boolean;
58 set comm_live(x: boolean);
59 /**
60 * Send a custom msg over the comm.
61 */
62 send(content: {}, callbacks: {}, buffers?: ArrayBuffer[] | ArrayBufferView[]): void;
63 /**
64 * Close model
65 *
66 * @param comm_closed - true if the comm is already being closed. If false, the comm will be closed.
67 *
68 * @returns - a promise that is fulfilled when all the associated views have been removed.
69 */
70 close(comm_closed?: boolean): Promise<void>;
71 /**
72 * Handle when a widget comm is closed.
73 */
74 _handle_comm_closed(msg: KernelMessage.ICommCloseMsg): void;
75 /**
76 * Handle incoming comm msg.
77 */
78 _handle_comm_msg(msg: KernelMessage.ICommMsgMsg): Promise<void>;
79 /**
80 * Handle when a widget is updated from the backend.
81 *
82 * This function is meant for internal use only. Values set here will not be propagated on a sync.
83 */
84 set_state(state: Dict<unknown>): void;
85 /**
86 * Get the serializable state of the model.
87 *
88 * If drop_default is truthy, attributes that are equal to their default
89 * values are dropped.
90 */
91 get_state(drop_defaults?: boolean): JSONObject;
92 /**
93 * Handle status msgs.
94 *
95 * execution_state : ('busy', 'idle', 'starting')
96 */
97 _handle_status(msg: KernelMessage.IStatusMsg): void;
98 /**
99 * Create msg callbacks for a comm msg.
100 */
101 callbacks(view?: WidgetView): ICallbacks;
102 /**
103 * Set one or more values.
104 *
105 * We just call the super method, in which val and options are optional.
106 * Handles both "key", value and {key: value} -style arguments.
107 */
108 set(key: any, val?: any, options?: any): any;
109 /**
110 * Handle sync to the back-end. Called when a model.save() is called.
111 *
112 * Make sure a comm exists.
113 *
114 * Parameters
115 * ----------
116 * method : create, update, patch, delete, read
117 * create/update always send the full attribute set
118 * patch - only send attributes listed in options.attrs, and if we
119 * are queuing up messages, combine with previous messages that have
120 * not been sent yet
121 * model : the model we are syncing
122 * will normally be the same as `this`
123 * options : dict
124 * the `attrs` key, if it exists, gives an {attr: value} dict that
125 * should be synced, otherwise, sync all attributes.
126 *
127 */
128 sync(method: string, model: WidgetModel, options?: any): any;
129 rememberLastUpdateFor(msgId: string): void;
130 /**
131 * Serialize widget state.
132 *
133 * A serializer is a function which takes in a state attribute and a widget,
134 * and synchronously returns a JSONable object. The returned object will
135 * have toJSON called if possible, and the final result should be a
136 * primitive object that is a snapshot of the widget state that may have
137 * binary array buffers.
138 */
139 serialize(state: Dict<any>): JSONObject;
140 /**
141 * Send a sync message to the kernel.
142 *
143 * If a message is sent successfully, this returns the message ID of that
144 * message. Otherwise it returns an empty string
145 */
146 send_sync_message(state: JSONObject, callbacks?: any): string;
147 /**
148 * Push this model's state to the back-end
149 *
150 * This invokes a Backbone.Sync.
151 */
152 save_changes(callbacks?: {}): void;
153 /**
154 * on_some_change(['key1', 'key2'], foo, context) differs from
155 * on('change:key1 change:key2', foo, context).
156 * If the widget attributes key1 and key2 are both modified,
157 * the second form will result in foo being called twice
158 * while the first will call foo only once.
159 */
160 on_some_change(keys: string[], callback: (...args: any[]) => void, context: any): void;
161 /**
162 * Serialize the model. See the deserialization function at the top of this file
163 * and the kernel-side serializer/deserializer.
164 */
165 toJSON(options?: {}): string;
166 /**
167 * Returns a promise for the deserialized state. The second argument
168 * is an instance of widget manager, which is required for the
169 * deserialization of widget models.
170 */
171 static _deserialize_state(state: Dict<BufferJSON>, manager: IWidgetManager): Promise<utils.Dict<unknown>>;
172 static serializers: ISerializers;
173 widget_manager: IWidgetManager;
174 model_id: string;
175 views?: {
176 [key: string]: Promise<WidgetView>;
177 };
178 state_change: Promise<any>;
179 comm?: IClassicComm;
180 name: string;
181 module: string;
182 private _comm_live;
183 private _closed;
184 private _state_lock;
185 private _buffered_state_diff;
186 private _buffered_state_diff_synced;
187 private _msg_buffer;
188 private _msg_buffer_callbacks;
189 private _pending_msgs;
190 private _expectedEchoMsgIds;
191 private _attrsToUpdate;
192}
193export declare class DOMWidgetModel extends WidgetModel {
194 static serializers: ISerializers;
195 defaults(): Backbone.ObjectHash;
196}
197export declare class WidgetView extends NativeView<WidgetModel> {
198 /**
199 * Public constructor.
200 */
201 constructor(options?: Backbone.ViewOptions<WidgetModel> & {
202 options?: any;
203 });
204 /**
205 * Initializer, called at the end of the constructor.
206 */
207 initialize(parameters: WidgetView.IInitializeParameters): void;
208 /**
209 * Handle message sent to the front end.
210 *
211 * Used to focus or blur the widget.
212 */
213 handle_message(content: any): void;
214 /**
215 * Triggered on model change.
216 *
217 * Update view to be consistent with this.model
218 */
219 update(options?: any): void;
220 /**
221 * Render a view
222 *
223 * @returns the view or a promise to the view.
224 */
225 render(): any;
226 /**
227 * Create and promise that resolves to a child view of a given model
228 */
229 create_child_view<VT extends DOMWidgetView = DOMWidgetView>(child_model: DOMWidgetModel, options?: any): Promise<VT>;
230 create_child_view<VT extends WidgetView = WidgetView>(child_model: WidgetModel, options?: any): Promise<VT>;
231 /**
232 * Create msg callbacks for a comm msg.
233 */
234 callbacks(): ICallbacks;
235 /**
236 * Send a custom msg associated with this view.
237 */
238 send(content: {}, buffers?: ArrayBuffer[] | ArrayBufferView[]): void;
239 touch(): void;
240 remove(): any;
241 options: any;
242 /**
243 * A promise that resolves to the parent view when a child view is displayed.
244 */
245 displayed: Promise<WidgetView>;
246}
247export declare namespace WidgetView {
248 interface IInitializeParameters<T extends WidgetModel = WidgetModel> extends Backbone.ViewOptions<T> {
249 options: any;
250 }
251}
252export declare namespace JupyterLuminoWidget {
253 interface IOptions {
254 view: DOMWidgetView;
255 }
256}
257export declare class JupyterLuminoWidget extends Widget {
258 constructor(options: Widget.IOptions & JupyterLuminoWidget.IOptions);
259 /**
260 * Dispose the widget.
261 *
262 * This causes the view to be destroyed as well with 'remove'
263 */
264 dispose(): void;
265 /**
266 * Process the Lumino message.
267 *
268 * Any custom Lumino widget used inside a Jupyter widget should override
269 * the processMessage function like this.
270 */
271 processMessage(msg: Message): void;
272 private _view;
273}
274/**
275 * @deprecated Use {@link JupyterLuminoWidget} instead (Since 8.0).
276 */
277export declare const JupyterPhosphorWidget: typeof JupyterLuminoWidget;
278export declare class JupyterLuminoPanelWidget extends Panel {
279 constructor(options: JupyterLuminoWidget.IOptions & Panel.IOptions);
280 /**
281 * Process the Lumino message.
282 *
283 * Any custom Lumino widget used inside a Jupyter widget should override
284 * the processMessage function like this.
285 */
286 processMessage(msg: Message): void;
287 /**
288 * Dispose the widget.
289 *
290 * This causes the view to be destroyed as well with 'remove'
291 */
292 dispose(): void;
293 private _view;
294}
295export declare class DOMWidgetView extends WidgetView {
296 /**
297 * Public constructor
298 */
299 initialize(parameters: WidgetView.IInitializeParameters): void;
300 setLayout(layout: LayoutModel, oldLayout?: LayoutModel): void;
301 setStyle(style: StyleModel, oldStyle?: StyleModel): void;
302 updateTooltip(): void;
303 /**
304 * Update the DOM classes applied to an element, default to this.el.
305 */
306 update_classes(old_classes: string[], new_classes: string[], el?: HTMLElement): void;
307 /**
308 * Update the DOM classes applied to the widget based on a single
309 * trait's value.
310 *
311 * Given a trait value classes map, this function automatically
312 * handles applying the appropriate classes to the widget element
313 * and removing classes that are no longer valid.
314 *
315 * Parameters
316 * ----------
317 * class_map: dictionary
318 * Dictionary of trait values to class lists.
319 * Example:
320 * {
321 * success: ['alert', 'alert-success'],
322 * info: ['alert', 'alert-info'],
323 * warning: ['alert', 'alert-warning'],
324 * danger: ['alert', 'alert-danger']
325 * };
326 * trait_name: string
327 * Name of the trait to check the value of.
328 * el: optional DOM element handle, defaults to this.el
329 * Element that the classes are applied to.
330 */
331 update_mapped_classes(class_map: Dict<string[]>, trait_name: string, el?: HTMLElement): void;
332 set_mapped_classes(class_map: Dict<string[]>, trait_name: string, el?: HTMLElement): void;
333 _setElement(el: HTMLElement): void;
334 remove(): any;
335 processLuminoMessage(msg: Message): void;
336 private _comm_live_update;
337 updateTabindex(): void;
338 /**
339 * @deprecated Use {@link luminoWidget} instead (Since 8.0).
340 */
341 get pWidget(): Widget;
342 el: HTMLElement;
343 '$el': any;
344 luminoWidget: Widget;
345 layoutPromise: Promise<any>;
346 stylePromise: Promise<any>;
347}
348//# sourceMappingURL=widget.d.ts.map
\No newline at end of file