@@ -11,39 +11,32 @@ angular.module('ui.codemirror', [])
11
11
restrict : 'EA' ,
12
12
require : '?ngModel' ,
13
13
priority : 1 ,
14
- compile : function compile ( tElement ) {
14
+ compile : function compile ( ) {
15
15
16
16
// Require CodeMirror
17
17
if ( angular . isUndefined ( window . CodeMirror ) ) {
18
18
throw new Error ( 'ui-codemirror need CodeMirror to work... (o rly?)' ) ;
19
19
}
20
20
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 ) {
21
+ return function postLink ( scope , iElement , iAttrs , ngModel ) {
27
22
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
- } ) ;
35
-
36
- // FIX replaceWith throw not parent Error !
37
- if ( tElement . parent ( ) . length <= 0 ) {
38
- tElement . wrap ( '<div>' ) ;
39
- }
40
23
41
- tElement . replaceWith ( cm_el ) ;
42
- } , { value : value } ) ;
24
+ var options , opts , codeMirror , value ;
43
25
44
- return function postLink ( scope , iElement , iAttrs , ngModel ) {
26
+ value = iElement . text ( ) ;
45
27
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
+ }
47
40
48
41
options = uiCodemirrorConfig . codemirror || { } ;
49
42
opts = angular . extend ( { } , options , scope . $eval ( iAttrs . uiCodemirror ) , scope . $eval ( iAttrs . uiCodemirrorOpts ) ) ;
@@ -58,7 +51,7 @@ angular.module('ui.codemirror', [])
58
51
59
52
updateOptions ( opts ) ;
60
53
61
- if ( angular . isDefined ( scope . $eval ( iAttrs . uiCodemirror ) ) ) {
54
+ if ( iAttrs . uiCodemirror ) {
62
55
scope . $watch ( iAttrs . uiCodemirror , updateOptions , true ) ;
63
56
}
64
57
@@ -69,8 +62,7 @@ angular.module('ui.codemirror', [])
69
62
ngModel . $formatters . push ( function ( value ) {
70
63
if ( angular . isUndefined ( value ) || value === null ) {
71
64
return '' ;
72
- }
73
- else if ( angular . isObject ( value ) || angular . isArray ( value ) ) {
65
+ } else if ( angular . isObject ( value ) || angular . isArray ( value ) ) {
74
66
throw new Error ( 'ui-codemirror cannot use an object or an array as a model' ) ;
75
67
}
76
68
return value ;
@@ -105,7 +97,7 @@ angular.module('ui.codemirror', [])
105
97
scope . $watch ( iAttrs . uiRefresh , function ( newVal , oldVal ) {
106
98
// Skip the initial watch firing
107
99
if ( newVal !== oldVal ) {
108
- codeMirror . refresh ( ) ;
100
+ codeMirror . refresh ( ) ;
109
101
}
110
102
} ) ;
111
103
}
@@ -126,6 +118,7 @@ angular.module('ui.codemirror', [])
126
118
if ( angular . isFunction ( opts . onLoad ) ) {
127
119
opts . onLoad ( codeMirror ) ;
128
120
}
121
+
129
122
} ;
130
123
}
131
124
} ;
0 commit comments