widget_selection.js
592 lines
| 22.1 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", | ||
Nicholas Bollweg
|
r20287 | "underscore", | ||
MinRK
|
r17312 | "bootstrap", | ||
Nicholas Bollweg
|
r20287 | ], function(widget, utils, $, _){ | ||
Jonathan Frederic
|
r14366 | |||
Jonathan Frederic
|
r17198 | var DropdownView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14263 | render : function(){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* Called when view is rendered. | ||||
*/ | ||||
Jonathan Frederic
|
r14263 | this.$el | ||
Jason Grout
|
r18120 | .addClass('widget-hbox widget-dropdown'); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r17929 | .addClass('widget-label') | ||
Jonathan Frederic
|
r14292 | .hide(); | ||
Jonathan Frederic
|
r14263 | this.$buttongroup = $('<div />') | ||
Jonathan Frederic
|
r14365 | .addClass('widget_item') | ||
.addClass('btn-group') | ||||
.appendTo(this.$el); | ||||
Jonathan Frederic
|
r14263 | this.$droplabel = $('<button />') | ||
Jonathan Frederic
|
r16913 | .addClass('btn btn-default') | ||
Jonathan Frederic
|
r14365 | .addClass('widget-combo-btn') | ||
MinRK
|
r15329 | .html(" ") | ||
Jonathan Frederic
|
r14365 | .appendTo(this.$buttongroup); | ||
Jonathan Frederic
|
r14263 | this.$dropbutton = $('<button />') | ||
Jonathan Frederic
|
r16913 | .addClass('btn btn-default') | ||
Jonathan Frederic
|
r14365 | .addClass('dropdown-toggle') | ||
.addClass('widget-combo-carrot-btn') | ||||
.attr('data-toggle', 'dropdown') | ||||
Jonathan Frederic
|
r14663 | .append($('<span />').addClass("caret")) | ||
Jonathan Frederic
|
r14365 | .appendTo(this.$buttongroup); | ||
Jonathan Frederic
|
r14263 | this.$droplist = $('<ul />') | ||
Jonathan Frederic
|
r14365 | .addClass('dropdown-menu') | ||
.appendTo(this.$buttongroup); | ||||
Jonathan Frederic
|
r17728 | |||
this.model.on('change:button_style', function(model, value) { | ||||
this.update_button_style(); | ||||
}, this); | ||||
this.update_button_style(''); | ||||
Jonathan Frederic
|
r14263 | |||
// Set defaults. | ||||
this.update(); | ||||
}, | ||||
Jonathan Frederic
|
r14257 | |||
Jonathan Frederic
|
r14570 | update : function(options){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* Update the contents of this view | ||||
* | ||||
Nicholas Bollweg
|
r20287 | * Called when the model is changed. The model may have been | ||
Jonathan Frederic
|
r19176 | * changed by another view or by a state update from the back-end. | ||
*/ | ||||
Jonathan Frederic
|
r14359 | |||
Jonathan Frederic
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
Nicholas Bollweg
|
r20287 | var selected_item_text = this.model.get('selected_label'); | ||
MinRK
|
r15332 | if (selected_item_text.trim().length === 0) { | ||
MinRK
|
r15329 | this.$droplabel.html(" "); | ||
Jonathan Frederic
|
r14570 | } else { | ||
Jonathan Frederic
|
r14663 | this.$droplabel.text(selected_item_text); | ||
Jonathan Frederic
|
r14570 | } | ||
Nicholas Bollweg
|
r20287 | var items = this.model.get('_options_labels'); | ||
Jonathan Frederic
|
r14616 | var $replace_droplist = $('<ul />') | ||
.addClass('dropdown-menu'); | ||||
Jonathan Frederic
|
r17723 | // Copy the style | ||
$replace_droplist.attr('style', this.$droplist.attr('style')); | ||||
Jonathan Frederic
|
r14686 | var that = this; | ||
Jonathan Frederic
|
r14664 | _.each(items, function(item, i) { | ||
Jonathan Frederic
|
r14570 | var item_button = $('<a href="#"/>') | ||
Jonathan Frederic
|
r14664 | .text(item) | ||
Jonathan Frederic
|
r14686 | .on('click', $.proxy(that.handle_click, that)); | ||
Jonathan Frederic
|
r14616 | $replace_droplist.append($('<li />').append(item_button)); | ||
Jonathan Frederic
|
r14664 | }); | ||
Jonathan Frederic
|
r14616 | this.$droplist.replaceWith($replace_droplist); | ||
this.$droplist.remove(); | ||||
this.$droplist = $replace_droplist; | ||||
Jonathan Frederic
|
r14570 | |||
if (this.model.get('disabled')) { | ||||
this.$buttongroup.attr('disabled','disabled'); | ||||
this.$droplabel.attr('disabled','disabled'); | ||||
this.$dropbutton.attr('disabled','disabled'); | ||||
this.$droplist.attr('disabled','disabled'); | ||||
} else { | ||||
this.$buttongroup.removeAttr('disabled'); | ||||
this.$droplabel.removeAttr('disabled'); | ||||
this.$dropbutton.removeAttr('disabled'); | ||||
this.$droplist.removeAttr('disabled'); | ||||
} | ||||
Jonathan Frederic
|
r14292 | |||
Jonathan Frederic
|
r14570 | var description = this.model.get('description'); | ||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
Nicholas Bollweg (Nick)
|
r19196 | this.typeset(this.$label, description); | ||
Jonathan Frederic
|
r14570 | this.$label.show(); | ||
} | ||||
Jonathan Frederic
|
r14292 | } | ||
Jonathan Frederic
|
r14583 | return DropdownView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14466 | |||
Jonathan Frederic
|
r17728 | update_button_style: function(previous_trait_value) { | ||
var class_map = { | ||||
primary: ['btn-primary'], | ||||
success: ['btn-success'], | ||||
info: ['btn-info'], | ||||
warning: ['btn-warning'], | ||||
danger: ['btn-danger'] | ||||
}; | ||||
this.update_mapped_classes(class_map, 'button_style', previous_trait_value, this.$droplabel); | ||||
this.update_mapped_classes(class_map, 'button_style', previous_trait_value, this.$dropbutton); | ||||
}, | ||||
Jonathan Frederic
|
r17723 | update_attr: function(name, value) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Set a css attr of the widget view. | ||||
*/ | ||||
Jonathan Frederic
|
r17723 | if (name.substring(0, 6) == 'border' || name == 'background' || name == 'color') { | ||
this.$droplabel.css(name, value); | ||||
this.$dropbutton.css(name, value); | ||||
this.$droplist.css(name, value); | ||||
} else if (name == 'width') { | ||||
Jonathan Frederic
|
r17728 | this.$droplist.css(name, value); | ||
this.$droplabel.css(name, value); | ||||
} else if (name == 'padding') { | ||||
this.$droplist.css(name, value); | ||||
this.$buttongroup.css(name, value); | ||||
} else if (name == 'margin') { | ||||
this.$buttongroup.css(name, value); | ||||
Jonathan Frederic
|
r17723 | } else if (name == 'height') { | ||
Jonathan Frederic
|
r17724 | this.$droplabel.css(name, value); | ||
Jonathan Frederic
|
r17723 | this.$dropbutton.css(name, value); | ||
Jonathan Frederic
|
r19366 | } else if (name == 'padding' || name == 'margin') { | ||
this.$el.css(name, value); | ||||
Jonathan Frederic
|
r17723 | } else { | ||
this.$droplist.css(name, value); | ||||
Jonathan Frederic
|
r17724 | this.$droplabel.css(name, value); | ||
Jonathan Frederic
|
r17723 | } | ||
}, | ||||
Jonathan Frederic
|
r14466 | handle_click: function (e) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Handle when a value is clicked. | ||||
* | ||||
* Calling model.set will trigger all of the other views of the | ||||
* model to update. | ||||
*/ | ||||
Nicholas Bollweg
|
r20287 | this.model.set('selected_label', $(e.target).text(), {updated_view: this}); | ||
Jonathan Frederic
|
r14482 | this.touch(); | ||
Jonathan Frederic
|
r19386 | |||
// Manually hide the droplist. | ||||
e.stopPropagation(); | ||||
e.preventDefault(); | ||||
this.$buttongroup.removeClass('open'); | ||||
Jonathan Frederic
|
r14466 | }, | ||
Jonathan Frederic
|
r14263 | |||
}); | ||||
Jonathan Frederic
|
r14609 | |||
Jonathan Frederic
|
r17198 | var RadioButtonsView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14263 | render : function(){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* Called when view is rendered. | ||||
*/ | ||||
Jonathan Frederic
|
r14263 | this.$el | ||
Jason Grout
|
r18120 | .addClass('widget-hbox widget-radio'); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r17929 | .addClass('widget-label') | ||
Jonathan Frederic
|
r14292 | .hide(); | ||
Jonathan Frederic
|
r14295 | this.$container = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r15364 | .addClass('widget-radio-box'); | ||
Jonathan Frederic
|
r14263 | this.update(); | ||
}, | ||||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14570 | update : function(options){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* 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
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
// Add missing items to the DOM. | ||||
Nicholas Bollweg
|
r20287 | var items = this.model.get('_options_labels'); | ||
Jonathan Frederic
|
r14570 | var disabled = this.model.get('disabled'); | ||
Jonathan Frederic
|
r14686 | var that = this; | ||
Jonathan Frederic
|
r14664 | _.each(items, function(item, index) { | ||
Min RK
|
r19900 | var item_query = ' :input[data-value="' + encodeURIComponent(item) + '"]'; | ||
Jonathan Frederic
|
r14686 | if (that.$el.find(item_query).length === 0) { | ||
Jonathan Frederic
|
r14570 | var $label = $('<label />') | ||
.addClass('radio') | ||||
Jonathan Frederic
|
r14664 | .text(item) | ||
Jonathan Frederic
|
r14686 | .appendTo(that.$container); | ||
Jonathan Frederic
|
r14570 | |||
$('<input />') | ||||
.attr('type', 'radio') | ||||
Jonathan Frederic
|
r14686 | .addClass(that.model) | ||
Jonathan Frederic
|
r14664 | .val(item) | ||
Min RK
|
r19900 | .attr('data-value', encodeURIComponent(item)) | ||
Jonathan Frederic
|
r14570 | .prependTo($label) | ||
Jonathan Frederic
|
r14686 | .on('click', $.proxy(that.handle_click, that)); | ||
Jonathan Frederic
|
r14570 | } | ||
Jonathan Frederic
|
r14687 | var $item_element = that.$container.find(item_query); | ||
Nicholas Bollweg
|
r20287 | if (that.model.get('selected_label') == item) { | ||
Jonathan Frederic
|
r14570 | $item_element.prop('checked', true); | ||
} else { | ||||
$item_element.prop('checked', false); | ||||
Jonathan Frederic
|
r14263 | } | ||
Jonathan Frederic
|
r14570 | $item_element.prop('disabled', disabled); | ||
Jonathan Frederic
|
r14664 | }); | ||
Jonathan Frederic
|
r14263 | |||
Jonathan Frederic
|
r14570 | // Remove items that no longer exist. | ||
this.$container.find('input').each(function(i, obj) { | ||||
var value = $(obj).val(); | ||||
var found = false; | ||||
Jonathan Frederic
|
r14664 | _.each(items, function(item, index) { | ||
if (item == value) { | ||||
Jonathan Frederic
|
r14570 | found = true; | ||
Jonathan Frederic
|
r14678 | return false; | ||
Jonathan Frederic
|
r14570 | } | ||
Jonathan Frederic
|
r14664 | }); | ||
Jonathan Frederic
|
r14570 | |||
if (!found) { | ||||
$(obj).parent().remove(); | ||||
} | ||||
}); | ||||
Jonathan Frederic
|
r14292 | |||
Jonathan Frederic
|
r14570 | var description = this.model.get('description'); | ||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
Jonathan Frederic
|
r14663 | this.$label.text(description); | ||
Nicholas Bollweg (Nick)
|
r19196 | this.typeset(this.$label, description); | ||
Jonathan Frederic
|
r14570 | this.$label.show(); | ||
} | ||||
Jonathan Frederic
|
r14292 | } | ||
Jonathan Frederic
|
r14583 | return RadioButtonsView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14466 | |||
Jonathan Frederic
|
r17724 | update_attr: function(name, value) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Set a css attr of the widget view. | ||||
*/ | ||||
Jonathan Frederic
|
r19366 | if (name == 'padding' || name == 'margin') { | ||
this.$el.css(name, value); | ||||
} else { | ||||
this.$container.css(name, value); | ||||
} | ||||
Jonathan Frederic
|
r17724 | }, | ||
Jonathan Frederic
|
r14466 | handle_click: function (e) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Handle when a value is clicked. | ||||
* | ||||
* Calling model.set will trigger all of the other views of the | ||||
* model to update. | ||||
*/ | ||||
Nicholas Bollweg
|
r20287 | this.model.set('selected_label', $(e.target).val(), {updated_view: this}); | ||
Jonathan Frederic
|
r14482 | this.touch(); | ||
Jonathan Frederic
|
r14466 | }, | ||
Jonathan Frederic
|
r14263 | }); | ||
Jonathan Frederic
|
r17198 | |||
Jonathan Frederic
|
r14258 | |||
Jonathan Frederic
|
r17198 | var ToggleButtonsView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r17724 | initialize: function() { | ||
this._css_state = {}; | ||||
ToggleButtonsView.__super__.initialize.apply(this, arguments); | ||||
}, | ||||
render: function() { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* Called when view is rendered. | ||||
*/ | ||||
Jonathan Frederic
|
r14263 | this.$el | ||
Jason Grout
|
r18120 | .addClass('widget-hbox widget-toggle-buttons'); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r17929 | .addClass('widget-label') | ||
Jonathan Frederic
|
r14292 | .hide(); | ||
Jonathan Frederic
|
r14263 | this.$buttongroup = $('<div />') | ||
.addClass('btn-group') | ||||
.appendTo(this.$el); | ||||
Jonathan Frederic
|
r17728 | |||
this.model.on('change:button_style', function(model, value) { | ||||
this.update_button_style(); | ||||
}, this); | ||||
this.update_button_style(''); | ||||
Jonathan Frederic
|
r14263 | this.update(); | ||
}, | ||||
Jonathan Frederic
|
r14258 | |||
Jonathan Frederic
|
r14570 | update : function(options){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* 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
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
// Add missing items to the DOM. | ||||
Nicholas Bollweg
|
r20287 | var items = this.model.get('_options_labels'); | ||
Sylvain Corlay
|
r20446 | var icons = this.model.get('icons'); | ||
var previous_icons = this.model.previous('icons') || []; | ||||
Jonathan Frederic
|
r14570 | var disabled = this.model.get('disabled'); | ||
Jonathan Frederic
|
r14686 | var that = this; | ||
MinRK
|
r15332 | var item_html; | ||
Jonathan Frederic
|
r14664 | _.each(items, function(item, index) { | ||
Sylvain Corlay
|
r20446 | if (item.trim().length === 0 && (!icons[index] || | ||
icons[index].trim().length === 0)) { | ||||
MinRK
|
r15332 | item_html = " "; | ||
} else { | ||||
Jonathan Frederic
|
r17199 | item_html = utils.escape_html(item); | ||
MinRK
|
r15332 | } | ||
Min RK
|
r19900 | var item_query = '[data-value="' + encodeURIComponent(item) + '"]'; | ||
MinRK
|
r15332 | var $item_element = that.$buttongroup.find(item_query); | ||
Sylvain Corlay
|
r20446 | var $icon_element = $item_element.find('.fa'); | ||
MinRK
|
r15332 | if (!$item_element.length) { | ||
$item_element = $('<button/>') | ||||
Jonathan Frederic
|
r14570 | .attr('type', 'button') | ||
Jonathan Frederic
|
r16913 | .addClass('btn btn-default') | ||
MinRK
|
r15332 | .html(item_html) | ||
Jonathan Frederic
|
r14686 | .appendTo(that.$buttongroup) | ||
Min RK
|
r19900 | .attr('data-value', encodeURIComponent(item)) | ||
Sylvain Corlay
|
r20445 | .attr('data-toggle', 'tooltip') | ||
Min RK
|
r19900 | .attr('value', item) | ||
Jonathan Frederic
|
r14686 | .on('click', $.proxy(that.handle_click, that)); | ||
Jonathan Frederic
|
r17728 | that.update_style_traits($item_element); | ||
Sylvain Corlay
|
r20446 | $icon_element = $('<i class="fa"></i>').prependTo($item_element); | ||
Jonathan Frederic
|
r14570 | } | ||
Nicholas Bollweg
|
r20287 | if (that.model.get('selected_label') == item) { | ||
Jonathan Frederic
|
r14570 | $item_element.addClass('active'); | ||
} else { | ||||
$item_element.removeClass('active'); | ||||
Jonathan Frederic
|
r14263 | } | ||
Sylvain Corlay
|
r20445 | $item_element.prop('disabled', disabled); | ||
Sylvain Corlay
|
r20446 | $item_element.attr('title', that.model.get('tooltips')[index]); | ||
$icon_element | ||||
.removeClass(previous_icons[index]) | ||||
.addClass(icons[index]); | ||||
Jonathan Frederic
|
r14664 | }); | ||
Jonathan Frederic
|
r14263 | |||
Jonathan Frederic
|
r14570 | // Remove items that no longer exist. | ||
this.$buttongroup.find('button').each(function(i, obj) { | ||||
Min RK
|
r19900 | var value = $(obj).attr('value'); | ||
Jonathan Frederic
|
r14570 | var found = false; | ||
Jonathan Frederic
|
r14664 | _.each(items, function(item, index) { | ||
if (item == value) { | ||||
Jonathan Frederic
|
r14570 | found = true; | ||
Jonathan Frederic
|
r14678 | return false; | ||
Jonathan Frederic
|
r14570 | } | ||
Jonathan Frederic
|
r14664 | }); | ||
Jonathan Frederic
|
r14570 | if (!found) { | ||
$(obj).remove(); | ||||
} | ||||
}); | ||||
Jonathan Frederic
|
r14292 | |||
Jonathan Frederic
|
r14570 | var description = this.model.get('description'); | ||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
Nicholas Bollweg (Nick)
|
r19195 | this.$label.text(); | ||
Nicholas Bollweg (Nick)
|
r19196 | this.typeset(this.$label, description); | ||
Jonathan Frederic
|
r14570 | this.$label.show(); | ||
} | ||||
Jonathan Frederic
|
r14292 | } | ||
Jonathan Frederic
|
r14583 | return ToggleButtonsView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14466 | |||
Jonathan Frederic
|
r17724 | update_attr: function(name, value) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Set a css attr of the widget view. | ||||
*/ | ||||
Jonathan Frederic
|
r19366 | if (name == 'padding' || name == 'margin') { | ||
this.$el.css(name, value); | ||||
} else { | ||||
this._css_state[name] = value; | ||||
this.update_style_traits(); | ||||
} | ||||
Jonathan Frederic
|
r17724 | }, | ||
Jonathan Frederic
|
r17728 | update_style_traits: function(button) { | ||
Jonathan Frederic
|
r17724 | for (var name in this._css_state) { | ||
Jonathan Frederic
|
r17728 | if (this._css_state.hasOwnProperty(name)) { | ||
if (name == 'margin') { | ||||
this.$buttongroup.css(name, this._css_state[name]); | ||||
} else if (name != 'width') { | ||||
if (button) { | ||||
button.css(name, this._css_state[name]); | ||||
} else { | ||||
this.$buttongroup.find('button').css(name, this._css_state[name]); | ||||
} | ||||
} | ||||
Jonathan Frederic
|
r17724 | } | ||
} | ||||
}, | ||||
Jonathan Frederic
|
r17728 | update_button_style: function(previous_trait_value) { | ||
var class_map = { | ||||
primary: ['btn-primary'], | ||||
success: ['btn-success'], | ||||
info: ['btn-info'], | ||||
warning: ['btn-warning'], | ||||
danger: ['btn-danger'] | ||||
}; | ||||
this.update_mapped_classes(class_map, 'button_style', previous_trait_value, this.$buttongroup.find('button')); | ||||
}, | ||||
Jonathan Frederic
|
r14466 | handle_click: function (e) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Handle when a value is clicked. | ||||
* | ||||
* Calling model.set will trigger all of the other views of the | ||||
* model to update. | ||||
*/ | ||||
Nicholas Bollweg
|
r20287 | this.model.set('selected_label', $(e.target).attr('value'), {updated_view: this}); | ||
Jonathan Frederic
|
r14482 | this.touch(); | ||
Jonathan Frederic
|
r14609 | }, | ||
Jonathan Frederic
|
r14263 | }); | ||
Jonathan Frederic
|
r17198 | |||
Jonathan Frederic
|
r14609 | |||
Jonathan Frederic
|
r17198 | var SelectView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14375 | render : function(){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* Called when view is rendered. | ||||
*/ | ||||
Jonathan Frederic
|
r14375 | this.$el | ||
Jason Grout
|
r18120 | .addClass('widget-hbox widget-select'); | ||
Jonathan Frederic
|
r14375 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r17929 | .addClass('widget-label') | ||
Jonathan Frederic
|
r14375 | .hide(); | ||
this.$listbox = $('<select />') | ||||
Jonathan Frederic
|
r16952 | .addClass('widget-listbox form-control') | ||
Jonathan Frederic
|
r14375 | .attr('size', 6) | ||
Nicholas Bollweg
|
r20288 | .appendTo(this.$el) | ||
.on('change', $.proxy(this.handle_change, this)); | ||||
Jonathan Frederic
|
r14375 | this.update(); | ||
}, | ||||
Jonathan Frederic
|
r14570 | update : function(options){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* 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
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
// Add missing items to the DOM. | ||||
Nicholas Bollweg
|
r20287 | var items = this.model.get('_options_labels'); | ||
Jonathan Frederic
|
r14686 | var that = this; | ||
Jonathan Frederic
|
r14664 | _.each(items, function(item, index) { | ||
Min RK
|
r19900 | var item_query = 'option[data-value="' + encodeURIComponent(item) + '"]'; | ||
Jonathan Frederic
|
r14686 | if (that.$listbox.find(item_query).length === 0) { | ||
Jonathan Frederic
|
r14570 | $('<option />') | ||
Jonathan Frederic
|
r14664 | .text(item) | ||
Min RK
|
r19900 | .attr('data-value', encodeURIComponent(item)) | ||
Nicholas Bollweg
|
r20287 | .attr('selected_label', item) | ||
Nicholas Bollweg
|
r20289 | .on("click", $.proxy(that.handle_click, that)) | ||
Nicholas Bollweg
|
r20288 | .appendTo(that.$listbox); | ||
Jonathan Frederic
|
r14664 | } | ||
}); | ||||
Jonathan Frederic
|
r14570 | |||
// Select the correct element | ||||
Nicholas Bollweg
|
r20287 | this.$listbox.val(this.model.get('selected_label')); | ||
Jonathan Frederic
|
r14375 | |||
Jonathan Frederic
|
r14570 | // Disable listbox if needed | ||
var disabled = this.model.get('disabled'); | ||||
this.$listbox.prop('disabled', disabled); | ||||
// Remove items that no longer exist. | ||||
this.$listbox.find('option').each(function(i, obj) { | ||||
Jonathan Frederic
|
r14663 | var value = $(obj).text(); | ||
Jonathan Frederic
|
r14570 | var found = false; | ||
Jonathan Frederic
|
r14664 | _.each(items, function(item, index) { | ||
if (item == value) { | ||||
Jonathan Frederic
|
r14570 | found = true; | ||
Jonathan Frederic
|
r14678 | return false; | ||
Jonathan Frederic
|
r14570 | } | ||
Jonathan Frederic
|
r14664 | }); | ||
Jonathan Frederic
|
r14570 | |||
if (!found) { | ||||
$(obj).remove(); | ||||
} | ||||
}); | ||||
Jonathan Frederic
|
r14375 | |||
Jonathan Frederic
|
r14570 | var description = this.model.get('description'); | ||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
Nicholas Bollweg (Nick)
|
r19196 | this.typeset(this.$label, description); | ||
Jonathan Frederic
|
r14570 | this.$label.show(); | ||
} | ||||
Jonathan Frederic
|
r14375 | } | ||
Jonathan Frederic
|
r14834 | return SelectView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14375 | }, | ||
Jonathan Frederic
|
r14466 | |||
Jonathan Frederic
|
r17724 | update_attr: function(name, value) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Set a css attr of the widget view. | ||||
*/ | ||||
Jonathan Frederic
|
r19366 | if (name == 'padding' || name == 'margin') { | ||
this.$el.css(name, value); | ||||
} else { | ||||
this.$listbox.css(name, value); | ||||
} | ||||
Jonathan Frederic
|
r17724 | }, | ||
Nicholas Bollweg
|
r20289 | handle_click: function (e) { | ||
/** | ||||
* Handle when a new value is clicked. | ||||
*/ | ||||
this.$listbox.val($(e.target).val()).change(); | ||||
}, | ||||
Nicholas Bollweg
|
r20288 | handle_change: function (e) { | ||
Jonathan Frederic
|
r19176 | /** | ||
Nicholas Bollweg
|
r20288 | * Handle when a new value is selected. | ||
Jonathan Frederic
|
r19176 | * | ||
* Calling model.set will trigger all of the other views of the | ||||
* model to update. | ||||
*/ | ||||
Nicholas Bollweg
|
r20288 | this.model.set('selected_label', this.$listbox.val(), {updated_view: this}); | ||
Jonathan Frederic
|
r14482 | this.touch(); | ||
Nicholas Bollweg
|
r20287 | }, | ||
}); | ||||
Nicholas Bollweg
|
r20288 | |||
Nicholas Bollweg
|
r20287 | var SelectMultipleView = SelectView.extend({ | ||
render: function(){ | ||||
Nicholas Bollweg
|
r20288 | /** | ||
* Called when view is rendered. | ||||
*/ | ||||
Nicholas Bollweg
|
r20287 | SelectMultipleView.__super__.render.apply(this); | ||
this.$el.removeClass('widget-select') | ||||
.addClass('widget-select-multiple'); | ||||
this.$listbox.attr('multiple', true) | ||||
Nicholas Bollweg
|
r20288 | .on('change', $.proxy(this.handle_change, this)); | ||
Nicholas Bollweg
|
r20287 | return this; | ||
}, | ||||
update: function(){ | ||||
Nicholas Bollweg
|
r20288 | /** | ||
* 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. | ||||
*/ | ||||
Nicholas Bollweg
|
r20287 | SelectMultipleView.__super__.update.apply(this, arguments); | ||
this.$listbox.val(this.model.get('selected_labels')); | ||||
}, | ||||
Nicholas Bollweg
|
r20676 | |||
handle_click: function(){ | ||||
/** | ||||
* Overload click from select | ||||
* | ||||
* Apparently it's needed from there for testing purposes, | ||||
* but breaks behavior of this. | ||||
*/ | ||||
}, | ||||
Nicholas Bollweg
|
r20287 | |||
Nicholas Bollweg
|
r20288 | handle_change: function (e) { | ||
/** | ||||
* Handle when a new value is selected. | ||||
* | ||||
* Calling model.set will trigger all of the other views of the | ||||
* model to update. | ||||
*/ | ||||
Nicholas Bollweg
|
r20287 | this.model.set('selected_labels', | ||
(this.$listbox.val() || []).slice(), | ||||
{updated_view: this}); | ||||
this.touch(); | ||||
}, | ||||
Jonathan Frederic
|
r14375 | }); | ||
Jonathan Frederic
|
r17198 | |||
Nicholas Bollweg
|
r20288 | |||
Jonathan Frederic
|
r17198 | return { | ||
'DropdownView': DropdownView, | ||||
'RadioButtonsView': RadioButtonsView, | ||||
'ToggleButtonsView': ToggleButtonsView, | ||||
'SelectView': SelectView, | ||||
Nicholas Bollweg
|
r20287 | 'SelectMultipleView': SelectMultipleView, | ||
Jonathan Frederic
|
r17198 | }; | ||
Jonathan Frederic
|
r14263 | }); | ||