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

Commit d10d816

Browse files
committed
refacto: move back the instantiation in postLink
1 parent 18317b7 commit d10d816

File tree

3 files changed

+29
-38
lines changed

3 files changed

+29
-38
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"main": "./ui-codemirror.js",
99
"dependencies": {},
1010
"devDependencies": {
11-
"angular-ui-publisher": "1.2.x",
11+
"angular-ui-publisher": "angular-ui/angular-ui-publisher#06e016272e0064eb030f52cffbe00daa48b56e20",
1212
"grunt": "~0.4.2",
1313
"grunt-contrib-connect": "~0.5.0",
1414
"grunt-contrib-copy": "~0.4.1",

src/ui-codemirror.js

+20-27
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,32 @@ angular.module('ui.codemirror', [])
1111
restrict: 'EA',
1212
require: '?ngModel',
1313
priority: 1,
14-
compile: function compile(tElement) {
14+
compile: function compile() {
1515

1616
// Require CodeMirror
1717
if (angular.isUndefined(window.CodeMirror)) {
1818
throw new Error('ui-codemirror need CodeMirror to work... (o rly?)');
1919
}
2020

21-
// Create a codemirror instance with
22-
// - the function that will to place the editor into the document.
23-
// - the initial content of the editor.
24-
// see http://codemirror.net/doc/manual.html#api_constructor
25-
var value = tElement.text();
26-
var codeMirror = new window.CodeMirror(function (cm_el) {
27-
28-
angular.forEach(tElement.prop('attributes'), function (a) {
29-
if (a.name === 'ui-codemirror') {
30-
cm_el.setAttribute('ui-codemirror-opts', a.textContent);
31-
} else {
32-
cm_el.setAttribute(a.name, a.textContent);
33-
}
34-
});
21+
return function postLink(scope, iElement, iAttrs, ngModel) {
3522

36-
// FIX replaceWith throw not parent Error !
37-
if (tElement.parent().length <= 0) {
38-
tElement.wrap('<div>');
39-
}
4023

41-
tElement.replaceWith(cm_el);
42-
}, {value: value});
24+
var options, opts, codeMirror, value;
4325

44-
return function postLink(scope, iElement, iAttrs, ngModel) {
26+
value = iElement.text();
4527

46-
var options, opts;
28+
if (iElement[0].tagName === 'TEXTAREA') {
29+
codeMirror = window.CodeMirror.fromTextArea(iElement[0], {
30+
value: value
31+
});
32+
} else {
33+
iElement.html('');
34+
codeMirror = new window.CodeMirror(function(cm_el) {
35+
iElement.replaceWith(cm_el);
36+
}, {
37+
value: value
38+
});
39+
}
4740

4841
options = uiCodemirrorConfig.codemirror || {};
4942
opts = angular.extend({}, options, scope.$eval(iAttrs.uiCodemirror), scope.$eval(iAttrs.uiCodemirrorOpts));
@@ -58,7 +51,7 @@ angular.module('ui.codemirror', [])
5851

5952
updateOptions(opts);
6053

61-
if (angular.isDefined(scope.$eval(iAttrs.uiCodemirror))) {
54+
if (iAttrs.uiCodemirror) {
6255
scope.$watch(iAttrs.uiCodemirror, updateOptions, true);
6356
}
6457
// Specialize change event
@@ -79,8 +72,7 @@ angular.module('ui.codemirror', [])
7972
ngModel.$formatters.push(function (value) {
8073
if (angular.isUndefined(value) || value === null) {
8174
return '';
82-
}
83-
else if (angular.isObject(value) || angular.isArray(value)) {
75+
} else if (angular.isObject(value) || angular.isArray(value)) {
8476
throw new Error('ui-codemirror cannot use an object or an array as a model');
8577
}
8678
return value;
@@ -103,7 +95,7 @@ angular.module('ui.codemirror', [])
10395
scope.$watch(iAttrs.uiRefresh, function (newVal, oldVal) {
10496
// Skip the initial watch firing
10597
if (newVal !== oldVal) {
106-
codeMirror.refresh();
98+
codeMirror.refresh();
10799
}
108100
});
109101
}
@@ -112,6 +104,7 @@ angular.module('ui.codemirror', [])
112104
if (angular.isFunction(opts.onLoad)) {
113105
opts.onLoad(codeMirror);
114106
}
107+
115108
};
116109
}
117110
};

test/codemirror.spec.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('uiCodemirror', function () {
8787
});
8888

8989
it('should replace the element with a div.CodeMirror', function () {
90-
// Explicit a parent node to support the scope.
90+
// Explicit a parent node to support the directive.
9191
var element = $compile('<div><div ui-codemirror></div></div>')(scope).children();
9292

9393
expect(element).toBeDefined();
@@ -189,31 +189,29 @@ describe('uiCodemirror', function () {
189189

190190

191191
it('when the IDE changes should update the model', function () {
192-
// Explicit a parent node to support the scope.
193-
var element = $compile('<div><div ui-codemirror ng-model="foo"></div></div>')(scope).children();
192+
var element = $compile('<div ui-codemirror ng-model="foo"></div>')(scope);
194193

195194
expect(element).toBeDefined();
196-
expect(element.attr('class')).toEqual('CodeMirror cm-s-default ng-pristine ng-valid');
195+
expect(element.attr('class')).toEqual('ng-scope ng-pristine ng-valid');
197196

198197
var value = 'baz';
199198
codemirror.setValue(value);
200199
expect(scope.foo).toBe(value);
201200

202-
expect(element.attr('class')).toEqual('CodeMirror cm-s-default ng-valid ng-dirty');
201+
expect(element.attr('class')).toEqual('ng-scope ng-valid ng-dirty');
203202

204203
});
205204

206205
it('when the model changes should update the IDE', function () {
207-
// Explicit a parent node to support the scope.
208-
var element = $compile('<div><div ui-codemirror ng-model="foo"></div></div>')(scope).children();
206+
var element = $compile('<div ui-codemirror ng-model="foo"></div>')(scope);
209207

210208
expect(element).toBeDefined();
211-
expect(element.attr('class')).toEqual('CodeMirror cm-s-default ng-pristine ng-valid');
209+
expect(element.attr('class')).toEqual('ng-scope ng-pristine ng-valid');
212210

213211
scope.$apply('foo = "bar"');
214212
expect(codemirror.getValue()).toBe(scope.foo);
215213

216-
expect(element.attr('class')).toEqual('CodeMirror cm-s-default ng-pristine ng-valid');
214+
expect(element.attr('class')).toEqual('ng-scope ng-pristine ng-valid');
217215
});
218216

219217

@@ -298,4 +296,4 @@ describe('uiCodemirror', function () {
298296
expect(compileWithArray).toThrow();
299297
});
300298

301-
});
299+
});

0 commit comments

Comments
 (0)