Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
//----------------------------------------------------------------------------
2
// Copyright (C) 2013 The IPython Development Team
3
//
4
// Distributed under the terms of the BSD License. The full license is in
5
// the file COPYING, distributed as part of this software.
6
//----------------------------------------------------------------------------
7
8
//============================================================================
9
// StringWidget
10
//============================================================================
11
12
/**
13
* @module IPython
14
* @namespace IPython
15
**/
16
17
define(["widgets/js/widget"], function(WidgetManager){
18
19
var HTMLView = IPython.DOMWidgetView.extend({
20
render : function(){
21
// Called when view is rendered.
22
this.update(); // Set defaults.
23
},
24
25
update : function(){
26
// Update the contents of this view
27
//
28
// Called when the model is changed. The model may have been
29
// changed by another view or by a state update from the back-end.
30
this.$el.html(this.model.get('value')); // CAUTION! .html(...) CALL MANDITORY!!!
31
return HTMLView.__super__.update.apply(this);
32
},
33
});
34
WidgetManager.register_widget_view('HTMLView', HTMLView);
35
36
37
var LatexView = IPython.DOMWidgetView.extend({
38
render : function(){
39
// Called when view is rendered.
40
this.update(); // Set defaults.
41
},
42
43
update : function(){
44
// Update the contents of this view
45
//
46
// Called when the model is changed. The model may have been
47
// changed by another view or by a state update from the back-end.
48
this.$el.text(this.model.get('value'));
49
MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]);
50
51
return LatexView.__super__.update.apply(this);
52
},
53
});
54
WidgetManager.register_widget_view('LatexView', LatexView);
55
56
57
var TextareaView = IPython.DOMWidgetView.extend({
58
render: function(){
59
// Called when view is rendered.
60
this.$el
61
.addClass('widget-hbox');
62
this.$label = $('<div />')
63
.appendTo(this.$el)
64
.addClass('widget-hlabel')
65
.hide();
66
this.$textbox = $('<textarea />')
67
.attr('rows', 5)
68
.addClass('widget-text')
69
.appendTo(this.$el);
70
this.$el_to_style = this.$textbox; // Set default element to style
71
this.update(); // Set defaults.
72
73
this.model.on('msg:custom', $.proxy(this._handle_textarea_msg, this));
74
},
75
76
_handle_textarea_msg: function (content){
77
// Handle when a custom msg is recieved from the back-end.
78
if (content.method == "scroll_to_bottom") {
79
this.scroll_to_bottom();
80
}
81
},
82
83
scroll_to_bottom: function (){
84
// Scroll the text-area view to the bottom.
85
this.$textbox.scrollTop(this.$textbox[0].scrollHeight);
86
},
87
88
update: function(options){
89
// Update the contents of this view
90
//
91
// Called when the model is changed. The model may have been
92
// changed by another view or by a state update from the back-end.
93
if (options === undefined || options.updated_view != this) {
94
this.$textbox.val(this.model.get('value'));
95
96
var disabled = this.model.get('disabled');
97
this.$textbox.prop('disabled', disabled);
98
99
var description = this.model.get('description');
100
if (description.length === 0) {
101
this.$label.hide();
102
} else {
103
this.$label.text(description);
104
this.$label.show();
105
}
106
}
107
return TextareaView.__super__.update.apply(this);
108
},
109
110
events: {
111
// Dictionary of events and their handlers.
112
"keyup textarea" : "handleChanging",
113
"paste textarea" : "handleChanging",
114
"cut textarea" : "handleChanging"
115
},
116
117
handleChanging: function(e) {
118
// Handles and validates user input.
119
120
// Calling model.set will trigger all of the other views of the
121
// model to update.
122
this.model.set('value', e.target.value, {updated_view: this});
123
this.touch();
124
},
125
});
126
WidgetManager.register_widget_view('TextareaView', TextareaView);
127
128
129
var TextView = IPython.DOMWidgetView.extend({
130
render: function(){
131
// Called when view is rendered.
132
this.$el
133
.addClass('widget-hbox-single');
134
this.$label = $('<div />')
135
.addClass('widget-hlabel')
136
.appendTo(this.$el)
137
.hide();
138
this.$textbox = $('<input type="text" />')
139
.addClass('input')
140
.addClass('widget-text')
141
.appendTo(this.$el);
142
this.$el_to_style = this.$textbox; // Set default element to style
143
this.update(); // Set defaults.
144
},
145
146
update: function(options){
147
// Update the contents of this view
148
//
149
// Called when the model is changed. The model may have been
150
// changed by another view or by a state update from the back-end.
151
if (options === undefined || options.updated_view != this) {
152
if (this.$textbox.val() != this.model.get('value')) {
153
this.$textbox.val(this.model.get('value'));
154
}
155
156
var disabled = this.model.get('disabled');
157
this.$textbox.prop('disabled', disabled);
158
159
var description = this.model.get('description');
160
if (description.length === 0) {
161
this.$label.hide();
162
} else {
163
this.$label.text(description);
164
this.$label.show();
165
}
166
}
167
return TextView.__super__.update.apply(this);
168
},
169
170
events: {
171
// Dictionary of events and their handlers.
172
"keyup input" : "handleChanging",
173
"paste input" : "handleChanging",
174
"cut input" : "handleChanging",
175
"keypress input" : "handleKeypress",
176
"blur input" : "handleBlur",
177
"focusout input" : "handleFocusOut"
178
},
179
180
handleChanging: function(e) {
181
// Handles user input.
182
183
// Calling model.set will trigger all of the other views of the
184
// model to update.
185
this.model.set('value', e.target.value, {updated_view: this});
186
this.touch();
187
},
188
189
handleKeypress: function(e) {
190
// Handles text submition
191
if (e.keyCode == 13) { // Return key
192
this.send({event: 'submit'});
193
event.stopPropagation();
194
event.preventDefault();
195
return false;
196
}
197
},
198
199
handleBlur: function(e) {
200
// Prevent a blur from firing if the blur was not user intended.
201
// This is a workaround for the return-key focus loss bug.
202
// TODO: Is the original bug actually a fault of the keyboard
203
// manager?
204
if (e.relatedTarget === null) {
205
event.stopPropagation();
206
event.preventDefault();
207
return false;
208
}
209
},
210
211
handleFocusOut: function(e) {
212
// Prevent a blur from firing if the blur was not user intended.
213
// This is a workaround for the return-key focus loss bug.
214
if (e.relatedTarget === null) {
215
event.stopPropagation();
216
event.preventDefault();
217
return false;
218
}
219
},
220
});
221
WidgetManager.register_widget_view('TextView', TextView);
222
});
223
224