Skip to content

Commit 7365132

Browse files
authored
Merge pull request #17 from osscar-org/theme-update
Theme update
2 parents 3055774 + 05868a9 commit 7365132

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

src/__tests__/index.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66

77
import { createTestModel } from './utils';
88

9-
import { ExampleModel } from '..';
9+
import { WidgetCodeModel } from '..';
1010

1111
describe('Example', () => {
1212
describe('ExampleModel', () => {
1313
it('should be createable', () => {
14-
const model = createTestModel(ExampleModel);
15-
expect(model).toBeInstanceOf(ExampleModel);
14+
const model = createTestModel(WidgetCodeModel);
15+
expect(model).toBeInstanceOf(WidgetCodeModel);
1616
expect(model.get('value')).toEqual('Hello World');
1717
});
1818

1919
it('should be createable with a value', () => {
2020
const state = { value: 'Foo Bar!' };
21-
const model = createTestModel(ExampleModel, state);
22-
expect(model).toBeInstanceOf(ExampleModel);
21+
const model = createTestModel(WidgetCodeModel, state);
22+
expect(model).toBeInstanceOf(WidgetCodeModel);
2323
expect(model.get('value')).toEqual('Foo Bar!');
2424
});
2525
});

src/widget.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ export class WidgetCodeModel extends DOMWidgetModel {
4444
};
4545
}
4646

47-
initialize() {
48-
DOMWidgetModel.prototype.initialize.apply(this, arguments as any);
47+
initialize(...args: any[]) {
48+
const [attributes, options] = args;
49+
DOMWidgetModel.prototype.initialize.apply(this, [attributes, options]);
4950
this.attributes['function_body_id'] = _.uniqueId('function_body');
5051
}
5152

@@ -92,10 +93,8 @@ export class WidgetCodeView extends DOMWidgetView {
9293
theTextareaId +
9394
'-body"></textarea>';
9495

95-
const _this = this; // Backbone.js view object
96-
9796
_.defer(() => {
98-
_this.mySignatureCodeMirror = CodeMirror.fromTextArea(
97+
this.mySignatureCodeMirror = CodeMirror.fromTextArea(
9998
<HTMLTextAreaElement>(
10099
document.getElementById(theTextareaId + '-signature')
101100
),
@@ -114,18 +113,18 @@ export class WidgetCodeView extends DOMWidgetView {
114113
}
115114
);
116115

117-
_this.mySignatureCodeMirror.setOption('theme', _this.theme);
118-
_this.mySignatureCodeMirror.display.wrapper.classList.add(
116+
this.mySignatureCodeMirror.setOption('theme', this.theme);
117+
this.mySignatureCodeMirror.display.wrapper.classList.add(
119118
'widget-code-input-signature'
120119
);
121120

122-
_this.myDocstringCodeMirror = CodeMirror.fromTextArea(
121+
this.myDocstringCodeMirror = CodeMirror.fromTextArea(
123122
<HTMLTextAreaElement>(
124123
document.getElementById(theTextareaId + '-docstring')
125124
),
126125
{
127126
lineNumbers: true,
128-
//firstLineNumber: // This will be set later by changing the content, that
127+
//firstLineNumber: // This will be set later by changing the content, this
129128
// indirectly calls the function to update the line numbers
130129
mode: {
131130
name: 'python',
@@ -140,16 +139,16 @@ export class WidgetCodeView extends DOMWidgetView {
140139
}
141140
);
142141
// Add CSS
143-
_this.myDocstringCodeMirror.setOption('theme', _this.theme);
144-
_this.myDocstringCodeMirror.display.wrapper.classList.add(
142+
this.myDocstringCodeMirror.setOption('theme', this.theme);
143+
this.myDocstringCodeMirror.display.wrapper.classList.add(
145144
'widget-code-input-docstring'
146145
);
147146

148-
_this.myBodyCodeMirror = CodeMirror.fromTextArea(
147+
this.myBodyCodeMirror = CodeMirror.fromTextArea(
149148
<HTMLTextAreaElement>document.getElementById(theTextareaId + '-body'),
150149
{
151150
lineNumbers: true,
152-
//firstLineNumber: // This will be set later by changing the content, that
151+
//firstLineNumber: // This will be set later by changing the content, this
153152
// indirectly calls the function to update the line numbers
154153
mode: {
155154
name: 'python',
@@ -165,44 +164,48 @@ export class WidgetCodeView extends DOMWidgetView {
165164
);
166165

167166
// Add CSS
168-
_this.myBodyCodeMirror.setOption('theme', _this.theme);
169-
_this.myBodyCodeMirror.display.wrapper.classList.add(
167+
this.myBodyCodeMirror.setOption('theme', this.theme);
168+
this.myBodyCodeMirror.display.wrapper.classList.add(
170169
'widget-code-input-body'
171170
);
172171
// I need to attach the backboneView, since I'm going to use
173172
// a CodeMirror event rather than a backboneView, but
174173
// bodyChange needs to access the Backbone.js View
175-
_this.myBodyCodeMirror._backboneView = _this;
174+
this.myBodyCodeMirror._backboneView = this;
176175

177176
// Set the initial values of the cells
178-
_this.signatureValueChanged();
179-
_this.docstringValueChanged();
180-
_this.bodyValueChanged();
177+
this.signatureValueChanged();
178+
this.docstringValueChanged();
179+
this.bodyValueChanged();
181180

182181
// When the value is changed in python, update the value in the widget
183182
// I set this in the 'defer' so that bodyValueChanged is called only
184183
// when the CodeWidget has been rendered.
185-
_this.model.on(
186-
'change:function_name',
187-
_this.signatureValueChanged,
188-
_this
189-
);
190-
_this.model.on(
184+
this.model.on('change:function_name', this.signatureValueChanged, this);
185+
this.model.on(
191186
'change:function_parameters',
192-
_this.signatureValueChanged,
193-
_this
187+
this.signatureValueChanged,
188+
this
194189
);
195-
_this.model.on('change:docstring', _this.docstringValueChanged, _this);
196-
_this.model.on('change:function_body', _this.bodyValueChanged, _this);
190+
this.model.on('change:docstring', this.docstringValueChanged, this);
191+
this.model.on('change:function_body', this.bodyValueChanged, this);
192+
this.model.on('change:code_theme', this.themeChanged, this);
197193

198194
// For a proper CodeMirror functioning, we use CodeMirror events
199195
// rather than Backbone.js events.
200196
// Only the function_body is not read-only, so we need only this
201197
// event
202-
_this.myBodyCodeMirror.on('change', _this.bodyChange);
198+
this.myBodyCodeMirror.on('change', this.bodyChange);
203199
});
204200
}
205201

202+
themeChanged() {
203+
this.theme = this.model.get('code_theme');
204+
this.mySignatureCodeMirror.setOption('theme', this.theme);
205+
this.myDocstringCodeMirror.setOption('theme', this.theme);
206+
this.myBodyCodeMirror.setOption('theme', this.theme);
207+
}
208+
206209
bodyChange(instance: any, changeObj: any) {
207210
// On change of the text in the code widget, send back the content
208211
// to python.

widget_code_input/tests/test_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
import pytest
88

9-
from ..example import ExampleWidget
9+
from ..widget_code_input import WidgetCodeInput
1010

1111

1212
def test_example_creation_blank():
13-
w = ExampleWidget()
14-
assert w.value == 'Hello World'
13+
w = WidgetCodeInput("my_function")
14+
assert w.function_name == 'my_function'

0 commit comments

Comments
 (0)