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