1
1
angular . module ( 'examples' , [ ] )
2
2
3
- . directive ( 'sourceEdit' , function ( getEmbeddedTemplate ) {
4
- return {
5
- template : '<div class="btn-group pull-right">' +
6
- '<a class="btn dropdown-toggle btn-primary" data-toggle="dropdown" href>' +
7
- ' <i class="icon-pencil icon-white"></i> Edit<span class="caret"></span>' +
8
- '</a>' +
9
- '<ul class="dropdown-menu">' +
10
- ' <li><a ng-click="plunkr($event)" href="">In Plunkr</a></li>' +
11
- ' <li><a ng-click="fiddle($event)" href="">In JsFiddle</a></li>' +
12
- '</ul>' +
13
- '</div>' ,
14
- scope : true ,
15
- controller : function ( $scope , $attrs , openJsFiddle , openPlunkr ) {
16
- var sources = {
17
- module : $attrs . sourceEdit ,
18
- deps : read ( $attrs . sourceEditDeps ) ,
19
- html : read ( $attrs . sourceEditHtml ) ,
20
- css : read ( $attrs . sourceEditCss ) ,
21
- js : read ( $attrs . sourceEditJs ) ,
22
- json : read ( $attrs . sourceEditJson ) ,
23
- unit : read ( $attrs . sourceEditUnit ) ,
24
- scenario : read ( $attrs . sourceEditScenario )
25
- } ;
26
- $scope . fiddle = function ( e ) {
27
- e . stopPropagation ( ) ;
28
- openJsFiddle ( sources ) ;
29
- } ;
30
- $scope . plunkr = function ( e ) {
31
- e . stopPropagation ( ) ;
32
- openPlunkr ( sources ) ;
33
- } ;
34
- }
35
- } ;
36
-
37
- function read ( text ) {
38
- var files = [ ] ;
39
- angular . forEach ( text ? text . split ( ' ' ) : [ ] , function ( refId ) {
40
- // refId is index.html-343, so we need to strip the unique ID when exporting the name
41
- files . push ( { name : refId . replace ( / - \d + $ / , '' ) , content : getEmbeddedTemplate ( refId ) } ) ;
42
- } ) ;
43
- return files ;
44
- }
45
- } )
46
-
47
-
48
- . factory ( 'angularUrls' , function ( $document ) {
49
- var urls = { } ;
50
-
51
- angular . forEach ( $document . find ( 'script' ) , function ( script ) {
52
- var match = script . src . match ( / ^ .* \/ ( a n g u l a r [ ^ \/ ] * \. j s ) $ / ) ;
53
- if ( match ) {
54
- urls [ match [ 1 ] . replace ( / ( \- \d .* ) ? ( \. m i n ) ? \. j s $ / , '.js' ) ] = match [ 0 ] ;
55
- }
56
- } ) ;
57
-
58
- return urls ;
59
- } )
60
-
61
-
62
- . factory ( 'formPostData' , function ( $document ) {
3
+ . factory ( 'formPostData' , [ '$document' , function ( $document ) {
63
4
return function ( url , fields ) {
64
5
var form = angular . element ( '<form style="display: none;" method="post" action="' + url + '" target="_blank"></form>' ) ;
65
6
angular . forEach ( fields , function ( value , name ) {
@@ -71,196 +12,61 @@ angular.module('examples', [])
71
12
form [ 0 ] . submit ( ) ;
72
13
form . remove ( ) ;
73
14
} ;
74
- } )
75
-
15
+ } ] )
76
16
77
- . factory ( 'prepareDefaultAppModule' , function ( ) {
78
- return function ( content ) {
79
- var deps = [ ] ;
80
- angular . forEach ( content . deps , function ( file ) {
81
- if ( file . name == 'angular-animate.js' ) {
82
- deps . push ( 'ngAnimate' ) ;
83
- }
84
- } ) ;
85
-
86
- var moduleName = 'App' ;
87
- return {
88
- module : moduleName ,
89
- script : "angular.module('" + moduleName + "', [" +
90
- ( deps . length ? "'" + deps . join ( "','" ) + "'" : "" ) + "]);\n\n"
91
- } ;
92
- } ;
93
- } )
94
17
95
- . factory ( 'prepareEditorAssetTags' , function ( angularUrls ) {
96
- return function ( content , options ) {
97
- options = options || { } ;
98
- var includeLocalFiles = options . includeLocalFiles ;
99
- var html = makeScriptTag ( angularUrls [ 'angular.js' ] ) ;
100
-
101
- var allFiles = [ ] . concat ( content . js , content . css , content . html , content . json ) ;
102
- angular . forEach ( content . deps , function ( file ) {
103
- if ( file . name !== 'angular.js' ) {
104
- var isLocal = false ;
105
- for ( var i = 0 ; i < allFiles . length ; i ++ ) {
106
- if ( allFiles [ i ] . name == file . name ) {
107
- isLocal = true ;
108
- break ;
109
- }
110
- }
111
- if ( ! ( isLocal && ! includeLocalFiles ) ) {
112
- var assetUrl = angularUrls [ file . name ] || file . name ;
113
- html += makeScriptTag ( assetUrl ) ;
114
- }
115
- }
116
- } ) ;
117
-
118
- if ( includeLocalFiles ) {
119
- angular . forEach ( content . css , function ( file , index ) {
120
- html += makeCssLinkTag ( file . name ) ;
121
- } ) ;
122
- }
123
-
124
- return html ;
125
-
126
-
127
- function makeScriptTag ( src ) {
128
- return '<script type="text/javascript" src="' + src + '"></script>\n' ;
129
- }
130
-
131
- function makeCssLinkTag ( src ) {
132
- return '<link rel="stylesheet" type="text/css" href="' + src + '" />\n' ;
133
- }
134
- } ;
135
- } )
136
-
137
-
138
- . factory ( 'openPlunkr' , function ( templateMerge , formPostData , prepareEditorAssetTags , prepareDefaultAppModule ) {
139
- return function ( content ) {
140
- var hasRouting = false ;
141
- angular . forEach ( content . deps , function ( file ) {
142
- hasRouting = hasRouting || file . name == 'angular-route.js' ;
143
- } ) ;
144
- var indexHtmlContent = '<!doctype html>\n' +
145
- '<html ng-app="{{module}}">\n' +
146
- ' <head>\n' +
147
- '{{scriptDeps}}' ;
18
+ . factory ( 'openPlunkr' , [ 'formPostData' , '$http' , '$q' , function ( formPostData , $http , $q ) {
19
+ return function ( exampleFolder ) {
148
20
149
- if ( hasRouting ) {
150
- indexHtmlContent += '<script type="text/javascript">\n' +
151
- '//this is here to make plunkr work with AngularJS routing\n' +
152
- 'angular.element(document.getElementsByTagName(\'head\')).append(' +
153
- 'angular.element(\'<base href="\' + window.location.pathname + \'" />\')' +
154
- ');\n' +
155
- '</script>\n' ;
156
- }
21
+ var exampleName = 'AngularJS Example' ;
157
22
158
- indexHtmlContent += '</head>\n' +
159
- ' <body>\n\n' +
160
- '{{indexContents}}\n\n' +
161
- ' </body>\n' +
162
- '</html>\n' ;
23
+ // Load the manifest for the example
24
+ $http . get ( exampleFolder + '/manifest.json' )
25
+ . then ( function ( response ) {
26
+ return response . data ;
27
+ } )
28
+ . then ( function ( manifest ) {
29
+ var filePromises = [ ] ;
163
30
164
- indexProp = {
165
- module : content . module ,
166
- scriptDeps : prepareEditorAssetTags ( content , { includeLocalFiles : true } ) ,
167
- indexContents : content . html [ 0 ] . content
168
- } ;
169
-
170
- var allFiles = [ ] . concat ( content . js , content . css , content . html , content . json ) ;
171
-
172
- if ( ! content . module ) {
173
- var moduleData = prepareDefaultAppModule ( content ) ;
174
- indexProp . module = moduleData . module ;
175
-
176
- var found = false ;
177
- angular . forEach ( content . js , function ( file ) {
178
- if ( file . name == 'script.js' ) {
179
- file . content = moduleData . script + file . content ;
180
- found = true ;
181
- }
182
- } ) ;
183
- if ( ! found ) {
184
- indexProp . scriptDeps += '<script type="text/javascript" src="script.js"></script>\n' ;
185
- allFiles . push ( {
186
- name : 'script.js' ,
187
- content : moduleData . script
31
+ // Build a pretty title for the Plunkr
32
+ var exampleNameParts = manifest . name . split ( '-' ) ;
33
+ exampleNameParts . unshift ( 'AngularJS' ) ;
34
+ angular . forEach ( exampleNameParts , function ( part , index ) {
35
+ exampleNameParts [ index ] = part . charAt ( 0 ) . toUpperCase ( ) + part . substr ( 1 ) ;
188
36
} ) ;
189
- }
190
- }
191
-
192
- var postData = { } ;
193
-
194
- angular . forEach ( allFiles , function ( file , index ) {
195
- if ( file . content && file . name != 'index.html' ) {
196
- postData [ 'files[' + file . name + ']' ] = file . content ;
197
- }
198
- } ) ;
199
-
200
- postData [ 'files[index.html]' ] = templateMerge ( indexHtmlContent , indexProp ) ;
201
- postData [ 'tags[]' ] = "angularjs" ;
202
-
203
- postData . private = true ;
204
- postData . description = 'AngularJS Example Plunkr' ;
205
-
206
- formPostData ( 'http://plnkr.co/edit/?p=preview' , postData ) ;
207
- } ;
208
- } )
209
-
210
- . factory ( 'openJsFiddle' , function ( templateMerge , formPostData , prepareEditorAssetTags , prepareDefaultAppModule ) {
211
- var HTML = '<div ng-app=\"{{module}}\">\n{{html:2}}</div>' ,
212
- CSS = '</style> <!-- Ugly Hack to make remote files preload in jsFiddle --> \n' +
213
- '{{head:0}}<style>{{css}}' ,
214
- SCRIPT = '{{script}}' ,
215
- SCRIPT_CACHE = '\n\n<!-- {{name}} -->\n<script type="text/ng-template" id="{{name}}">\n{{content:2}}</script>' ,
216
- BASE_HREF_TAG = '<!-- Ugly Hack to make AngularJS routing work inside of jsFiddle -->\n' +
217
- '<base href="/" />\n\n' ;
218
-
219
- return function ( content ) {
220
- var prop = {
221
- module : content . module ,
222
- html : '' ,
223
- css : '' ,
224
- script : ''
225
- } ;
226
- if ( ! prop . module ) {
227
- var moduleData = prepareDefaultAppModule ( content ) ;
228
- prop . script = moduleData . script ;
229
- prop . module = moduleData . module ;
230
- }
231
-
232
- angular . forEach ( content . html , function ( file , index ) {
233
- if ( index ) {
234
- prop . html += templateMerge ( SCRIPT_CACHE , file ) ;
235
- } else {
236
- prop . html += file . content ;
237
- }
238
- } ) ;
239
-
240
- prop . head = prepareEditorAssetTags ( content , { includeLocalFiles : false } ) ;
241
-
242
- angular . forEach ( content . js , function ( file , index ) {
243
- prop . script += file . content ;
244
- } ) ;
37
+ exampleName = exampleNameParts . join ( ' - ' ) ;
38
+
39
+ angular . forEach ( manifest . files , function ( filename ) {
40
+ filePromises . push ( $http . get ( exampleFolder + '/' + filename )
41
+ . then ( function ( response ) {
42
+
43
+ // The manifests provide the production index file but Plunkr wants
44
+ // a straight index.html
45
+ if ( filename === "index-production.html" ) {
46
+ filename = "index.html"
47
+ }
48
+
49
+ return {
50
+ name : filename ,
51
+ content : response . data
52
+ } ;
53
+ } ) ) ;
54
+ } ) ;
55
+ return $q . all ( filePromises ) ;
56
+ } )
57
+ . then ( function ( files ) {
58
+ var postData = { } ;
245
59
246
- angular . forEach ( content . css , function ( file , index ) {
247
- prop . css + = file . content ;
248
- } ) ;
60
+ angular . forEach ( files , function ( file ) {
61
+ postData [ 'files[' + file . name + ']' ] = file . content ;
62
+ } ) ;
249
63
250
- var hasRouting = false ;
251
- angular . forEach ( content . deps , function ( file ) {
252
- hasRouting = hasRouting || file . name == 'angular-route.js' ;
253
- } ) ;
64
+ postData [ 'tags[0]' ] = "angularjs" ;
65
+ postData [ 'tags[1]' ] = "example" ;
66
+ postData . private = true ;
67
+ postData . description = exampleName ;
254
68
255
- var compiledHTML = templateMerge ( HTML , prop ) ;
256
- if ( hasRouting ) {
257
- compiledHTML = BASE_HREF_TAG + compiledHTML ;
258
- }
259
- formPostData ( "http://jsfiddle.net/api/post/library/pure/" , {
260
- title : 'AngularJS Example' ,
261
- html : compiledHTML ,
262
- js : templateMerge ( SCRIPT , prop ) ,
263
- css : templateMerge ( CSS , prop )
264
- } ) ;
69
+ formPostData ( 'http://plnkr.co/edit/?p=preview' , postData ) ;
70
+ } ) ;
265
71
} ;
266
- } ) ;
72
+ } ] ) ;
0 commit comments