1 //angular.module('ui.codemirror', [])
2 // .constant('uiCodemirrorConfig', {})
3 // .directive('uiCodemirror', uiCodemirrorDirective);
4 define(['app/yangui/yangui.module'], function(yangui) {
6 yangui.register.constant('uiCodemirrorConfig', {});
7 yangui.register.directive('uiCodemirror', uiCodemirrorDirective);
12 function uiCodemirrorDirective($timeout, uiCodemirrorConfig) {
17 compile: function compile() {
20 if (angular.isUndefined(window.CodeMirror)) {
21 throw new Error('ui-codemirror needs CodeMirror to work... (o rly?)');
28 function postLink(scope, iElement, iAttrs, ngModel) {
29 var codemirrorOptions = angular.extend(
30 { value: iElement.text() },
31 uiCodemirrorConfig.codemirror || {},
32 scope.$eval(iAttrs.uiCodemirror),
33 scope.$eval(iAttrs.uiCodemirrorOpts)
36 var codemirror = newCodemirrorEditor(iElement, codemirrorOptions);
40 iAttrs.uiCodemirror || iAttrs.uiCodemirrorOpts,
44 configNgModelLink(codemirror, ngModel, scope);
46 configUiRefreshAttribute(codemirror, iAttrs.uiRefresh, scope);
48 // Allow access to the CodeMirror instance through a broadcasted event
49 // eg: $broadcast('CodeMirror', function(cm){...});
50 scope.$on('CodeMirror', function(event, callback) {
51 if (angular.isFunction(callback)) {
54 throw new Error('the CodeMirror event requires a callback function');
59 if (angular.isFunction(codemirrorOptions.onLoad)) {
60 codemirrorOptions.onLoad(codemirror);
64 function newCodemirrorEditor(iElement, codemirrorOptions) {
67 if (iElement[0].tagName === 'TEXTAREA') {
68 // Might bug but still ...
69 codemirrot = window.CodeMirror.fromTextArea(iElement[0], codemirrorOptions);
72 codemirrot = new window.CodeMirror(function(cm_el) {
73 iElement.append(cm_el);
74 }, codemirrorOptions);
80 function configOptionsWatcher(codemirrot, uiCodemirrorAttr, scope) {
81 if (!uiCodemirrorAttr) { return; }
83 var codemirrorDefaultsKeys = Object.keys(window.CodeMirror.defaults);
84 scope.$watch(uiCodemirrorAttr, updateOptions, true);
85 function updateOptions(newValues, oldValue) {
86 if (!angular.isObject(newValues)) { return; }
87 codemirrorDefaultsKeys.forEach(function(key) {
88 if (newValues.hasOwnProperty(key)) {
90 if (oldValue && newValues[key] === oldValue[key]) {
94 codemirrot.setOption(key, newValues[key]);
100 function configNgModelLink(codemirror, ngModel, scope) {
101 if (!ngModel) { return; }
102 // CodeMirror expects a string, so make sure it gets one.
103 // This does not change the model.
104 ngModel.$formatters.push(function(value) {
105 if (angular.isUndefined(value) || value === null) {
107 } else if (angular.isObject(value) || angular.isArray(value)) {
108 throw new Error('ui-codemirror cannot use an object or an array as a model');
114 // Override the ngModelController $render method, which is what gets called when the model is updated.
115 // This takes care of the synchronizing the codeMirror element with the underlying model, in the case that it is changed by something else.
116 ngModel.$render = function() {
117 //Code mirror expects a string so make sure it gets one
118 //Although the formatter have already done this, it can be possible that another formatter returns undefined (for example the required directive)
119 var safeViewValue = ngModel.$viewValue || '';
120 codemirror.setValue(safeViewValue);
124 // Keep the ngModel in sync with changes from CodeMirror
125 codemirror.on('change', function(instance) {
126 var newValue = instance.getValue();
127 if (newValue !== ngModel.$viewValue) {
128 scope.$evalAsync(function() {
129 ngModel.$setViewValue(newValue);
135 // Fix for using codemirror in tabs (or modal window)
136 codemirror.on('changes', function(instance, changes) {
143 function configUiRefreshAttribute(codeMirror, uiRefreshAttr, scope) {
144 if (!uiRefreshAttr) { return; }
146 scope.$watch(uiRefreshAttr, function(newVal, oldVal) {
147 // Skip the initial watch firing
148 if (newVal !== oldVal) {
149 $timeout(function() {
150 codeMirror.refresh();
157 uiCodemirrorDirective.$inject = ["$timeout", "uiCodemirrorConfig"];