multicontainer.js
208 lines
| 7.7 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. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// MultiContainerWidget | ||||
//============================================================================ | ||||
/** | ||||
* @module IPython | ||||
* @namespace IPython | ||||
**/ | ||||
Jonathan Frederic
|
r14470 | define(["notebook/js/widgets/base"], function(widget_manager){ | ||
Jonathan Frederic
|
r14284 | var MulticontainerModel = IPython.WidgetModel.extend({}); | ||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_model('MulticontainerWidgetModel', MulticontainerModel); | ||
Jonathan Frederic
|
r14284 | |||
var AccordionView = IPython.WidgetView.extend({ | ||||
render: function(){ | ||||
Jonathan Frederic
|
r14398 | var guid = 'accordion' + IPython.utils.uuid(); | ||
Jonathan Frederic
|
r14455 | this.$el | ||
.attr('id', guid) | ||||
Jonathan Frederic
|
r14284 | .addClass('accordion'); | ||
this.containers = []; | ||||
Jason Grout
|
r14503 | this.update_children([], this.model.get('children')); | ||
this.model.on('change:children', function(model, value, options) { | ||||
Jonathan Frederic
|
r14507 | this.update_children(model.previous('children'), value); | ||
}, this); | ||||
Jason Grout
|
r14503 | }, | ||
update_children: function(old_list, new_list) { | ||||
_.each(this.containers, function(element, index, list) { | ||||
element.remove(); | ||||
}, this); | ||||
this.containers = []; | ||||
this.update_child_views(old_list, new_list); | ||||
_.each(new_list, function(element, index, list) { | ||||
this.add_child_view(this.child_views[element]); | ||||
}, this) | ||||
Jonathan Frederic
|
r14284 | }, | ||
Jason Grout
|
r14503 | |||
Jonathan Frederic
|
r14284 | update: function() { | ||
Jonathan Frederic
|
r14288 | // Set tab titles | ||
var titles = this.model.get('_titles'); | ||||
for (var page_index in titles) { | ||||
Jonathan Frederic
|
r14289 | |||
Jonathan Frederic
|
r14466 | var accordian = this.containers[page_index]; | ||
if (accordian !== undefined) { | ||||
Jonathan Frederic
|
r14289 | accordian | ||
.find('.accordion-heading') | ||||
.find('.accordion-toggle') | ||||
.html(titles[page_index]); | ||||
} | ||||
Jonathan Frederic
|
r14288 | } | ||
Jonathan Frederic
|
r14284 | |||
Jonathan Frederic
|
r14370 | // Set selected page | ||
var selected_index = this.model.get("selected_index"); | ||||
if (0 <= selected_index && selected_index < this.containers.length) { | ||||
for (var index in this.containers) { | ||||
if (index==selected_index) { | ||||
this.containers[index].find('.accordion-body').collapse('show'); | ||||
} else { | ||||
this.containers[index].find('.accordion-body').collapse('hide'); | ||||
} | ||||
} | ||||
} | ||||
Jonathan Frederic
|
r14284 | return IPython.WidgetView.prototype.update.call(this); | ||
}, | ||||
Jason Grout
|
r14489 | add_child_view: function(view) { | ||
Jonathan Frederic
|
r14284 | |||
var index = this.containers.length; | ||||
var uuid = IPython.utils.uuid(); | ||||
var accordion_group = $('<div />') | ||||
.addClass('accordion-group') | ||||
.appendTo(this.$el); | ||||
var accordion_heading = $('<div />') | ||||
.addClass('accordion-heading') | ||||
.appendTo(accordion_group); | ||||
Jonathan Frederic
|
r14370 | var that = this; | ||
Jonathan Frederic
|
r14284 | var accordion_toggle = $('<a />') | ||
.addClass('accordion-toggle') | ||||
.attr('data-toggle', 'collapse') | ||||
.attr('data-parent', '#' + this.$el.attr('id')) | ||||
.attr('href', '#' + uuid) | ||||
Jonathan Frederic
|
r14370 | .click(function(evt){ | ||
that.model.set("selected_index", index); | ||||
Jonathan Frederic
|
r14482 | that.touch(); | ||
Jonathan Frederic
|
r14370 | }) | ||
Jonathan Frederic
|
r14284 | .html('Page ' + index) | ||
.appendTo(accordion_heading); | ||||
var accordion_body = $('<div />', {id: uuid}) | ||||
Jonathan Frederic
|
r14288 | .addClass('accordion-body collapse') | ||
Jonathan Frederic
|
r14284 | .appendTo(accordion_group); | ||
var accordion_inner = $('<div />') | ||||
.addClass('accordion-inner') | ||||
.appendTo(accordion_body); | ||||
this.containers.push(accordion_group); | ||||
accordion_inner.append(view.$el); | ||||
Jonathan Frederic
|
r14370 | |||
Jonathan Frederic
|
r14289 | this.update(); | ||
Jonathan Frederic
|
r14370 | |||
// Stupid workaround to close the bootstrap accordion tabs which | ||||
// open by default even though they don't have the `in` class | ||||
// attached to them. For some reason a delay is required. | ||||
// TODO: Better fix. | ||||
Jonathan Frederic
|
r14466 | setTimeout(function(){ that.update(); }, 500); | ||
Jonathan Frederic
|
r14284 | }, | ||
}); | ||||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_view('AccordionView', AccordionView); | ||
Jonathan Frederic
|
r14290 | |||
var TabView = IPython.WidgetView.extend({ | ||||
Jonathan Frederic
|
r14507 | initialize: function() { | ||
this.containers = []; | ||||
IPython.WidgetView.prototype.initialize.apply(this, arguments); | ||||
}, | ||||
Jason Grout
|
r14486 | |||
Jonathan Frederic
|
r14290 | render: function(){ | ||
Jonathan Frederic
|
r14398 | var uuid = 'tabs'+IPython.utils.uuid(); | ||
Jonathan Frederic
|
r14307 | var that = this; | ||
Jonathan Frederic
|
r14290 | this.$tabs = $('<div />', {id: uuid}) | ||
.addClass('nav') | ||||
.addClass('nav-tabs') | ||||
.appendTo(this.$el); | ||||
this.$tab_contents = $('<div />', {id: uuid + 'Content'}) | ||||
.addClass('tab-content') | ||||
.appendTo(this.$el); | ||||
Jonathan Frederic
|
r14507 | this.containers = []; | ||
this.update_children([], this.model.get('children')); | ||||
this.model.on('change:children', function(model, value, options) { | ||||
this.update_children(model.previous('children'), value); | ||||
}, this); | ||||
}, | ||||
update_children: function(old_list, new_list) { | ||||
_.each(this.containers, function(element, index, list) { | ||||
element.remove(); | ||||
}, this); | ||||
this.containers = []; | ||||
this.update_child_views(old_list, new_list); | ||||
_.each(new_list, function(element, index, list) { | ||||
this.add_child_view(this.child_views[element]); | ||||
}, this) | ||||
Jonathan Frederic
|
r14290 | }, | ||
Jonathan Frederic
|
r14370 | |||
Jonathan Frederic
|
r14290 | update: function() { | ||
// Set tab titles | ||||
var titles = this.model.get('_titles'); | ||||
for (var page_index in titles) { | ||||
Jonathan Frederic
|
r14466 | var tab_text = this.containers[page_index]; | ||
if (tab_text !== undefined) { | ||||
Jonathan Frederic
|
r14290 | tab_text.html(titles[page_index]); | ||
} | ||||
} | ||||
Jonathan Frederic
|
r14307 | var selected_index = this.model.get('selected_index'); | ||
if (0 <= selected_index && selected_index < this.containers.length) { | ||||
this.select_page(selected_index); | ||||
} | ||||
Jonathan Frederic
|
r14290 | return IPython.WidgetView.prototype.update.call(this); | ||
}, | ||||
Jason Grout
|
r14487 | add_child_view: function(view) { | ||
Jonathan Frederic
|
r14290 | var index = this.containers.length; | ||
var uuid = IPython.utils.uuid(); | ||||
var that = this; | ||||
var tab = $('<li />') | ||||
.css('list-style-type', 'none') | ||||
.appendTo(this.$tabs); | ||||
var tab_text = $('<a />') | ||||
.attr('href', '#' + uuid) | ||||
.attr('data-toggle', 'tab') | ||||
.html('Page ' + index) | ||||
.appendTo(tab) | ||||
.click(function (e) { | ||||
Jonathan Frederic
|
r14307 | that.model.set("selected_index", index); | ||
Jonathan Frederic
|
r14482 | that.touch(); | ||
Jonathan Frederic
|
r14307 | that.select_page(index); | ||
Jonathan Frederic
|
r14290 | }); | ||
this.containers.push(tab_text); | ||||
var contents_div = $('<div />', {id: uuid}) | ||||
.addClass('tab-pane') | ||||
.addClass('fade') | ||||
.append(view.$el) | ||||
.appendTo(this.$tab_contents); | ||||
}, | ||||
Jonathan Frederic
|
r14307 | |||
select_page: function(index) { | ||||
this.$tabs.find('li') | ||||
.removeClass('active'); | ||||
this.containers[index].tab('show'); | ||||
}, | ||||
Jonathan Frederic
|
r14290 | }); | ||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_view('TabView', TabView); | ||
Jonathan Frederic
|
r14290 | }); | ||