Show More
@@ -10,6 +10,31 b' define(["widgets/js/manager",' | |||||
10 | ], function(widgetmanager, _, Backbone, $, utils, IPython){ |
|
10 | ], function(widgetmanager, _, Backbone, $, utils, IPython){ | |
11 | "use strict"; |
|
11 | "use strict"; | |
12 |
|
12 | |||
|
13 | var unpack_models = function unpack_models(value, model) { | |||
|
14 | /** | |||
|
15 | * Replace model ids with models recursively. | |||
|
16 | */ | |||
|
17 | var unpacked; | |||
|
18 | if ($.isArray(value)) { | |||
|
19 | unpacked = []; | |||
|
20 | _.each(value, function(sub_value, key) { | |||
|
21 | unpacked.push(unpack_models(sub_value, model)); | |||
|
22 | }); | |||
|
23 | return Promise.all(unpacked); | |||
|
24 | } else if (value instanceof Object) { | |||
|
25 | unpacked = {}; | |||
|
26 | _.each(value, function(sub_value, key) { | |||
|
27 | unpacked[key] = unpack_models(sub_value, model); | |||
|
28 | }); | |||
|
29 | return utils.resolve_promises_dict(unpacked); | |||
|
30 | } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") { | |||
|
31 | // get_model returns a promise already | |||
|
32 | return model.widget_manager.get_model(value.slice(10, value.length)); | |||
|
33 | } else { | |||
|
34 | return Promise.resolve(value); | |||
|
35 | } | |||
|
36 | }; | |||
|
37 | ||||
13 | var WidgetModel = Backbone.Model.extend({ |
|
38 | var WidgetModel = Backbone.Model.extend({ | |
14 | constructor: function (widget_manager, model_id, comm) { |
|
39 | constructor: function (widget_manager, model_id, comm) { | |
15 | /** |
|
40 | /** | |
@@ -767,6 +792,7 b' define(["widgets/js/manager",' | |||||
767 | }); |
|
792 | }); | |
768 |
|
793 | |||
769 | var widget = { |
|
794 | var widget = { | |
|
795 | 'unpack_models': unpack_models, | |||
770 | 'WidgetModel': WidgetModel, |
|
796 | 'WidgetModel': WidgetModel, | |
771 | 'WidgetView': WidgetView, |
|
797 | 'WidgetView': WidgetView, | |
772 | 'DOMWidgetView': DOMWidgetView, |
|
798 | 'DOMWidgetView': DOMWidgetView, |
@@ -9,34 +9,10 b' define([' | |||||
9 | "bootstrap", |
|
9 | "bootstrap", | |
10 | ], function(widget, $, _, utils){ |
|
10 | ], function(widget, $, _, utils){ | |
11 | "use strict"; |
|
11 | "use strict"; | |
12 | var unpack_models = function unpack_models(value, model) { |
|
|||
13 | /** |
|
|||
14 | * Replace model ids with models recursively. |
|
|||
15 | */ |
|
|||
16 | var unpacked; |
|
|||
17 | if ($.isArray(value)) { |
|
|||
18 | unpacked = []; |
|
|||
19 | _.each(value, function(sub_value, key) { |
|
|||
20 | unpacked.push(unpack_models(sub_value, model)); |
|
|||
21 | }); |
|
|||
22 | return Promise.all(unpacked); |
|
|||
23 | } else if (value instanceof Object) { |
|
|||
24 | unpacked = {}; |
|
|||
25 | _.each(value, function(sub_value, key) { |
|
|||
26 | unpacked[key] = unpack_models(sub_value, model); |
|
|||
27 | }); |
|
|||
28 | return utils.resolve_promises_dict(unpacked); |
|
|||
29 | } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") { |
|
|||
30 | // get_model returns a promise already |
|
|||
31 | return model.widget_manager.get_model(value.slice(10, value.length)); |
|
|||
32 | } else { |
|
|||
33 | return Promise.resolve(value); |
|
|||
34 | } |
|
|||
35 | }; |
|
|||
36 |
|
12 | |||
37 | var BoxModel = widget.WidgetModel.extend({}, { |
|
13 | var BoxModel = widget.WidgetModel.extend({}, { | |
38 | serializers: _.extend({ |
|
14 | serializers: _.extend({ | |
39 | children: {deserialize: unpack_models} |
|
15 | children: {deserialize: widget.unpack_models} | |
40 | }, widget.WidgetModel.serializers) |
|
16 | }, widget.WidgetModel.serializers) | |
41 | }); |
|
17 | }); | |
42 |
|
18 | |||
@@ -179,7 +155,6 b' define([' | |||||
179 | }); |
|
155 | }); | |
180 |
|
156 | |||
181 | return { |
|
157 | return { | |
182 | 'unpack_models': unpack_models, |
|
|||
183 | 'BoxModel': BoxModel, |
|
158 | 'BoxModel': BoxModel, | |
184 | 'BoxView': BoxView, |
|
159 | 'BoxView': BoxView, | |
185 | 'FlexBoxView': FlexBoxView, |
|
160 | 'FlexBoxView': FlexBoxView, |
@@ -1,4 +1,4 b'' | |||||
1 | from .widget import Widget, DOMWidget, CallbackDispatcher, register |
|
1 | from .widget import Widget, DOMWidget, CallbackDispatcher, register, widget_serialization | |
2 |
|
2 | |||
3 | from .trait_types import Color |
|
3 | from .trait_types import Color | |
4 |
|
4 |
@@ -24,6 +24,33 b' from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \\' | |||||
24 | from IPython.utils.py3compat import string_types |
|
24 | from IPython.utils.py3compat import string_types | |
25 | from .trait_types import Color |
|
25 | from .trait_types import Color | |
26 |
|
26 | |||
|
27 | ||||
|
28 | def _widget_to_json(x): | |||
|
29 | if isinstance(x, dict): | |||
|
30 | return {k: _widget_to_json(v) for k, v in x.items()} | |||
|
31 | elif isinstance(x, (list, tuple)): | |||
|
32 | return [_widget_to_json(v) for v in x] | |||
|
33 | elif isinstance(x, Widget): | |||
|
34 | return "IPY_MODEL_" + x.model_id | |||
|
35 | else: | |||
|
36 | return x | |||
|
37 | ||||
|
38 | def _json_to_widget(x): | |||
|
39 | if isinstance(x, dict): | |||
|
40 | return {k: _json_to_widget(v) for k, v in x.items()} | |||
|
41 | elif isinstance(x, (list, tuple)): | |||
|
42 | return [_json_to_widget(v) for v in x] | |||
|
43 | elif isinstance(x, string_types) and x.startswith('IPY_MODEL_') and x[10:] in Widget.widgets: | |||
|
44 | return Widget.widgets[x[10:]] | |||
|
45 | else: | |||
|
46 | return x | |||
|
47 | ||||
|
48 | widget_serialization = { | |||
|
49 | 'from_json': _json_to_widget, | |||
|
50 | 'to_json': _widget_to_json | |||
|
51 | } | |||
|
52 | ||||
|
53 | ||||
27 | #----------------------------------------------------------------------------- |
|
54 | #----------------------------------------------------------------------------- | |
28 | # Classes |
|
55 | # Classes | |
29 | #----------------------------------------------------------------------------- |
|
56 | #----------------------------------------------------------------------------- |
@@ -6,35 +6,10 b' Represents a container that can be used to group other widgets.' | |||||
6 | # Copyright (c) IPython Development Team. |
|
6 | # Copyright (c) IPython Development Team. | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 |
|
8 | |||
9 | from .widget import DOMWidget, Widget, register |
|
9 | from .widget import DOMWidget, Widget, register, widget_serialization | |
10 | from IPython.utils.traitlets import Unicode, Tuple, TraitError, Int, CaselessStrEnum |
|
10 | from IPython.utils.traitlets import Unicode, Tuple, TraitError, Int, CaselessStrEnum | |
11 | from .deprecated import DeprecatedClass |
|
11 | from .deprecated import DeprecatedClass | |
12 |
|
12 | |||
13 | def _widget_to_json(x): |
|
|||
14 | if isinstance(x, dict): |
|
|||
15 | return {k: _widget_to_json(v) for k, v in x.items()} |
|
|||
16 | elif isinstance(x, (list, tuple)): |
|
|||
17 | return [_widget_to_json(v) for v in x] |
|
|||
18 | elif isinstance(x, Widget): |
|
|||
19 | return "IPY_MODEL_" + x.model_id |
|
|||
20 | else: |
|
|||
21 | return x |
|
|||
22 |
|
||||
23 | def _json_to_widget(x): |
|
|||
24 | if isinstance(x, dict): |
|
|||
25 | return {k: _json_to_widget(v) for k, v in x.items()} |
|
|||
26 | elif isinstance(x, (list, tuple)): |
|
|||
27 | return [_json_to_widget(v) for v in x] |
|
|||
28 | elif isinstance(x, string_types) and x.startswith('IPY_MODEL_') and x[10:] in Widget.widgets: |
|
|||
29 | return Widget.widgets[x[10:]] |
|
|||
30 | else: |
|
|||
31 | return x |
|
|||
32 |
|
||||
33 | widget_serialization = { |
|
|||
34 | 'from_json': _json_to_widget, |
|
|||
35 | 'to_json': _widget_to_json |
|
|||
36 | } |
|
|||
37 |
|
||||
38 |
|
13 | |||
39 | @register('IPython.Box') |
|
14 | @register('IPython.Box') | |
40 | class Box(DOMWidget): |
|
15 | class Box(DOMWidget): |
General Comments 0
You need to be logged in to leave comments.
Login now