Skip to content

Commit b59c394

Browse files
author
Philipp
committed
Basic implementation ready
1 parent d69c153 commit b59c394

File tree

2 files changed

+66
-37
lines changed

2 files changed

+66
-37
lines changed

index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
<div class="margin10 code-snippet" data-test style="width:250px;height:300px;border:1px solid black;"></div>
1010

11-
<div class="margin10 code-snippet" style="width:350px;height:150px;border:1px solid black;"></div>
11+
<div class="margin10 code-snippet" data-codemirror-config='{"showCursorWhenSelecting": true, "readOnly": true, "value": "Hello world"}'
12+
style="width:350px;height:150px;border:1px solid black;"></div>
1213

1314
<div class="margin10 code-snippet" data-no-lines style="width:500px;height:100px;border:1px solid black;"></div>
1415

@@ -25,7 +26,7 @@
2526
<script>
2627
$(function () {
2728

28-
$('.code-snippet').codemirrorInit();
29+
$('.code-snippet').codemirrorInit({readOnly: "nocursor"});
2930

3031
$('.code-snippet[data-test]').codemirror().setValue('Any string');
3132

philsweb.jquery.codemirror.js

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,75 @@
11
(function ($) {
2-
$.fn.codemirrorInit = function () {
2+
$.fn.codemirrorInit = function (options) {
33

44
$(this).init.prototype = $.extend({}, $(this).init.prototype, {
55

6-
codemirror: function(){
7-
var $this = this;
8-
return {
9-
setValue: function (value) {
10-
for (var i = 0; i < $this.length; i++) {
11-
$.data($this[i], 'codemirror').setValue(value);
12-
}
13-
},
14-
getValue: function () {
15-
if (this.length > 1) {
16-
throw new Error("Only one DOM element can be selected");
17-
}
18-
return $.data($this[0], 'codemirror').getValue();
19-
},
20-
setOption: function (key, value) {
21-
for (var i = 0; i < $this.length; i++) {
22-
$.data($this[i], 'codemirror').setOption(key, value);
23-
}
24-
},
25-
setOptions: function (optionsObject) {
26-
for (var i = 0; i < this.length; i++) {
27-
var element = $this[i];
28-
$.each(optionsObject, function (key, value) {
29-
$.data(element, 'codemirror').setOption(key, value);
30-
});
31-
}
32-
}
33-
}
6+
codemirror: function () {
7+
var $this = this;
8+
return {
9+
setValue: function (value) {
10+
for (var i = 0; i < $this.length; i++) {
11+
$.data($this[i], 'codemirror').setValue(value);
12+
}
13+
},
14+
getValue: function () {
15+
if (this.length > 1) {
16+
throw new Error("Only one DOM element can be selected");
17+
}
18+
return $.data($this[0], 'codemirror').getValue();
19+
},
20+
setOption: function (option, value) {
21+
$.each($this, function (key, element) {
22+
$.data(element, 'codemirror').setOption(option, value);
23+
});
24+
},
25+
setOptions: function (optionsObject) {
26+
$.each($this, function (key, element) {
27+
$.each(optionsObject, function (option, value) {
28+
$.data(element, 'codemirror').setOption(option, value);
29+
});
30+
});
31+
}
32+
}
3433
}
3534
});
3635

36+
var defaults = {
37+
mode: "text/html",
38+
lineNumbers: true,
39+
lineWrapping: true
40+
};
41+
42+
function getLocalConfigs(element) {
43+
var configJson = $(element).attr('data-codemirror-config');
44+
45+
if (configJson === undefined) {
46+
return {};
47+
}
48+
49+
var isJson = true;
50+
try {
51+
var json = $.parseJSON(configJson);
52+
}
53+
catch (error) {
54+
isJson = false;
55+
}
56+
57+
if (isJson) {
58+
return $.parseJSON(configJson);
59+
}
60+
61+
return {};
62+
63+
}
64+
3765
return this.each(function () {
3866

39-
$.data(this, 'codemirror', CodeMirror(this, {
40-
//value: '<div> Hello world! </div>',
41-
mode: "text/html",
42-
lineNumbers: true,
43-
lineWrapping: true
44-
}));
67+
var basicConfigs = $.extend({}, defaults, options);
68+
var localConfigs = getLocalConfigs(this);
69+
var configs = $.extend({}, basicConfigs, localConfigs);
70+
71+
$.data(this, 'codemirror', CodeMirror(this, configs));
72+
4573
});
4674
};
4775

0 commit comments

Comments
 (0)