Skip to content

Commit

Permalink
Merge pull request #198 from dbrans/master
Browse files Browse the repository at this point in the history
Gantt chart - add minutes and seconds durations
  • Loading branch information
knsv committed Aug 29, 2015
2 parents 3ec7c6d + 1d371e0 commit 4ee0f9b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
42 changes: 24 additions & 18 deletions src/diagrams/gantt/ganttDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var getStartDate = function(prevTime, dateFormat, str){
}
return task.endTime;
}

// Check for actual date set
if(moment(str,dateFormat.trim(),true).isValid()){
return moment(str,dateFormat.trim(),true).toDate();
Expand All @@ -86,27 +86,33 @@ var getStartDate = function(prevTime, dateFormat, str){
log.debug('With date format:'+dateFormat.trim());
//log.debug('----');
}

// Default date - now
return new Date();
};

var getEndDate = function(prevTime, dateFormat, str){
str = str.trim();
// Check for actual date

// Check for actual date
if(moment(str,dateFormat.trim(),true).isValid()){

return moment(str,dateFormat.trim()).toDate();
}

var d = moment(prevTime);
// Check for length
var re = /^([\d]+)([wdh])/;
var re = /^([\d]+)([wdhms])/;
var durationStatement = re.exec(str.trim());

if(durationStatement!== null){
switch(durationStatement[2]){
case 's':
d.add(durationStatement[1], 'seconds');
break;
case 'm':
d.add(durationStatement[1], 'minutes');
break;
case 'h':
d.add(durationStatement[1], 'hours');
break;
Expand Down Expand Up @@ -144,21 +150,21 @@ var parseId = function(idStr){

var compileData = function(prevTask, dataStr){
var ds;

if(dataStr.substr(0,1) === ':'){
ds = dataStr.substr(1,dataStr.length);
}
else{
ds=dataStr;
}

var data = ds.split(',');


var task = {};
var df = exports.getDateFormat();


// Get tags like active, done cand crit
var matchFound = true;
while(matchFound){
Expand All @@ -167,7 +173,7 @@ var compileData = function(prevTask, dataStr){
task.active = true;
data.shift(1);
matchFound = true;

}
if(data[0].match(/^\s*done\s*$/)){
task.done = true;
Expand All @@ -184,8 +190,8 @@ var compileData = function(prevTask, dataStr){
for(i=0;i<data.length;i++){
data[i] = data[i].trim();
}


switch(data.length){
case 1:
task.id = parseId();
Expand All @@ -203,7 +209,7 @@ var compileData = function(prevTask, dataStr){
task.endTime = getEndDate(task.startTime, df, data[2]);
break;
default:

}

return task;
Expand Down Expand Up @@ -232,4 +238,4 @@ exports.addTask = function(descr,data){

exports.parseError = function(err,hash){
mermaidAPI.parseError(err,hash);
};
};
26 changes: 23 additions & 3 deletions src/diagrams/gantt/ganttDb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('when using the ganttDb',function() {
expect(tasks[0].id ).toEqual('id1');
expect(tasks[0].description).toEqual('test1');
});
it('should handle duration instead of fixed date to determine end date', function () {
it('should handle duration (days) instead of fixed date to determine end date', function () {
gDb.setDateFormat('YYYY-MM-DD');
gDb.addSection('testa1');
gDb.addTask('test1','id1,2013-01-01,2d');
Expand All @@ -37,7 +37,7 @@ describe('when using the ganttDb',function() {
expect(tasks[0].id ).toEqual('id1');
expect(tasks[0].description).toEqual('test1');
});
it('should handle duration instead of fixed date to determine end date', function () {
it('should handle duration (hours) instead of fixed date to determine end date', function () {
gDb.setDateFormat('YYYY-MM-DD');
gDb.addSection('testa1');
gDb.addTask('test1','id1,2013-01-01,2h');
Expand All @@ -47,7 +47,27 @@ describe('when using the ganttDb',function() {
expect(tasks[0].id ).toEqual('id1');
expect(tasks[0].description).toEqual('test1');
});
it('should handle ', function () {
it('should handle duration (minutes) instead of fixed date to determine end date', function () {
gDb.setDateFormat('YYYY-MM-DD');
gDb.addSection('testa1');
gDb.addTask('test1','id1,2013-01-01,2m');
var tasks = gDb.getTasks();
expect(tasks[0].startTime).toEqual(moment('2013-01-01', 'YYYY-MM-DD').toDate());
expect(tasks[0].endTime ).toEqual(moment('2013-01-01 00:02', 'YYYY-MM-DD hh:mm').toDate());
expect(tasks[0].id ).toEqual('id1');
expect(tasks[0].description).toEqual('test1');
});
it('should handle duration (seconds) instead of fixed date to determine end date', function () {
gDb.setDateFormat('YYYY-MM-DD');
gDb.addSection('testa1');
gDb.addTask('test1','id1,2013-01-01,2s');
var tasks = gDb.getTasks();
expect(tasks[0].startTime).toEqual(moment('2013-01-01', 'YYYY-MM-DD').toDate());
expect(tasks[0].endTime ).toEqual(moment('2013-01-01 00:00:02', 'YYYY-MM-DD hh:mm:ss').toDate());
expect(tasks[0].id ).toEqual('id1');
expect(tasks[0].description).toEqual('test1');
});
it('should handle duration (weeks) instead of fixed date to determine end date', function () {
gDb.setDateFormat('YYYY-MM-DD');
gDb.addSection('testa1');
gDb.addTask('test1','id1,2013-01-01,2w');
Expand Down

0 comments on commit 4ee0f9b

Please sign in to comment.