forked from daniellmb/JavaScript-Scope-Context-Coloring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.js
More file actions
66 lines (54 loc) · 1.71 KB
/
Copy pathdemo.js
File metadata and controls
66 lines (54 loc) · 1.71 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
/*global jQuery, JSLINT, CodeMirror, location*/
(function ($, lint, cm) {
'use strict';
var editor,
editorChanged = false,
scopeMode = {
'name': 'scope',
'hasChanged': function () {
return editorChanged;
},
'getLevels': function () {
//check that JSLINT has been called
if (lint.hasOwnProperty('errors')) {
editorChanged = false;
return lint.color(lint.data());
}
}
};
//set up code editor
editor = cm($('#editor')[0], { 'theme': 'ambiance', 'mode': scopeMode });
//lint the code in the editor
function lintCode() {
lint(editor.getValue());
editorChanged = true;
}
//lint code when it changes
editor.on('change', lintCode);
//Shouldn't need the code below this line, it's just for this demo
function loadSample(id) {
var sample = $('#' + id.substr(id.lastIndexOf('#') + 1));
editor.setValue(sample.text());
}
//load the default code sample
$(function() {
loadSample(location.hash || 'minimonad');
});
//support changing modes
function selectMode(mode) {
if (mode === 'pro') {
lintCode();
editor.setOption('mode', scopeMode);
} else {
editor.setOption('mode', 'javascript');
}
}
//bind mode change event handler
$('#toggleMode').on('click', 'button', function() {
selectMode($(this).data('mode'));
});
//bind code sample change handler
$('#samples').on('click', 'a', function() {
loadSample(this.href);
});
}(jQuery, JSLINT, CodeMirror));