string.js
147 lines
| 5.0 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
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. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// StringWidget | ||||
//============================================================================ | ||||
/** | ||||
* @module IPython | ||||
* @namespace IPython | ||||
**/ | ||||
Jonathan Frederic
|
r14374 | define(["notebook/js/widget"], function(widget_manager){ | ||
Jonathan Frederic
|
r14263 | var StringWidgetModel = IPython.WidgetModel.extend({}); | ||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_model('StringWidgetModel', StringWidgetModel); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | var LabelView = IPython.WidgetView.extend({ | ||
// Called when view is rendered. | ||||
render : function(){ | ||||
Jonathan Frederic
|
r14278 | this.$el = $('<div />'); | ||
Jonathan Frederic
|
r14263 | this.update(); // Set defaults. | ||
}, | ||||
// Handles: Backend -> Frontend Sync | ||||
// Frontent -> Frontend Sync | ||||
update : function(){ | ||||
Jonathan Frederic
|
r14268 | this.$el.html(this.model.get('value')); | ||
Jonathan Frederic
|
r14279 | return IPython.WidgetView.prototype.update.call(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
}); | ||||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_view('LabelView', LabelView); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14291 | var TextAreaView = IPython.WidgetView.extend({ | ||
Jonathan Frederic
|
r14263 | |||
// Called when view is rendered. | ||||
render : function(){ | ||||
this.$el | ||||
Jonathan Frederic
|
r14295 | .addClass('widget-hbox') | ||
Jonathan Frederic
|
r14278 | .html(''); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r14297 | .addClass('widget-hlabel') | ||
Jonathan Frederic
|
r14292 | .hide(); | ||
Jonathan Frederic
|
r14263 | this.$textbox = $('<textarea />') | ||
.attr('rows', 5) | ||||
Jonathan Frederic
|
r14295 | .addClass('widget-text') | ||
Jonathan Frederic
|
r14263 | .appendTo(this.$el); | ||
Jonathan Frederic
|
r14314 | this.$el_to_style = this.$textbox; // Set default element to style | ||
Jonathan Frederic
|
r14263 | this.update(); // Set defaults. | ||
}, | ||||
// Handles: Backend -> Frontend Sync | ||||
// Frontent -> Frontend Sync | ||||
update : function(){ | ||||
if (!this.user_invoked_update) { | ||||
this.$textbox.val(this.model.get('value')); | ||||
} | ||||
Jonathan Frederic
|
r14292 | |||
Jonathan Frederic
|
r14302 | var disabled = this.model.get('disabled'); | ||
this.$textbox.prop('disabled', disabled); | ||||
Jonathan Frederic
|
r14292 | var description = this.model.get('description'); | ||
if (description.length == 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
Jonathan Frederic
|
r14279 | return IPython.WidgetView.prototype.update.call(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
events: {"keyup textarea" : "handleChanging", | ||||
"paste textarea" : "handleChanging", | ||||
"cut textarea" : "handleChanging"}, | ||||
// Handles and validates user input. | ||||
handleChanging: function(e) { | ||||
this.user_invoked_update = true; | ||||
this.model.set('value', e.target.value); | ||||
Jonathan Frederic
|
r14278 | this.model.update_other_views(this); | ||
Jonathan Frederic
|
r14263 | this.user_invoked_update = false; | ||
}, | ||||
}); | ||||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_view('TextAreaView', TextAreaView); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14291 | var TextBoxView = IPython.WidgetView.extend({ | ||
Jonathan Frederic
|
r14263 | |||
// Called when view is rendered. | ||||
render : function(){ | ||||
this.$el | ||||
Jonathan Frederic
|
r14295 | .addClass('widget-hbox-single') | ||
Jonathan Frederic
|
r14278 | .html(''); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
Jonathan Frederic
|
r14297 | .addClass('widget-hlabel') | ||
Jonathan Frederic
|
r14292 | .appendTo(this.$el) | ||
.hide(); | ||||
Jonathan Frederic
|
r14263 | this.$textbox = $('<input type="text" />') | ||
.addClass('input') | ||||
Jonathan Frederic
|
r14295 | .addClass('widget-text') | ||
Jonathan Frederic
|
r14263 | .appendTo(this.$el); | ||
Jonathan Frederic
|
r14314 | this.$el_to_style = this.$textbox; // Set default element to style | ||
Jonathan Frederic
|
r14263 | this.update(); // Set defaults. | ||
}, | ||||
// Handles: Backend -> Frontend Sync | ||||
// Frontent -> Frontend Sync | ||||
update : function(){ | ||||
if (!this.user_invoked_update) { | ||||
this.$textbox.val(this.model.get('value')); | ||||
} | ||||
Jonathan Frederic
|
r14292 | |||
Jonathan Frederic
|
r14302 | var disabled = this.model.get('disabled'); | ||
this.$textbox.prop('disabled', disabled); | ||||
Jonathan Frederic
|
r14292 | var description = this.model.get('description'); | ||
if (description.length == 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
Jonathan Frederic
|
r14279 | return IPython.WidgetView.prototype.update.call(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
events: {"keyup input" : "handleChanging", | ||||
"paste input" : "handleChanging", | ||||
"cut input" : "handleChanging"}, | ||||
// Handles and validates user input. | ||||
handleChanging: function(e) { | ||||
this.user_invoked_update = true; | ||||
this.model.set('value', e.target.value); | ||||
Jonathan Frederic
|
r14278 | this.model.update_other_views(this); | ||
Jonathan Frederic
|
r14263 | this.user_invoked_update = false; | ||
}, | ||||
}); | ||||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_view('TextBoxView', TextBoxView); | ||
Jonathan Frederic
|
r14263 | }); | ||