##// END OF EJS Templates
Add scroll_to_bottom method for TextAreaView (StringWidget).
Jonathan Frederic -
Show More
@@ -1,154 +1,162 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2013 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // StringWidget
10 10 //============================================================================
11 11
12 12 /**
13 13 * @module IPython
14 14 * @namespace IPython
15 15 **/
16 16
17 17 define(["notebook/js/widget"], function(widget_manager){
18 18 var StringWidgetModel = IPython.WidgetModel.extend({});
19 19 widget_manager.register_widget_model('StringWidgetModel', StringWidgetModel);
20 20
21 21 var LabelView = IPython.WidgetView.extend({
22 22
23 23 // Called when view is rendered.
24 24 render : function(){
25 25 this.$el = $('<div />');
26 26 this.update(); // Set defaults.
27 27 },
28 28
29 29 // Handles: Backend -> Frontend Sync
30 30 // Frontent -> Frontend Sync
31 31 update : function(){
32 32 this.$el.html(this.model.get('value'));
33 33 return IPython.WidgetView.prototype.update.call(this);
34 34 },
35 35
36 36 });
37 37
38 38 widget_manager.register_widget_view('LabelView', LabelView);
39 39
40 40 var TextAreaView = IPython.WidgetView.extend({
41 41
42 42 // Called when view is rendered.
43 43 render : function(){
44 44 this.$el
45 45 .addClass('widget-hbox')
46 46 .html('');
47 47 this.$label = $('<div />')
48 48 .appendTo(this.$el)
49 49 .addClass('widget-hlabel')
50 50 .hide();
51 51 this.$textbox = $('<textarea />')
52 52 .attr('rows', 5)
53 53 .addClass('widget-text')
54 54 .appendTo(this.$el);
55 55 this.$el_to_style = this.$textbox; // Set default element to style
56 56 this.update(); // Set defaults.
57 57 },
58 58
59 59 // Handles: Backend -> Frontend Sync
60 60 // Frontent -> Frontend Sync
61 61 update : function(){
62 62 if (!this.user_invoked_update) {
63 63 this.$textbox.val(this.model.get('value'));
64 64 }
65 65
66 if (this.last_scroll_to_bottom == undefined) {
67 this.last_scroll_to_bottom = 0;
68 }
69 if (this.last_scroll_to_bottom < this.model.get('scroll_to_bottoms')) {
70 this.last_scroll_to_bottom < this.model.get('scroll_to_bottoms');
71 this.$textbox.scrollTop(this.$textbox[0].scrollHeight);
72 }
73
66 74 var disabled = this.model.get('disabled');
67 75 this.$textbox.prop('disabled', disabled);
68 76
69 77 var description = this.model.get('description');
70 78 if (description.length == 0) {
71 79 this.$label.hide();
72 80 } else {
73 81 this.$label.html(description);
74 82 this.$label.show();
75 83 }
76 84 return IPython.WidgetView.prototype.update.call(this);
77 85 },
78 86
79 87 events: {"keyup textarea" : "handleChanging",
80 88 "paste textarea" : "handleChanging",
81 89 "cut textarea" : "handleChanging"},
82 90
83 91 // Handles and validates user input.
84 92 handleChanging: function(e) {
85 93 this.user_invoked_update = true;
86 94 this.model.set('value', e.target.value);
87 95 this.model.update_other_views(this);
88 96 this.user_invoked_update = false;
89 97 },
90 98 });
91 99
92 100 widget_manager.register_widget_view('TextAreaView', TextAreaView);
93 101
94 102 var TextBoxView = IPython.WidgetView.extend({
95 103
96 104 // Called when view is rendered.
97 105 render : function(){
98 106 this.$el
99 107 .addClass('widget-hbox-single')
100 108 .html('');
101 109 this.$label = $('<div />')
102 110 .addClass('widget-hlabel')
103 111 .appendTo(this.$el)
104 112 .hide();
105 113 this.$textbox = $('<input type="text" />')
106 114 .addClass('input')
107 115 .addClass('widget-text')
108 116 .appendTo(this.$el);
109 117 this.$el_to_style = this.$textbox; // Set default element to style
110 118 this.update(); // Set defaults.
111 119 },
112 120
113 121 // Handles: Backend -> Frontend Sync
114 122 // Frontent -> Frontend Sync
115 123 update : function(){
116 124 if (this.$textbox.val() != this.model.get('value')) {
117 125 this.$textbox.val(this.model.get('value'));
118 126 }
119 127
120 128 var disabled = this.model.get('disabled');
121 129 this.$textbox.prop('disabled', disabled);
122 130
123 131 var description = this.model.get('description');
124 132 if (description.length == 0) {
125 133 this.$label.hide();
126 134 } else {
127 135 this.$label.html(description);
128 136 this.$label.show();
129 137 }
130 138 return IPython.WidgetView.prototype.update.call(this);
131 139 },
132 140
133 141 events: {"keyup input" : "handleChanging",
134 142 "paste input" : "handleChanging",
135 143 "cut input" : "handleChanging",
136 144 "keypress input" : "handleKeypress"},
137 145
138 146 // Handles and validates user input.
139 147 handleChanging: function(e) {
140 148 this.model.set('value', e.target.value);
141 149 this.model.update_other_views(this);
142 150 },
143 151
144 152 // Handles text submition
145 153 handleKeypress: function(e) {
146 154 if (e.keyCode == 13) { // Return key
147 155 this.model.set('submits', this.model.get('submits') + 1);
148 156 this.model.update_other_views(this);
149 157 }
150 158 },
151 159 });
152 160
153 161 widget_manager.register_widget_view('TextBoxView', TextBoxView);
154 162 });
@@ -1,79 +1,84 b''
1 1 """StringWidget class.
2 2
3 3 Represents a unicode string using a widget.
4 4 """
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 import inspect
17 17 import types
18 18
19 19 from .widget import Widget
20 20 from IPython.utils.traitlets import Unicode, Bool, List, Int
21 21
22 22 #-----------------------------------------------------------------------------
23 23 # Classes
24 24 #-----------------------------------------------------------------------------
25 25 class StringWidget(Widget):
26 26 target_name = Unicode('StringWidgetModel')
27 27 default_view_name = Unicode('TextBoxView')
28 28
29 29 # Keys
30 _keys = ['value', 'disabled', 'description', 'submits']
30 _keys = ['value', 'disabled', 'description', 'submits', 'scroll_to_bottoms']
31 31 value = Unicode(help="String value")
32 32 disabled = Bool(False, help="Enable or disable user changes")
33 33 description = Unicode(help="Description of the value this widget represents")
34 34 submits = Int(0, help="Used to capture and fire submission ")
35 scroll_to_bottoms = Int(0, help="Used to scroll a TextAreaView to the bottom")
35 36
36 37
37 38 def __init__(self, **kwargs):
38 39 super(StringWidget, self).__init__(**kwargs)
39 40 self._submission_callbacks = []
40 41
41 42
43 def scroll_to_bottom(self):
44 self.scroll_to_bottoms += 1
45
46
42 47 def on_submit(self, callback, remove=False):
43 48 """Register a callback to handle text submission (triggered when the
44 49 user clicks enter).
45 50
46 51 Parameters
47 52 callback: Method handle
48 53 Function to be called when the text has been submitted. Function
49 54 can have two possible signatures:
50 55 callback()
51 56 callback(sender)
52 57 remove: bool (optional)
53 58 Whether or not to unregister the callback"""
54 59 if remove and callback in self._submission_callbacks:
55 60 self._submission_callbacks.remove(callback)
56 61 elif not remove and not callback in self._submission_callbacks:
57 62 self._submission_callbacks.append(callback)
58 63
59 64
60 65 def _submits_changed(self, name, old_value, new_value):
61 66 """Handles when a string widget view is submitted."""
62 67 if new_value > old_value:
63 68 for handler in self._submission_callbacks:
64 69 if callable(handler):
65 70 argspec = inspect.getargspec(handler)
66 71 nargs = len(argspec[0])
67 72
68 73 # Bound methods have an additional 'self' argument
69 74 if isinstance(handler, types.MethodType):
70 75 nargs -= 1
71 76
72 77 # Call the callback
73 78 if nargs == 0:
74 79 handler()
75 80 elif nargs == 1:
76 81 handler(self)
77 82 else:
78 83 raise TypeError('StringWidget submit callback must ' \
79 84 'accept 0 or 1 arguments.')
General Comments 0
You need to be logged in to leave comments. Login now