Show More
@@ -1,213 +1,214 | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2013 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // WidgetModel, WidgetView, and WidgetManager |
|
10 | 10 | //============================================================================ |
|
11 | 11 | /** |
|
12 | 12 | * Base Widget classes |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | * @submodule widget |
|
16 | 16 | */ |
|
17 | 17 | |
|
18 | 18 | (function () { |
|
19 | 19 | "use strict"; |
|
20 | 20 | |
|
21 | 21 | // Use require.js 'define' method so that require.js is intelligent enough to |
|
22 | 22 | // syncronously load everything within this file when it is being 'required' |
|
23 | 23 | // elsewhere. |
|
24 | 24 | define(["underscore", |
|
25 | 25 | "backbone", |
|
26 | 26 | ], function (_, Backbone) { |
|
27 | 27 | |
|
28 | 28 | //-------------------------------------------------------------------- |
|
29 | 29 | // WidgetManager class |
|
30 | 30 | //-------------------------------------------------------------------- |
|
31 | 31 | var WidgetManager = function (comm_manager) { |
|
32 | 32 | // Public constructor |
|
33 | 33 | WidgetManager._managers.push(this); |
|
34 | 34 | |
|
35 | 35 | // Attach a comm manager to the |
|
36 | 36 | this.comm_manager = comm_manager; |
|
37 | 37 | this._models = {}; /* Dictionary of model ids and model instances */ |
|
38 | 38 | |
|
39 | 39 | // Register already-registered widget model types with the comm manager. |
|
40 | 40 | var that = this; |
|
41 | 41 | _.each(WidgetManager._model_types, function(model_type, model_name) { |
|
42 | 42 | that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that)); |
|
43 | 43 | }); |
|
44 | 44 | }; |
|
45 | 45 | |
|
46 | 46 | //-------------------------------------------------------------------- |
|
47 | 47 | // Class level |
|
48 | 48 | //-------------------------------------------------------------------- |
|
49 | 49 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ |
|
50 | 50 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ |
|
51 | 51 | WidgetManager._managers = []; /* List of widget managers */ |
|
52 | 52 | |
|
53 | 53 | WidgetManager.register_widget_model = function (model_name, model_type) { |
|
54 | 54 | // Registers a widget model by name. |
|
55 | 55 | WidgetManager._model_types[model_name] = model_type; |
|
56 | 56 | |
|
57 | 57 | // Register the widget with the comm manager. Make sure to pass this object's context |
|
58 | 58 | // in so `this` works in the call back. |
|
59 | 59 | _.each(WidgetManager._managers, function(instance, i) { |
|
60 | 60 | if (instance.comm_manager !== null) { |
|
61 | 61 | instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance)); |
|
62 | 62 | } |
|
63 | 63 | }); |
|
64 | 64 | }; |
|
65 | 65 | |
|
66 | 66 | WidgetManager.register_widget_view = function (view_name, view_type) { |
|
67 | 67 | // Registers a widget view by name. |
|
68 | 68 | WidgetManager._view_types[view_name] = view_type; |
|
69 | 69 | }; |
|
70 | 70 | |
|
71 | 71 | //-------------------------------------------------------------------- |
|
72 | 72 | // Instance level |
|
73 | 73 | //-------------------------------------------------------------------- |
|
74 | 74 | WidgetManager.prototype.display_view = function(msg, model) { |
|
75 | 75 | // Displays a view for a particular model. |
|
76 | 76 | var cell = this.get_msg_cell(msg.parent_header.msg_id); |
|
77 | 77 | if (cell === null) { |
|
78 | 78 | console.log("Could not determine where the display" + |
|
79 | 79 | " message was from. Widget will not be displayed"); |
|
80 | 80 | } else { |
|
81 | 81 | var view = this.create_view(model, {cell: cell}); |
|
82 | 82 | if (view === null) { |
|
83 | 83 | console.error("View creation failed", model); |
|
84 | 84 | } |
|
85 | 85 | if (cell.widget_subarea) { |
|
86 | 86 | cell.widget_area.show(); |
|
87 | 87 | this._handle_display_view(view); |
|
88 | 88 | cell.widget_subarea.append(view.$el); |
|
89 | 89 | } |
|
90 | 90 | } |
|
91 | 91 | }; |
|
92 | 92 | |
|
93 | 93 | WidgetManager.prototype._handle_display_view = function (view) { |
|
94 | 94 | // Have the IPython keyboard manager disable its event |
|
95 | 95 | // handling so the widget can capture keyboard input. |
|
96 | 96 | // Note, this is only done on the outer most widgets. |
|
97 | 97 | IPython.keyboard_manager.register_events(view.$el); |
|
98 | 98 | |
|
99 | 99 | if (view.additional_elements) { |
|
100 | 100 | for (var i = 0; i < view.additional_elements.length; i++) { |
|
101 | 101 | IPython.keyboard_manager.register_events(view.additional_elements[i]); |
|
102 | 102 | } |
|
103 | 103 | } |
|
104 | 104 | }; |
|
105 | 105 | |
|
106 | 106 | WidgetManager.prototype.create_view = function(model, options, view) { |
|
107 | 107 | // Creates a view for a particular model. |
|
108 | 108 | var view_name = model.get('_view_name'); |
|
109 | 109 | var ViewType = WidgetManager._view_types[view_name]; |
|
110 | 110 | if (ViewType) { |
|
111 | 111 | |
|
112 | 112 | // If a view is passed into the method, use that view's cell as |
|
113 | 113 | // the cell for the view that is created. |
|
114 | 114 | options = options || {}; |
|
115 | 115 | if (view !== undefined) { |
|
116 | 116 | options.cell = view.options.cell; |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | // Create and render the view... |
|
120 | 120 | var parameters = {model: model, options: options}; |
|
121 | 121 | view = new ViewType(parameters); |
|
122 | 122 | view.render(); |
|
123 | 123 | model.on('destroy', view.remove, view); |
|
124 | 124 | return view; |
|
125 | 125 | } |
|
126 | console.log('VIEW NOT REGISTERED?!'); | |
|
126 | 127 | return null; |
|
127 | 128 | }; |
|
128 | 129 | |
|
129 | 130 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
130 | 131 | var cell = null; |
|
131 | 132 | // First, check to see if the msg was triggered by cell execution. |
|
132 | 133 | if (IPython.notebook) { |
|
133 | 134 | cell = IPython.notebook.get_msg_cell(msg_id); |
|
134 | 135 | } |
|
135 | 136 | if (cell !== null) { |
|
136 | 137 | return cell; |
|
137 | 138 | } |
|
138 | 139 | // Second, check to see if a get_cell callback was defined |
|
139 | 140 | // for the message. get_cell callbacks are registered for |
|
140 | 141 | // widget messages, so this block is actually checking to see if the |
|
141 | 142 | // message was triggered by a widget. |
|
142 | 143 | var kernel = this.comm_manager.kernel; |
|
143 | 144 | if (kernel) { |
|
144 | 145 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
145 | 146 | if (callbacks && callbacks.iopub && |
|
146 | 147 | callbacks.iopub.get_cell !== undefined) { |
|
147 | 148 | return callbacks.iopub.get_cell(); |
|
148 | 149 | } |
|
149 | 150 | } |
|
150 | 151 | |
|
151 | 152 | // Not triggered by a cell or widget (no get_cell callback |
|
152 | 153 | // exists). |
|
153 | 154 | return null; |
|
154 | 155 | }; |
|
155 | 156 | |
|
156 | 157 | WidgetManager.prototype.callbacks = function (view) { |
|
157 | 158 | // callback handlers specific a view |
|
158 | 159 | var callbacks = {}; |
|
159 | 160 | if (view && view.options.cell) { |
|
160 | 161 | |
|
161 | 162 | // Try to get output handlers |
|
162 | 163 | var cell = view.options.cell; |
|
163 | 164 | var handle_output = null; |
|
164 | 165 | var handle_clear_output = null; |
|
165 | 166 | if (cell.output_area) { |
|
166 | 167 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); |
|
167 | 168 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); |
|
168 | 169 | } |
|
169 | 170 | |
|
170 | 171 | // Create callback dict using what is known |
|
171 | 172 | var that = this; |
|
172 | 173 | callbacks = { |
|
173 | 174 | iopub : { |
|
174 | 175 | output : handle_output, |
|
175 | 176 | clear_output : handle_clear_output, |
|
176 | 177 | |
|
177 | 178 | // Special function only registered by widget messages. |
|
178 | 179 | // Allows us to get the cell for a message so we know |
|
179 | 180 | // where to add widgets if the code requires it. |
|
180 | 181 | get_cell : function () { |
|
181 | 182 | return cell; |
|
182 | 183 | }, |
|
183 | 184 | }, |
|
184 | 185 | }; |
|
185 | 186 | } |
|
186 | 187 | return callbacks; |
|
187 | 188 | }; |
|
188 | 189 | |
|
189 | 190 | WidgetManager.prototype.get_model = function (model_id) { |
|
190 | 191 | // Look-up a model instance by its id. |
|
191 | 192 | var model = this._models[model_id]; |
|
192 | 193 | if (model !== undefined && model.id == model_id) { |
|
193 | 194 | return model; |
|
194 | 195 | } |
|
195 | 196 | return null; |
|
196 | 197 | }; |
|
197 | 198 | |
|
198 | 199 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
199 | 200 | // Handle when a comm is opened. |
|
200 | 201 | var that = this; |
|
201 | 202 | var model_id = comm.comm_id; |
|
202 | 203 | var widget_type_name = msg.content.target_name; |
|
203 | 204 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); |
|
204 | 205 | widget_model.on('comm:close', function () { |
|
205 | 206 | delete that._models[model_id]; |
|
206 | 207 | }); |
|
207 | 208 | this._models[model_id] = widget_model; |
|
208 | 209 | }; |
|
209 | 210 | |
|
210 | 211 | IPython.WidgetManager = WidgetManager; |
|
211 | 212 | return IPython.WidgetManager; |
|
212 | 213 | }); |
|
213 | 214 | }()); |
@@ -1,282 +1,315 | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2013 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // ContainerWidget |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | 12 | /** |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | **/ |
|
16 | 16 | |
|
17 | 17 | define(["widgets/js/widget"], function(WidgetManager) { |
|
18 | 18 | |
|
19 | 19 | var ContainerView = IPython.DOMWidgetView.extend({ |
|
20 | 20 | render: function(){ |
|
21 | 21 | // Called when view is rendered. |
|
22 | 22 | this.$el.addClass('widget-container') |
|
23 | 23 | .addClass('vbox'); |
|
24 | 24 | this.children={}; |
|
25 | 25 | this.update_children([], this.model.get('_children')); |
|
26 | 26 | this.model.on('change:_children', function(model, value, options) { |
|
27 | 27 | this.update_children(model.previous('_children'), value); |
|
28 | 28 | }, this); |
|
29 | 29 | this.update(); |
|
30 | ||
|
31 | // Trigger model displayed events for any models that are child to | |
|
32 | // this model when this model is displayed. | |
|
33 | var that = this; | |
|
34 | this.model.on('displayed', function(){ | |
|
35 | that.is_displayed = true; | |
|
36 | for (var property in that.child_views) { | |
|
37 | if (that.child_views.hasOwnProperty(property)) { | |
|
38 | that.child_views[property].model.trigger('displayed'); | |
|
39 | } | |
|
40 | } | |
|
41 | }); | |
|
30 | 42 | }, |
|
31 | 43 | |
|
32 | 44 | update_children: function(old_list, new_list) { |
|
33 | 45 | // Called when the children list changes. |
|
34 | 46 | this.do_diff(old_list, |
|
35 | 47 | new_list, |
|
36 | 48 | $.proxy(this.remove_child_model, this), |
|
37 | 49 | $.proxy(this.add_child_model, this)); |
|
38 | 50 | }, |
|
39 | 51 | |
|
40 | 52 | remove_child_model: function(model) { |
|
41 | 53 | // Called when a model is removed from the children list. |
|
42 | 54 | this.child_views[model.id].remove(); |
|
43 | 55 | this.delete_child_view(model); |
|
44 | 56 | }, |
|
45 | 57 | |
|
46 | 58 | add_child_model: function(model) { |
|
47 | 59 | // Called when a model is added to the children list. |
|
48 | 60 | var view = this.create_child_view(model); |
|
49 | 61 | this.$el.append(view.$el); |
|
62 | ||
|
63 | // Trigger the displayed event if this model is displayed. | |
|
64 | if (this.is_displayed) { | |
|
65 | model.trigger('displayed'); | |
|
66 | } | |
|
50 | 67 | }, |
|
51 | 68 | |
|
52 | 69 | update: function(){ |
|
53 | 70 | // Update the contents of this view |
|
54 | 71 | // |
|
55 | 72 | // Called when the model is changed. The model may have been |
|
56 | 73 | // changed by another view or by a state update from the back-end. |
|
57 | 74 | return ContainerView.__super__.update.apply(this); |
|
58 | 75 | }, |
|
59 | 76 | }); |
|
60 | 77 | |
|
61 | 78 | WidgetManager.register_widget_view('ContainerView', ContainerView); |
|
62 | 79 | |
|
63 | 80 | var PopupView = IPython.DOMWidgetView.extend({ |
|
64 | 81 | render: function(){ |
|
65 | 82 | // Called when view is rendered. |
|
66 | 83 | var that = this; |
|
67 | 84 | this.children={}; |
|
68 | 85 | |
|
69 | 86 | this.$el.on("remove", function(){ |
|
70 | 87 | that.$window.remove(); |
|
71 | 88 | }); |
|
72 | 89 | this.$window = $('<div />') |
|
73 | 90 | .addClass('modal widget-modal') |
|
74 | 91 | .appendTo($('#notebook-container')) |
|
75 | 92 | .mousedown(function(){ |
|
76 | 93 | that.bring_to_front(); |
|
77 | 94 | }); |
|
78 | 95 | |
|
79 | 96 | // Set the elements array since the this.$window element is not child |
|
80 | 97 | // of this.$el and the parent widget manager or other widgets may |
|
81 | 98 | // need to know about all of the top-level widgets. The IPython |
|
82 | 99 | // widget manager uses this to register the elements with the |
|
83 | 100 | // keyboard manager. |
|
84 | this.additional_elements = [this.$window] | |
|
101 | this.additional_elements = [this.$window]; | |
|
85 | 102 | |
|
86 | 103 | this.$title_bar = $('<div />') |
|
87 | 104 | .addClass('popover-title') |
|
88 | 105 | .appendTo(this.$window) |
|
89 | 106 | .mousedown(function(){ |
|
90 | 107 | that.bring_to_front(); |
|
91 | 108 | }); |
|
92 | 109 | this.$close = $('<button />') |
|
93 | 110 | .addClass('close icon-remove') |
|
94 | 111 | .css('margin-left', '5px') |
|
95 | 112 | .appendTo(this.$title_bar) |
|
96 | 113 | .click(function(){ |
|
97 | 114 | that.hide(); |
|
98 | 115 | event.stopPropagation(); |
|
99 | 116 | }); |
|
100 | 117 | this.$minimize = $('<button />') |
|
101 | 118 | .addClass('close icon-arrow-down') |
|
102 | 119 | .appendTo(this.$title_bar) |
|
103 | 120 | .click(function(){ |
|
104 | 121 | that.popped_out = !that.popped_out; |
|
105 | 122 | if (!that.popped_out) { |
|
106 | 123 | that.$minimize |
|
107 | 124 | .removeClass('icon-arrow-down') |
|
108 | 125 | .addClass('icon-arrow-up'); |
|
109 | 126 | |
|
110 | 127 | that.$window |
|
111 | 128 | .draggable('destroy') |
|
112 | 129 | .resizable('destroy') |
|
113 | 130 | .removeClass('widget-modal modal') |
|
114 | 131 | .addClass('docked-widget-modal') |
|
115 | 132 | .detach() |
|
116 | 133 | .insertBefore(that.$show_button); |
|
117 | 134 | that.$show_button.hide(); |
|
118 | 135 | that.$close.hide(); |
|
119 | 136 | } else { |
|
120 | 137 | that.$minimize |
|
121 | 138 | .addClass('icon-arrow-down') |
|
122 | 139 | .removeClass('icon-arrow-up'); |
|
123 | 140 | |
|
124 | 141 | that.$window |
|
125 | 142 | .removeClass('docked-widget-modal') |
|
126 | 143 | .addClass('widget-modal modal') |
|
127 | 144 | .detach() |
|
128 | 145 | .appendTo($('#notebook-container')) |
|
129 | 146 | .draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'}) |
|
130 | 147 | .resizable() |
|
131 | 148 | .children('.ui-resizable-handle').show(); |
|
132 | 149 | that.show(); |
|
133 | 150 | that.$show_button.show(); |
|
134 | 151 | that.$close.show(); |
|
135 | 152 | } |
|
136 | 153 | event.stopPropagation(); |
|
137 | 154 | }); |
|
138 | 155 | this.$title = $('<div />') |
|
139 | 156 | .addClass('widget-modal-title') |
|
140 | 157 | .html(" ") |
|
141 | 158 | .appendTo(this.$title_bar); |
|
142 | 159 | this.$body = $('<div />') |
|
143 | 160 | .addClass('modal-body') |
|
144 | 161 | .addClass('widget-modal-body') |
|
145 | 162 | .addClass('widget-container') |
|
146 | 163 | .addClass('vbox') |
|
147 | 164 | .appendTo(this.$window); |
|
148 | 165 | |
|
149 | 166 | this.$show_button = $('<button />') |
|
150 | 167 | .html(" ") |
|
151 | 168 | .addClass('btn btn-info widget-modal-show') |
|
152 | 169 | .appendTo(this.$el) |
|
153 | 170 | .click(function(){ |
|
154 | 171 | that.show(); |
|
155 | 172 | }); |
|
156 | 173 | |
|
157 | 174 | this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'}); |
|
158 | 175 | this.$window.resizable(); |
|
159 | 176 | this.$window.on('resize', function(){ |
|
160 | 177 | that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight()); |
|
161 | 178 | }); |
|
162 | 179 | |
|
163 | 180 | this.$el_to_style = this.$body; |
|
164 | 181 | this._shown_once = false; |
|
165 | 182 | this.popped_out = true; |
|
166 | 183 | |
|
167 | 184 | this.update_children([], this.model.get('_children')); |
|
168 | 185 | this.model.on('change:_children', function(model, value, options) { |
|
169 | 186 | this.update_children(model.previous('_children'), value); |
|
170 | 187 | }, this); |
|
171 | 188 | this.update(); |
|
189 | ||
|
190 | // Trigger model displayed events for any models that are child to | |
|
191 | // this model when this model is displayed. | |
|
192 | this.model.on('displayed', function(){ | |
|
193 | that.is_displayed = true; | |
|
194 | for (var property in that.child_views) { | |
|
195 | if (that.child_views.hasOwnProperty(property)) { | |
|
196 | that.child_views[property].model.trigger('displayed'); | |
|
197 | } | |
|
198 | } | |
|
199 | }); | |
|
172 | 200 | }, |
|
173 | 201 | |
|
174 | 202 | hide: function() { |
|
175 | 203 | // Called when the modal hide button is clicked. |
|
176 | 204 | this.$window.hide(); |
|
177 | 205 | this.$show_button.removeClass('btn-info'); |
|
178 | 206 | }, |
|
179 | 207 | |
|
180 | 208 | show: function() { |
|
181 | 209 | // Called when the modal show button is clicked. |
|
182 | 210 | this.$show_button.addClass('btn-info'); |
|
183 | 211 | this.$window.show(); |
|
184 | 212 | if (this.popped_out) { |
|
185 | 213 | this.$window.css("positon", "absolute"); |
|
186 | 214 | this.$window.css("top", "0px"); |
|
187 | 215 | this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) + |
|
188 | 216 | $(window).scrollLeft()) + "px"); |
|
189 | 217 | this.bring_to_front(); |
|
190 | 218 | } |
|
191 | 219 | }, |
|
192 | 220 | |
|
193 | 221 | bring_to_front: function() { |
|
194 | 222 | // Make the modal top-most, z-ordered about the other modals. |
|
195 | 223 | var $widget_modals = $(".widget-modal"); |
|
196 | 224 | var max_zindex = 0; |
|
197 | 225 | $widget_modals.each(function (index, el){ |
|
198 | 226 | max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index'))); |
|
199 | 227 | }); |
|
200 | 228 | |
|
201 | 229 | // Start z-index of widget modals at 2000 |
|
202 | 230 | max_zindex = Math.max(max_zindex, 2000); |
|
203 | 231 | |
|
204 | 232 | $widget_modals.each(function (index, el){ |
|
205 | 233 | $el = $(el); |
|
206 | 234 | if (max_zindex == parseInt($el.css('z-index'))) { |
|
207 | 235 | $el.css('z-index', max_zindex - 1); |
|
208 | 236 | } |
|
209 | 237 | }); |
|
210 | 238 | this.$window.css('z-index', max_zindex); |
|
211 | 239 | }, |
|
212 | 240 | |
|
213 | 241 | update_children: function(old_list, new_list) { |
|
214 | 242 | // Called when the children list is modified. |
|
215 | 243 | this.do_diff(old_list, |
|
216 | 244 | new_list, |
|
217 | 245 | $.proxy(this.remove_child_model, this), |
|
218 | 246 | $.proxy(this.add_child_model, this)); |
|
219 | 247 | }, |
|
220 | 248 | |
|
221 | 249 | remove_child_model: function(model) { |
|
222 | 250 | // Called when a child is removed from children list. |
|
223 | 251 | this.child_views[model.id].remove(); |
|
224 | 252 | this.delete_child_view(model); |
|
225 | 253 | }, |
|
226 | 254 | |
|
227 | 255 | add_child_model: function(model) { |
|
228 | 256 | // Called when a child is added to children list. |
|
229 | 257 | var view = this.create_child_view(model); |
|
230 | 258 | this.$body.append(view.$el); |
|
259 | ||
|
260 | // Trigger the displayed event if this model is displayed. | |
|
261 | if (this.is_displayed) { | |
|
262 | model.trigger('displayed'); | |
|
263 | } | |
|
231 | 264 | }, |
|
232 | 265 | |
|
233 | 266 | update: function(){ |
|
234 | 267 | // Update the contents of this view |
|
235 | 268 | // |
|
236 | 269 | // Called when the model is changed. The model may have been |
|
237 | 270 | // changed by another view or by a state update from the back-end. |
|
238 | 271 | var description = this.model.get('description'); |
|
239 | 272 | if (description.trim().length === 0) { |
|
240 | 273 | this.$title.html(" "); // Preserve title height |
|
241 | 274 | } else { |
|
242 | 275 | this.$title.text(description); |
|
243 | 276 | } |
|
244 | 277 | |
|
245 | 278 | var button_text = this.model.get('button_text'); |
|
246 | 279 | if (button_text.trim().length === 0) { |
|
247 | 280 | this.$show_button.html(" "); // Preserve button height |
|
248 | 281 | } else { |
|
249 | 282 | this.$show_button.text(button_text); |
|
250 | 283 | } |
|
251 | 284 | |
|
252 | 285 | if (!this._shown_once) { |
|
253 | 286 | this._shown_once = true; |
|
254 | 287 | this.show(); |
|
255 | 288 | } |
|
256 | 289 | |
|
257 | 290 | return PopupView.__super__.update.apply(this); |
|
258 | 291 | }, |
|
259 | 292 | |
|
260 | 293 | _get_selector_element: function(selector) { |
|
261 | 294 | // Get an element view a 'special' jquery selector. (see widget.js) |
|
262 | 295 | // |
|
263 | 296 | // Since the modal actually isn't within the $el in the DOM, we need to extend |
|
264 | 297 | // the selector logic to allow the user to set css on the modal if need be. |
|
265 | 298 | // The convention used is: |
|
266 | 299 | // "modal" - select the modal div |
|
267 | 300 | // "modal [selector]" - select element(s) within the modal div. |
|
268 | 301 | // "[selector]" - select elements within $el |
|
269 | 302 | // "" - select the $el_to_style |
|
270 | 303 | if (selector.substring(0, 5) == 'modal') { |
|
271 | 304 | if (selector == 'modal') { |
|
272 | 305 | return this.$window; |
|
273 | 306 | } else { |
|
274 | 307 | return this.$window.find(selector.substring(6)); |
|
275 | 308 | } |
|
276 | 309 | } else { |
|
277 | 310 | return PopupView.__super__._get_selector_element.apply(this, [selector]); |
|
278 | 311 | } |
|
279 | 312 | }, |
|
280 | 313 | }); |
|
281 | 314 | WidgetManager.register_widget_view('PopupView', PopupView); |
|
282 | 315 | }); |
@@ -1,243 +1,273 | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2013 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // SelectionContainerWidget |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | 12 | /** |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | **/ |
|
16 | 16 | |
|
17 | 17 | define(["widgets/js/widget"], function(WidgetManager){ |
|
18 | 18 | |
|
19 | 19 | var AccordionView = IPython.DOMWidgetView.extend({ |
|
20 | 20 | render: function(){ |
|
21 | 21 | // Called when view is rendered. |
|
22 | 22 | var guid = 'accordion' + IPython.utils.uuid(); |
|
23 | 23 | this.$el |
|
24 | 24 | .attr('id', guid) |
|
25 | 25 | .addClass('accordion'); |
|
26 | 26 | this.containers = []; |
|
27 | 27 | this.model_containers = {}; |
|
28 | 28 | this.update_children([], this.model.get('_children')); |
|
29 | 29 | this.model.on('change:_children', function(model, value, options) { |
|
30 | 30 | this.update_children(model.previous('_children'), value); |
|
31 | 31 | }, this); |
|
32 | 32 | this.model.on('change:selected_index', function(model, value, options) { |
|
33 | 33 | this.update_selected_index(model.previous('selected_index'), value, options); |
|
34 | 34 | }, this); |
|
35 | 35 | this.model.on('change:_titles', function(model, value, options) { |
|
36 | 36 | this.update_titles(value); |
|
37 | 37 | }, this); |
|
38 | var that = this; | |
|
38 | 39 | this.model.on('displayed', function() { |
|
39 | 40 | this.update_titles(); |
|
41 | // Trigger model displayed events for any models that are child to | |
|
42 | // this model when this model is displayed. | |
|
43 | that.is_displayed = true; | |
|
44 | for (var property in that.child_views) { | |
|
45 | if (that.child_views.hasOwnProperty(property)) { | |
|
46 | that.child_views[property].model.trigger('displayed'); | |
|
47 | } | |
|
48 | } | |
|
40 | 49 | }, this); |
|
41 | 50 | }, |
|
42 | 51 | |
|
43 | 52 | update_titles: function(titles) { |
|
44 | 53 | // Set tab titles |
|
45 | 54 | if (!titles) { |
|
46 | 55 | titles = this.model.get('_titles'); |
|
47 | 56 | } |
|
48 | 57 | |
|
49 | 58 | var that = this; |
|
50 | 59 | _.each(titles, function(title, page_index) { |
|
51 | 60 | var accordian = that.containers[page_index]; |
|
52 | 61 | if (accordian !== undefined) { |
|
53 | 62 | accordian |
|
54 | 63 | .find('.accordion-heading') |
|
55 | 64 | .find('.accordion-toggle') |
|
56 | 65 | .text(title); |
|
57 | 66 | } |
|
58 | 67 | }); |
|
59 | 68 | }, |
|
60 | 69 | |
|
61 | 70 | update_selected_index: function(old_index, new_index, options) { |
|
62 | 71 | // Only update the selection if the selection wasn't triggered |
|
63 | 72 | // by the front-end. It must be triggered by the back-end. |
|
64 | 73 | if (options === undefined || options.updated_view != this) { |
|
65 | 74 | this.containers[old_index].find('.accordion-body').collapse('hide'); |
|
66 | 75 | if (0 <= new_index && new_index < this.containers.length) { |
|
67 | 76 | this.containers[new_index].find('.accordion-body').collapse('show'); |
|
68 | 77 | } |
|
69 | 78 | } |
|
70 | 79 | }, |
|
71 | 80 | |
|
72 | 81 | update_children: function(old_list, new_list) { |
|
73 | 82 | // Called when the children list is modified. |
|
74 | 83 | this.do_diff(old_list, |
|
75 | 84 | new_list, |
|
76 | 85 | $.proxy(this.remove_child_model, this), |
|
77 | 86 | $.proxy(this.add_child_model, this)); |
|
78 | 87 | }, |
|
79 | 88 | |
|
80 | 89 | remove_child_model: function(model) { |
|
81 | 90 | // Called when a child is removed from children list. |
|
82 | 91 | var accordion_group = this.model_containers[model.id]; |
|
83 | 92 | this.containers.splice(accordion_group.container_index, 1); |
|
84 | 93 | delete this.model_containers[model.id]; |
|
85 | 94 | accordion_group.remove(); |
|
86 | 95 | this.delete_child_view(model); |
|
87 | 96 | }, |
|
88 | 97 | |
|
89 | 98 | add_child_model: function(model) { |
|
90 | 99 | // Called when a child is added to children list. |
|
91 | 100 | var view = this.create_child_view(model); |
|
92 | 101 | var index = this.containers.length; |
|
93 | 102 | var uuid = IPython.utils.uuid(); |
|
94 | 103 | var accordion_group = $('<div />') |
|
95 | 104 | .addClass('accordion-group') |
|
96 | 105 | .appendTo(this.$el); |
|
97 | 106 | var accordion_heading = $('<div />') |
|
98 | 107 | .addClass('accordion-heading') |
|
99 | 108 | .appendTo(accordion_group); |
|
100 | 109 | var that = this; |
|
101 | 110 | var accordion_toggle = $('<a />') |
|
102 | 111 | .addClass('accordion-toggle') |
|
103 | 112 | .attr('data-toggle', 'collapse') |
|
104 | 113 | .attr('data-parent', '#' + this.$el.attr('id')) |
|
105 | 114 | .attr('href', '#' + uuid) |
|
106 | 115 | .click(function(evt){ |
|
107 | 116 | |
|
108 | 117 | // Calling model.set will trigger all of the other views of the |
|
109 | 118 | // model to update. |
|
110 | 119 | that.model.set("selected_index", index, {updated_view: that}); |
|
111 | 120 | that.touch(); |
|
112 | 121 | }) |
|
113 | 122 | .text('Page ' + index) |
|
114 | 123 | .appendTo(accordion_heading); |
|
115 | 124 | var accordion_body = $('<div />', {id: uuid}) |
|
116 | 125 | .addClass('accordion-body collapse') |
|
117 | 126 | .appendTo(accordion_group); |
|
118 | 127 | var accordion_inner = $('<div />') |
|
119 | 128 | .addClass('accordion-inner') |
|
120 | 129 | .appendTo(accordion_body); |
|
121 | 130 | var container_index = this.containers.push(accordion_group) - 1; |
|
122 | 131 | accordion_group.container_index = container_index; |
|
123 | 132 | this.model_containers[model.id] = accordion_group; |
|
124 | 133 | accordion_inner.append(view.$el); |
|
125 | 134 | |
|
126 | 135 | this.update(); |
|
127 | 136 | this.update_titles(); |
|
137 | ||
|
138 | // Trigger the displayed event if this model is displayed. | |
|
139 | if (this.is_displayed) { | |
|
140 | model.trigger('displayed'); | |
|
141 | } | |
|
128 | 142 | }, |
|
129 | 143 | }); |
|
130 | 144 | WidgetManager.register_widget_view('AccordionView', AccordionView); |
|
131 | 145 | |
|
132 | 146 | |
|
133 | 147 | var TabView = IPython.DOMWidgetView.extend({ |
|
134 | 148 | initialize: function() { |
|
135 | 149 | // Public constructor. |
|
136 | 150 | this.containers = []; |
|
137 | 151 | TabView.__super__.initialize.apply(this, arguments); |
|
138 | 152 | }, |
|
139 | 153 | |
|
140 | 154 | render: function(){ |
|
141 | 155 | // Called when view is rendered. |
|
142 | 156 | var uuid = 'tabs'+IPython.utils.uuid(); |
|
143 | 157 | var that = this; |
|
144 | 158 | this.$tabs = $('<div />', {id: uuid}) |
|
145 | 159 | .addClass('nav') |
|
146 | 160 | .addClass('nav-tabs') |
|
147 | 161 | .appendTo(this.$el); |
|
148 | 162 | this.$tab_contents = $('<div />', {id: uuid + 'Content'}) |
|
149 | 163 | .addClass('tab-content') |
|
150 | 164 | .appendTo(this.$el); |
|
151 | 165 | this.containers = []; |
|
152 | 166 | this.update_children([], this.model.get('_children')); |
|
153 | 167 | this.model.on('change:_children', function(model, value, options) { |
|
154 | 168 | this.update_children(model.previous('_children'), value); |
|
155 | 169 | }, this); |
|
170 | ||
|
171 | // Trigger model displayed events for any models that are child to | |
|
172 | // this model when this model is displayed. | |
|
173 | this.model.on('displayed', function(){ | |
|
174 | that.is_displayed = true; | |
|
175 | for (var property in that.child_views) { | |
|
176 | if (that.child_views.hasOwnProperty(property)) { | |
|
177 | that.child_views[property].model.trigger('displayed'); | |
|
178 | } | |
|
179 | } | |
|
180 | }); | |
|
156 | 181 | }, |
|
157 | 182 | |
|
158 | 183 | update_children: function(old_list, new_list) { |
|
159 | 184 | // Called when the children list is modified. |
|
160 | 185 | this.do_diff(old_list, |
|
161 | 186 | new_list, |
|
162 | 187 | $.proxy(this.remove_child_model, this), |
|
163 | 188 | $.proxy(this.add_child_model, this)); |
|
164 | 189 | }, |
|
165 | 190 | |
|
166 | 191 | remove_child_model: function(model) { |
|
167 | 192 | // Called when a child is removed from children list. |
|
168 | 193 | var view = this.child_views[model.id]; |
|
169 | 194 | this.containers.splice(view.parent_tab.tab_text_index, 1); |
|
170 | 195 | view.parent_tab.remove(); |
|
171 | 196 | view.parent_container.remove(); |
|
172 | 197 | view.remove(); |
|
173 | 198 | this.delete_child_view(model); |
|
174 | 199 | }, |
|
175 | 200 | |
|
176 | 201 | add_child_model: function(model) { |
|
177 | 202 | // Called when a child is added to children list. |
|
178 | 203 | var view = this.create_child_view(model); |
|
179 | 204 | var index = this.containers.length; |
|
180 | 205 | var uuid = IPython.utils.uuid(); |
|
181 | 206 | |
|
182 | 207 | var that = this; |
|
183 | 208 | var tab = $('<li />') |
|
184 | 209 | .css('list-style-type', 'none') |
|
185 | 210 | .appendTo(this.$tabs); |
|
186 | 211 | view.parent_tab = tab; |
|
187 | 212 | |
|
188 | 213 | var tab_text = $('<a />') |
|
189 | 214 | .attr('href', '#' + uuid) |
|
190 | 215 | .attr('data-toggle', 'tab') |
|
191 | 216 | .text('Page ' + index) |
|
192 | 217 | .appendTo(tab) |
|
193 | 218 | .click(function (e) { |
|
194 | 219 | |
|
195 | 220 | // Calling model.set will trigger all of the other views of the |
|
196 | 221 | // model to update. |
|
197 | 222 | that.model.set("selected_index", index, {updated_view: this}); |
|
198 | 223 | that.touch(); |
|
199 | 224 | that.select_page(index); |
|
200 | 225 | }); |
|
201 | 226 | tab.tab_text_index = this.containers.push(tab_text) - 1; |
|
202 | 227 | |
|
203 | 228 | var contents_div = $('<div />', {id: uuid}) |
|
204 | 229 | .addClass('tab-pane') |
|
205 | 230 | .addClass('fade') |
|
206 | 231 | .append(view.$el) |
|
207 | 232 | .appendTo(this.$tab_contents); |
|
208 | 233 | view.parent_container = contents_div; |
|
234 | ||
|
235 | // Trigger the displayed event if this model is displayed. | |
|
236 | if (this.is_displayed) { | |
|
237 | model.trigger('displayed'); | |
|
238 | } | |
|
209 | 239 | }, |
|
210 | 240 | |
|
211 | 241 | update: function(options) { |
|
212 | 242 | // Update the contents of this view |
|
213 | 243 | // |
|
214 | 244 | // Called when the model is changed. The model may have been |
|
215 | 245 | // changed by another view or by a state update from the back-end. |
|
216 | 246 | if (options === undefined || options.updated_view != this) { |
|
217 | 247 | // Set tab titles |
|
218 | 248 | var titles = this.model.get('_titles'); |
|
219 | 249 | var that = this; |
|
220 | 250 | _.each(titles, function(title, page_index) { |
|
221 | 251 | var tab_text = that.containers[page_index]; |
|
222 | 252 | if (tab_text !== undefined) { |
|
223 | 253 | tab_text.text(title); |
|
224 | 254 | } |
|
225 | 255 | }); |
|
226 | 256 | |
|
227 | 257 | var selected_index = this.model.get('selected_index'); |
|
228 | 258 | if (0 <= selected_index && selected_index < this.containers.length) { |
|
229 | 259 | this.select_page(selected_index); |
|
230 | 260 | } |
|
231 | 261 | } |
|
232 | 262 | return TabView.__super__.update.apply(this); |
|
233 | 263 | }, |
|
234 | 264 | |
|
235 | 265 | select_page: function(index) { |
|
236 | 266 | // Select a page. |
|
237 | 267 | this.$tabs.find('li') |
|
238 | 268 | .removeClass('active'); |
|
239 | 269 | this.containers[index].tab('show'); |
|
240 | 270 | }, |
|
241 | 271 | }); |
|
242 | 272 | WidgetManager.register_widget_view('TabView', TabView); |
|
243 | 273 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now