widget_selectioncontainer.js
273 lines
| 10.8 KiB
| application/javascript
|
JavascriptLexer
|
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. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
|
r14578 | // SelectionContainerWidget | ||
|
r14366 | //============================================================================ | ||
/** | ||||
* @module IPython | ||||
* @namespace IPython | ||||
**/ | ||||
|
r15427 | define(["widgets/js/widget"], function(WidgetManager){ | ||
|
r14609 | |||
|
r14564 | var AccordionView = IPython.DOMWidgetView.extend({ | ||
|
r14284 | render: function(){ | ||
|
r14609 | // Called when view is rendered. | ||
|
r14398 | var guid = 'accordion' + IPython.utils.uuid(); | ||
|
r14455 | this.$el | ||
.attr('id', guid) | ||||
|
r14284 | .addClass('accordion'); | ||
this.containers = []; | ||||
|
r14598 | this.model_containers = {}; | ||
|
r14611 | this.update_children([], this.model.get('_children')); | ||
this.model.on('change:_children', function(model, value, options) { | ||||
this.update_children(model.previous('_children'), value); | ||||
|
r14507 | }, this); | ||
|
r15950 | this.model.on('change:selected_index', function(model, value, options) { | ||
this.update_selected_index(model.previous('selected_index'), value, options); | ||||
}, this); | ||||
this.model.on('change:_titles', function(model, value, options) { | ||||
this.update_titles(value); | ||||
}, this); | ||||
|
r16660 | var that = this; | ||
|
r15955 | this.model.on('displayed', function() { | ||
this.update_titles(); | ||||
|
r16660 | // Trigger model displayed events for any models that are child to | ||
// this model when this model is displayed. | ||||
that.is_displayed = true; | ||||
for (var property in that.child_views) { | ||||
if (that.child_views.hasOwnProperty(property)) { | ||||
that.child_views[property].model.trigger('displayed'); | ||||
} | ||||
} | ||||
|
r15955 | }, this); | ||
|
r14503 | }, | ||
|
r14284 | |||
|
r15950 | update_titles: function(titles) { | ||
// Set tab titles | ||||
|
r15955 | if (!titles) { | ||
titles = this.model.get('_titles'); | ||||
} | ||||
|
r15950 | var that = this; | ||
_.each(titles, function(title, page_index) { | ||||
var accordian = that.containers[page_index]; | ||||
if (accordian !== undefined) { | ||||
accordian | ||||
.find('.accordion-heading') | ||||
.find('.accordion-toggle') | ||||
.text(title); | ||||
} | ||||
}); | ||||
}, | ||||
update_selected_index: function(old_index, new_index, options) { | ||||
// Only update the selection if the selection wasn't triggered | ||||
// by the front-end. It must be triggered by the back-end. | ||||
if (options === undefined || options.updated_view != this) { | ||||
this.containers[old_index].find('.accordion-body').collapse('hide'); | ||||
if (0 <= new_index && new_index < this.containers.length) { | ||||
this.containers[new_index].find('.accordion-body').collapse('show'); | ||||
|
r14370 | } | ||
} | ||||
|
r14284 | }, | ||
|
r14598 | |||
update_children: function(old_list, new_list) { | ||||
|
r14609 | // Called when the children list is modified. | ||
|
r14598 | this.do_diff(old_list, | ||
new_list, | ||||
$.proxy(this.remove_child_model, this), | ||||
$.proxy(this.add_child_model, this)); | ||||
}, | ||||
|
r14284 | |||
|
r14598 | remove_child_model: function(model) { | ||
|
r14609 | // Called when a child is removed from children list. | ||
|
r14598 | var accordion_group = this.model_containers[model.id]; | ||
this.containers.splice(accordion_group.container_index, 1); | ||||
delete this.model_containers[model.id]; | ||||
accordion_group.remove(); | ||||
this.delete_child_view(model); | ||||
}, | ||||
|
r14284 | |||
|
r14598 | add_child_model: function(model) { | ||
|
r14609 | // Called when a child is added to children list. | ||
|
r14598 | var view = this.create_child_view(model); | ||
|
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); | ||||
|
r14370 | var that = this; | ||
|
r14284 | var accordion_toggle = $('<a />') | ||
.addClass('accordion-toggle') | ||||
.attr('data-toggle', 'collapse') | ||||
.attr('data-parent', '#' + this.$el.attr('id')) | ||||
.attr('href', '#' + uuid) | ||||
|
r14370 | .click(function(evt){ | ||
|
r14569 | |||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
|
r15950 | that.model.set("selected_index", index, {updated_view: that}); | ||
|
r14482 | that.touch(); | ||
|
r14370 | }) | ||
|
r14663 | .text('Page ' + index) | ||
|
r14284 | .appendTo(accordion_heading); | ||
var accordion_body = $('<div />', {id: uuid}) | ||||
|
r14288 | .addClass('accordion-body collapse') | ||
|
r14284 | .appendTo(accordion_group); | ||
var accordion_inner = $('<div />') | ||||
.addClass('accordion-inner') | ||||
.appendTo(accordion_body); | ||||
|
r14598 | var container_index = this.containers.push(accordion_group) - 1; | ||
accordion_group.container_index = container_index; | ||||
this.model_containers[model.id] = accordion_group; | ||||
|
r14284 | accordion_inner.append(view.$el); | ||
|
r14370 | |||
|
r14289 | this.update(); | ||
|
r15955 | this.update_titles(); | ||
|
r16660 | |||
// Trigger the displayed event if this model is displayed. | ||||
if (this.is_displayed) { | ||||
model.trigger('displayed'); | ||||
} | ||||
|
r14284 | }, | ||
}); | ||||
|
r14627 | WidgetManager.register_widget_view('AccordionView', AccordionView); | ||
|
r14290 | |||
|
r14609 | |||
var TabView = IPython.DOMWidgetView.extend({ | ||||
|
r14507 | initialize: function() { | ||
|
r14609 | // Public constructor. | ||
|
r14507 | this.containers = []; | ||
|
r14583 | TabView.__super__.initialize.apply(this, arguments); | ||
|
r14507 | }, | ||
|
r14486 | |||
|
r14290 | render: function(){ | ||
|
r14609 | // Called when view is rendered. | ||
|
r14398 | var uuid = 'tabs'+IPython.utils.uuid(); | ||
|
r14307 | var that = this; | ||
|
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); | ||||
|
r14507 | this.containers = []; | ||
|
r14611 | this.update_children([], this.model.get('_children')); | ||
this.model.on('change:_children', function(model, value, options) { | ||||
this.update_children(model.previous('_children'), value); | ||||
|
r14507 | }, this); | ||
|
r16660 | |||
// Trigger model displayed events for any models that are child to | ||||
// this model when this model is displayed. | ||||
this.model.on('displayed', function(){ | ||||
that.is_displayed = true; | ||||
for (var property in that.child_views) { | ||||
if (that.child_views.hasOwnProperty(property)) { | ||||
that.child_views[property].model.trigger('displayed'); | ||||
} | ||||
} | ||||
}); | ||||
|
r14507 | }, | ||
|
r14609 | |||
|
r14598 | update_children: function(old_list, new_list) { | ||
|
r14609 | // Called when the children list is modified. | ||
|
r14598 | this.do_diff(old_list, | ||
new_list, | ||||
$.proxy(this.remove_child_model, this), | ||||
$.proxy(this.add_child_model, this)); | ||||
}, | ||||
|
r14370 | |||
|
r14598 | remove_child_model: function(model) { | ||
|
r14609 | // Called when a child is removed from children list. | ||
|
r14598 | var view = this.child_views[model.id]; | ||
this.containers.splice(view.parent_tab.tab_text_index, 1); | ||||
view.parent_tab.remove(); | ||||
view.parent_container.remove(); | ||||
view.remove(); | ||||
this.delete_child_view(model); | ||||
|
r14290 | }, | ||
|
r14598 | add_child_model: function(model) { | ||
|
r14609 | // Called when a child is added to children list. | ||
|
r14598 | var view = this.create_child_view(model); | ||
|
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); | ||||
|
r14598 | view.parent_tab = tab; | ||
|
r14290 | var tab_text = $('<a />') | ||
.attr('href', '#' + uuid) | ||||
.attr('data-toggle', 'tab') | ||||
|
r14663 | .text('Page ' + index) | ||
|
r14290 | .appendTo(tab) | ||
.click(function (e) { | ||||
|
r14569 | |||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
|
r14570 | that.model.set("selected_index", index, {updated_view: this}); | ||
|
r14482 | that.touch(); | ||
|
r14307 | that.select_page(index); | ||
|
r14290 | }); | ||
|
r14598 | tab.tab_text_index = this.containers.push(tab_text) - 1; | ||
|
r14290 | |||
var contents_div = $('<div />', {id: uuid}) | ||||
.addClass('tab-pane') | ||||
.addClass('fade') | ||||
.append(view.$el) | ||||
.appendTo(this.$tab_contents); | ||||
|
r14598 | view.parent_container = contents_div; | ||
|
r16660 | |||
// Trigger the displayed event if this model is displayed. | ||||
if (this.is_displayed) { | ||||
model.trigger('displayed'); | ||||
} | ||||
|
r14598 | }, | ||
update: function(options) { | ||||
// 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. | ||||
if (options === undefined || options.updated_view != this) { | ||||
// Set tab titles | ||||
var titles = this.model.get('_titles'); | ||||
|
r14686 | var that = this; | ||
|
r14664 | _.each(titles, function(title, page_index) { | ||
|
r14686 | var tab_text = that.containers[page_index]; | ||
|
r14598 | if (tab_text !== undefined) { | ||
|
r14664 | tab_text.text(title); | ||
} | ||||
}); | ||||
|
r14598 | |||
var selected_index = this.model.get('selected_index'); | ||||
if (0 <= selected_index && selected_index < this.containers.length) { | ||||
this.select_page(selected_index); | ||||
} | ||||
} | ||||
return TabView.__super__.update.apply(this); | ||||
|
r14290 | }, | ||
|
r14307 | |||
select_page: function(index) { | ||||
|
r14609 | // Select a page. | ||
|
r14307 | this.$tabs.find('li') | ||
.removeClass('active'); | ||||
this.containers[index].tab('show'); | ||||
}, | ||||
|
r14290 | }); | ||
|
r14627 | WidgetManager.register_widget_view('TabView', TabView); | ||
|
r14290 | }); | ||