container.js
193 lines
| 7.1 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
|
r14374 | define(["notebook/js/widget"], function(widget_manager) { | ||
Jonathan Frederic
|
r14418 | |||
var set_flex_property = function(element, property_name, enabled) { | ||||
if (enabled) { | ||||
element.addClass(property_name); | ||||
} else { | ||||
element.removeClass(property_name); | ||||
} | ||||
}; | ||||
var set_flex_properties = function(context, element) { | ||||
// Apply flexible box model properties by adding and removing | ||||
// corrosponding CSS classes. | ||||
// Defined in IPython/html/static/base/less/flexbox.less | ||||
set_flex_property(element, 'vbox', context.model.get('_vbox')); | ||||
set_flex_property(element, 'hbox', context.model.get('_hbox')); | ||||
set_flex_property(element, 'start', context.model.get('_pack_start')); | ||||
set_flex_property(element, 'center', context.model.get('_pack_center')); | ||||
set_flex_property(element, 'end', context.model.get('_pack_end')); | ||||
set_flex_property(element, 'align-start', context.model.get('_align_start')); | ||||
set_flex_property(element, 'align-center', context.model.get('_align_center')); | ||||
set_flex_property(element, 'align-end', context.model.get('_align_end')); | ||||
set_flex_property(element, 'box-flex0', context.model.get('_flex0')); | ||||
set_flex_property(element, 'box-flex1', context.model.get('_flex1')); | ||||
set_flex_property(element, 'box-flex2', context.model.get('_flex2')); | ||||
}; | ||||
Jonathan Frederic
|
r14263 | var ContainerModel = IPython.WidgetModel.extend({}); | ||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_model('ContainerWidgetModel', ContainerModel); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | var ContainerView = IPython.WidgetView.extend({ | ||
Jonathan Frederic
|
r14333 | render: function(){ | ||
Jonathan Frederic
|
r14396 | this.$el | ||
Jonathan Frederic
|
r14293 | .addClass('widget-container'); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14333 | update: function(){ | ||
Jonathan Frederic
|
r14418 | set_flex_properties(this, this.$el); | ||
Jonathan Frederic
|
r14279 | return IPython.WidgetView.prototype.update.call(this); | ||
Jonathan Frederic
|
r14268 | }, | ||
Jonathan Frederic
|
r14278 | |||
Jonathan Frederic
|
r14333 | display_child: function(view) { | ||
Jonathan Frederic
|
r14280 | this.$el.append(view.$el); | ||
Jonathan Frederic
|
r14278 | }, | ||
Jonathan Frederic
|
r14263 | }); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_view('ContainerView', ContainerView); | ||
Jonathan Frederic
|
r14409 | |||
var ModalView = IPython.WidgetView.extend({ | ||||
render: function(){ | ||||
var that = this; | ||||
this.$el | ||||
.html('') | ||||
.on("remove", function(){ | ||||
that.$window.remove(); | ||||
}); | ||||
this.$window = $('<div />') | ||||
.addClass('modal widget-modal') | ||||
.appendTo($('#notebook-container')); | ||||
Jonathan Frederic
|
r14410 | this.$title_bar = $('<div />') | ||
Jonathan Frederic
|
r14409 | .addClass('popover-title') | ||
.appendTo(this.$window); | ||||
var that = this; | ||||
$('<button />') | ||||
.addClass('close') | ||||
.html('×') | ||||
Jonathan Frederic
|
r14410 | .appendTo(this.$title_bar) | ||
Jonathan Frederic
|
r14409 | .click(function(){ | ||
that.hide(); | ||||
event.stopPropagation(); | ||||
}); | ||||
this.$title = $('<div />') | ||||
.addClass('widget-modal-title') | ||||
.html(' ') | ||||
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 />') | ||||
.html(' ') | ||||
.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
|
r14419 | this.$el_to_style = this.$body; | ||
Jonathan Frederic
|
r14409 | this._shown_once = false; | ||
}, | ||||
hide: function() { | ||||
this.$window.hide(); | ||||
this.$show_button.removeClass('btn-info'); | ||||
}, | ||||
show: function() { | ||||
this.$show_button.addClass('btn-info'); | ||||
Jonathan Frederic
|
r14425 | |||
this.$window.show(); | ||||
this.$window.css("positon", "absolute") | ||||
Jonathan Frederic
|
r14426 | this.$window.css("top", "0px"); | ||
Jonathan Frederic
|
r14425 | this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) + | ||
$(window).scrollLeft()) + "px"); | ||||
Jonathan Frederic
|
r14409 | }, | ||
update: function(){ | ||||
Jonathan Frederic
|
r14418 | set_flex_properties(this, this.$body); | ||
Jonathan Frederic
|
r14409 | |||
var description = this.model.get('description'); | ||||
description = description.replace(/ /g, ' ', 'm'); | ||||
description = description.replace(/\n/g, '<br>\n', 'm'); | ||||
if (description.length == 0) { | ||||
this.$title.html(' '); // Preserve title height | ||||
} else { | ||||
this.$title.html(description); | ||||
} | ||||
var button_text = this.model.get('button_text'); | ||||
button_text = button_text.replace(/ /g, ' ', 'm'); | ||||
button_text = button_text.replace(/\n/g, '<br>\n', 'm'); | ||||
if (button_text.length == 0) { | ||||
this.$show_button.html(' '); // Preserve button height | ||||
} else { | ||||
this.$show_button.html(button_text); | ||||
} | ||||
if (!this._shown_once) { | ||||
this._shown_once = true; | ||||
this.show(); | ||||
} | ||||
return IPython.WidgetView.prototype.update.call(this); | ||||
}, | ||||
display_child: function(view) { | ||||
this.$body.append(view.$el); | ||||
}, | ||||
Jonathan Frederic
|
r14419 | _get_selector_element: function(selector) { | ||
// 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 { | ||||
return IPython.WidgetView.prototype._get_selector_element.call(this, selector); | ||||
} | ||||
}, | ||||
Jonathan Frederic
|
r14409 | |||
}); | ||||
widget_manager.register_widget_view('ModalView', ModalView); | ||||
}); | ||||