Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define([
5
'jquery',
6
'base/js/utils',
7
],
8
function($, utils) {
9
"use strict";
10
var ConfigSection = function(section_name, options) {
11
this.section_name = section_name;
12
this.base_url = options.base_url;
13
this.data = {};
14
15
var that = this;
16
17
/* .loaded is a promise, fulfilled the first time the config is loaded
18
* from the server. Code can do:
19
* conf.loaded.then(function() { ... using conf.data ... });
20
*/
21
this._one_load_finished = false;
22
this.loaded = new Promise(function(resolve, reject) {
23
that._finish_firstload = resolve;
24
});
25
};
26
27
ConfigSection.prototype.api_url = function() {
28
return utils.url_join_encode(this.base_url, 'api/config', this.section_name);
29
};
30
31
ConfigSection.prototype._load_done = function() {
32
if (!this._one_load_finished) {
33
this._one_load_finished = true;
34
this._finish_firstload();
35
}
36
};
37
38
ConfigSection.prototype.load = function() {
39
var that = this;
40
return utils.promising_ajax(this.api_url(), {
41
cache: false,
42
type: "GET",
43
dataType: "json",
44
}).then(function(data) {
45
that.data = data;
46
that._load_done();
47
return data;
48
});
49
};
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
*/
56
ConfigSection.prototype.update = function(newdata) {
57
$.extend(true, this.data, newdata); // true -> recursive update
58
59
var that = this;
60
return utils.promising_ajax(this.api_url(), {
61
processData: false,
62
type : "PATCH",
63
data: JSON.stringify(newdata),
64
dataType : "json",
65
contentType: 'application/json',
66
}).then(function(data) {
67
that.data = data;
68
that._load_done();
69
return data;
70
});
71
};
72
73
74
var ConfigWithDefaults = function(section, defaults, classname) {
75
this.section = section;
76
this.defaults = defaults;
77
this.classname = classname;
78
};
79
80
ConfigWithDefaults.prototype._class_data = function() {
81
if (this.classname) {
82
return this.section.data[this.classname] || {};
83
} else {
84
return this.section.data
85
}
86
};
87
88
/**
89
* Wait for config to have loaded, then get a value or the default.
90
* Returns a promise.
91
*/
92
ConfigWithDefaults.prototype.get = function(key) {
93
var that = this;
94
return this.section.loaded.then(function() {
95
return this._class_data()[key] || this.defaults[key]
96
});
97
};
98
99
/**
100
* Return a config value. If config is not yet loaded, return the default
101
* instead of waiting for it to load.
102
*/
103
ConfigWithDefaults.prototype.get_sync = function(key) {
104
return this._class_data()[key] || this.defaults[key];
105
};
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
125
return {ConfigSection: ConfigSection,
126
ConfigWithDefaults: ConfigWithDefaults,
127
};
128
129
});
130
131