@@ -22,15 +22,14 @@ angular.module('examples', [])
22
22
} ;
23
23
} ] )
24
24
25
-
26
- . factory ( 'openPlunkr' , [ 'formPostData' , '$http' , '$q' , function ( formPostData , $http , $q ) {
27
-
25
+ . factory ( 'createCopyrightNotice' , function ( ) {
28
26
var COPYRIGHT = 'Copyright ' + ( new Date ( ) ) . getFullYear ( ) + ' Google Inc. All Rights Reserved.\n'
29
27
+ 'Use of this source code is governed by an MIT-style license that\n'
30
28
+ 'can be found in the LICENSE file at http://angular.io/license' ;
31
29
var COPYRIGHT_JS_CSS = '\n\n/*\n' + COPYRIGHT + '\n*/' ;
32
30
var COPYRIGHT_HTML = '\n\n<!-- \n' + COPYRIGHT + '\n-->' ;
33
- function getCopyright ( filename ) {
31
+
32
+ return function getCopyright ( filename ) {
34
33
switch ( filename . substr ( filename . lastIndexOf ( '.' ) ) ) {
35
34
case '.html' :
36
35
return COPYRIGHT_HTML ;
@@ -41,37 +40,100 @@ angular.module('examples', [])
41
40
return COPYRIGHT ;
42
41
}
43
42
return '' ;
44
- }
43
+ } ;
44
+ } )
45
+
46
+ . directive ( 'plnkrOpener' , [ '$q' , 'getExampleData' , 'formPostData' , 'createCopyrightNotice' , function ( $q , getExampleData , formPostData , createCopyrightNotice ) {
47
+ return {
48
+ scope : { } ,
49
+ bindToController : {
50
+ 'examplePath' : '@'
51
+ } ,
52
+ controllerAs : 'plnkr' ,
53
+ template : '<button ng-click="plnkr.open($event)" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</button> ' ,
54
+ controller : [ function ( ) {
55
+ var ctrl = this ;
56
+
57
+ ctrl . example = {
58
+ path : ctrl . examplePath ,
59
+ manifest : undefined ,
60
+ files : undefined ,
61
+ name : 'AngularJS Example'
62
+ } ;
63
+
64
+ ctrl . prepareExampleData = function ( ) {
65
+ if ( ctrl . example . manifest ) {
66
+ return $q . when ( ctrl . example ) ;
67
+ }
68
+
69
+ return getExampleData ( ctrl . examplePath ) . then ( function ( data ) {
70
+ ctrl . example . files = data . files ;
71
+ ctrl . example . manifest = data . manifest ;
72
+
73
+ // Build a pretty title for the Plunkr
74
+ var exampleNameParts = data . manifest . name . split ( '-' ) ;
75
+ exampleNameParts . unshift ( 'AngularJS' ) ;
76
+ angular . forEach ( exampleNameParts , function ( part , index ) {
77
+ exampleNameParts [ index ] = part . charAt ( 0 ) . toUpperCase ( ) + part . substr ( 1 ) ;
78
+ } ) ;
79
+ ctrl . example . name = exampleNameParts . join ( ' - ' ) ;
80
+
81
+ return ctrl . example ;
82
+ } ) ;
83
+ } ;
84
+
85
+ ctrl . open = function ( clickEvent ) {
86
+
87
+ var newWindow = clickEvent . ctrlKey || clickEvent . metaKey ;
45
88
46
- return function ( exampleFolder , clickEvent ) {
89
+ var postData = {
90
+ 'tags[0]' : "angularjs" ,
91
+ 'tags[1]' : "example" ,
92
+ 'private' : true
93
+ } ;
47
94
48
- var exampleName = 'AngularJS Example' ;
49
- var newWindow = clickEvent . ctrlKey || clickEvent . metaKey ;
95
+ // Make sure the example data is available.
96
+ // If an XHR must be made, this might break some pop-up blockers when
97
+ // new window is requested
98
+ ctrl . prepareExampleData ( )
99
+ . then ( function ( ) {
100
+ angular . forEach ( ctrl . example . files , function ( file ) {
101
+ postData [ 'files[' + file . name + ']' ] = file . content + createCopyrightNotice ( file . name ) ;
102
+ } ) ;
50
103
104
+ postData . description = ctrl . example . name ;
105
+
106
+ formPostData ( 'http://plnkr.co/edit/?p=preview' , newWindow , postData ) ;
107
+ } ) ;
108
+
109
+ } ;
110
+
111
+ // Initialize the example data, so it's ready when clicking the open button.
112
+ // Otherwise pop-up blockers will prevent a new window from opening
113
+ ctrl . prepareExampleData ( ctrl . example . path ) ;
114
+
115
+ } ]
116
+ } ;
117
+ } ] )
118
+
119
+ . factory ( 'getExampleData' , [ '$http' , '$q' , function ( $http , $q ) {
120
+ return function ( exampleFolder ) {
51
121
// Load the manifest for the example
52
- $http . get ( exampleFolder + '/manifest.json' )
122
+ return $http . get ( exampleFolder + '/manifest.json' )
53
123
. then ( function ( response ) {
54
124
return response . data ;
55
125
} )
56
126
. then ( function ( manifest ) {
57
127
var filePromises = [ ] ;
58
128
59
- // Build a pretty title for the Plunkr
60
- var exampleNameParts = manifest . name . split ( '-' ) ;
61
- exampleNameParts . unshift ( 'AngularJS' ) ;
62
- angular . forEach ( exampleNameParts , function ( part , index ) {
63
- exampleNameParts [ index ] = part . charAt ( 0 ) . toUpperCase ( ) + part . substr ( 1 ) ;
64
- } ) ;
65
- exampleName = exampleNameParts . join ( ' - ' ) ;
66
-
67
129
angular . forEach ( manifest . files , function ( filename ) {
68
130
filePromises . push ( $http . get ( exampleFolder + '/' + filename , { transformResponse : [ ] } )
69
131
. then ( function ( response ) {
70
132
71
133
// The manifests provide the production index file but Plunkr wants
72
134
// a straight index.html
73
135
if ( filename === "index-production.html" ) {
74
- filename = "index.html"
136
+ filename = "index.html" ;
75
137
}
76
138
77
139
return {
@@ -80,21 +142,11 @@ angular.module('examples', [])
80
142
} ;
81
143
} ) ) ;
82
144
} ) ;
83
- return $q . all ( filePromises ) ;
84
- } )
85
- . then ( function ( files ) {
86
- var postData = { } ;
87
145
88
- angular . forEach ( files , function ( file ) {
89
- postData [ 'files[' + file . name + ']' ] = file . content + getCopyright ( file . name ) ;
146
+ return $q . all ( {
147
+ manifest : manifest ,
148
+ files : $q . all ( filePromises )
90
149
} ) ;
91
-
92
- postData [ 'tags[0]' ] = "angularjs" ;
93
- postData [ 'tags[1]' ] = "example" ;
94
- postData . private = true ;
95
- postData . description = exampleName ;
96
-
97
- formPostData ( 'http://plnkr.co/edit/?p=preview' , newWindow , postData ) ;
98
150
} ) ;
99
151
} ;
100
- } ] ) ;
152
+ } ] ) ;
0 commit comments