1
+ import * as Assimilate from '../' ;
2
+ Assimilate . Validator . use ( 'tv4' ) ;
3
+ // @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt Expat
4
+ //
5
+ // To test the tinymce addon, uncomment the files above and inject 'tx-tinymce' below.
6
+ /*global alert*/
7
+ var app = angular . module ( 'test' , [ 'schemaForm' , 'ui.ace' , 'tx-tinymce' ] ) ;
8
+ app . config ( [ 'sfErrorMessageProvider' , function ( sfErrorMessageProvider ) {
9
+ sfErrorMessageProvider . setDefaultMessage ( 10001 , 'Whoa! Can you double check that email address for me?' ) ;
10
+ } ] ) ;
11
+ app . controller ( 'TestCtrl' , function ( $scope , $http , $location ) {
12
+ tv4 . defineError ( 'EMAIL' , 10001 , 'Invalid email address' ) ;
13
+ tv4 . defineKeyword ( 'email' , function ( data , value , schema ) {
14
+ if ( schema . email ) {
15
+ if ( / ^ \S + @ \S + $ / . test ( data ) ) {
16
+ return null ;
17
+ }
18
+ return {
19
+ code : 10001
20
+ } ;
21
+ }
22
+ return null ;
23
+ } ) ;
24
+
25
+ $scope . tests = [
26
+ { name : "JSON Ref" , data : 'data/jsonref.json' } ,
27
+ { name : "Simple" , data : 'data/simple.json' } ,
28
+ { name : "Basic JSON Schema Type" , data : 'data/types.json' } ,
29
+ { name : "Bootstrap Grid" , data : 'data/grid.json' } ,
30
+ { name : "Complex Key Support" , data : 'data/complex-keys.json' } ,
31
+ { name : "Array" , data : 'data/array.json' } ,
32
+ { name : "Array of types" , data : 'data/array-of-types.json' } ,
33
+ { name : "Tab Array" , data : 'data/tabarray.json' } ,
34
+ { name : "Deep Array" , data : 'data/deep-array.json' } ,
35
+ //{ name: "Array Radio-Buttons", data: 'data/array-radiobuttons.json' },
36
+ { name : "TitleMap Examples" , data : 'data/titlemaps.json' } ,
37
+ { name : "Kitchen Sink" , data : 'data/sink.json' } ,
38
+ { name : "Calculate" , data : 'data/calculate.json' } ,
39
+ { name : "Custom Error" , data : 'data/custom-error.json' } ,
40
+ { name : "Hack: Conditional required" , data : 'data/conditional-required.json' }
41
+ ] ;
42
+
43
+ $scope . navbarMode = 'default' ;
44
+
45
+ // Load data from gist.
46
+ if ( window . location . hash . length > 4 ) {
47
+ $scope . navbarMode = 'loaded' ;
48
+ var gistId = window . location . hash . replace ( / [ \! \# \/ ] * / g, '' ) ;
49
+ $scope . loading = true ;
50
+ $http . get ( 'https://api.github.com/gists/' + gistId )
51
+ . then ( function ( response ) {
52
+ $scope . error = null ;
53
+ $scope . tests . unshift ( { name : 'Gist' } ) ;
54
+ $scope . selectedTest = $scope . tests [ 0 ] ;
55
+ $scope . loadedData = {
56
+ created : moment ( response . data . created_at ) . fromNow ( ) ,
57
+ user : response . data . user !== null ? response . data . user . login : 'Anonymous'
58
+ }
59
+ $scope . loading = false ;
60
+ $scope . schemaJson = response . data . files [ 'schema.json' ] . content ;
61
+ $scope . formJson = response . data . files [ 'form.json' ] . content ;
62
+ $scope . modelData = JSON . parse ( response . data . files [ 'model.json' ] . content ) ;
63
+ } ,
64
+ function ( ) {
65
+ $scope . loadedData = 'dummy' ;
66
+ $scope . error = 'Failed to load gist.' ;
67
+ $scope . selectedTest = $scope . tests [ 0 ] ;
68
+ }
69
+ ) ;
70
+ } else {
71
+ $scope . selectedTest = $scope . tests [ 0 ] ;
72
+ }
73
+
74
+ $scope . $watch ( 'selectedTest' , function ( val ) {
75
+ if ( val && val . data !== undefined ) {
76
+ $http . get ( val . data ) . then ( function ( res ) { setNewData ( res . data ) ; } ) ;
77
+ }
78
+ } ) ;
79
+
80
+ $scope . decorator = 'bootstrap-decorator' ;
81
+
82
+ $scope . itParses = true ;
83
+ $scope . itParsesForm = true ;
84
+
85
+
86
+ $scope . $watch ( 'schemaJson' , function ( val , old ) {
87
+ if ( val && val !== old ) {
88
+ try {
89
+ $scope . schema = JSON . parse ( $scope . schemaJson ) ;
90
+ $scope . itParses = true ;
91
+ } catch ( e ) {
92
+ $scope . itParses = false ;
93
+ }
94
+ }
95
+ } ) ;
96
+
97
+ $scope . $watch ( 'formJson' , function ( val , old ) {
98
+ if ( val && val !== old ) {
99
+ try {
100
+ $scope . form = JSON . parse ( $scope . formJson ) ;
101
+ $scope . itParsesForm = true ;
102
+ } catch ( e ) {
103
+ $scope . itParsesForm = false ;
104
+ }
105
+ }
106
+ } ) ;
107
+
108
+ var setNewData = function ( data ) {
109
+ $scope . schema = data . schema ;
110
+ $scope . form = data . form ;
111
+ $scope . schemaJson = JSON . stringify ( $scope . schema , undefined , 2 ) ;
112
+ $scope . formJson = JSON . stringify ( $scope . form , undefined , 2 ) ;
113
+ $scope . modelData = data . model || { } ;
114
+ } ;
115
+
116
+ $scope . pretty = function ( ) {
117
+ return typeof $scope . modelData === 'string' ? $scope . modelData : angular . toJson ( $scope . modelData ) ;
118
+ } ;
119
+
120
+ $scope . log = function ( msg ) {
121
+ console . log ( "Simon says" , msg ) ;
122
+ } ;
123
+
124
+ $scope . sayNo = function ( ) {
125
+ alert ( 'Noooooooo' ) ;
126
+ } ;
127
+
128
+ $scope . say = function ( msg ) {
129
+ alert ( msg ) ;
130
+ } ;
131
+
132
+ $scope . save = function ( ) {
133
+ // To be able to save invalid json and point out errors, we
134
+ // don't save the schema/form but rather the ones in the input.
135
+
136
+ $scope . navbarMode = 'saved' ;
137
+
138
+ var gist = {
139
+ "description" : "A saved configuration for a schema form example, http://textalk.github.io/angular-schema-form/examples/bootstrap-example.html" ,
140
+ "public" : true ,
141
+ "files" : {
142
+ "schema.json" : {
143
+ "content" : $scope . schemaJson
144
+ } ,
145
+ "form.json" : {
146
+ "content" : $scope . formJson
147
+ } ,
148
+ "model.json" : {
149
+ "content" : JSON . stringify ( $scope . modelData , undefined , 2 )
150
+ }
151
+ }
152
+ } ;
153
+
154
+ $http . post ( 'https://api.github.com/gists' , gist )
155
+ . then ( function ( response ) {
156
+ $scope . error = null ;
157
+ //$location.hash(response.data.id);
158
+ window . location . hash = response . data . id ;
159
+ window . location . hash = response . data . id ;
160
+ $scope . savedGistData = {
161
+ data : response . data ,
162
+ url : $location . absUrl ( )
163
+ } ;
164
+ } ,
165
+ function ( ) {
166
+ $scope . error = 'Failed to save gist.' ;
167
+ }
168
+ ) ;
169
+ } ;
170
+
171
+ $scope . submitForm = function ( form ) {
172
+ // First we broadcast an event so all fields validate themselves
173
+ $scope . $broadcast ( 'schemaFormValidate' ) ;
174
+ // Then we check if the form is valid
175
+ if ( form . $valid ) {
176
+ alert ( 'You did it!' ) ;
177
+ }
178
+ } ;
179
+
180
+ } ) ;
181
+ // @license -end
0 commit comments