@@ -34,58 +34,67 @@ const TAGS: string[] = ['angular', 'material', 'example'];
3434 */
3535@Injectable ( )
3636export class PlunkerWriter {
37- form : HTMLFormElement ;
38- exampleData : ExampleData ;
39-
4037 constructor ( private _http : Http ) { }
4138
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 ( ) ;
4745
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 ) ;
5149
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 ) ) ;
5453
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 ) ) ;
5856
59- Promise . all ( templateContents . concat ( exampleContents ) ) . then ( ( _ ) => this . form . submit ( ) ) ;
57+ Promise . all ( templateContents . concat ( exampleContents ) ) . then ( ( ) => {
58+ resolve ( form ) ;
59+ } ) ;
60+ } ) ;
6061 }
6162
63+ /** Constructs a new form element that will navigate to the plunker url. */
6264 _createFormElement ( ) : HTMLFormElement {
63- var form = document . createElement ( 'form' ) ;
65+ const form = document . createElement ( 'form' ) ;
6466 form . action = PLUNKER_URL ;
6567 form . method = 'post' ;
6668 form . target = '_blank' ;
6769 return form ;
6870 }
6971
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' ) ;
7275 input . type = 'hidden' ;
7376 input . name = name ;
7477 input . value = value ;
75- this . form . appendChild ( input ) ;
78+ form . appendChild ( input ) ;
7679 }
7780
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 ) ,
8185 error => console . log ( error ) ) ;
8286 }
8387
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 ) {
8594 if ( path == TEMPLATE_PATH ) {
86- content = this . _replaceExamplePlaceholderNames ( filename , content ) ;
95+ content = this . _replaceExamplePlaceholderNames ( data , filename , content ) ;
8796 }
88- this . _createFormInput ( `files[${ filename } ]` , this . _appendCopyright ( filename , content ) ) ;
97+ this . _appendFormInput ( form , `files[${ filename } ]` , this . _appendCopyright ( filename , content ) ) ;
8998 }
9099
91100 /**
@@ -94,18 +103,20 @@ export class PlunkerWriter {
94103 * This will replace those placeholders with the names from the example metadata,
95104 * e.g. "<basic-button-example>" and "BasicButtonExample"
96105 */
97- _replaceExamplePlaceholderNames ( fileName : string , fileContent : string ) : string {
106+ _replaceExamplePlaceholderNames ( data : ExampleData ,
107+ fileName : string ,
108+ fileContent : string ) : string {
98109 if ( fileName == 'index.html' ) {
99110 // 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
101112 // <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 ) ;
103114 } else if ( fileName == 'main.ts' ) {
104115 // Replace the component name in `main.ts`.
105116 // For example, `import {MaterialDocsExample} from 'material-docs-example'`
106117 // 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 ) ;
109120 }
110121 return fileContent ;
111122 }
0 commit comments