@@ -34,58 +34,67 @@ const TAGS: string[] = ['angular', 'material', 'example'];
34
34
*/
35
35
@Injectable ( )
36
36
export class PlunkerWriter {
37
- form : HTMLFormElement ;
38
- exampleData : ExampleData ;
39
-
40
37
constructor ( private _http : Http ) { }
41
38
42
- /** Construct the plunker content */
43
- openPlunker ( data : ExampleData ) {
44
- this . exampleData = data ;
45
-
46
- this . form = this . _createFormElement ( ) ;
39
+ /**
40
+ * Returns an HTMLFormElement that will open a new plunker template with the example data when
41
+ * called with submit().
42
+ */
43
+ constructPlunkerForm ( data : ExampleData ) : Promise < HTMLFormElement > {
44
+ let form = this . _createFormElement ( ) ;
47
45
48
- for ( let i = 0 ; i < TAGS . length ; i ++ ) {
49
- this . _createFormInput ( `tags[ ${ i } ]` , TAGS [ i ] ) ;
50
- }
46
+ TAGS . forEach ( ( tag , i ) => this . _appendFormInput ( form , `tags[ ${ i } ]` , tag ) ) ;
47
+ this . _appendFormInput ( form , 'private' , 'true' ) ;
48
+ this . _appendFormInput ( form , 'description' , data . description ) ;
51
49
52
- this . _createFormInput ( 'private' , 'true' ) ;
53
- this . _createFormInput ( 'description' , this . exampleData . description ) ;
50
+ return new Promise ( resolve => {
51
+ let templateContents = TEMPLATE_FILES
52
+ . map ( file => this . _readFile ( form , data , file , TEMPLATE_PATH ) ) ;
54
53
55
- var templateContents = TEMPLATE_FILES . map ( ( file ) => this . _readFile ( file , TEMPLATE_PATH ) ) ;
56
- var exampleContents = this . exampleData . exampleFiles . map (
57
- ( file ) => this . _readFile ( file , this . exampleData . examplePath ) ) ;
54
+ let exampleContents = data . exampleFiles
55
+ . map ( file => this . _readFile ( form , data , file , data . examplePath ) ) ;
58
56
59
- Promise . all ( templateContents . concat ( exampleContents ) ) . then ( ( _ ) => this . form . submit ( ) ) ;
57
+ Promise . all ( templateContents . concat ( exampleContents ) ) . then ( ( ) => {
58
+ resolve ( form ) ;
59
+ } ) ;
60
+ } ) ;
60
61
}
61
62
63
+ /** Constructs a new form element that will navigate to the plunker url. */
62
64
_createFormElement ( ) : HTMLFormElement {
63
- var form = document . createElement ( 'form' ) ;
65
+ const form = document . createElement ( 'form' ) ;
64
66
form . action = PLUNKER_URL ;
65
67
form . method = 'post' ;
66
68
form . target = '_blank' ;
67
69
return form ;
68
70
}
69
71
70
- _createFormInput ( name : string , value : string ) {
71
- var input = document . createElement ( 'input' ) ;
72
+ /** Appends the name and value as an input to the form. */
73
+ _appendFormInput ( form : HTMLFormElement , name : string , value : string ) : void {
74
+ const input = document . createElement ( 'input' ) ;
72
75
input . type = 'hidden' ;
73
76
input . name = name ;
74
77
input . value = value ;
75
- this . form . appendChild ( input ) ;
78
+ form . appendChild ( input ) ;
76
79
}
77
80
78
- _readFile ( filename : string , path : string ) {
79
- return this . _http . get ( path + filename ) . toPromise ( ) . then (
80
- response => this . _addFileToForm ( response . text ( ) , filename , path ) ,
81
+ /** Reads the file and adds its text to the form */
82
+ _readFile ( form : HTMLFormElement , data : ExampleData , filename : string , path : string ) : void {
83
+ this . _http . get ( path + filename ) . toPromise ( ) . then (
84
+ response => this . _addFileToForm ( form , data , response . text ( ) , filename , path ) ,
81
85
error => console . log ( error ) ) ;
82
86
}
83
87
84
- _addFileToForm ( content : string , filename : string , path : string ) {
88
+ /** Adds the file text to the form. */
89
+ _addFileToForm ( form : HTMLFormElement ,
90
+ data : ExampleData ,
91
+ content : string ,
92
+ filename : string ,
93
+ path : string ) {
85
94
if ( path == TEMPLATE_PATH ) {
86
- content = this . _replaceExamplePlaceholderNames ( filename , content ) ;
95
+ content = this . _replaceExamplePlaceholderNames ( data , filename , content ) ;
87
96
}
88
- this . _createFormInput ( `files[${ filename } ]` , this . _appendCopyright ( filename , content ) ) ;
97
+ this . _appendFormInput ( form , `files[${ filename } ]` , this . _appendCopyright ( filename , content ) ) ;
89
98
}
90
99
91
100
/**
@@ -94,18 +103,20 @@ export class PlunkerWriter {
94
103
* This will replace those placeholders with the names from the example metadata,
95
104
* e.g. "<basic-button-example>" and "BasicButtonExample"
96
105
*/
97
- _replaceExamplePlaceholderNames ( fileName : string , fileContent : string ) : string {
106
+ _replaceExamplePlaceholderNames ( data : ExampleData ,
107
+ fileName : string ,
108
+ fileContent : string ) : string {
98
109
if ( fileName == 'index.html' ) {
99
110
// Replace the component selector in `index,html`.
100
- // For example, <material-docs-example></material-docs-exmaple > will be replaced as
111
+ // For example, <material-docs-example></material-docs-example > will be replaced as
101
112
// <button-demo></button-demo>
102
- fileContent = fileContent . replace ( / m a t e r i a l - d o c s - e x a m p l e / g, this . exampleData . selectorName ) ;
113
+ fileContent = fileContent . replace ( / m a t e r i a l - d o c s - e x a m p l e / g, data . selectorName ) ;
103
114
} else if ( fileName == 'main.ts' ) {
104
115
// Replace the component name in `main.ts`.
105
116
// For example, `import {MaterialDocsExample} from 'material-docs-example'`
106
117
// will be replaced as `import {ButtonDemo} from './button-demo'`
107
- fileContent = fileContent . replace ( / M a t e r i a l D o c s E x a m p l e / g, this . exampleData . componentName ) ;
108
- fileContent = fileContent . replace ( / m a t e r i a l - d o c s - e x a m p l e / g, this . exampleData . indexFilename ) ;
118
+ fileContent = fileContent . replace ( / M a t e r i a l D o c s E x a m p l e / g, data . componentName ) ;
119
+ fileContent = fileContent . replace ( / m a t e r i a l - d o c s - e x a m p l e / g, data . indexFilename ) ;
109
120
}
110
121
return fileContent ;
111
122
}
0 commit comments