ide.js
13.4 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
;(function(window){
function gxbIde(){
this.init.apply(this, arguments);
}
var treeLinkEditor = [], // 缓存整个目录树结构和代码编辑器的关系
treeData = {}, // 缓存目录树返回结果
mainPath, // 运行编译时候需要知道当前是哪个文件
fileList, // 有值的文件数据结构
treeObj; // 获取ztree对象
var isReplayPage = false; // initJavaPage 判断输出
var ZTREE = {
/**
* ztreeSetting 配置文件
* @Author syantao
* Created by Keystion on 2017-01-19
*/
config: {
view: { showLine: false }
,data: {
simpleData: { enable: true }
}
,callback: {
/**
* ztree 每一项点击事件
* @Author syantao
* Created by Keystion on 2017-01-19
*/
beforeClick: function(treeId, treeNode){
console.log('点击了 ' + treeNode.name);
console.log(treeNode);
// 判断是否点击了文件夹
if (treeNode.isParent) {
return false;
}
treeObj = $.fn.zTree.getZTreeObj("folder");
treeObj.expandNode(treeNode);
// 处理显示目录树对应的编辑器目录
ZTREE.showEdite({'treeNode': treeNode});
}
}
},
/**
* 通过目录树的点击来显示对应的编辑器
* @Author syantao
* Created by Keystion on 2017-01-19
* @param {[type]} treeNode 点击了哪一个目录
* TODO treeNode放到一个变量或者隐藏域里面
*/
showEdite: function (obj){
console.info('ZTREE.showEdite');
console.log(obj);
var _this = this,
_file,
_CodeMirrorRecordId,
_CodeMirrorReplayId,
_num = 0;
// 判断是否是初始化选时候调用 showEdite
if(obj){
for (var i = 0; i < treeLinkEditor.length; i++) {
if(treeLinkEditor[i].name == _this.manageName(obj.treeNode.name)){
_num = i;
}
}
if(typeof obj.record != 'undefined' && obj.record){
$('#recordertab a:eq(0)').tab('show');
}else if(typeof obj.replay != 'undefined'&& obj.replay){
$('#recordertab a:eq(1)').tab('show');
}
}
_file = treeLinkEditor[_num].file;
_CodeMirrorRecordId = treeLinkEditor[_num].CodeMirrorRecordId;
_CodeMirrorReplayId = treeLinkEditor[_num].CodeMirrorReplayId;
mainPath = treeLinkEditor[_num].filePath; // 更新mainPath值
// 显示代码编辑器
$('#' + _CodeMirrorRecordId).show().parent().show().siblings().hide().find('.CodeMirror').hide();
$('#' + _CodeMirrorReplayId).show().parent().show().siblings().hide().find('.CodeMirror').hide();
console.log('当前选中文件(mainPath):'+ mainPath);
console.log('当前显示代码编辑器:'+ '#' + _CodeMirrorReplayId);
// 更新目录树选中状态
_this.selectTreeItem(_file);
},
/**
* 返回目录结构路径
* @Author syantao
* Created by Keystion on 2017-01-19
* @param {[type]} treeNode 点击了哪一个目录
* TODO treeNode放到一个变量或者隐藏域里面
*/
// 根据提供的 name 获取 pId , 再根据 pId 获取 夫级 name
getTreePath: function (name){
console.info('ZTREE.getTreePath');
var _pid, _folder, _filename;
for (var i = 0; i < treeData.length; i++) {
if(treeData[i].name == name){
_pid = treeData[i].pId;
_filename = treeData[i].name;
}
}
for (var i = 0; i < treeData.length; i++) {
if(treeData[i].id == _pid){
_folder = treeData[i].name;
}
}
return _folder + '/' + _filename;
}
/**
* 处理name
* @Author syantao
* Created by Keystion on 2017-01-23
* @param {[type]} name Class1.java
* @return {[type]} Class1
*/
,manageName: function (name){
console.info('ZTREE.manageName');
if(!name){
return false;
}
return name.split('.')[0];
}
/**
* 根据id获取当前编辑器对应的目录树文件
* @Author syantao
* Created by Keystion on 2017-01-19
* @param {[type]} id 代码编辑器的id
*/
,getCurrentEditor: function (id){
// console.info('ZTREE.getCurrentEditor');
var _id = id || treeLinkEditor[0].CodeMirrorRecordId,
_name;
for (var i = 0; i < treeLinkEditor.length; i++) {
if(treeLinkEditor[i].CodeMirrorRecordId == _id){
// console.log(treeLinkEditor[i]);
_name = treeLinkEditor[i].name;
}
}
return _name;
}
/**
* 返回编辑器的数据,
* @Author syantao
* Created by Keystion on 2017-01-22
* @param {[type]} name 非必选,如果有折返回单个文件的数据
* @return {[type]} Array/false false 每一个编辑器均无值
*/
,getTreeData: function(name){
console.info('ZTREE.getTreeData');
var _arr = [];
if(!isReplayPage){
// 判断是否是需要返回单文件结果
if(name){
for (var i = 0; i < treeLinkEditor.length; i++) {
if(treeLinkEditor[i].name == name && $.trim(treeLinkEditor[i].CodeMirrorRecord.getValue()) != ''){
_arr.push({
"path": treeLinkEditor[i].filePath,
"content": $.trim(treeLinkEditor[i].CodeMirrorRecord.getValue())
})
}
}
}else{
for (var i = 0; i < treeLinkEditor.length; i++) {
if($.trim(treeLinkEditor[i].CodeMirrorRecord.getValue()) != ''){
_arr.push({
"path": treeLinkEditor[i].filePath,
"content": $.trim(treeLinkEditor[i].CodeMirrorRecord.getValue())
})
}
}
}
}else{
// 判断是否是需要返回单文件结果
if(name){
for (var i = 0; i < treeLinkEditor.length; i++) {
if(treeLinkEditor[i].name == name && $.trim(treeLinkEditor[i].CodeMirrorReplay.getValue()) != ''){
_arr.push({
"path": treeLinkEditor[i].filePath,
"content": $.trim(treeLinkEditor[i].CodeMirrorReplay.getValue())
})
}
}
}else{
for (var i = 0; i < treeLinkEditor.length; i++) {
if($.trim(treeLinkEditor[i].CodeMirrorReplay.getValue()) != ''){
_arr.push({
"path": treeLinkEditor[i].filePath,
"content": $.trim(treeLinkEditor[i].CodeMirrorReplay.getValue())
})
}
}
}
}
//
if(_arr < 1){
return false;
}
return _arr;
}
/**
* 通过文件名选中对应的目录
* @Author syantao
* Created by Keystion on 2017-01-23
* @param {[type]} name 例:Class1.java
*/
,selectTreeItem: function (name){
console.info('ZTREE.selectTreeItem');
var _thisName = name || treeData[1].name;
// 处理菜单选中状态
var node = treeObj.getNodeByParam("name", _thisName, null);
treeObj.selectNode(node, false, true);
}
}
gxbIde.prototype = {
config: {
tree: true,
languageid: null
},
init: function(config){
this.config = $.extend(this.options, config);
console.log(this.options)
this.getTreeList();
},
template: function(){
console.log('--------')
},
controler: function(){
},
getTreeList: function(){
var setting = { };
$.ajax({
type: "GET",
url: "js/tree.json",
dataType: "json",
contentType: "application/json",
success: function(data) {
console.log(data);
treeData = data.tree;
$.fn.zTree.init($("#folder"), ZTREE.config, treeData);
initJavaPage(treeData)
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log('Ajax tree.json error');
console.log(XMLHttpRequest);
}
});
},
}
function initJavaPage(treeData){
console.info('initJavaPage');
var _datalist = treeData || [];
var recordTemplate = '';
var replayTemplate = '';
var obj = []
if(!_datalist.length){
return false;
}
// setHtmlButton(_languageid, 'JAVA');
// 生成 textarea
for (var i = 0; i < _datalist.length; i++) {
if(!_datalist[i].isParent){
var _file = _datalist[i].name;
var _name = ZTREE.manageName(_file);
var defaultValue;
defaultValue = 'package '+ ZTREE.getTreePath(_file).split('/')[0] +';';
treeLinkEditor.push({
"name": _name,
"file": _file,
"filePath": ZTREE.getTreePath(_file),
"record": _name + 'Record',
"replay": _name + 'Replay'
});
// 判断是否是播放页面,如果是则不显示录制区相关dom
if(!isReplayPage){
recordTemplate += '<div id="'+ _name +'RecordWrap"><textarea id="'+ _name + 'Record" title="'+ _name +'" style="display: none;">'+defaultValue+'</textarea></div>'
}
replayTemplate += '<div id="'+ _name +'ReplayWrap"><textarea id="'+ _name + 'Replay" title="'+ _name +'" style="display: none;"></textarea></div>'
}
}
// 判断是否是播放页面,如果是则不显示录制区相关dom
if(!isReplayPage){
$('#recordzone').append(recordTemplate);
}
$('#replayzone').append(replayTemplate);
// 生成 CodeMirror
for (var i = 0; i < treeLinkEditor.length; i++) {
// 判断是否是播放页面,如果是则不显示录制区相关dom
if(!isReplayPage){
treeLinkEditor[i].CodeMirrorRecord = CodeMirror.fromTextArea($('#' + treeLinkEditor[i].record)[0], {
value: '',
mode: "text/x-java",
lineNumbers: true,
smartIndent: false,
onChange: function(em, changeobj) {
},
onFocus: function(em) {
// console.log('onFocus');
// console.log(em);
},
onCursorActivity: function(em) {
// console.log('onCursorActivity');
// console.log(em);
}
});
treeLinkEditor[i].CodeMirrorRecordId = treeLinkEditor[i].record + 'CodeMirror';
$('#' + treeLinkEditor[i].record).next().attr('id', treeLinkEditor[i].CodeMirrorRecordId).hide();
}
treeLinkEditor[i].CodeMirrorReplay = CodeMirror.fromTextArea($('#' + treeLinkEditor[i].replay)[0], {
value: '',
mode: "text/x-java",
lineNumbers: true,
smartIndent: false,
onChange: function(em, changeobj) {
},
onFocus: function(em) {
},
onCursorActivity: function(em) {
}
});
treeLinkEditor[i].CodeMirrorReplayId = treeLinkEditor[i].replay + 'CodeMirror';
$('#' + treeLinkEditor[i].replay).next().attr('id', treeLinkEditor[i].CodeMirrorReplayId).hide();
}
// treeLinkEditor[0].CodeMirrorEditor.hide()
// 默认显示第一个文件夹的第一个文件
ZTREE.showEdite();
}
window.gxbIde = gxbIde;
})(window)