Show More
@@ -1,104 +1,129 b'' | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | 'jquery', |
|
5 | 'jquery', | |
6 | 'base/js/utils', |
|
6 | 'base/js/utils', | |
7 | ], |
|
7 | ], | |
8 | function($, utils) { |
|
8 | function($, utils) { | |
9 | "use strict"; |
|
9 | "use strict"; | |
10 | var ConfigSection = function(section_name, options) { |
|
10 | var ConfigSection = function(section_name, options) { | |
11 | this.section_name = section_name; |
|
11 | this.section_name = section_name; | |
12 | this.base_url = options.base_url; |
|
12 | this.base_url = options.base_url; | |
13 | this.data = {}; |
|
13 | this.data = {}; | |
14 |
|
14 | |||
15 | var that = this; |
|
15 | var that = this; | |
16 |
|
16 | |||
17 | /* .loaded is a promise, fulfilled the first time the config is loaded |
|
17 | /* .loaded is a promise, fulfilled the first time the config is loaded | |
18 | * from the server. Code can do: |
|
18 | * from the server. Code can do: | |
19 | * conf.loaded.then(function() { ... using conf.data ... }); |
|
19 | * conf.loaded.then(function() { ... using conf.data ... }); | |
20 | */ |
|
20 | */ | |
21 | this._one_load_finished = false; |
|
21 | this._one_load_finished = false; | |
22 | this.loaded = new Promise(function(resolve, reject) { |
|
22 | this.loaded = new Promise(function(resolve, reject) { | |
23 | that._finish_firstload = resolve; |
|
23 | that._finish_firstload = resolve; | |
24 | }); |
|
24 | }); | |
25 | }; |
|
25 | }; | |
26 |
|
26 | |||
27 | ConfigSection.prototype.api_url = function() { |
|
27 | ConfigSection.prototype.api_url = function() { | |
28 | return utils.url_join_encode(this.base_url, 'api/config', this.section_name); |
|
28 | return utils.url_join_encode(this.base_url, 'api/config', this.section_name); | |
29 | }; |
|
29 | }; | |
30 |
|
30 | |||
31 | ConfigSection.prototype._load_done = function() { |
|
31 | ConfigSection.prototype._load_done = function() { | |
32 | if (!this._one_load_finished) { |
|
32 | if (!this._one_load_finished) { | |
33 | this._one_load_finished = true; |
|
33 | this._one_load_finished = true; | |
34 | this._finish_firstload(); |
|
34 | this._finish_firstload(); | |
35 | } |
|
35 | } | |
36 | }; |
|
36 | }; | |
37 |
|
37 | |||
38 | ConfigSection.prototype.load = function() { |
|
38 | ConfigSection.prototype.load = function() { | |
39 | var that = this; |
|
39 | var that = this; | |
40 | return utils.promising_ajax(this.api_url(), { |
|
40 | return utils.promising_ajax(this.api_url(), { | |
41 | cache: false, |
|
41 | cache: false, | |
42 | type: "GET", |
|
42 | type: "GET", | |
43 | dataType: "json", |
|
43 | dataType: "json", | |
44 | }).then(function(data) { |
|
44 | }).then(function(data) { | |
45 | that.data = data; |
|
45 | that.data = data; | |
46 | that._load_done(); |
|
46 | that._load_done(); | |
47 | return data; |
|
47 | return data; | |
48 | }); |
|
48 | }); | |
49 | }; |
|
49 | }; | |
50 |
|
50 | |||
|
51 | /** | |||
|
52 | * Modify the config values stored. Update the local data immediately, | |||
|
53 | * send the change to the server, and use the updated data from the server | |||
|
54 | * when the reply comes. | |||
|
55 | */ | |||
51 | ConfigSection.prototype.update = function(newdata) { |
|
56 | ConfigSection.prototype.update = function(newdata) { | |
|
57 | $.extend(true, this.data, newdata); // true -> recursive update | |||
|
58 | ||||
52 | var that = this; |
|
59 | var that = this; | |
53 | return utils.promising_ajax(this.api_url(), { |
|
60 | return utils.promising_ajax(this.api_url(), { | |
54 | processData: false, |
|
61 | processData: false, | |
55 | type : "PATCH", |
|
62 | type : "PATCH", | |
56 | data: JSON.stringify(newdata), |
|
63 | data: JSON.stringify(newdata), | |
57 | dataType : "json", |
|
64 | dataType : "json", | |
58 | contentType: 'application/json', |
|
65 | contentType: 'application/json', | |
59 | }).then(function(data) { |
|
66 | }).then(function(data) { | |
60 | that.data = data; |
|
67 | that.data = data; | |
61 | that._load_done(); |
|
68 | that._load_done(); | |
62 | return data; |
|
69 | return data; | |
63 | }); |
|
70 | }); | |
64 | }; |
|
71 | }; | |
65 |
|
72 | |||
66 |
|
73 | |||
67 | var ConfigWithDefaults = function(section, defaults, classname) { |
|
74 | var ConfigWithDefaults = function(section, defaults, classname) { | |
68 | this.section = section; |
|
75 | this.section = section; | |
69 | this.defaults = defaults; |
|
76 | this.defaults = defaults; | |
70 | this.classname = classname; |
|
77 | this.classname = classname; | |
71 | }; |
|
78 | }; | |
72 |
|
79 | |||
73 | ConfigWithDefaults.prototype._class_data = function() { |
|
80 | ConfigWithDefaults.prototype._class_data = function() { | |
74 | if (this.classname) { |
|
81 | if (this.classname) { | |
75 | return this.section.data[this.classname] || {}; |
|
82 | return this.section.data[this.classname] || {}; | |
76 | } else { |
|
83 | } else { | |
77 | return this.section.data |
|
84 | return this.section.data | |
78 | } |
|
85 | } | |
79 | }; |
|
86 | }; | |
80 |
|
87 | |||
81 | /** |
|
88 | /** | |
82 | * Wait for config to have loaded, then get a value or the default. |
|
89 | * Wait for config to have loaded, then get a value or the default. | |
83 | * Returns a promise. |
|
90 | * Returns a promise. | |
84 | */ |
|
91 | */ | |
85 | ConfigWithDefaults.prototype.get = function(key) { |
|
92 | ConfigWithDefaults.prototype.get = function(key) { | |
86 | var that = this; |
|
93 | var that = this; | |
87 | return this.section.loaded.then(function() { |
|
94 | return this.section.loaded.then(function() { | |
88 | return this._class_data()[key] || this.defaults[key] |
|
95 | return this._class_data()[key] || this.defaults[key] | |
89 | }); |
|
96 | }); | |
90 | }; |
|
97 | }; | |
91 |
|
98 | |||
92 | /** |
|
99 | /** | |
93 | * Return a config value. If config is not yet loaded, return the default |
|
100 | * Return a config value. If config is not yet loaded, return the default | |
94 | * instead of waiting for it to load. |
|
101 | * instead of waiting for it to load. | |
95 | */ |
|
102 | */ | |
96 | ConfigWithDefaults.prototype.get_sync = function(key) { |
|
103 | ConfigWithDefaults.prototype.get_sync = function(key) { | |
97 | return this._class_data()[key] || this.defaults[key]; |
|
104 | return this._class_data()[key] || this.defaults[key]; | |
98 | }; |
|
105 | }; | |
99 |
|
106 | |||
|
107 | /** | |||
|
108 | * Set a config value. Send the update to the server, and change our | |||
|
109 | * local copy of the data immediately. | |||
|
110 | * Returns a promise which is fulfilled when the server replies to the | |||
|
111 | * change. | |||
|
112 | */ | |||
|
113 | ConfigWithDefaults.prototype.set = function(key, value) { | |||
|
114 | var d = {}; | |||
|
115 | d[key] = value; | |||
|
116 | if (this.classname) { | |||
|
117 | var d2 = {}; | |||
|
118 | d2[this.classname] = d; | |||
|
119 | return this.section.update(d2); | |||
|
120 | } else { | |||
|
121 | return this.section.update(d); | |||
|
122 | } | |||
|
123 | }; | |||
|
124 | ||||
100 | return {ConfigSection: ConfigSection, |
|
125 | return {ConfigSection: ConfigSection, | |
101 | ConfigWithDefaults: ConfigWithDefaults, |
|
126 | ConfigWithDefaults: ConfigWithDefaults, | |
102 | }; |
|
127 | }; | |
103 |
|
128 | |||
104 | }); |
|
129 | }); |
@@ -1,77 +1,77 b'' | |||||
1 |
|
1 | |||
2 | // Test |
|
2 | // Test | |
3 | casper.notebook_test(function () { |
|
3 | casper.notebook_test(function () { | |
4 | var a = 'print("a")'; |
|
4 | var a = 'print("a")'; | |
5 | var index = this.append_cell(a); |
|
5 | var index = this.append_cell(a); | |
6 | this.execute_cell_then(index); |
|
6 | this.execute_cell_then(index); | |
7 |
|
7 | |||
8 | var b = 'print("b")'; |
|
8 | var b = 'print("b")'; | |
9 | index = this.append_cell(b); |
|
9 | index = this.append_cell(b); | |
10 | this.execute_cell_then(index); |
|
10 | this.execute_cell_then(index); | |
11 |
|
11 | |||
12 | var c = 'print("c")'; |
|
12 | var c = 'print("c")'; | |
13 | index = this.append_cell(c); |
|
13 | index = this.append_cell(c); | |
14 | this.execute_cell_then(index); |
|
14 | this.execute_cell_then(index); | |
15 |
|
15 | |||
16 | this.thenEvaluate(function() { |
|
16 | this.thenEvaluate(function() { | |
17 | IPython.notebook.default_cell_type = 'code'; |
|
17 | IPython.notebook.default_cell_type = 'code'; | |
18 | }); |
|
18 | }); | |
19 |
|
19 | |||
20 | this.then(function () { |
|
20 | this.then(function () { | |
21 | // Cell insertion |
|
21 | // Cell insertion | |
22 | this.select_cell(2); |
|
22 | this.select_cell(2); | |
23 | this.trigger_keydown('m'); // Make it markdown |
|
23 | this.trigger_keydown('m'); // Make it markdown | |
24 | this.trigger_keydown('a'); // Creates one cell |
|
24 | this.trigger_keydown('a'); // Creates one cell | |
25 | this.test.assertEquals(this.get_cell_text(2), '', 'a; New cell 2 text is empty'); |
|
25 | this.test.assertEquals(this.get_cell_text(2), '', 'a; New cell 2 text is empty'); | |
26 | this.test.assertEquals(this.get_cell(2).cell_type, 'code', 'a; inserts a code cell'); |
|
26 | this.test.assertEquals(this.get_cell(2).cell_type, 'code', 'a; inserts a code cell'); | |
27 | this.validate_notebook_state('a', 'command', 2); |
|
27 | this.validate_notebook_state('a', 'command', 2); | |
28 | this.trigger_keydown('b'); // Creates one cell |
|
28 | this.trigger_keydown('b'); // Creates one cell | |
29 | this.test.assertEquals(this.get_cell_text(2), '', 'b; Cell 2 text is still empty'); |
|
29 | this.test.assertEquals(this.get_cell_text(2), '', 'b; Cell 2 text is still empty'); | |
30 | this.test.assertEquals(this.get_cell_text(3), '', 'b; New cell 3 text is empty'); |
|
30 | this.test.assertEquals(this.get_cell_text(3), '', 'b; New cell 3 text is empty'); | |
31 | this.test.assertEquals(this.get_cell(3).cell_type, 'code', 'b; inserts a code cell'); |
|
31 | this.test.assertEquals(this.get_cell(3).cell_type, 'code', 'b; inserts a code cell'); | |
32 | this.validate_notebook_state('b', 'command', 3); |
|
32 | this.validate_notebook_state('b', 'command', 3); | |
33 | }); |
|
33 | }); | |
34 |
|
34 | |||
35 | this.thenEvaluate(function() { |
|
35 | this.thenEvaluate(function() { | |
36 |
IPython.notebook.default_cell_type |
|
36 | IPython.notebook.class_config.set('default_cell_type', 'selected'); | |
37 | }); |
|
37 | }); | |
38 |
|
38 | |||
39 | this.then(function () { |
|
39 | this.then(function () { | |
40 | this.select_cell(2); |
|
40 | this.select_cell(2); | |
41 | this.trigger_keydown('m'); // switch it to markdown for the next test |
|
41 | this.trigger_keydown('m'); // switch it to markdown for the next test | |
42 | this.test.assertEquals(this.get_cell(2).cell_type, 'markdown', 'test cell is markdown'); |
|
42 | this.test.assertEquals(this.get_cell(2).cell_type, 'markdown', 'test cell is markdown'); | |
43 | this.trigger_keydown('a'); // new cell above |
|
43 | this.trigger_keydown('a'); // new cell above | |
44 | this.test.assertEquals(this.get_cell(2).cell_type, 'markdown', 'a; inserts a markdown cell when markdown selected'); |
|
44 | this.test.assertEquals(this.get_cell(2).cell_type, 'markdown', 'a; inserts a markdown cell when markdown selected'); | |
45 | this.trigger_keydown('b'); // new cell below |
|
45 | this.trigger_keydown('b'); // new cell below | |
46 | this.test.assertEquals(this.get_cell(3).cell_type, 'markdown', 'b; inserts a markdown cell when markdown selected'); |
|
46 | this.test.assertEquals(this.get_cell(3).cell_type, 'markdown', 'b; inserts a markdown cell when markdown selected'); | |
47 | }); |
|
47 | }); | |
48 |
|
48 | |||
49 | this.thenEvaluate(function() { |
|
49 | this.thenEvaluate(function() { | |
50 |
IPython.notebook.default_cell_type |
|
50 | IPython.notebook.class_config.set('default_cell_type', 'above'); | |
51 | }); |
|
51 | }); | |
52 |
|
52 | |||
53 | this.then(function () { |
|
53 | this.then(function () { | |
54 | this.select_cell(2); |
|
54 | this.select_cell(2); | |
55 | this.trigger_keydown('y'); // switch it to code for the next test |
|
55 | this.trigger_keydown('y'); // switch it to code for the next test | |
56 | this.test.assertEquals(this.get_cell(2).cell_type, 'code', 'test cell is code'); |
|
56 | this.test.assertEquals(this.get_cell(2).cell_type, 'code', 'test cell is code'); | |
57 | this.trigger_keydown('b'); // new cell below |
|
57 | this.trigger_keydown('b'); // new cell below | |
58 | this.test.assertEquals(this.get_cell(3).cell_type, 'code', 'b; inserts a code cell below code cell'); |
|
58 | this.test.assertEquals(this.get_cell(3).cell_type, 'code', 'b; inserts a code cell below code cell'); | |
59 | this.trigger_keydown('a'); // new cell above |
|
59 | this.trigger_keydown('a'); // new cell above | |
60 | this.test.assertEquals(this.get_cell(3).cell_type, 'code', 'a; inserts a code cell above code cell'); |
|
60 | this.test.assertEquals(this.get_cell(3).cell_type, 'code', 'a; inserts a code cell above code cell'); | |
61 | }); |
|
61 | }); | |
62 |
|
62 | |||
63 | this.thenEvaluate(function() { |
|
63 | this.thenEvaluate(function() { | |
64 |
IPython.notebook.default_cell_type |
|
64 | IPython.notebook.class_config.set('default_cell_type', 'below'); | |
65 | }); |
|
65 | }); | |
66 |
|
66 | |||
67 | this.then(function () { |
|
67 | this.then(function () { | |
68 | this.select_cell(2); |
|
68 | this.select_cell(2); | |
69 | this.trigger_keydown('r'); // switch it to markdown for the next test |
|
69 | this.trigger_keydown('r'); // switch it to markdown for the next test | |
70 | this.test.assertEquals(this.get_cell(2).cell_type, 'raw', 'test cell is raw'); |
|
70 | this.test.assertEquals(this.get_cell(2).cell_type, 'raw', 'test cell is raw'); | |
71 | this.trigger_keydown('a'); // new cell above |
|
71 | this.trigger_keydown('a'); // new cell above | |
72 | this.test.assertEquals(this.get_cell(2).cell_type, 'raw', 'a; inserts a raw cell above raw cell'); |
|
72 | this.test.assertEquals(this.get_cell(2).cell_type, 'raw', 'a; inserts a raw cell above raw cell'); | |
73 | this.trigger_keydown('y'); // switch it to code for the next test |
|
73 | this.trigger_keydown('y'); // switch it to code for the next test | |
74 | this.trigger_keydown('b'); // new cell below |
|
74 | this.trigger_keydown('b'); // new cell below | |
75 | this.test.assertEquals(this.get_cell(3).cell_type, 'raw', 'b; inserts a raw cell below raw cell'); |
|
75 | this.test.assertEquals(this.get_cell(3).cell_type, 'raw', 'b; inserts a raw cell below raw cell'); | |
76 | }); |
|
76 | }); | |
77 | }); |
|
77 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now