1
+ ( function ( window , angular ) {
2
+ 'use strict' ;
3
+ // Source: src/angular-json-editor.js
4
+
5
+
6
+ angular . module ( 'angular-json-editor' , [ ] ) . provider ( 'JSONEditor' , function ( ) {
7
+ var configuration = {
8
+ defaults : {
9
+ options : {
10
+ iconlib : 'bootstrap3' ,
11
+ theme : 'bootstrap3'
12
+ }
13
+ }
14
+ } ;
15
+
16
+ this . configure = function ( options ) {
17
+ extendDeep ( configuration , options ) ;
18
+ } ;
19
+
20
+ this . $get = [ '$window' , function ( $window ) {
21
+ var JSONEditor = $window . JSONEditor ;
22
+ extendDeep ( JSONEditor , configuration ) ;
23
+ return $window . JSONEditor ;
24
+ } ] ;
25
+
26
+ function extendDeep ( dst ) {
27
+ angular . forEach ( arguments , function ( obj ) {
28
+ if ( obj !== dst ) {
29
+ angular . forEach ( obj , function ( value , key ) {
30
+ if ( dst [ key ] && dst [ key ] . constructor && dst [ key ] . constructor === Object ) {
31
+ extendDeep ( dst [ key ] , value ) ;
32
+ } else {
33
+ dst [ key ] = value ;
34
+ }
35
+ } ) ;
36
+ }
37
+ } ) ;
38
+ return dst ;
39
+ }
40
+
41
+ } ) . directive ( 'jsonEditor' , [ '$q' , 'JSONEditor' , function ( $q , JSONEditor ) {
42
+
43
+ return {
44
+ restrict : 'E' ,
45
+ transclude : true ,
46
+ scope : {
47
+ schema : '=' ,
48
+ startval : '=' ,
49
+ buttonsController : '@' ,
50
+ onChange : '&'
51
+ } ,
52
+ controller : [ '$scope' , '$attrs' , '$controller' , function ( $scope , $attrs , $controller ) {
53
+
54
+ var controller , controllerScope , controllerName = $attrs . buttonsController ;
55
+ if ( angular . isString ( controllerName ) && controllerName !== '' ) {
56
+ controllerScope = {
57
+ $scope : $scope
58
+ } ;
59
+
60
+ try {
61
+ controller = $controller ( controllerName , controllerScope ) ;
62
+ } catch ( e ) {
63
+ // Any exceptions thrown will probably be because the controller specified does not exist
64
+ throw new Error ( 'json-editor: buttons-controller attribute must be a valid controller.' ) ;
65
+ }
66
+ }
67
+
68
+ } ] ,
69
+ link : function ( scope , element , attrs , controller , transclude ) {
70
+ var valueToResolve ,
71
+ startValPromise = $q . when ( { } ) ,
72
+ schemaPromise = $q . when ( null ) ;
73
+
74
+ scope . isValid = false ;
75
+
76
+ if ( ! angular . isString ( attrs . schema ) ) {
77
+ throw new Error ( 'json-editor: schema attribute has to be defined.' ) ;
78
+ }
79
+ if ( angular . isObject ( scope . schema ) ) {
80
+ schemaPromise = $q . when ( scope . schema ) ;
81
+ }
82
+ if ( angular . isObject ( scope . startval ) ) {
83
+ // Support both $http (i.e. $q) and $resource promises, and also normal object.
84
+ valueToResolve = scope . startval ;
85
+ if ( angular . isDefined ( valueToResolve . $promise ) ) {
86
+ startValPromise = $q . when ( valueToResolve . $promise ) ;
87
+
88
+ } else {
89
+ startValPromise = $q . when ( valueToResolve ) ;
90
+ }
91
+ }
92
+
93
+ // Wait for the start value and schema to resolve before building the editor.
94
+ $q . all ( [ schemaPromise , startValPromise ] ) . then ( function ( result ) {
95
+
96
+ // Support $http promise response with the 'data' property.
97
+ var schema = result [ 0 ] . data || result [ 0 ] ,
98
+ startVal = result [ 1 ] ;
99
+ if ( schema === null ) {
100
+ throw new Error ( 'json-editor: could not resolve schema data.' ) ;
101
+ }
102
+
103
+ scope . editor = new JSONEditor ( element [ 0 ] , {
104
+ startval : startVal ,
105
+ schema : schema
106
+ } ) ;
107
+
108
+ var editor = scope . editor ;
109
+
110
+ editor . on ( 'ready' , function ( ) {
111
+ scope . isValid = ( editor . validate ( ) . length === 0 ) ;
112
+ } ) ;
113
+
114
+ editor . on ( 'change' , function ( ) {
115
+ // Fire the onChange callback
116
+ if ( typeof scope . onChange === 'function' ) {
117
+ scope . onChange ( {
118
+ $editorValue : editor . getValue ( )
119
+ } ) ;
120
+ }
121
+ scope . $apply ( function ( ) {
122
+ scope . isValid = ( editor . validate ( ) . length === 0 ) ;
123
+ } ) ;
124
+ } ) ;
125
+
126
+ // Transclude the buttons at the bottom.
127
+ var buttons = transclude ( scope , function ( clone ) {
128
+ return clone ;
129
+ } ) ;
130
+
131
+ element . append ( buttons ) ;
132
+ } ) ;
133
+ }
134
+ } ;
135
+
136
+ } ] ) ;
137
+
138
+ } ) ( window , angular ) ;
0 commit comments