|
5 | 5 | */ |
6 | 6 | angular.module('ui.codemirror', []) |
7 | 7 | .constant('uiCodemirrorConfig', {}) |
8 | | - .directive('uiCodemirror', ['uiCodemirrorConfig', function (uiCodemirrorConfig) { |
| 8 | + .directive('uiCodemirror', uiCodemirrorDirective); |
9 | 9 |
|
10 | | - return { |
11 | | - restrict: 'EA', |
12 | | - require: '?ngModel', |
13 | | - priority: 1, |
14 | | - compile: function compile() { |
| 10 | +function uiCodemirrorDirective(uiCodemirrorConfig) { |
15 | 11 |
|
16 | | - // Require CodeMirror |
17 | | - if (angular.isUndefined(window.CodeMirror)) { |
18 | | - throw new Error('ui-codemirror need CodeMirror to work... (o rly?)'); |
19 | | - } |
| 12 | + return { |
| 13 | + restrict: 'EA', |
| 14 | + require: '?ngModel', |
| 15 | + compile: function compile() { |
20 | 16 |
|
21 | | - return function postLink(scope, iElement, iAttrs, ngModel) { |
| 17 | + // Require CodeMirror |
| 18 | + if (angular.isUndefined(window.CodeMirror)) { |
| 19 | + throw new Error('ui-codemirror need CodeMirror to work... (o rly?)'); |
| 20 | + } |
22 | 21 |
|
| 22 | + return postLink; |
| 23 | + } |
| 24 | + }; |
23 | 25 |
|
24 | | - var options, opts, codeMirror, initialTextValue; |
| 26 | + function postLink(scope, iElement, iAttrs, ngModel) { |
25 | 27 |
|
26 | | - initialTextValue = iElement.text(); |
| 28 | + var codemirrorOptions = angular.extend( |
| 29 | + { value: iElement.text() }, |
| 30 | + uiCodemirrorConfig.codemirror || {}, |
| 31 | + scope.$eval(iAttrs.uiCodemirror), |
| 32 | + scope.$eval(iAttrs.uiCodemirrorOpts) |
| 33 | + ); |
27 | 34 |
|
28 | | - options = uiCodemirrorConfig.codemirror || {}; |
29 | | - opts = angular.extend({ value: initialTextValue }, options, scope.$eval(iAttrs.uiCodemirror), scope.$eval(iAttrs.uiCodemirrorOpts)); |
| 35 | + var codemirror = newCodemirrorEditor(iElement, codemirrorOptions); |
30 | 36 |
|
31 | | - if (iElement[0].tagName === 'TEXTAREA') { |
32 | | - // Might bug but still ... |
33 | | - codeMirror = window.CodeMirror.fromTextArea(iElement[0], opts); |
34 | | - } else { |
35 | | - iElement.html(''); |
36 | | - codeMirror = new window.CodeMirror(function(cm_el) { |
37 | | - iElement.append(cm_el); |
38 | | - }, opts); |
39 | | - } |
| 37 | + configOptionsWatcher( |
| 38 | + codemirror, |
| 39 | + iAttrs.uiCodemirror || iAttrs.uiCodemirrorOpts, |
| 40 | + scope |
| 41 | + ); |
40 | 42 |
|
41 | | - if (iAttrs.uiCodemirror || iAttrs.uiCodemirrorOpts) { |
42 | | - var codemirrorDefaultsKeys = Object.keys(window.CodeMirror.defaults); |
43 | | - scope.$watch(iAttrs.uiCodemirror || iAttrs.uiCodemirrorOpts, function updateOptions(newValues, oldValue) { |
44 | | - if (! angular.isObject(newValues)){ |
45 | | - return; |
46 | | - } |
47 | | - codemirrorDefaultsKeys.forEach(function (key) { |
48 | | - if (newValues.hasOwnProperty(key)) { |
49 | | - |
50 | | - if (oldValue && newValues[key] === oldValue[key]){ |
51 | | - return; |
52 | | - } |
53 | | - |
54 | | - codeMirror.setOption(key, newValues[key]); |
55 | | - } |
56 | | - }); |
57 | | - }, true); |
58 | | - } |
| 43 | + configNgModelLink(codemirror, ngModel, scope); |
59 | 44 |
|
60 | | - if (ngModel) { |
61 | | - // CodeMirror expects a string, so make sure it gets one. |
62 | | - // This does not change the model. |
63 | | - ngModel.$formatters.push(function (value) { |
64 | | - if (angular.isUndefined(value) || value === null) { |
65 | | - return ''; |
66 | | - } else if (angular.isObject(value) || angular.isArray(value)) { |
67 | | - throw new Error('ui-codemirror cannot use an object or an array as a model'); |
68 | | - } |
69 | | - return value; |
70 | | - }); |
71 | | - |
72 | | - |
73 | | - // Override the ngModelController $render method, which is what gets called when the model is updated. |
74 | | - // This takes care of the synchronizing the codeMirror element with the underlying model, in the case that it is changed by something else. |
75 | | - ngModel.$render = function () { |
76 | | - //Code mirror expects a string so make sure it gets one |
77 | | - //Although the formatter have already done this, it can be possible that another formatter returns undefined (for example the required directive) |
78 | | - var safeViewValue = ngModel.$viewValue || ''; |
79 | | - codeMirror.setValue(safeViewValue); |
80 | | - }; |
81 | | - |
82 | | - |
83 | | - // Keep the ngModel in sync with changes from CodeMirror |
84 | | - codeMirror.on('change', function (instance) { |
85 | | - var newValue = instance.getValue(); |
86 | | - if (newValue !== ngModel.$viewValue) { |
87 | | - // Changes to the model from a callback need to be wrapped in $apply or angular will not notice them |
88 | | - scope.$apply(function() { |
89 | | - ngModel.$setViewValue(newValue); |
90 | | - }); |
91 | | - } |
92 | | - }); |
| 45 | + configUiRefreshAttribute(codemirror, iAttrs.uiRefresh, scope); |
| 46 | + |
| 47 | + // Allow access to the CodeMirror instance through a broadcasted event |
| 48 | + // eg: $broadcast('CodeMirror', function(cm){...}); |
| 49 | + scope.$on('CodeMirror', function(event, callback) { |
| 50 | + if (angular.isFunction(callback)) { |
| 51 | + callback(codemirror); |
| 52 | + } else { |
| 53 | + throw new Error('the CodeMirror event requires a callback function'); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + // onLoad callback |
| 58 | + if (angular.isFunction(codemirrorOptions.onLoad)) { |
| 59 | + codemirrorOptions.onLoad(codemirror); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + function newCodemirrorEditor(iElement, codemirrorOptions) { |
| 64 | + var codemirrot; |
| 65 | + |
| 66 | + if (iElement[0].tagName === 'TEXTAREA') { |
| 67 | + // Might bug but still ... |
| 68 | + codemirrot = window.CodeMirror.fromTextArea(iElement[0], codemirrorOptions); |
| 69 | + } else { |
| 70 | + iElement.html(''); |
| 71 | + codemirrot = new window.CodeMirror(function(cm_el) { |
| 72 | + iElement.append(cm_el); |
| 73 | + }, codemirrorOptions); |
| 74 | + } |
| 75 | + |
| 76 | + return codemirrot; |
| 77 | + } |
| 78 | + |
| 79 | + function configOptionsWatcher(codemirrot, uiCodemirrorAttr, scope) { |
| 80 | + if (!uiCodemirrorAttr) { return; } |
| 81 | + |
| 82 | + var codemirrorDefaultsKeys = Object.keys(window.CodeMirror.defaults); |
| 83 | + scope.$watch(uiCodemirrorAttr, updateOptions, true); |
| 84 | + function updateOptions(newValues, oldValue) { |
| 85 | + if (!angular.isObject(newValues)) { return; } |
| 86 | + codemirrorDefaultsKeys.forEach(function(key) { |
| 87 | + if (newValues.hasOwnProperty(key)) { |
| 88 | + |
| 89 | + if (oldValue && newValues[key] === oldValue[key]) { |
| 90 | + return; |
93 | 91 | } |
94 | 92 |
|
| 93 | + codemirrot.setOption(key, newValues[key]); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + function configNgModelLink(codemirror, ngModel, scope) { |
| 100 | + if (!ngModel) { return; } |
| 101 | + // CodeMirror expects a string, so make sure it gets one. |
| 102 | + // This does not change the model. |
| 103 | + ngModel.$formatters.push(function(value) { |
| 104 | + if (angular.isUndefined(value) || value === null) { |
| 105 | + return ''; |
| 106 | + } else if (angular.isObject(value) || angular.isArray(value)) { |
| 107 | + throw new Error('ui-codemirror cannot use an object or an array as a model'); |
| 108 | + } |
| 109 | + return value; |
| 110 | + }); |
95 | 111 |
|
96 | | - // Watch ui-refresh and refresh the directive |
97 | | - if (iAttrs.uiRefresh) { |
98 | | - scope.$watch(iAttrs.uiRefresh, function (newVal, oldVal) { |
99 | | - // Skip the initial watch firing |
100 | | - if (newVal !== oldVal) { |
101 | | - codeMirror.refresh(); |
102 | | - } |
103 | | - }); |
104 | | - } |
105 | 112 |
|
| 113 | + // Override the ngModelController $render method, which is what gets called when the model is updated. |
| 114 | + // This takes care of the synchronizing the codeMirror element with the underlying model, in the case that it is changed by something else. |
| 115 | + ngModel.$render = function() { |
| 116 | + //Code mirror expects a string so make sure it gets one |
| 117 | + //Although the formatter have already done this, it can be possible that another formatter returns undefined (for example the required directive) |
| 118 | + var safeViewValue = ngModel.$viewValue || ''; |
| 119 | + codemirror.setValue(safeViewValue); |
| 120 | + }; |
106 | 121 |
|
107 | | - // Allow access to the CodeMirror instance through a broadcasted event |
108 | | - // eg: $broadcast('CodeMirror', function(cm){...}); |
109 | | - scope.$on('CodeMirror', function(event, callback) { |
110 | | - if (angular.isFunction(callback)) { |
111 | | - callback(codeMirror); |
112 | | - } else { |
113 | | - throw new Error('the CodeMirror event requires a callback function'); |
114 | | - } |
115 | | - }); |
116 | 122 |
|
| 123 | + // Keep the ngModel in sync with changes from CodeMirror |
| 124 | + codemirror.on('change', function(instance) { |
| 125 | + var newValue = instance.getValue(); |
| 126 | + if (newValue !== ngModel.$viewValue) { |
| 127 | + // Changes to the model from a callback need to be wrapped in $apply or angular will not notice them |
| 128 | + scope.$apply(function() { |
| 129 | + ngModel.$setViewValue(newValue); |
| 130 | + }); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
117 | 134 |
|
118 | | - // onLoad callback |
119 | | - if (angular.isFunction(opts.onLoad)) { |
120 | | - opts.onLoad(codeMirror); |
121 | | - } |
| 135 | + function configUiRefreshAttribute(codeMirror, uiRefreshAttr, scope) { |
| 136 | + if (!uiRefreshAttr) { return; } |
122 | 137 |
|
123 | | - }; |
| 138 | + scope.$watch(uiRefreshAttr, function(newVal, oldVal) { |
| 139 | + // Skip the initial watch firing |
| 140 | + if (newVal !== oldVal) { |
| 141 | + codeMirror.refresh(); |
124 | 142 | } |
125 | | - }; |
126 | | - }]); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments