Skip to content

Commit a6f02b9

Browse files
committed
feat: generate w.format, more chart types
1 parent 3065fcf commit a6f02b9

File tree

7 files changed

+61
-19
lines changed

7 files changed

+61
-19
lines changed

generators/generator-widget/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ npm install
3838
#### Todo
3939
---
4040

41-
- w.format function from chart selection
4241
- selection of widgets-settings
42+
- completion message
43+
- "dont forget to add impac-angular module declaration"
44+
- define default data values & options for charts so upon generation, charts are displayable!
45+
- specs
4346

4447
#### Notes
4548
---

generators/generator-widget/generators/app/index.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,24 @@ var inquirer = require('inquirer');
77
var ejs = require('ejs');
88
var beautify = require('js-beautify');
99
var fs = require('fs');
10-
var fsHelpers = require('./src/fs-helpers.js');
1110

1211
module.exports = yeoman.generators.Base.extend({
1312
initializing: function () {
13+
// TODO: refactor value / type keys, chart partials are all the same. Keep in mind different charting lib support that may occur in the future.
1414
this.charts = [
1515
new inquirer.Separator(),
16-
{ id: 1, name: 'Pie Chart', value: 'pie', type: 'graph' },
17-
{ id: 2, name: 'Line Chart', value: 'line', type: 'graph' },
16+
{ name: 'Pie Chart', value: 'pie', type: 'graph', defaultInputData: [] },
17+
{ name: 'Line Chart', value: 'line', type: 'graph', defaultInputData: [] },
18+
{ name: 'Bar Chart', value: 'bar', type: 'graph', defaultInputData: {} },
19+
{ name: 'Combined Bar Chart', value: 'combined-bar', type: 'graph', defaultInputData: {} },
1820
new inquirer.Separator(),
19-
{ id: 3, name: 'Blank - content only / custom', value: 'blank' },
21+
{ name: 'Blank - content only / custom', value: 'blank' },
2022
new inquirer.Separator(),
2123
];
2224
this.chartAddOns = [
23-
{ value: 'legend', name: 'Legend', checked: false }
25+
{ name: 'Legend', value: 'legend', checked: false }
2426
];
27+
// TODO: add filtering capability.
2528
this.buildPartialsPaths = function (options) {
2629
var templatePath = this.templatePath(), path;
2730
return _.compact(_.map(options, function (option) {
@@ -82,8 +85,11 @@ module.exports = yeoman.generators.Base.extend({
8285
this.prompt(prompts, function (props) {
8386
// Array of selections made after prompting.
8487
this.props = props;
85-
this.chartType = _.find(charts, { value: props.chartName }).type;
88+
89+
this.selectedChart = _.find(charts, { value: props.chartName });
90+
this.chartType = this.selectedChart.type;
8691
this.componentNames = this.buildComponentNames();
92+
this.hasChart = this.chartType === 'graph';
8793

8894
done();
8995
}.bind(this));
@@ -101,9 +107,8 @@ module.exports = yeoman.generators.Base.extend({
101107
return prop;
102108
})))
103109
};
104-
if (this.chartType === 'graph') {
105-
data.chartContainerClass = 'chart-container';
106-
}
110+
111+
if (this.hasChart) data.chartContainerClass = 'chart-container';
107112

108113
path = this.destinationPath(this.buildDestinationPath('.tmpl.html')),
109114

@@ -117,14 +122,20 @@ module.exports = yeoman.generators.Base.extend({
117122
},
118123
// Write task for generating the new widgets directive component file.
119124
widgetDirective: function () {
120-
var html, template, data, settingsPromises, path;
125+
var html, template, data, deps, settingsPromises, path;
121126

127+
deps = ['$scope', '$q'];
122128
settingsPromises = [];
123-
if (this.chartType === 'graph') settingsPromises.push('chart');
129+
130+
if (this.hasChart) settingsPromises.push('chart'); deps.push('ChartFormatterSvc');
124131

125132
data = {
126133
componentNames: this.componentNames,
127-
settingsPromises: settingsPromises
134+
deps: deps,
135+
settingsPromises: settingsPromises,
136+
hasChart: this.hasChart,
137+
chartName: _.camelCase(this.selectedChart.value),
138+
defaultInputData: this.selectedChart.defaultInputData
128139
};
129140

130141
path = this.destinationPath(this.buildDestinationPath('.directive.coffee'));
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
<div impac-chart draw-trigger="::drawTrigger.promise" deferred="::chartDeferred"></div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
<h3>Hello World! - From Impac! Widget Generator.</h3>
12
<p>Add your widget content here.</p>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
<div impac-chart draw-trigger="::drawTrigger.promise" deferred="::chartDeferred"></div>

generators/generator-widget/generators/app/templates/widget-component/widget-component.directive.coffee

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
1+
#
2+
# Component generated by Impac! Widget Generator!
3+
#
14
module = angular.module('impac.components.widgets.<%= data.componentNames.mod %>', [])
2-
module.controller('Widget<%= data.componentNames.ctrl %>Ctrl', ($scope, $q) ->
5+
module.controller('Widget<%= data.componentNames.ctrl %>Ctrl', (<%= data.deps %>) ->
36

47
w = $scope.widget
58

69
# Define settings
710
# --------------------------------------
8-
<%_ data.settingsPromises.forEach(function (name) { _%>
11+
<%_ data.settingsPromises.forEach(function (name) { _%>
912
$scope.<%= name + 'Deferred' %> = $q.defer();
10-
<%_ }) _%>
13+
<%_ }) _%>
1114

1215
settingsPromises = [
13-
<%_ data.settingsPromises.forEach(function (name) { _%>
16+
<%_ data.settingsPromises.forEach(function (name) { _%>
1417
$scope.<%= name + 'Deferred' %>.promise,
15-
<%_ }) _%>
18+
<%_ }) _%>
1619
]
1720

1821
# Widget specific methods
1922
# --------------------------------------
2023
w.initContext = ->
2124
$scope.isDataFound = w.content?
2225

26+
<%_ if (data.hasChart) { %>
2327
# Chart formating function
2428
# --------------------------------------
29+
$scope.drawTrigger = $q.defer()
30+
# Format the widget content data for ChartJS.
31+
w.format = ->
32+
if $scope.isDataFound
33+
# Data displayed in the chart.
34+
inputData = <%= data.defaultInputData %>
35+
<% if (data.chartName === 'bar') { %>
36+
# maximum capacity for chartjs bar-chart
37+
inputData.labels.length = 15 if inputData.labels.length > 15
38+
inputData.values.length = 15 if inputData.values.length > 15
39+
<% } %>
40+
# Options for configuring the ChartFormatterSvc formatting & the chartJs lib itself.
41+
options = {}
42+
# Standard formatting required dependant on chart type.
43+
chartData = ChartFormatterSvc.<%= data.chartName %>Chart(inputData, options)
44+
# Notifies the impac-chart directive's drawTrigger promise of data format complete,
45+
# which then draws the chart.
46+
$scope.drawTrigger.notify(chartData)
47+
<% } %>
2548

2649
# Widget is ready: can trigger the "wait for settings to be ready"
2750
# --------------------------------------

generators/generator-widget/generators/app/templates/widget-component/widget-component.tmpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
Template generated by Impac! Widget Generator!
2+
Component generated by Impac! Widget Generator!
33
-->
44
<div widget-<%= componentNames.mod %>>
55
<!-- Settings Panel -->

0 commit comments

Comments
 (0)