Skip to content
This repository was archived by the owner on May 25, 2019. It is now read-only.

Commit c2cc9d6

Browse files
committed
Merge pull request #77 from angular-ui/fix-support-angular-1.3-x
Fix support angular 1.3 x
2 parents 8bd844b + 142f09d commit c2cc9d6

File tree

6 files changed

+250
-197
lines changed

6 files changed

+250
-197
lines changed

.jshintrc

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
{
2-
"boss": true,
3-
"browser": true,
4-
"eqnull": true,
5-
"expr": true,
2+
"bitwise": true,
3+
"camelcase": false,
4+
"curly": true,
5+
"eqeqeq": true,
6+
"freeze": true,
67
"globalstrict": true,
78
"immed": true,
8-
"laxbreak": true,
9-
"loopfunc": true,
9+
"latedef": false,
1010
"newcap": true,
1111
"noarg": true,
1212
"noempty": true,
13-
"nonew": true,
14-
"quotmark": true,
15-
"smarttabs": true,
16-
"sub": true,
17-
"trailing": true,
13+
"quotmark": "single",
1814
"undef": true,
1915
"unused": true,
16+
"maxdepth": 4,
17+
"maxcomplexity": 8,
18+
19+
"eqnull": false,
20+
"esnext": true,
21+
22+
"browser": true,
23+
"jasmine": true,
24+
"validthis": true,
25+
2026
"globals": {
21-
"angular": false
27+
"angular": true
2228
}
2329
}

bower.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
"url": "https://github.com/angular-ui/ui-codemirror.git"
2222
},
2323
"dependencies": {
24-
"angular": "^1",
25-
"codemirror": "4.3",
24+
"angular": "~1.3",
25+
"codemirror": "~4.8",
2626
"angular-mocks": "1.2.19"
2727
},
2828
"devDependencies": {
29-
"angular-mocks": "1.2.19"
29+
"angular-mocks": "~1.3"
3030
}
3131
}

gruntFile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module.exports = function (grunt) {
1212
function fakeTargetTask(prefix){
1313
return function(){
1414

15-
if (this.args.length !== 1) return grunt.log.fail('Just give the name of the ' + prefix + ' you want like :\ngrunt ' + prefix + ':bower');
15+
if (this.args.length !== 1) {
16+
return grunt.log.fail('Just give the name of the ' + prefix + ' you want like :\ngrunt ' + prefix + ':bower');
17+
}
1618

1719
var done = this.async();
1820
var spawn = require('child_process').spawn;

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010
"devDependencies": {
1111
"angular-ui-publisher": "angular-ui/angular-ui-publisher#06e016272e0064eb030f52cffbe00daa48b56e20",
1212
"grunt": "~0.4.2",
13-
"grunt-contrib-connect": "~0.7.0",
14-
"grunt-contrib-copy": "~0.5.0",
13+
"grunt-contrib-connect": "~0.9.0",
14+
"grunt-contrib-copy": "~0.7.0",
1515
"grunt-contrib-jshint": "~0.10.0",
16-
"grunt-contrib-uglify": "~0.4.0",
16+
"grunt-contrib-uglify": "~0.6.0",
1717
"grunt-contrib-watch": "~0.6.0",
1818
"grunt-conventional-changelog": "~1.1.0",
19-
"grunt-karma": "~0.8.0",
19+
"grunt-karma": "~0.9.0",
2020
"grunt-ngmin": "0.0.3",
2121
"karma": "~0.12.0",
2222
"karma-chrome-launcher": "~0.1.1",
2323
"karma-coffee-preprocessor": "~0.2.1",
2424
"karma-firefox-launcher": "~0.1.2",
2525
"karma-html2js-preprocessor": "~0.1.0",
26-
"karma-jasmine": "~0.1.4",
26+
"karma-jasmine": "~0.3.0",
2727
"karma-phantomjs-launcher": "~0.1.1",
2828
"karma-requirejs": "~0.2.0",
2929
"karma-script-launcher": "~0.1.0",
30-
"load-grunt-tasks": "~0.4.0",
30+
"load-grunt-tasks": "~1.0.0",
3131
"requirejs": "~2.1.9"
3232
},
3333
"scripts": {

src/ui-codemirror.js

Lines changed: 120 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -5,122 +5,142 @@
55
*/
66
angular.module('ui.codemirror', [])
77
.constant('uiCodemirrorConfig', {})
8-
.directive('uiCodemirror', ['uiCodemirrorConfig', function (uiCodemirrorConfig) {
8+
.directive('uiCodemirror', uiCodemirrorDirective);
99

10-
return {
11-
restrict: 'EA',
12-
require: '?ngModel',
13-
priority: 1,
14-
compile: function compile() {
10+
function uiCodemirrorDirective(uiCodemirrorConfig) {
1511

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() {
2016

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+
}
2221

22+
return postLink;
23+
}
24+
};
2325

24-
var options, opts, codeMirror, initialTextValue;
26+
function postLink(scope, iElement, iAttrs, ngModel) {
2527

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+
);
2734

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);
3036

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+
);
4042

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);
5944

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;
9391
}
9492

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+
});
95111

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-
}
105112

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+
};
106121

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-
});
116122

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+
}
117134

118-
// onLoad callback
119-
if (angular.isFunction(opts.onLoad)) {
120-
opts.onLoad(codeMirror);
121-
}
135+
function configUiRefreshAttribute(codeMirror, uiRefreshAttr, scope) {
136+
if (!uiRefreshAttr) { return; }
122137

123-
};
138+
scope.$watch(uiRefreshAttr, function(newVal, oldVal) {
139+
// Skip the initial watch firing
140+
if (newVal !== oldVal) {
141+
codeMirror.refresh();
124142
}
125-
};
126-
}]);
143+
});
144+
}
145+
146+
}

0 commit comments

Comments
 (0)