widget_box.js
187 lines
| 6.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
|
r17216 | "jqueryui", | ||
Jason Grout
|
r20935 | "underscore", | ||
Jonathan Frederic
|
r18895 | "base/js/utils", | ||
MinRK
|
r17312 | "bootstrap", | ||
Jason Grout
|
r20935 | ], function(widget, $, _, utils){ | ||
Matthias Bussonnier
|
r19739 | "use strict"; | ||
Jason Grout
|
r20935 | var unpack_models = function unpack_models(value, model) { | ||
/** | ||||
* Replace model ids with models recursively. | ||||
*/ | ||||
var unpacked; | ||||
if ($.isArray(value)) { | ||||
unpacked = []; | ||||
_.each(value, function(sub_value, key) { | ||||
unpacked.push(unpack_models(sub_value, model)); | ||||
}); | ||||
return Promise.all(unpacked); | ||||
} else if (value instanceof Object) { | ||||
unpacked = {}; | ||||
_.each(value, function(sub_value, key) { | ||||
unpacked[key] = unpack_models(sub_value, model); | ||||
}); | ||||
return utils.resolve_promises_dict(unpacked); | ||||
} else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") { | ||||
// get_model returns a promise already | ||||
return model.widget_manager.get_model(value.slice(10, value.length)); | ||||
} else { | ||||
return Promise.resolve(value); | ||||
} | ||||
}; | ||||
var BoxModel = widget.WidgetModel.extend({}, { | ||||
serializers: _.extend({ | ||||
children: {deserialize: unpack_models} | ||||
}, widget.WidgetModel.serializers) | ||||
}); | ||||
Jonathan Frederic
|
r14366 | |||
Jonathan Frederic
|
r17637 | var BoxView = widget.DOMWidgetView.extend({ | ||
sylvain.corlay
|
r17349 | initialize: function(){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* Public constructor | ||||
*/ | ||||
Jonathan Frederic
|
r17637 | BoxView.__super__.initialize.apply(this, arguments); | ||
Jason Grout
|
r18996 | this.children_views = new widget.ViewList(this.add_child_model, null, this); | ||
this.listenTo(this.model, 'change:children', function(model, value) { | ||||
this.children_views.update(value); | ||||
Jason Grout
|
r14503 | }, this); | ||
Jason Grout
|
r18996 | this.listenTo(this.model, 'change:overflow_x', function(model, value) { | ||
Jonathan Frederic
|
r17727 | this.update_overflow_x(); | ||
}, this); | ||||
Jason Grout
|
r18996 | this.listenTo(this.model, 'change:overflow_y', function(model, value) { | ||
Jonathan Frederic
|
r17727 | this.update_overflow_y(); | ||
}, this); | ||||
Jason Grout
|
r18996 | this.listenTo(this.model, 'change:box_style', function(model, value) { | ||
Jonathan Frederic
|
r17728 | this.update_box_style(); | ||
}, this); | ||||
Jason Grout
|
r14503 | }, | ||
sylvain.corlay
|
r17349 | |||
Jonathan Frederic
|
r17722 | update_attr: function(name, value) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Set a css attr of the widget view. | ||||
*/ | ||||
Jonathan Frederic
|
r17722 | this.$box.css(name, value); | ||
}, | ||||
sylvain.corlay
|
r17349 | render: function(){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* Called when view is rendered. | ||||
*/ | ||||
Jonathan Frederic
|
r17663 | this.$box = this.$el; | ||
Jonathan Frederic
|
r17661 | this.$box.addClass('widget-box'); | ||
Jason Grout
|
r18996 | this.children_views.update(this.model.get('children')); | ||
Jonathan Frederic
|
r17727 | this.update_overflow_x(); | ||
this.update_overflow_y(); | ||||
Jonathan Frederic
|
r17728 | this.update_box_style(''); | ||
Jonathan Frederic
|
r17727 | }, | ||
update_overflow_x: function() { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* Called when the x-axis overflow setting is changed. | ||||
*/ | ||||
Jonathan Frederic
|
r17727 | this.$box.css('overflow-x', this.model.get('overflow_x')); | ||
}, | ||||
update_overflow_y: function() { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* Called when the y-axis overflow setting is changed. | ||||
*/ | ||||
Jonathan Frederic
|
r17727 | this.$box.css('overflow-y', this.model.get('overflow_y')); | ||
Jason Grout
|
r14503 | }, | ||
Jonathan Frederic
|
r17728 | |||
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); | ||||
}, | ||||
Jason Grout
|
r14503 | |||
Jonathan Frederic
|
r14598 | add_child_model: function(model) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Called when a model is added to the children list. | ||||
*/ | ||||
Thomas Kluyver
|
r18142 | var that = this; | ||
Jonathan Frederic
|
r18883 | var dummy = $('<div/>'); | ||
that.$box.append(dummy); | ||||
Jonathan Frederic
|
r18896 | return this.create_child_view(model).then(function(view) { | ||
Jason Grout
|
r18888 | dummy.replaceWith(view.el); | ||
Jonathan Frederic
|
r16660 | |||
Thomas Kluyver
|
r18142 | // Trigger the displayed event of the child view. | ||
that.after_displayed(function() { | ||||
view.trigger('displayed'); | ||||
}); | ||||
Jonathan Frederic
|
r18896 | return view; | ||
Jason Grout
|
r19080 | }).catch(utils.reject("Couldn't add child view to box", true)); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jason Grout
|
r18996 | |||
remove: function() { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* We remove this widget before removing the children as an optimization | ||||
* we want to remove the entire container from the DOM first before | ||||
* removing each individual child separately. | ||||
*/ | ||||
Jason Grout
|
r18996 | BoxView.__super__.remove.apply(this, arguments); | ||
this.children_views.remove(); | ||||
}, | ||||
Jonathan Frederic
|
r14263 | }); | ||
Jonathan Frederic
|
r17596 | |||
Jonathan Frederic
|
r17637 | var FlexBoxView = BoxView.extend({ | ||
Jonathan Frederic
|
r17596 | render: function(){ | ||
Jonathan Frederic
|
r17637 | FlexBoxView.__super__.render.apply(this); | ||
Jason Grout
|
r18996 | this.listenTo(this.model, 'change:orientation', this.update_orientation, this); | ||
this.listenTo(this.model, 'change:flex', this._flex_changed, this); | ||||
this.listenTo(this.model, 'change:pack', this._pack_changed, this); | ||||
this.listenTo(this.model, 'change:align', this._align_changed, this); | ||||
Jonathan Frederic
|
r17596 | this._flex_changed(); | ||
this._pack_changed(); | ||||
this._align_changed(); | ||||
Jonathan Frederic
|
r17688 | this.update_orientation(); | ||
Sylvain Corlay
|
r17600 | }, | ||
update_orientation: function(){ | ||||
var orientation = this.model.get("orientation"); | ||||
if (orientation == "vertical") { | ||||
Jonathan Frederic
|
r17661 | this.$box.removeClass("hbox").addClass("vbox"); | ||
Sylvain Corlay
|
r17600 | } else { | ||
Jonathan Frederic
|
r17661 | this.$box.removeClass("vbox").addClass("hbox"); | ||
Sylvain Corlay
|
r17600 | } | ||
Jonathan Frederic
|
r17596 | }, | ||
_flex_changed: function(){ | ||||
if (this.model.previous('flex')) { | ||||
Jonathan Frederic
|
r17661 | this.$box.removeClass('box-flex' + this.model.previous('flex')); | ||
Jonathan Frederic
|
r17596 | } | ||
Jonathan Frederic
|
r17661 | this.$box.addClass('box-flex' + this.model.get('flex')); | ||
Jonathan Frederic
|
r17596 | }, | ||
_pack_changed: function(){ | ||||
if (this.model.previous('pack')) { | ||||
Jonathan Frederic
|
r17661 | this.$box.removeClass(this.model.previous('pack')); | ||
Jonathan Frederic
|
r17596 | } | ||
Jonathan Frederic
|
r17661 | this.$box.addClass(this.model.get('pack')); | ||
Jonathan Frederic
|
r17596 | }, | ||
_align_changed: function(){ | ||||
if (this.model.previous('align')) { | ||||
Jonathan Frederic
|
r17661 | this.$box.removeClass('align-' + this.model.previous('align')); | ||
Jonathan Frederic
|
r17596 | } | ||
Jonathan Frederic
|
r17661 | this.$box.addClass('align-' + this.model.get('align')); | ||
Jonathan Frederic
|
r17596 | }, | ||
}); | ||||
Jonathan Frederic
|
r17198 | return { | ||
Jason Grout
|
r21029 | 'unpack_models': unpack_models, | ||
Jason Grout
|
r20935 | 'BoxModel': BoxModel, | ||
Jonathan Frederic
|
r17637 | 'BoxView': BoxView, | ||
'FlexBoxView': FlexBoxView, | ||||
Jonathan Frederic
|
r17198 | }; | ||
Jonathan Frederic
|
r14409 | }); | ||