Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define([
5
"widgets/js/widget",
6
"jquery",
7
"bootstrap",
8
], function(widget, $){
9
10
var HTMLView = widget.DOMWidgetView.extend({
11
render : function(){
12
/**
13
* Called when view is rendered.
14
*/
15
this.update(); // Set defaults.
16
},
17
18
update : function(){
19
/**
20
* Update the contents of this view
21
*
22
* Called when the model is changed. The model may have been
23
* changed by another view or by a state update from the back-end.
24
*/
25
this.$el.html(this.model.get('value')); // CAUTION! .html(...) CALL MANDITORY!!!
26
return HTMLView.__super__.update.apply(this);
27
},
28
});
29
30
31
var LatexView = widget.DOMWidgetView.extend({
32
render : function(){
33
/**
34
* Called when view is rendered.
35
*/
36
this.update(); // Set defaults.
37
},
38
39
update : function(){
40
/**
41
* Update the contents of this view
42
*
43
* Called when the model is changed. The model may have been
44
* changed by another view or by a state update from the back-end.
45
*/
46
this.typeset(this.$el, this.model.get('value'));
47
return LatexView.__super__.update.apply(this);
48
},
49
});
50
51
52
var TextareaView = widget.DOMWidgetView.extend({
53
render: function(){
54
/**
55
* Called when view is rendered.
56
*/
57
this.$el
58
.addClass('widget-hbox widget-textarea');
59
this.$label = $('<div />')
60
.appendTo(this.$el)
61
.addClass('widget-label')
62
.hide();
63
this.$textbox = $('<textarea />')
64
.attr('rows', 5)
65
.addClass('widget-text form-control')
66
.appendTo(this.$el);
67
this.update(); // Set defaults.
68
69
this.model.on('msg:custom', $.proxy(this._handle_textarea_msg, this));
70
this.model.on('change:placeholder', function(model, value, options) {
71
this.update_placeholder(value);
72
}, this);
73
74
this.update_placeholder();
75
},
76
77
_handle_textarea_msg: function (content){
78
/**
79
* Handle when a custom msg is recieved from the back-end.
80
*/
81
if (content.method == "scroll_to_bottom") {
82
this.scroll_to_bottom();
83
}
84
},
85
86
update_placeholder: function(value) {
87
if (!value) {
88
value = this.model.get('placeholder');
89
}
90
this.$textbox.attr('placeholder', value);
91
},
92
93
scroll_to_bottom: function (){
94
/**
95
* Scroll the text-area view to the bottom.
96
*/
97
this.$textbox.scrollTop(this.$textbox[0].scrollHeight);
98
},
99
100
update: function(options){
101
/**
102
* Update the contents of this view
103
*
104
* Called when the model is changed. The model may have been
105
* changed by another view or by a state update from the back-end.
106
*/
107
if (options === undefined || options.updated_view != this) {
108
this.$textbox.val(this.model.get('value'));
109
110
var disabled = this.model.get('disabled');
111
this.$textbox.prop('disabled', disabled);
112
113
var description = this.model.get('description');
114
if (description.length === 0) {
115
this.$label.hide();
116
} else {
117
this.typeset(this.$label, description);
118
this.$label.show();
119
}
120
}
121
return TextareaView.__super__.update.apply(this);
122
},
123
124
update_attr: function(name, value) {
125
/**
126
* Set a css attr of the widget view.
127
*/
128
if (name == 'padding' || name == 'margin') {
129
this.$el.css(name, value);
130
} else {
131
this.$textbox.css(name, value);
132
}
133
},
134
135
events: {
136
// Dictionary of events and their handlers.
137
"keyup textarea" : "handleChanging",
138
"paste textarea" : "handleChanging",
139
"cut textarea" : "handleChanging"
140
},
141
142
handleChanging: function(e) {
143
/**
144
* Handles and validates user input.
145
*
146
* Calling model.set will trigger all of the other views of the
147
* model to update.
148
*/
149
this.model.set('value', e.target.value, {updated_view: this});
150
this.touch();
151
},
152
});
153
154
155
var TextView = widget.DOMWidgetView.extend({
156
render: function(){
157
/**
158
* Called when view is rendered.
159
*/
160
this.$el
161
.addClass('widget-hbox widget-text');
162
this.$label = $('<div />')
163
.addClass('widget-label')
164
.appendTo(this.$el)
165
.hide();
166
this.$textbox = $('<input type="text" />')
167
.addClass('input')
168
.addClass('widget-text form-control')
169
.appendTo(this.$el);
170
this.update(); // Set defaults.
171
this.model.on('change:placeholder', function(model, value, options) {
172
this.update_placeholder(value);
173
}, this);
174
175
this.update_placeholder();
176
},
177
178
update_placeholder: function(value) {
179
if (!value) {
180
value = this.model.get('placeholder');
181
}
182
this.$textbox.attr('placeholder', value);
183
},
184
185
update: function(options){
186
/**
187
* Update the contents of this view
188
*
189
* Called when the model is changed. The model may have been
190
* changed by another view or by a state update from the back-end.
191
*/
192
if (options === undefined || options.updated_view != this) {
193
if (this.$textbox.val() != this.model.get('value')) {
194
this.$textbox.val(this.model.get('value'));
195
}
196
197
var disabled = this.model.get('disabled');
198
this.$textbox.prop('disabled', disabled);
199
200
var description = this.model.get('description');
201
if (description.length === 0) {
202
this.$label.hide();
203
} else {
204
this.typeset(this.$label, description);
205
this.$label.show();
206
}
207
}
208
return TextView.__super__.update.apply(this);
209
},
210
211
update_attr: function(name, value) {
212
/**
213
* Set a css attr of the widget view.
214
*/
215
if (name == 'padding' || name == 'margin') {
216
this.$el.css(name, value);
217
} else {
218
this.$textbox.css(name, value);
219
}
220
},
221
222
events: {
223
// Dictionary of events and their handlers.
224
"keyup input" : "handleChanging",
225
"paste input" : "handleChanging",
226
"cut input" : "handleChanging",
227
"keypress input" : "handleKeypress",
228
"blur input" : "handleBlur",
229
"focusout input" : "handleFocusOut"
230
},
231
232
handleChanging: function(e) {
233
/**
234
* Handles user input.
235
*
236
* Calling model.set will trigger all of the other views of the
237
* model to update.
238
*/
239
this.model.set('value', e.target.value, {updated_view: this});
240
this.touch();
241
},
242
243
handleKeypress: function(e) {
244
/**
245
* Handles text submition
246
*/
247
if (e.keyCode == 13) { // Return key
248
this.send({event: 'submit'});
249
e.stopPropagation();
250
e.preventDefault();
251
return false;
252
}
253
},
254
255
handleBlur: function(e) {
256
/**
257
* Prevent a blur from firing if the blur was not user intended.
258
* This is a workaround for the return-key focus loss bug.
259
* TODO: Is the original bug actually a fault of the keyboard
260
* manager?
261
*/
262
if (e.relatedTarget === null) {
263
e.stopPropagation();
264
e.preventDefault();
265
return false;
266
}
267
},
268
269
handleFocusOut: function(e) {
270
/**
271
* Prevent a blur from firing if the blur was not user intended.
272
* This is a workaround for the return-key focus loss bug.
273
*/
274
if (e.relatedTarget === null) {
275
e.stopPropagation();
276
e.preventDefault();
277
return false;
278
}
279
},
280
});
281
282
return {
283
'HTMLView': HTMLView,
284
'LatexView': LatexView,
285
'TextareaView': TextareaView,
286
'TextView': TextView,
287
};
288
});
289
290