@@ -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 } ;
0 commit comments