widget_container.js
272 lines
| 10.6 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r14366 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2013 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// ContainerWidget | ||||
//============================================================================ | ||||
/** | ||||
* @module IPython | ||||
* @namespace IPython | ||||
**/ | ||||
Jonathan Frederic
|
r14627 | define(["notebook/js/widgets/widget"], function(WidgetManager) { | ||
Jonathan Frederic
|
r14609 | |||
var ContainerView = IPython.DOMWidgetView.extend({ | ||||
Jonathan Frederic
|
r14333 | render: function(){ | ||
Jonathan Frederic
|
r14609 | // Called when view is rendered. | ||
Jonathan Frederic
|
r14396 | this.$el | ||
Jonathan Frederic
|
r14293 | .addClass('widget-container'); | ||
Jason Grout
|
r14503 | this.children={}; | ||
Jonathan Frederic
|
r14611 | this.update_children([], this.model.get('_children')); | ||
this.model.on('change:_children', function(model, value, options) { | ||||
this.update_children(model.previous('_children'), value); | ||||
Jason Grout
|
r14503 | }, this); | ||
this.update() | ||||
}, | ||||
update_children: function(old_list, new_list) { | ||||
Jonathan Frederic
|
r14609 | // Called when the children list changes. | ||
Jonathan Frederic
|
r14598 | this.do_diff(old_list, | ||
new_list, | ||||
$.proxy(this.remove_child_model, this), | ||||
$.proxy(this.add_child_model, this)); | ||||
}, | ||||
remove_child_model: function(model) { | ||||
Jonathan Frederic
|
r14609 | // Called when a model is removed from the children list. | ||
Jonathan Frederic
|
r14598 | this.child_views[model.id].remove(); | ||
this.delete_child_view(model); | ||||
}, | ||||
add_child_model: function(model) { | ||||
Jonathan Frederic
|
r14609 | // Called when a model is added to the children list. | ||
Jonathan Frederic
|
r14598 | var view = this.create_child_view(model); | ||
this.$el.append(view.$el); | ||||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14333 | update: function(){ | ||
Jonathan Frederic
|
r14568 | // Update the contents of this view | ||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
Jonathan Frederic
|
r14583 | return ContainerView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14268 | }, | ||
Jonathan Frederic
|
r14263 | }); | ||
Jonathan Frederic
|
r14627 | WidgetManager.register_widget_view('ContainerView', ContainerView); | ||
Jonathan Frederic
|
r14409 | |||
Jonathan Frederic
|
r14676 | var PopupView = IPython.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14409 | render: function(){ | ||
Jonathan Frederic
|
r14609 | // Called when view is rendered. | ||
Jonathan Frederic
|
r14409 | var that = this; | ||
Jonathan Frederic
|
r14507 | this.children={}; | ||
Jonathan Frederic
|
r14611 | this.update_children([], this.model.get('_children')); | ||
this.model.on('change:_children', function(model, value, options) { | ||||
this.update_children(model.previous('_children'), value); | ||||
Jonathan Frederic
|
r14507 | }, this); | ||
Jonathan Frederic
|
r14409 | this.$el | ||
.on("remove", function(){ | ||||
that.$window.remove(); | ||||
}); | ||||
this.$window = $('<div />') | ||||
.addClass('modal widget-modal') | ||||
Jonathan Frederic
|
r14443 | .appendTo($('#notebook-container')) | ||
.mousedown(function(){ | ||||
that.bring_to_front(); | ||||
}); | ||||
Jonathan Frederic
|
r14410 | this.$title_bar = $('<div />') | ||
Jonathan Frederic
|
r14409 | .addClass('popover-title') | ||
Jonathan Frederic
|
r14443 | .appendTo(this.$window) | ||
.mousedown(function(){ | ||||
that.bring_to_front(); | ||||
Jonathan Frederic
|
r14466 | }); | ||
Jonathan Frederic
|
r14443 | this.$close = $('<button />') | ||
.addClass('close icon-remove') | ||||
.css('margin-left', '5px') | ||||
Jonathan Frederic
|
r14410 | .appendTo(this.$title_bar) | ||
Jonathan Frederic
|
r14409 | .click(function(){ | ||
that.hide(); | ||||
event.stopPropagation(); | ||||
}); | ||||
Jonathan Frederic
|
r14443 | this.$minimize = $('<button />') | ||
.addClass('close icon-arrow-down') | ||||
.appendTo(this.$title_bar) | ||||
.click(function(){ | ||||
that.popped_out = !that.popped_out; | ||||
if (!that.popped_out) { | ||||
that.$minimize | ||||
.removeClass('icon-arrow-down') | ||||
.addClass('icon-arrow-up'); | ||||
that.$window | ||||
.draggable('destroy') | ||||
.resizable('destroy') | ||||
.removeClass('widget-modal modal') | ||||
.addClass('docked-widget-modal') | ||||
.detach() | ||||
.insertBefore(that.$show_button); | ||||
that.$show_button.hide(); | ||||
that.$close.hide(); | ||||
} else { | ||||
that.$minimize | ||||
.addClass('icon-arrow-down') | ||||
.removeClass('icon-arrow-up'); | ||||
that.$window | ||||
.removeClass('docked-widget-modal') | ||||
.addClass('widget-modal modal') | ||||
.detach() | ||||
.appendTo($('#notebook-container')) | ||||
.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'}) | ||||
.resizable() | ||||
.children('.ui-resizable-handle').show(); | ||||
that.show(); | ||||
that.$show_button.show(); | ||||
that.$close.show(); | ||||
} | ||||
event.stopPropagation(); | ||||
}); | ||||
Jonathan Frederic
|
r14409 | this.$title = $('<div />') | ||
.addClass('widget-modal-title') | ||||
Jonathan Frederic
|
r14663 | .text(' ') | ||
Jonathan Frederic
|
r14410 | .appendTo(this.$title_bar); | ||
Jonathan Frederic
|
r14409 | this.$body = $('<div />') | ||
.addClass('modal-body') | ||||
Jonathan Frederic
|
r14411 | .addClass('widget-modal-body') | ||
Jonathan Frederic
|
r14409 | .addClass('widget-container') | ||
.appendTo(this.$window); | ||||
this.$show_button = $('<button />') | ||||
Jonathan Frederic
|
r14663 | .text(' ') | ||
Jonathan Frederic
|
r14409 | .addClass('btn btn-info widget-modal-show') | ||
.appendTo(this.$el) | ||||
.click(function(){ | ||||
that.show(); | ||||
}); | ||||
this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'}); | ||||
this.$window.resizable(); | ||||
Jonathan Frederic
|
r14410 | this.$window.on('resize', function(){ | ||
that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight()); | ||||
Jonathan Frederic
|
r14466 | }); | ||
Jonathan Frederic
|
r14410 | |||
Jonathan Frederic
|
r14419 | this.$el_to_style = this.$body; | ||
Jonathan Frederic
|
r14409 | this._shown_once = false; | ||
Jonathan Frederic
|
r14443 | this.popped_out = true; | ||
Jonathan Frederic
|
r14409 | }, | ||
hide: function() { | ||||
Jonathan Frederic
|
r14609 | // Called when the modal hide button is clicked. | ||
Jonathan Frederic
|
r14409 | this.$window.hide(); | ||
this.$show_button.removeClass('btn-info'); | ||||
}, | ||||
show: function() { | ||||
Jonathan Frederic
|
r14609 | // Called when the modal show button is clicked. | ||
Jonathan Frederic
|
r14409 | this.$show_button.addClass('btn-info'); | ||
Jonathan Frederic
|
r14425 | this.$window.show(); | ||
Jonathan Frederic
|
r14443 | if (this.popped_out) { | ||
Jonathan Frederic
|
r14466 | this.$window.css("positon", "absolute"); | ||
Jonathan Frederic
|
r14443 | this.$window.css("top", "0px"); | ||
this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) + | ||||
$(window).scrollLeft()) + "px"); | ||||
this.bring_to_front(); | ||||
} | ||||
}, | ||||
bring_to_front: function() { | ||||
Jonathan Frederic
|
r14609 | // Make the modal top-most, z-ordered about the other modals. | ||
Jonathan Frederic
|
r14443 | var $widget_modals = $(".widget-modal"); | ||
var max_zindex = 0; | ||||
$widget_modals.each(function (index, el){ | ||||
max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index'))); | ||||
}); | ||||
// Start z-index of widget modals at 2000 | ||||
max_zindex = Math.max(max_zindex, 2000); | ||||
$widget_modals.each(function (index, el){ | ||||
Jonathan Frederic
|
r14466 | $el = $(el); | ||
Jonathan Frederic
|
r14443 | if (max_zindex == parseInt($el.css('z-index'))) { | ||
$el.css('z-index', max_zindex - 1); | ||||
} | ||||
}); | ||||
this.$window.css('z-index', max_zindex); | ||||
Jonathan Frederic
|
r14409 | }, | ||
Jonathan Frederic
|
r14507 | update_children: function(old_list, new_list) { | ||
Jonathan Frederic
|
r14609 | // Called when the children list is modified. | ||
Jonathan Frederic
|
r14598 | this.do_diff(old_list, | ||
new_list, | ||||
$.proxy(this.remove_child_model, this), | ||||
$.proxy(this.add_child_model, this)); | ||||
}, | ||||
remove_child_model: function(model) { | ||||
Jonathan Frederic
|
r14609 | // Called when a child is removed from children list. | ||
Jonathan Frederic
|
r14598 | this.child_views[model.id].remove(); | ||
this.delete_child_view(model); | ||||
}, | ||||
add_child_model: function(model) { | ||||
Jonathan Frederic
|
r14609 | // Called when a child is added to children list. | ||
Jonathan Frederic
|
r14598 | var view = this.create_child_view(model); | ||
this.$body.append(view.$el); | ||||
Jonathan Frederic
|
r14507 | }, | ||
Jonathan Frederic
|
r14409 | update: function(){ | ||
Jonathan Frederic
|
r14568 | // Update the contents of this view | ||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
Jonathan Frederic
|
r14409 | var description = this.model.get('description'); | ||
Jonathan Frederic
|
r14466 | if (description.length === 0) { | ||
Jonathan Frederic
|
r14663 | this.$title.text(' '); // Preserve title height | ||
Jonathan Frederic
|
r14409 | } else { | ||
Jonathan Frederic
|
r14663 | this.$title.text(description); | ||
Jonathan Frederic
|
r14409 | } | ||
var button_text = this.model.get('button_text'); | ||||
Jonathan Frederic
|
r14466 | if (button_text.length === 0) { | ||
Jonathan Frederic
|
r14663 | this.$show_button.text(' '); // Preserve button height | ||
Jonathan Frederic
|
r14409 | } else { | ||
Jonathan Frederic
|
r14663 | this.$show_button.text(button_text); | ||
Jonathan Frederic
|
r14409 | } | ||
if (!this._shown_once) { | ||||
this._shown_once = true; | ||||
this.show(); | ||||
} | ||||
Jonathan Frederic
|
r14676 | return PopupView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14409 | }, | ||
Jonathan Frederic
|
r14419 | _get_selector_element: function(selector) { | ||
Jonathan Frederic
|
r14609 | // Get an element view a 'special' jquery selector. (see widget.js) | ||
// | ||||
Jonathan Frederic
|
r14419 | // Since the modal actually isn't within the $el in the DOM, we need to extend | ||
// the selector logic to allow the user to set css on the modal if need be. | ||||
// The convention used is: | ||||
// "modal" - select the modal div | ||||
// "modal [selector]" - select element(s) within the modal div. | ||||
// "[selector]" - select elements within $el | ||||
// "" - select the $el_to_style | ||||
if (selector.substring(0, 5) == 'modal') { | ||||
if (selector == 'modal') { | ||||
return this.$window; | ||||
} else { | ||||
return this.$window.find(selector.substring(6)); | ||||
} | ||||
} else { | ||||
Jonathan Frederic
|
r14676 | return PopupView.__super__._get_selector_element.apply(this, [selector]); | ||
Jonathan Frederic
|
r14419 | } | ||
}, | ||||
Jonathan Frederic
|
r14409 | }); | ||
Jonathan Frederic
|
r14676 | WidgetManager.register_widget_view('PopupView', PopupView); | ||
Jonathan Frederic
|
r14409 | }); | ||