diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index f0f919c..ae80f9e 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -503,15 +503,54 @@ define(["widgets/js/manager", } }, - update_classes: function (old_classes, new_classes) { - // Update the DOM classes applied to the topmost element. + update_classes: function (old_classes, new_classes, $el) { + // Update the DOM classes applied to an element, default to this.$el. + if ($el===undefined) { + $el = this.$el; + } this.do_diff(old_classes, new_classes, function(removed) { - this.$el.removeClass(removed); + $el.removeClass(removed); }, function(added) { - this.$el.addClass(added); + $el.addClass(added); }); }, + update_mapped_classes: function(class_map, trait_name, previous_trait_value, $el) { + // Update the DOM classes applied to the widget based on a single + // trait's value. + // + // Given a trait value classes map, this function automatically + // handles applying the appropriate classes to the widget element + // and removing classes that are no longer valid. + // + // Parameters + // ---------- + // class_map: dictionary + // Dictionary of trait values to class lists. + // Example: + // { + // success: ['alert', 'alert-success'], + // info: ['alert', 'alert-info'], + // warning: ['alert', 'alert-warning'], + // danger: ['alert', 'alert-danger'] + // }; + // trait_name: string + // Name of the trait to check the value of. + // previous_trait_value: optional string, default '' + // Last trait value + // $el: optional jQuery element handle, defaults to this.$el + // Element that the classes are applied to. + var key = previous_trait_value; + if (key === undefined) { + key = this.model.previous(trait_name); + } + var old_classes = class_map[key] ? class_map[key] : []; + key = this.model.get(trait_name); + var new_classes = class_map[key] ? class_map[key] : []; + + this.update_classes(old_classes, new_classes, $el || this.$el); + }, + _get_selector_element: function (selector) { // Get the elements via the css selector. var elements; diff --git a/IPython/html/static/widgets/js/widget_bool.js b/IPython/html/static/widgets/js/widget_bool.js index 1b925af..9071420 100644 --- a/IPython/html/static/widgets/js/widget_bool.js +++ b/IPython/html/static/widgets/js/widget_bool.js @@ -77,8 +77,24 @@ define([ that.handle_click(); })); + this.model.on('change:button_style', function(model, value) { + this.update_button_style(); + }, this); + this.update_button_style(''); + this.update(); // Set defaults. }, + + 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); + }, update : function(options){ // Update the contents of this view diff --git a/IPython/html/static/widgets/js/widget_box.js b/IPython/html/static/widgets/js/widget_box.js index 68cb0ee..f5eb6bf 100644 --- a/IPython/html/static/widgets/js/widget_box.js +++ b/IPython/html/static/widgets/js/widget_box.js @@ -20,6 +20,9 @@ define([ this.model.on('change:overflow_y', function(model, value) { this.update_overflow_y(); }, this); + this.model.on('change:box_style', function(model, value) { + this.update_box_style(); + }, this); }, update_attr: function(name, value) { @@ -34,6 +37,7 @@ define([ this.update_children([], this.model.get('children')); this.update_overflow_x(); this.update_overflow_y(); + this.update_box_style(''); }, update_overflow_x: function() { @@ -45,6 +49,16 @@ define([ // Called when the y-axis overflow setting is changed. this.$box.css('overflow-y', this.model.get('overflow_y')); }, + + update_box_style: function(previous_trait_value) { + var class_map = { + success: ['alert', 'alert-success'], + info: ['alert', 'alert-info'], + warning: ['alert', 'alert-warning'], + danger: ['alert', 'alert-danger'] + }; + this.update_mapped_classes(class_map, 'box_style', previous_trait_value, this.$box); + }, update_children: function(old_list, new_list) { // Called when the children list changes. diff --git a/IPython/html/static/widgets/js/widget_button.js b/IPython/html/static/widgets/js/widget_button.js index 3ff7c23..ddae4d8 100644 --- a/IPython/html/static/widgets/js/widget_button.js +++ b/IPython/html/static/widgets/js/widget_button.js @@ -13,6 +13,11 @@ define([ this.setElement($("