widget_selectioncontainer.js
253 lines
| 9.5 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Jonathan Frederic
|
r14366 | |||
Jonathan Frederic
|
r17198 | define([ | ||
"widgets/js/widget", | ||||
Jonathan Frederic
|
r17199 | "base/js/utils", | ||
Jonathan Frederic
|
r17216 | "jquery", | ||
MinRK
|
r17312 | "bootstrap", | ||
Jonathan Frederic
|
r17216 | ], function(widget, utils, $){ | ||
Jonathan Frederic
|
r14366 | |||
Jonathan Frederic
|
r17198 | var AccordionView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14284 | render: function(){ | ||
Jonathan Frederic
|
r14609 | // Called when view is rendered. | ||
Jonathan Frederic
|
r17199 | var guid = 'panel-group' + utils.uuid(); | ||
Jonathan Frederic
|
r14455 | this.$el | ||
.attr('id', guid) | ||||
Jonathan Frederic
|
r16913 | .addClass('panel-group'); | ||
Jonathan Frederic
|
r14284 | this.containers = []; | ||
Jonathan Frederic
|
r14598 | this.model_containers = {}; | ||
Jonathan Frederic
|
r17173 | 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
|
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); | ||||
Jonathan Frederic
|
r16660 | var that = this; | ||
Jonathan Frederic
|
r17179 | this.on('displayed', function() { | ||
Jonathan Frederic
|
r15955 | this.update_titles(); | ||
sylvain.corlay
|
r17354 | }, this); | ||
Jason Grout
|
r14503 | }, | ||
Jonathan Frederic
|
r14284 | |||
Jonathan Frederic
|
r15950 | update_titles: function(titles) { | ||
// Set tab titles | ||||
Jonathan Frederic
|
r15955 | if (!titles) { | ||
titles = this.model.get('_titles'); | ||||
} | ||||
Jonathan Frederic
|
r15950 | var that = this; | ||
_.each(titles, function(title, page_index) { | ||||
var accordian = that.containers[page_index]; | ||||
if (accordian !== undefined) { | ||||
accordian | ||||
Jonathan Frederic
|
r16913 | .find('.panel-heading') | ||
Jonathan Frederic
|
r15950 | .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) { | ||||
Jonathan Frederic
|
r16913 | this.containers[old_index].find('.panel-collapse').collapse('hide'); | ||
Jonathan Frederic
|
r15950 | if (0 <= new_index && new_index < this.containers.length) { | ||
Jonathan Frederic
|
r16913 | this.containers[new_index].find('.panel-collapse').collapse('show'); | ||
Jonathan Frederic
|
r14370 | } | ||
} | ||||
Jonathan Frederic
|
r14284 | }, | ||
Jonathan Frederic
|
r14598 | |||
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)); | ||||
}, | ||||
Jonathan Frederic
|
r14284 | |||
Jonathan Frederic
|
r14598 | remove_child_model: function(model) { | ||
Jonathan Frederic
|
r14609 | // Called when a child is removed from children list. | ||
Jonathan Frederic
|
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(); | ||||
Jonathan Frederic
|
r17178 | this.pop_child_view(model); | ||
Jonathan Frederic
|
r14598 | }, | ||
Jonathan Frederic
|
r14284 | |||
Jonathan Frederic
|
r14598 | 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); | ||
Jonathan Frederic
|
r14284 | var index = this.containers.length; | ||
Jonathan Frederic
|
r17199 | var uuid = utils.uuid(); | ||
Jonathan Frederic
|
r14284 | var accordion_group = $('<div />') | ||
Jonathan Frederic
|
r16913 | .addClass('panel panel-default') | ||
Jonathan Frederic
|
r14284 | .appendTo(this.$el); | ||
var accordion_heading = $('<div />') | ||||
Jonathan Frederic
|
r16913 | .addClass('panel-heading') | ||
Jonathan Frederic
|
r14284 | .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){ | ||
Jonathan Frederic
|
r14569 | |||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
Jonathan Frederic
|
r15950 | that.model.set("selected_index", index, {updated_view: that}); | ||
Jonathan Frederic
|
r14482 | that.touch(); | ||
Jonathan Frederic
|
r14370 | }) | ||
Jonathan Frederic
|
r14663 | .text('Page ' + index) | ||
Jonathan Frederic
|
r14284 | .appendTo(accordion_heading); | ||
var accordion_body = $('<div />', {id: uuid}) | ||||
Jonathan Frederic
|
r16913 | .addClass('panel-collapse collapse') | ||
Jonathan Frederic
|
r14284 | .appendTo(accordion_group); | ||
var accordion_inner = $('<div />') | ||||
Jonathan Frederic
|
r16913 | .addClass('panel-body') | ||
Jonathan Frederic
|
r14284 | .appendTo(accordion_body); | ||
Jonathan Frederic
|
r14598 | var container_index = this.containers.push(accordion_group) - 1; | ||
accordion_group.container_index = container_index; | ||||
this.model_containers[model.id] = accordion_group; | ||||
Jonathan Frederic
|
r14284 | accordion_inner.append(view.$el); | ||
Jonathan Frederic
|
r14370 | |||
Jonathan Frederic
|
r14289 | this.update(); | ||
Jonathan Frederic
|
r15955 | this.update_titles(); | ||
Jonathan Frederic
|
r16660 | |||
sylvain.corlay
|
r17353 | // Trigger the displayed event of the child view. | ||
this.after_displayed(function() { | ||||
Jonathan Frederic
|
r17179 | view.trigger('displayed'); | ||
sylvain.corlay
|
r17353 | }); | ||
Jonathan Frederic
|
r14284 | }, | ||
}); | ||||
Jonathan Frederic
|
r14290 | |||
Jonathan Frederic
|
r14609 | |||
Jonathan Frederic
|
r17198 | var TabView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14507 | initialize: function() { | ||
Jonathan Frederic
|
r14609 | // Public constructor. | ||
Jonathan Frederic
|
r14507 | this.containers = []; | ||
Jonathan Frederic
|
r14583 | TabView.__super__.initialize.apply(this, arguments); | ||
Jonathan Frederic
|
r14507 | }, | ||
Jason Grout
|
r14486 | |||
Jonathan Frederic
|
r14290 | render: function(){ | ||
Jonathan Frederic
|
r14609 | // Called when view is rendered. | ||
Jonathan Frederic
|
r17199 | var uuid = 'tabs'+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 = []; | ||
Jonathan Frederic
|
r17173 | 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
|
r14609 | |||
Jonathan Frederic
|
r17724 | update_attr: function(name, value) { | ||
// Set a css attr of the widget view. | ||||
this.$tabs.css(name, value); | ||||
}, | ||||
Jonathan Frederic
|
r14598 | 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)); | ||||
}, | ||||
Jonathan Frederic
|
r14370 | |||
Jonathan Frederic
|
r14598 | remove_child_model: function(model) { | ||
Jonathan Frederic
|
r14609 | // Called when a child is removed from children list. | ||
Jonathan Frederic
|
r17178 | var view = this.pop_child_view(model); | ||
Jonathan Frederic
|
r14598 | this.containers.splice(view.parent_tab.tab_text_index, 1); | ||
view.parent_tab.remove(); | ||||
view.parent_container.remove(); | ||||
view.remove(); | ||||
Jonathan Frederic
|
r14290 | }, | ||
Jonathan Frederic
|
r14598 | 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); | ||
Jonathan Frederic
|
r14290 | var index = this.containers.length; | ||
Jonathan Frederic
|
r17199 | var uuid = utils.uuid(); | ||
Jonathan Frederic
|
r14290 | |||
var that = this; | ||||
var tab = $('<li />') | ||||
.css('list-style-type', 'none') | ||||
.appendTo(this.$tabs); | ||||
Jonathan Frederic
|
r14598 | view.parent_tab = tab; | ||
Jonathan Frederic
|
r14290 | var tab_text = $('<a />') | ||
.attr('href', '#' + uuid) | ||||
.attr('data-toggle', 'tab') | ||||
Jonathan Frederic
|
r14663 | .text('Page ' + index) | ||
Jonathan Frederic
|
r14290 | .appendTo(tab) | ||
.click(function (e) { | ||||
Jonathan Frederic
|
r14569 | |||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
Jonathan Frederic
|
r14570 | that.model.set("selected_index", index, {updated_view: this}); | ||
Jonathan Frederic
|
r14482 | that.touch(); | ||
Jonathan Frederic
|
r14307 | that.select_page(index); | ||
Jonathan Frederic
|
r14290 | }); | ||
Jonathan Frederic
|
r14598 | tab.tab_text_index = this.containers.push(tab_text) - 1; | ||
Jonathan Frederic
|
r14290 | |||
var contents_div = $('<div />', {id: uuid}) | ||||
.addClass('tab-pane') | ||||
.addClass('fade') | ||||
.append(view.$el) | ||||
.appendTo(this.$tab_contents); | ||||
Jonathan Frederic
|
r14598 | view.parent_container = contents_div; | ||
Jonathan Frederic
|
r16660 | |||
sylvain.corlay
|
r17353 | // Trigger the displayed event of the child view. | ||
this.after_displayed(function() { | ||||
Jonathan Frederic
|
r17179 | view.trigger('displayed'); | ||
sylvain.corlay
|
r17353 | }); | ||
Jonathan Frederic
|
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'); | ||||
Jonathan Frederic
|
r14686 | var that = this; | ||
Jonathan Frederic
|
r14664 | _.each(titles, function(title, page_index) { | ||
Jonathan Frederic
|
r14686 | var tab_text = that.containers[page_index]; | ||
Jonathan Frederic
|
r14598 | if (tab_text !== undefined) { | ||
Jonathan Frederic
|
r14664 | tab_text.text(title); | ||
} | ||||
}); | ||||
Jonathan Frederic
|
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); | ||||
Jonathan Frederic
|
r14290 | }, | ||
Jonathan Frederic
|
r14307 | |||
select_page: function(index) { | ||||
Jonathan Frederic
|
r14609 | // Select a page. | ||
Jonathan Frederic
|
r14307 | this.$tabs.find('li') | ||
.removeClass('active'); | ||||
this.containers[index].tab('show'); | ||||
}, | ||||
Jonathan Frederic
|
r14290 | }); | ||
Jonathan Frederic
|
r17198 | |||
return { | ||||
'AccordionView': AccordionView, | ||||
'TabView': TabView, | ||||
}; | ||||
Jonathan Frederic
|
r14290 | }); | ||