|
1 | 1 | (function ($) { |
2 | | - $.fn.codemirrorInit = function () { |
| 2 | + $.fn.codemirrorInit = function (options) { |
3 | 3 |
|
4 | 4 | $(this).init.prototype = $.extend({}, $(this).init.prototype, { |
5 | 5 |
|
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 | + } |
34 | 33 | } |
35 | 34 | }); |
36 | 35 |
|
| 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 | + |
37 | 65 | return this.each(function () { |
38 | 66 |
|
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 | + |
45 | 73 | }); |
46 | 74 | }; |
47 | 75 |
|
|
0 commit comments