forked from angular-ui/ui-codemirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui-codemirror.js
112 lines (97 loc) · 3.79 KB
/
ui-codemirror.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
/*global angular, CodeMirror, Error*/
/**
* Binds a CodeMirror widget to a <textarea> element.
*/
angular.module('ui.codemirror', [])
.constant('uiCodemirrorConfig', {})
.directive('uiCodemirror', ['uiCodemirrorConfig', '$timeout', function (uiCodemirrorConfig, $timeout) {
'use strict';
var events = ["cursorActivity", "viewportChange", "gutterClick", "focus", "blur", "scroll", "update"];
return {
restrict: 'A',
require: '?ngModel',
link: function (scope, elm, attrs, ngModel) {
var options, opts, onChange, deferCodeMirror, codeMirror;
if (elm[0].type !== 'textarea') {
throw new Error('uiCodemirror3 can only be applied to a textarea element');
}
options = uiCodemirrorConfig.codemirror || {};
opts = angular.extend({}, options, scope.$eval(attrs.uiCodemirror));
onChange = function (aEvent) {
return function (instance, changeObj) {
var newValue = instance.getValue();
if (ngModel && newValue !== ngModel.$viewValue) {
ngModel.$setViewValue(newValue);
}
if (typeof aEvent === "function") {
aEvent(instance, changeObj);
}
if (!scope.$$phase) {
scope.$apply();
}
};
};
deferCodeMirror = function () {
codeMirror = CodeMirror.fromTextArea(elm[0], opts);
if (angular.isDefined(scope[attrs.uiCodemirror])) {
scope.$watch(attrs.uiCodemirror, function (newValues) {
for (var key in newValues) {
if (newValues.hasOwnProperty(key)) {
codeMirror.setOption(key, newValues[key]);
}
}
}, true);
}
codeMirror.on("change", onChange(opts.onChange));
for (var i = 0, n = events.length, aEvent; i < n; ++i) {
aEvent = opts["on" + events[i].charAt(0).toUpperCase() + events[i].slice(1)];
if (aEvent === void 0) {
continue;
}
if (typeof aEvent !== "function") {
continue;
}
codeMirror.on(events[i], aEvent);
}
if(ngModel){
// CodeMirror expects a string, so make sure it gets one.
// This does not change the model.
ngModel.$formatters.push(function (value) {
if (angular.isUndefined(value) || value === null) {
return '';
}
else if (angular.isObject(value) || angular.isArray(value)) {
throw new Error('ui-codemirror cannot use an object or an array as a model');
}
return value;
});
// Override the ngModelController $render method, which is what gets called when the model is updated.
// This takes care of the synchronizing the codeMirror element with the underlying model, in the case that it is changed by something else.
ngModel.$render = function () {
codeMirror.setValue(ngModel.$viewValue);
};
if (!ngModel.$viewValue){
ngModel.$setViewValue(elm.text());
ngModel.$render();
}
}
// Watch ui-refresh and refresh the directive
if (attrs.uiRefresh) {
scope.$watch(attrs.uiRefresh, function (newVal, oldVal) {
// Skip the initial watch firing
if (newVal !== oldVal) {
$timeout(function () {
codeMirror.refresh();
});
}
});
}
// onLoad callback
if (angular.isFunction(opts.onLoad)) {
opts.onLoad(codeMirror);
}
};
$timeout(deferCodeMirror);
}
};
}]);