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