-
Notifications
You must be signed in to change notification settings - Fork 20
/
codeMgr.js
128 lines (109 loc) · 3.7 KB
/
codeMgr.js
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
/*******************************************************************************
*
* Copyright 2011 Zack Grossbart
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/**
* The code manager handles editing the animation source code in
* the page. It isn't required to make the animation run.
*/
codeMgr = {
/**
* Run the script contents of the editor
*/
runScript: function() {
try {
// Update script to edited version
var code = codeMgr.editor.getValue();
var canvas = codeMgr.scope.view.canvas;
codeMgr.scope.clear();
codeMgr.scope.setup(canvas);
codeMgr.scope.evaluate(code);
codeMgr.hideError();
$('#editContainer').hide();
$('#container').show();
} catch (e) {
codeMgr.showError(e);
}
},
/**
* Hide the error message if it is visible
*/
hideError: function(/*string*/ err) {
if ($('#err').is(":visible")) {
$('#err').hide();
$('#editContainer').css('height', ($(window).height() - 75) + 'px');
$('div.fullscreen').css('top', '0px');
codeMgr.editor.focus();
codeMgr.editor.refresh();
}
},
/**
* Show the specified error message above the script editor
*/
showError: function(/*string*/ err) {
console.log('err: ' + err);
$('#err').show().text('Error running script: ' + err);
$('#editContainer').css('height', ($(window).height() - 175) + 'px');
$('div.fullscreen').css('top', '100px');
codeMgr.editor.focus();
codeMgr.editor.refresh();
},
/**
* Show the script editor
*/
editScript: function() {
$('#container').hide();
$('#editContainer').show();
var editorDiv = $('.CodeMirror-scroll');
editorDiv.addClass('fullscreen');
editorDiv.height('100%');
editorDiv.width('100%');
codeMgr.editor.focus();
codeMgr.editor.refresh();
}
};
jQuery(document).ready(function() {
var file = (function func1() {
var result;
$.ajax({
type: "GET",
url: 'dandelion.pjs',
async: false,
processData: false,
dataType: 'html',
success: function(data){
result = data;
}
});
return result;
})();
$('#code').val(file);
codeMgr.editor = CodeMirror.fromTextArea(document.getElementById("code"), {
matchBrackets: true,
mode: 'javascript',
lineNumbers: true,
matchBrackets: true,
indentUnit: 4,
tabMode: 'shift'
});
$('#editContainer').css('height', ($(window).height() - 75) + 'px');
$('#run, #reload').click(function(evt) {
codeMgr.runScript();
});
$('#edit').click(function(evt) {
codeMgr.editScript();
});
});