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