chore(generators): update tabs

This commit is contained in:
Tim Lancina
2016-01-07 18:55:03 -06:00
parent ef6cd74cd4
commit adb060dfff
3 changed files with 82 additions and 91 deletions

View File

@ -1,94 +1,95 @@
var colors = require('colors'),
fs = require('fs'),
path = require('path'),
var path = require('path'),
inquirer = require('inquirer'),
Q = require('q'),
Generator = module.exports,
Generate = require('../../generate'),
path = require('path'),
Q = require('q');
Generator = require('../../generator'),
Generate = require('../../generate');
Generator.validate = function(input) {
// console.log(typeof parseInt(input));
module.exports = TabsGenerator;
function TabsGenerator(options) {
Generator.call(this, options);
this.directory = 'pages';
this.jsClassName += 'Page';
this.tabs = [];
this.tabNames = [];
}
TabsGenerator.prototype = Object.create(Generator.prototype);
TabsGenerator.prototype.run = function(){
return tabCountPrompt()
.then(function(numTabs){
var promise = Q();
for (var i = 0, j = parseInt(numTabs); i < j; i++) {
promise = promise.then(function(index) {
return tabNamePrompt.bind(this)(index);
}.bind(this, i));
}
return promise;
}.bind(this))
.then(function(){
var PageGenerator = Generate.loadGenerator('page');
this.tabNames.forEach(function(tabName){
var pageGenerator = new PageGenerator({
name: tabName,
generator: 'page',
appDirectory: this.appDirectory
});
pageGenerator.run();
this.tabs.push(pageGenerator);
}.bind(this));
this.makeDirectories();
this.renderTemplates();
}.bind(this))
.catch(function(err){
console.error(err.stack);
})
}
function tabCountPrompt(){
var q = Q.defer();
inquirer.prompt({
choices: ['1', '2', '3', '4', '5'],
message: 'How many tabs would you like?',
name: 'count',
type: 'list',
validate: validate
}, function(result) {
q.resolve(result.count);
});
return q.promise;
}
function tabNamePrompt(index){
var q = Q.defer();
var numberNames = ['first', 'second', 'third', 'fourth', 'fifth'];
inquirer.prompt({
message: 'Enter the ' + numberNames[index] + ' tab name:',
name: 'name',
type: 'input'
}, function(response) {
this.tabNames.push(response.name);
q.resolve();
}.bind(this));
return q.promise;
}
function validate(){
if (isNaN(parseInt(input))) {
// Pass the return value in the done callback
return 'You need to provide a number';
}
return true;
};
Generator.numberNames = ['first', 'second', 'third', 'fourth', 'fifth'];
Generator.promptForTabCount = function promptForTabCount() {
var q = Q.defer();
inquirer.prompt({choices: ['1', '2', '3', '4', '5'], message: 'How many tabs will you have?', name: 'count', type: 'list', validate: Generator.validate}, function(result) {
q.resolve(result.count);
});
return q.promise;
};
Generator.promptForTabName = function promptForTabName(tabIndex, options) {
var q = Q.defer();
inquirer.prompt({message: 'Enter the ' + Generator.numberNames[tabIndex] + ' tab name:', name: 'name', type: 'input'}, function(nameResult) {
Generator.tabs.push({ appDirectory: options.appDirectory, cssClassName: Generate.cssClassName(nameResult.name), fileName: Generate.fileName(nameResult.name), jsClassName: Generate.jsClassName(nameResult.name), name: nameResult.name });
q.resolve();
});
return q.promise;
}
Generator.run = function run(options) {
Generate.createScaffoldDirectories({appDirectory: options.appDirectory, componentDirectory: 'tabs', fileName: options.fileName});
//Need to query user for tabs:
options.rootDirectory = options.rootDirectory || path.join('app', 'tabs');
var savePath = path.join(options.appDirectory, options.rootDirectory, options.fileName);
Generator.tabs = [];
return Generator.promptForTabCount()
.then(function(count) {
var promise = Q();
for(var i = 0, j = parseInt(count); i < j; i++) {
(function(index) {
promise = promise.then(function() {
return Generator.promptForTabName(index, options);
});
})(i);//avoid closure loop var
}
return promise;
})
.then(function() {
var templates = Generate.loadGeneratorTemplates(__dirname);
//Generate the tabs container page templates
templates.forEach(function(template) {
options.templatePath = template.file;
options.tabs = Generator.tabs;
var renderedTemplate = Generate.renderTemplateFromFile(options);
var saveFilePath = path.join(savePath, [options.fileName, template.type].join(''));
// console.log('renderedTemplate', renderedTemplate, 'saving to', saveFilePath);
console.log('√ Create'.blue, path.relative(options.appDirectory, saveFilePath));
fs.writeFileSync(saveFilePath, renderedTemplate);
});
//Now render the individual tab pages
Generator.tabs.forEach(function(tab) {
tab.generatorName = 'page';
tab.appDirectory = tab.appDirectory;
Generate.generate(tab);
});
})
.catch(function(ex) {
console.log('Something went wrong', ex);
console.log(ex.stack);
throw ex;
})
.fin(function() {
console.log('√ Done'.green);
});
};

View File

@ -1,3 +1,5 @@
<ion-tabs><% _.forEach(tabs, function(tab) { %>
<ion-tab tab-title="<%= tab.name %>" tab-icon="globe" [root]="<%= tab.fileName %>"></ion-tab><% }); %>
<ion-tabs>
<% _.forEach(tabs, function(tab, i) { %>
<ion-tab [root]="tab<%= ++i %>Root" tab-title="<%= tab.name %>" tab-icon="star"></ion-tab>
<% }); %>
</ion-tabs>

View File

@ -1,15 +1,3 @@
import {NavController, Page} from 'ionic/ionic';
<% _.forEach(tabs, function(tab) { %>import {<%= tab.jsClassName %>} from '../../<%= tab.fileName %>/<%= tab.fileName %>';
<% }); %>
@Page({
templateUrl: 'app/<%= fileName %>/<%= fileName %>.html'
})
export class <%= jsClassName %> {
constructor(nav: NavController) {
// set the root pages for each tab
<% _.forEach(tabs, function(tab) { %>this.<%= tab.fileName %> = <%= tab.jsClassName %>;
<% }); %>
}
}