@@ -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 ) {
21+ return function postLink ( scope , iElement , iAttrs , ngModel ) {
2722
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- }
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
@@ -69,8 +62,7 @@ angular.module('ui.codemirror', [])
6962 ngModel . $formatters . push ( function ( value ) {
7063 if ( angular . isUndefined ( value ) || value === null ) {
7164 return '' ;
72- }
73- else if ( angular . isObject ( value ) || angular . isArray ( value ) ) {
65+ } else if ( angular . isObject ( value ) || angular . isArray ( value ) ) {
7466 throw new Error ( 'ui-codemirror cannot use an object or an array as a model' ) ;
7567 }
7668 return value ;
@@ -105,7 +97,7 @@ angular.module('ui.codemirror', [])
10597 scope . $watch ( iAttrs . uiRefresh , function ( newVal , oldVal ) {
10698 // Skip the initial watch firing
10799 if ( newVal !== oldVal ) {
108- codeMirror . refresh ( ) ;
100+ codeMirror . refresh ( ) ;
109101 }
110102 } ) ;
111103 }
@@ -126,6 +118,7 @@ angular.module('ui.codemirror', [])
126118 if ( angular . isFunction ( opts . onLoad ) ) {
127119 opts . onLoad ( codeMirror ) ;
128120 }
121+
129122 } ;
130123 }
131124 } ;
0 commit comments