-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathsettings.js
More file actions
243 lines (213 loc) · 8.46 KB
/
settings.js
File metadata and controls
243 lines (213 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
define([
'require'
, 'jquery'
, 'nbextensions/visualpython/src/common/vpCommon'
, 'nbextensions/visualpython/src/common/constant'
, 'nbextensions/visualpython/src/common/StringBuilder'
, 'nbextensions/visualpython/src/common/vpFuncJS'
, 'nbextensions/visualpython/src/common/vpSetting'
// TEST: CodeMirror
, 'codemirror/lib/codemirror'
, 'codemirror/mode/python/python'
, 'notebook/js/codemirror-ipython'
, 'codemirror/addon/display/placeholder'
], function (requirejs, $, vpCommon, vpConst, sb, vpFuncJS, vpSetting, CodeMirror, cmpython, cmip) {
// 옵션 속성
const funcOptProp = {
stepCount : 1
, funcName : "Setting"
, funcID : "com_setting"
}
/**
* html load 콜백. 고유 id 생성하여 부과하며 js 객체 클래스 생성하여 컨테이너로 전달
* @param {function} callback 호출자(컨테이너) 의 콜백함수
*/
var optionLoadCallback = function(callback, meta) {
// document.getElementsByTagName("head")[0].appendChild(link);
// 컨테이너에서 전달된 callback 함수가 존재하면 실행.
if (typeof(callback) === 'function') {
var uuid = vpCommon.getUUID();
// 최대 10회 중복되지 않도록 체크
for (var idx = 0; idx < 10; idx++) {
// 이미 사용중인 uuid 인 경우 다시 생성
if ($(vpConst.VP_CONTAINER_ID).find("." + uuid).length > 0) {
uuid = vpCommon.getUUID();
}
}
$(vpCommon.wrapSelector(vpCommon.formatString("#{0}", vpConst.OPTION_GREEN_ROOM))).find(vpCommon.formatString(".{0}", vpConst.API_OPTION_PAGE)).addClass(uuid);
// 옵션 객체 생성
var settingPackage = new SettingPackage(uuid);
settingPackage.metadata = meta;
// 옵션 속성 할당.
settingPackage.setOptionProp(funcOptProp);
// html 설정.
settingPackage.initHtml();
callback(settingPackage); // 공통 객체를 callback 인자로 전달
}
}
/**
* html 로드.
* @param {function} callback 호출자(컨테이너) 의 콜백함수
*/
var initOption = function(callback, meta) {
vpCommon.loadHtml(vpCommon.wrapSelector(vpCommon.formatString("#{0}", vpConst.OPTION_GREEN_ROOM)), "file_io/settings.html", optionLoadCallback, callback, meta);
}
/**
* 본 옵션 처리 위한 클래스
* @param {String} uuid 고유 id
*/
var SettingPackage = function(uuid) {
this.uuid = uuid; // Load html 영역의 uuid.
this.divId = 'vp_settingBox';
}
/**
* vpFuncJS 에서 상속
*/
SettingPackage.prototype = Object.create(vpFuncJS.VpFuncJS.prototype);
/**
* 유효성 검사
* @returns 유효성 검사 결과. 적합시 true
*/
SettingPackage.prototype.optionValidation = function() {
return true;
// 부모 클래스 유효성 검사 호출.
// vpFuncJS.VpFuncJS.prototype.optionValidation.apply(this);
}
/**
* html 내부 binding 처리
*/
SettingPackage.prototype.initHtml = function() {
this.bindSettings();
this.bindFunctions();
// TEST: Bind CodeMirror to Textarea
//this.bindCodeMirror();
// TEST: Bind CodeMirror stylesheet
//this.loadCss(Jupyter.notebook.base_url + "/codemirror/lib/codemirror.css");
//this.loadCss(Jupyter.notebook.base_url + "/codemirror/lib/codemirror.css");
}
/**
* Setting 목록 추가
*/
SettingPackage.prototype.bindSettings = function() {
this.loadSettings(true);
};
/**
* TEST: Bind CodeMirror to Textarea
*/
SettingPackage.prototype.bindCodeMirror = function() {
// var editor = CodeMirror.fromTextArea(document.getElementById('vp_testCodeMirror'), {
window.vp_testCodeMirror = CodeMirror.fromTextArea($(this.wrapSelector('#vp_testCodeMirror'))[0], {
mode: 'markdown', // text-cell(markdown cell) set to 'htmlmixed'
lineNumbers: true,
lineWrapping: true, // text-cell(markdown cell) set to true
theme: "default",
extraKeys: {"Enter": "newlineAndIndentContinueMarkdownList"}
});
/**
* CodeMirror reference : https://codemirror.net/index.html
*
* CodeMirror object Usage :
* 1. Get value
* vp_testCodeMirror.getValue()
* 2. Set value
* vp_testCodeMirror.setValue('string')
* 3. Apply to original textarea
* vp_testCodeMirror.save()
* 4. Get Textarea object
* vp_testCodeMirror.getTextArea()
*
* Prefix box Textarea
* var vp_prefix = CodeMirror.fromTextArea($('#vp_prefixBox textarea')[0], { mode:'htmlmixed', lineNumbers: true, theme: 'default'});
* Postfix box Textarea
* var vp_postfix = CodeMirror.fromTextArea($('#vp_postfixBox textarea')[0], { mode: 'htmlmixed', lineNumbers: true, theme: 'default'});
*/
}
/**
* Load Settings
* @param {boolean} showValue true : value / false : default value
*/
SettingPackage.prototype.loadSettings = function(showValue) {
var that = this;
$(this.wrapSelector('#' + that.divId)).html('');
// load setting list to div
var settingList = vpSetting.loadSettingsDataUsable();
settingList.forEach(obj => {
var value = (showValue == true? obj.value : obj.default);
var divSetItem = $('<div class="vp-setting-item form-group list-group-item"></div>');
var label = $(`<label for="${obj.name}">${obj.description}</label>`);
if (obj.type == 'options') {
// select options
var select = $(`<select id=${obj.name} class="form-control"></select>`);
obj.options.forEach(opt => {
// FIXME: if korean, can use opt.text
var option = $(`<option value="${opt.value}">${opt.value}</option>`);
select.append(option);
});
$(select).val(value);
divSetItem.append(label);
divSetItem.append(select);
} else if (obj.type == 'checkbox') {
// input checkbox
var checkbox = $(`<input id="${obj.name}" type="${obj.type}" value="${value}" ${value==true?"checked":""}/>`);
divSetItem.append(checkbox);
divSetItem.append(label);
} else if (obj.type == 'text') {
// input text
var input = $(`<input id="${obj.name}" class="form-control" type="${obj.type}" value="${value}" />`);
divSetItem.append(label);
divSetItem.append(input);
}
$(that.wrapSelector('#' + that.divId)).append(divSetItem);
});
}
/**
* save settings
*
* reference
* Jupyter.notebook.config.update({'vp': {'run_code_without_asking':true}});
* Jupyter.notebook.config.data.vp
*/
SettingPackage.prototype.saveSettings = function() {
var updateObj = {};
// make update object for setting list
Object.keys(vpSetting.default_settings).forEach(key => {
var tag = $(this.wrapSelector('#' + key));
var value = tag.val().toString();
if (tag.attr('type') == 'checkbox') {
value = tag.prop('checked');
}
updateObj[key] = value;
});
// vpSetting.saveSettingsJson(updateObj);
vpSetting.saveSettingsData(updateObj);
};
/**
* bind button function
*/
SettingPackage.prototype.bindFunctions = function() {
var that = this;
// load setting
$(this.wrapSelector('#vp_loadSetting')).click(function() {
that.loadSettings(true);
});
// save setting
$(this.wrapSelector('#vp_saveSetting')).click(function() {
that.saveSettings();
});
// set to default
$(this.wrapSelector('#vp_setDefault')).click(function() {
that.loadSettings(false);
});
}
/**
* 코드 생성
* @param {boolean} exec 실행여부
*/
SettingPackage.prototype.generateCode = function(addCell, exec) {
// if (!this.optionValidation()) return;
return "BREAK_RUN"
}
return {
initOption: initOption
};
});