@@ -223,17 +223,87 @@ export default {
223
223
* @description imports child components into <script>
224
224
*/
225
225
writeScript (componentName , children ) {
226
- let str = ' '
226
+ // let str = ''
227
+ // children.forEach(name => {
228
+ // str += `import ${name} from '@/components/${name}.vue';\n`
229
+ // })
230
+ // let childrenComponentNames = ''
231
+ // children.forEach(name => {
232
+ // childrenComponentNames += `\t\t${name},\n`
233
+ // })
234
+ // return `\n\n<script>\n${str}\nexport default {\n\tname: '${componentName}'
235
+ // ,\n\tcomponents: {\n${childrenComponentNames}\t}\n};\n<\/script>`
236
+ // },
237
+ // add import mapstate and mapactions if they exist
238
+ let imports = ' '
239
+ if (this .activeComponentObj .actions .length || this .activeComponentObj .state .length ){
240
+ imports += ' import { '
241
+ if (this .activeComponentObj .actions .length && this .activeComponentObj .state .length ) {
242
+ imports += ' mapState, mapActions'
243
+ }
244
+ else if (this .activeComponentObj .state .length ) imports += ' mapState'
245
+ else imports += ' mapActions'
246
+ imports += ' } from "vuex"\n '
247
+ }
248
+
249
+ // add imports for children
227
250
children .forEach (name => {
228
- str += ` import ${ name} from '@/components/${ name} .vue';\n `
251
+ imports += ` import ${ name} from '@/components/${ name} .vue';\n `
229
252
})
253
+
254
+ // add components section
230
255
let childrenComponentNames = ' '
231
256
children .forEach (name => {
232
- childrenComponentNames += ` \t\t ${ name} ,\n `
257
+ childrenComponentNames += ` ${ name} ,\n `
233
258
})
234
- return ` \n\n <script>\n ${ str} \n export default {\n\t name: '${ componentName} ',\n\t components: {\n ${ childrenComponentNames} \t }\n };\n <\/ script>`
235
- },
236
259
260
+ // if true add data section and populate with props
261
+ let data = ' '
262
+ if (this .activeComponentObj .props .length ){
263
+ data += ' data () {\n return {'
264
+ this .activeComponentObj .props .forEach (prop => {
265
+ data += ` \n ${ prop} : "PLACEHOLDER FOR VALUE",`
266
+ })
267
+ data += ' \n '
268
+ data += ' }\n '
269
+ data += ' },\n '
270
+ }
271
+
272
+ // if true add computed section and populate with state
273
+ let computed = ' '
274
+ if (this .activeComponentObj .state .length ){
275
+ computed += ' computed: {'
276
+ computed += ' \n ...mapState(['
277
+ this .activeComponentObj .state .forEach ((state ) => {
278
+ computed += ` \n "${ state} ",`
279
+ })
280
+ computed += ' \n ]),\n '
281
+ computed += ' },\n '
282
+ }
283
+
284
+ // if true add methods section and populate with actions
285
+ let methods = ' '
286
+ if (this .activeComponentObj .actions .length ){
287
+ methods += ' methods: {'
288
+ methods += ' \n ...mapActions(['
289
+ this .activeComponentObj .actions .forEach ((action ) => {
290
+ methods += ` \n "${ action} ",`
291
+ })
292
+ methods += ' \n ]),\n '
293
+ methods += ' },\n '
294
+ }
295
+
296
+ // concat all code within script tags
297
+ let output = ' \n\n <script>\n '
298
+ output += imports + ' \n export default {\n name: ' + componentName
299
+ output += ' ,\n components: {\n '
300
+ output += childrenComponentNames + ' },\n '
301
+ output += data
302
+ output += computed
303
+ output += methods
304
+ output += ' };\n <\/ script>'
305
+ return output
306
+ },
237
307
/**
238
308
* @description writes the <style> in vue component
239
309
* if component is 'App', writes css, else returns <style scoped>
@@ -402,7 +472,7 @@ export default {
402
472
}
403
473
},
404
474
computed: {
405
- ... mapState ([' componentMap' , ' imagePath' ])
475
+ ... mapState ([' componentMap' , ' imagePath' , ' activeComponentObj ' ])
406
476
}
407
477
}
408
478
0 commit comments