From 7834cf3df7a7465e7d6abd017522ffe93031c99f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Craveiro?= Date: Sun, 21 Sep 2014 16:43:10 +0100 Subject: [PATCH 1/4] #17 - Support ongoing items, improve range typography. If the end date is 'present', the bubble will extend to the end of the last year and the label will display an open range (with the correct em-dash). Also changed the closed range (e.g. 2001-2004) to use the correct dash (en). --- dist/timesheet.js | 2 +- source/javascripts/timesheet.bubble.js | 7 +++++-- source/javascripts/timesheet.js | 5 ++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/dist/timesheet.js b/dist/timesheet.js index 17b13b2..7a5b929 100644 --- a/dist/timesheet.js +++ b/dist/timesheet.js @@ -1 +1 @@ -!function(){"use strict";var Bubble=function(wMonth,min,start,end){this.min=min,this.start=start,this.end=end,this.widthMonth=wMonth};Bubble.prototype.formatMonth=function(num){return num=parseInt(num,10),num>=10?num:"0"+num},Bubble.prototype.getStartOffset=function(){return this.widthMonth/12*(12*(this.start.getFullYear()-this.min)+this.start.getMonth())},Bubble.prototype.getFullYears=function(){return(this.end&&this.end.getFullYear()||this.start.getFullYear())-this.start.getFullYear()},Bubble.prototype.getMonths=function(){var fullYears=this.getFullYears(),months=0;return this.end?this.end.hasMonth?(months+=this.end.getMonth()+1,months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1)):(months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1>0?fullYears-1:0)):months+=this.start.hasMonth?1:12,months},Bubble.prototype.getWidth=function(){return this.widthMonth/12*this.getMonths()},Bubble.prototype.getDateLabel=function(){return[(this.start.hasMonth?this.formatMonth(this.start.getMonth()+1)+"/":"")+this.start.getFullYear(),this.end?"-"+((this.end.hasMonth?this.formatMonth(this.end.getMonth()+1)+"/":"")+this.end.getFullYear()):""].join("")},window.TimesheetBubble=Bubble}(),function(){"use strict";var Timesheet=function(container,min,max,data){this.data=[],this.year={min:min,max:max},this.parse(data||[]),"undefined"!=typeof document&&(this.container="string"==typeof container?document.querySelector("#"+container):container,this.drawSections(),this.insertData())};Timesheet.prototype.insertData=function(){for(var html=[],widthMonth=this.container.querySelector(".scale section").offsetWidth,n=0,m=this.data.length;m>n;n++){var cur=this.data[n],bubble=new TimesheetBubble(widthMonth,this.year.min,cur.start,cur.end),line=['',''+bubble.getDateLabel()+" ",''+cur.label+""].join("");html.push("
  • "+line+"
  • ")}this.container.innerHTML+='"},Timesheet.prototype.drawSections=function(){for(var html=[],c=this.year.min;c<=this.year.max;c++)html.push("
    "+c+"
    ");this.container.className="timesheet color-scheme-default",this.container.innerHTML='
    '+html.join("")+"
    "},Timesheet.prototype.parseDate=function(date){return-1===date.indexOf("/")?(date=new Date(parseInt(date,10),0,1),date.hasMonth=!1):(date=date.split("/"),date=new Date(parseInt(date[1],10),parseInt(date[0],10)-1,1),date.hasMonth=!0),date},Timesheet.prototype.parse=function(data){for(var n=0,m=data.length;m>n;n++){var beg=this.parseDate(data[n][0]),end=4===data[n].length?this.parseDate(data[n][1]):null,lbl=4===data[n].length?data[n][2]:data[n][1],cat=data[n][3]||"default";beg.getFullYear()this.year.max?this.year.max=end.getFullYear():beg.getFullYear()>this.year.max&&(this.year.max=beg.getFullYear()),this.data.push({start:beg,end:end,label:lbl,type:cat})}},window.Timesheet=Timesheet}(); \ No newline at end of file +!function(){"use strict";var Bubble=function(wMonth,min,start,end,endless){this.min=min,this.start=start,this.end=end,this.endless=endless,this.widthMonth=wMonth};Bubble.prototype.formatMonth=function(num){return num=parseInt(num,10),num>=10?num:"0"+num},Bubble.prototype.getStartOffset=function(){return this.widthMonth/12*(12*(this.start.getFullYear()-this.min)+this.start.getMonth())},Bubble.prototype.getFullYears=function(){return(this.end&&this.end.getFullYear()||this.start.getFullYear())-this.start.getFullYear()},Bubble.prototype.getMonths=function(){var fullYears=this.getFullYears(),months=0;return this.end?this.end.hasMonth?(months+=this.end.getMonth()+1,months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1)):(months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1>0?fullYears-1:0)):months+=this.start.hasMonth?1:12,months},Bubble.prototype.getWidth=function(){return this.widthMonth/12*this.getMonths()},Bubble.prototype.getDateLabel=function(){return[(this.start.hasMonth?this.formatMonth(this.start.getMonth()+1)+"/":"")+this.start.getFullYear(),this.endless?"—":this.end?"–"+((this.end.hasMonth?this.formatMonth(this.end.getMonth()+1)+"/":"")+this.end.getFullYear()):""].join("")},window.TimesheetBubble=Bubble}(),function(){"use strict";var Timesheet=function(container,min,max,data){this.data=[],this.year={min:min,max:max},this.parse(data||[]),"undefined"!=typeof document&&(this.container="string"==typeof container?document.querySelector("#"+container):container,this.drawSections(),this.insertData())};Timesheet.prototype.insertData=function(){for(var html=[],widthMonth=this.container.querySelector(".scale section").offsetWidth,n=0,m=this.data.length;m>n;n++){var cur=this.data[n],bubble=new TimesheetBubble(widthMonth,this.year.min,cur.start,cur.end?cur.end:new Date(this.year.max+1,0,1),cur.end?!1:!0),line=['',''+bubble.getDateLabel()+" ",''+cur.label+""].join("");html.push("
  • "+line+"
  • ")}this.container.innerHTML+='"},Timesheet.prototype.drawSections=function(){for(var html=[],c=this.year.min;c<=this.year.max;c++)html.push("
    "+c+"
    ");this.container.className="timesheet color-scheme-default",this.container.innerHTML='
    '+html.join("")+"
    "},Timesheet.prototype.parseDate=function(date){return"present"===date?null:(-1===date.indexOf("/")?(date=new Date(parseInt(date,10),0,1),date.hasMonth=!1):(date=date.split("/"),date=new Date(parseInt(date[1],10),parseInt(date[0],10)-1,1),date.hasMonth=!0),date)},Timesheet.prototype.parse=function(data){for(var n=0,m=data.length;m>n;n++){var beg=this.parseDate(data[n][0]),end=4===data[n].length?this.parseDate(data[n][1]):null,lbl=4===data[n].length?data[n][2]:data[n][1],cat=data[n][3]||"default";beg.getFullYear()this.year.max?this.year.max=end.getFullYear():beg.getFullYear()>this.year.max&&(this.year.max=beg.getFullYear()),this.data.push({start:beg,end:end,label:lbl,type:cat})}},window.Timesheet=Timesheet}(); \ No newline at end of file diff --git a/source/javascripts/timesheet.bubble.js b/source/javascripts/timesheet.bubble.js index 98d3af8..2885e6f 100644 --- a/source/javascripts/timesheet.bubble.js +++ b/source/javascripts/timesheet.bubble.js @@ -4,10 +4,11 @@ /** * Timesheet Bubble */ - var Bubble = function(wMonth, min, start, end) { + var Bubble = function(wMonth, min, start, end, endless) { this.min = min; this.start = start; this.end = end; + this.endless = endless; this.widthMonth = wMonth; }; @@ -70,7 +71,9 @@ Bubble.prototype.getDateLabel = function() { return [ (this.start.hasMonth ? this.formatMonth(this.start.getMonth() + 1) + '/' : '' ) + this.start.getFullYear(), - (this.end ? '-' + ((this.end.hasMonth ? this.formatMonth(this.end.getMonth() + 1) + '/' : '' ) + this.end.getFullYear()) : '') + (this.endless ? '—' : + (this.end ? '–' + ((this.end.hasMonth ? this.formatMonth(this.end.getMonth() + 1) + '/' : '' ) + this.end.getFullYear()) : '') + ) ].join(''); }; diff --git a/source/javascripts/timesheet.js b/source/javascripts/timesheet.js index 3b79f15..0a7d773 100644 --- a/source/javascripts/timesheet.js +++ b/source/javascripts/timesheet.js @@ -33,7 +33,7 @@ for (var n = 0, m = this.data.length; n < m; n++) { var cur = this.data[n]; - var bubble = new TimesheetBubble(widthMonth, this.year.min, cur.start, cur.end); + var bubble = new TimesheetBubble(widthMonth, this.year.min, cur.start, cur.end ? cur.end : new Date(this.year.max+1, 0, 1), cur.end ? false : true); var line = [ '', @@ -65,6 +65,9 @@ * Parse data string */ Timesheet.prototype.parseDate = function(date) { + if (date === 'present') { + return null; + } if (date.indexOf('/') === -1) { date = new Date(parseInt(date, 10), 0, 1); date.hasMonth = false; From c3da8631287cb1ea1cc0b38f7897b77c541ea595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Craveiro?= Date: Sun, 21 Sep 2014 17:11:54 +0100 Subject: [PATCH 2/4] #17 - Improve implementation, so as not to break previous behavior for timelines with ommitted end date --- dist/timesheet.js | 2 +- source/javascripts/timesheet.bubble.js | 13 ++++++++----- source/javascripts/timesheet.js | 6 +++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/dist/timesheet.js b/dist/timesheet.js index 7a5b929..94e50e6 100644 --- a/dist/timesheet.js +++ b/dist/timesheet.js @@ -1 +1 @@ -!function(){"use strict";var Bubble=function(wMonth,min,start,end,endless){this.min=min,this.start=start,this.end=end,this.endless=endless,this.widthMonth=wMonth};Bubble.prototype.formatMonth=function(num){return num=parseInt(num,10),num>=10?num:"0"+num},Bubble.prototype.getStartOffset=function(){return this.widthMonth/12*(12*(this.start.getFullYear()-this.min)+this.start.getMonth())},Bubble.prototype.getFullYears=function(){return(this.end&&this.end.getFullYear()||this.start.getFullYear())-this.start.getFullYear()},Bubble.prototype.getMonths=function(){var fullYears=this.getFullYears(),months=0;return this.end?this.end.hasMonth?(months+=this.end.getMonth()+1,months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1)):(months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1>0?fullYears-1:0)):months+=this.start.hasMonth?1:12,months},Bubble.prototype.getWidth=function(){return this.widthMonth/12*this.getMonths()},Bubble.prototype.getDateLabel=function(){return[(this.start.hasMonth?this.formatMonth(this.start.getMonth()+1)+"/":"")+this.start.getFullYear(),this.endless?"—":this.end?"–"+((this.end.hasMonth?this.formatMonth(this.end.getMonth()+1)+"/":"")+this.end.getFullYear()):""].join("")},window.TimesheetBubble=Bubble}(),function(){"use strict";var Timesheet=function(container,min,max,data){this.data=[],this.year={min:min,max:max},this.parse(data||[]),"undefined"!=typeof document&&(this.container="string"==typeof container?document.querySelector("#"+container):container,this.drawSections(),this.insertData())};Timesheet.prototype.insertData=function(){for(var html=[],widthMonth=this.container.querySelector(".scale section").offsetWidth,n=0,m=this.data.length;m>n;n++){var cur=this.data[n],bubble=new TimesheetBubble(widthMonth,this.year.min,cur.start,cur.end?cur.end:new Date(this.year.max+1,0,1),cur.end?!1:!0),line=['',''+bubble.getDateLabel()+" ",''+cur.label+""].join("");html.push("
  • "+line+"
  • ")}this.container.innerHTML+='
      '+html.join("")+"
    "},Timesheet.prototype.drawSections=function(){for(var html=[],c=this.year.min;c<=this.year.max;c++)html.push("
    "+c+"
    ");this.container.className="timesheet color-scheme-default",this.container.innerHTML='
    '+html.join("")+"
    "},Timesheet.prototype.parseDate=function(date){return"present"===date?null:(-1===date.indexOf("/")?(date=new Date(parseInt(date,10),0,1),date.hasMonth=!1):(date=date.split("/"),date=new Date(parseInt(date[1],10),parseInt(date[0],10)-1,1),date.hasMonth=!0),date)},Timesheet.prototype.parse=function(data){for(var n=0,m=data.length;m>n;n++){var beg=this.parseDate(data[n][0]),end=4===data[n].length?this.parseDate(data[n][1]):null,lbl=4===data[n].length?data[n][2]:data[n][1],cat=data[n][3]||"default";beg.getFullYear()this.year.max?this.year.max=end.getFullYear():beg.getFullYear()>this.year.max&&(this.year.max=beg.getFullYear()),this.data.push({start:beg,end:end,label:lbl,type:cat})}},window.Timesheet=Timesheet}(); \ No newline at end of file +!function(){"use strict";var Bubble=function(wMonth,min,start,end){this.min=min,this.start=start,this.end=end,this.end&&this.end.getFullYear()&&this.end.getFullYear()>9999&&(this.endless=!0,this.end=new Date(end.getFullYear()-9999,this.end.getMonth(),1)),this.widthMonth=wMonth};Bubble.prototype.formatMonth=function(num){return num=parseInt(num,10),num>=10?num:"0"+num},Bubble.prototype.getStartOffset=function(){return this.widthMonth/12*(12*(this.start.getFullYear()-this.min)+this.start.getMonth())},Bubble.prototype.getFullYears=function(){return(this.end&&this.end.getFullYear()||this.start.getFullYear())-this.start.getFullYear()},Bubble.prototype.getMonths=function(){var fullYears=this.getFullYears(),months=0;return this.end?this.end.hasMonth?(months+=this.end.getMonth()+1,months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1)):(months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1>0?fullYears-1:0)):months+=this.start.hasMonth?1:12,months},Bubble.prototype.getWidth=function(){return this.widthMonth/12*this.getMonths()},Bubble.prototype.getDateLabel=function(){return[(this.start.hasMonth?this.formatMonth(this.start.getMonth()+1)+"/":"")+this.start.getFullYear(),this.end?this.endless?"—":"–"+((this.end.hasMonth?this.formatMonth(this.end.getMonth()+1)+"/":"")+this.end.getFullYear()):""].join("")},window.TimesheetBubble=Bubble}(),function(){"use strict";var Timesheet=function(container,min,max,data){this.data=[],this.year={min:min,max:max},this.parse(data||[]),"undefined"!=typeof document&&(this.container="string"==typeof container?document.querySelector("#"+container):container,this.drawSections(),this.insertData())};Timesheet.prototype.insertData=function(){for(var html=[],widthMonth=this.container.querySelector(".scale section").offsetWidth,n=0,m=this.data.length;m>n;n++){var cur=this.data[n],bubble=new TimesheetBubble(widthMonth,this.year.min,cur.start,cur.end),line=['',''+bubble.getDateLabel()+" ",''+cur.label+""].join("");html.push("
  • "+line+"
  • ")}this.container.innerHTML+='
      '+html.join("")+"
    "},Timesheet.prototype.drawSections=function(){for(var html=[],c=this.year.min;c<=this.year.max;c++)html.push("
    "+c+"
    ");this.container.className="timesheet color-scheme-default",this.container.innerHTML='
    '+html.join("")+"
    "},Timesheet.prototype.parseDate=function(date){return"present"===date?new Date(1e4+this.year.max,0,1):(-1===date.indexOf("/")?(date=new Date(parseInt(date,10),0,1),date.hasMonth=!1):(date=date.split("/"),date=new Date(parseInt(date[1],10),parseInt(date[0],10)-1,1),date.hasMonth=!0),date)},Timesheet.prototype.parse=function(data){for(var n=0,m=data.length;m>n;n++){var beg=this.parseDate(data[n][0]),end=4===data[n].length?this.parseDate(data[n][1]):null,lbl=4===data[n].length?data[n][2]:data[n][1],cat=data[n][3]||"default";beg.getFullYear()this.year.max?this.year.max=end.getFullYear():beg.getFullYear()>this.year.max&&(this.year.max=beg.getFullYear()),this.data.push({start:beg,end:end,label:lbl,type:cat})}},window.Timesheet=Timesheet}(); \ No newline at end of file diff --git a/source/javascripts/timesheet.bubble.js b/source/javascripts/timesheet.bubble.js index 2885e6f..ef56d18 100644 --- a/source/javascripts/timesheet.bubble.js +++ b/source/javascripts/timesheet.bubble.js @@ -4,11 +4,14 @@ /** * Timesheet Bubble */ - var Bubble = function(wMonth, min, start, end, endless) { + var Bubble = function(wMonth, min, start, end) { this.min = min; this.start = start; this.end = end; - this.endless = endless; + if (this.end && this.end.getFullYear() && this.end.getFullYear() > 9999) { + this.endless = true; + this.end = new Date(end.getFullYear()-9999, this.end.getMonth(), 1); + } this.widthMonth = wMonth; }; @@ -71,9 +74,9 @@ Bubble.prototype.getDateLabel = function() { return [ (this.start.hasMonth ? this.formatMonth(this.start.getMonth() + 1) + '/' : '' ) + this.start.getFullYear(), - (this.endless ? '—' : - (this.end ? '–' + ((this.end.hasMonth ? this.formatMonth(this.end.getMonth() + 1) + '/' : '' ) + this.end.getFullYear()) : '') - ) + (this.end + ? (this.endless ? '—' : '–' + ((this.end.hasMonth ? this.formatMonth(this.end.getMonth() + 1) + '/' : '' ) + this.end.getFullYear())) + : '') ].join(''); }; diff --git a/source/javascripts/timesheet.js b/source/javascripts/timesheet.js index 0a7d773..8c049f8 100644 --- a/source/javascripts/timesheet.js +++ b/source/javascripts/timesheet.js @@ -33,7 +33,7 @@ for (var n = 0, m = this.data.length; n < m; n++) { var cur = this.data[n]; - var bubble = new TimesheetBubble(widthMonth, this.year.min, cur.start, cur.end ? cur.end : new Date(this.year.max+1, 0, 1), cur.end ? false : true); + var bubble = new TimesheetBubble(widthMonth, this.year.min, cur.start, cur.end); var line = [ '', @@ -66,7 +66,7 @@ */ Timesheet.prototype.parseDate = function(date) { if (date === 'present') { - return null; + return new Date(10000 + this.year.max, 0, 1); } if (date.indexOf('/') === -1) { date = new Date(parseInt(date, 10), 0, 1); @@ -94,7 +94,7 @@ this.year.min = beg.getFullYear(); } - if (end && end.getFullYear() > this.year.max) { + if (end && end.getFullYear() < 10000 && end.getFullYear() > this.year.max) { this.year.max = end.getFullYear(); } else if (beg.getFullYear() > this.year.max) { this.year.max = beg.getFullYear(); From ba7602ae1068aa8dc762ec6f795ad3e32af58d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Craveiro?= Date: Mon, 16 Mar 2015 14:27:53 +0000 Subject: [PATCH 3/4] Fix merge (remove timesheet.bubble.js) --- source/javascripts/timesheet.bubble.js | 84 -------------------------- 1 file changed, 84 deletions(-) delete mode 100644 source/javascripts/timesheet.bubble.js diff --git a/source/javascripts/timesheet.bubble.js b/source/javascripts/timesheet.bubble.js deleted file mode 100644 index ef56d18..0000000 --- a/source/javascripts/timesheet.bubble.js +++ /dev/null @@ -1,84 +0,0 @@ -(function() { - 'use strict'; - - /** - * Timesheet Bubble - */ - var Bubble = function(wMonth, min, start, end) { - this.min = min; - this.start = start; - this.end = end; - if (this.end && this.end.getFullYear() && this.end.getFullYear() > 9999) { - this.endless = true; - this.end = new Date(end.getFullYear()-9999, this.end.getMonth(), 1); - } - this.widthMonth = wMonth; - }; - - /** - * Format month number - */ - Bubble.prototype.formatMonth = function(num) { - num = parseInt(num, 10); - - return num >= 10 ? num : '0' + num; - }; - - /** - * Calculate starting offset for bubble - */ - Bubble.prototype.getStartOffset = function() { - return (this.widthMonth/12) * (12 * (this.start.getFullYear() - this.min) + this.start.getMonth()); - }; - - /** - * Get count of full years from start to end - */ - Bubble.prototype.getFullYears = function() { - return ((this.end && this.end.getFullYear()) || this.start.getFullYear()) - this.start.getFullYear(); - }; - - /** - * Get count of all months in Timesheet Bubble - */ - Bubble.prototype.getMonths = function() { - var fullYears = this.getFullYears(); - var months = 0; - - if (!this.end) { - months += !this.start.hasMonth ? 12 : 1; - } else { - if (!this.end.hasMonth) { - months += 12 - (this.start.hasMonth ? this.start.getMonth() : 0); - months += 12 * (fullYears-1 > 0 ? fullYears-1 : 0); - } else { - months += this.end.getMonth() + 1; - months += 12 - (this.start.hasMonth ? this.start.getMonth() : 0); - months += 12 * (fullYears-1); - } - } - - return months; - }; - - /** - * Get bubble's width in pixel - */ - Bubble.prototype.getWidth = function() { - return (this.widthMonth/12) * this.getMonths(); - }; - - /** - * Get the bubble's label - */ - Bubble.prototype.getDateLabel = function() { - return [ - (this.start.hasMonth ? this.formatMonth(this.start.getMonth() + 1) + '/' : '' ) + this.start.getFullYear(), - (this.end - ? (this.endless ? '—' : '–' + ((this.end.hasMonth ? this.formatMonth(this.end.getMonth() + 1) + '/' : '' ) + this.end.getFullYear())) - : '') - ].join(''); - }; - - window.TimesheetBubble = Bubble; -})(); From 499595969935a291a6c4aa0474cd214587c72648 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Craveiro?= Date: Tue, 17 Mar 2015 22:04:47 +0000 Subject: [PATCH 4/4] Fix tests --- Gruntfile.js | 2 +- dist/timesheet.js | 2 +- test/timesheet.js | 21 ++++++++++----------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 786fdbf..4ba059b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -8,7 +8,7 @@ module.exports = function(grunt) { }, my_target: { files: { - 'dist/timesheet.js': ['source/javascripts/timesheet.bubble.js', 'source/javascripts/timesheet.js'] + 'dist/timesheet.js': ['source/javascripts/timesheet.js'] } } }, diff --git a/dist/timesheet.js b/dist/timesheet.js index 17b13b2..8dfe9c3 100644 --- a/dist/timesheet.js +++ b/dist/timesheet.js @@ -1 +1 @@ -!function(){"use strict";var Bubble=function(wMonth,min,start,end){this.min=min,this.start=start,this.end=end,this.widthMonth=wMonth};Bubble.prototype.formatMonth=function(num){return num=parseInt(num,10),num>=10?num:"0"+num},Bubble.prototype.getStartOffset=function(){return this.widthMonth/12*(12*(this.start.getFullYear()-this.min)+this.start.getMonth())},Bubble.prototype.getFullYears=function(){return(this.end&&this.end.getFullYear()||this.start.getFullYear())-this.start.getFullYear()},Bubble.prototype.getMonths=function(){var fullYears=this.getFullYears(),months=0;return this.end?this.end.hasMonth?(months+=this.end.getMonth()+1,months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1)):(months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1>0?fullYears-1:0)):months+=this.start.hasMonth?1:12,months},Bubble.prototype.getWidth=function(){return this.widthMonth/12*this.getMonths()},Bubble.prototype.getDateLabel=function(){return[(this.start.hasMonth?this.formatMonth(this.start.getMonth()+1)+"/":"")+this.start.getFullYear(),this.end?"-"+((this.end.hasMonth?this.formatMonth(this.end.getMonth()+1)+"/":"")+this.end.getFullYear()):""].join("")},window.TimesheetBubble=Bubble}(),function(){"use strict";var Timesheet=function(container,min,max,data){this.data=[],this.year={min:min,max:max},this.parse(data||[]),"undefined"!=typeof document&&(this.container="string"==typeof container?document.querySelector("#"+container):container,this.drawSections(),this.insertData())};Timesheet.prototype.insertData=function(){for(var html=[],widthMonth=this.container.querySelector(".scale section").offsetWidth,n=0,m=this.data.length;m>n;n++){var cur=this.data[n],bubble=new TimesheetBubble(widthMonth,this.year.min,cur.start,cur.end),line=['',''+bubble.getDateLabel()+" ",''+cur.label+""].join("");html.push("
  • "+line+"
  • ")}this.container.innerHTML+='
      '+html.join("")+"
    "},Timesheet.prototype.drawSections=function(){for(var html=[],c=this.year.min;c<=this.year.max;c++)html.push("
    "+c+"
    ");this.container.className="timesheet color-scheme-default",this.container.innerHTML='
    '+html.join("")+"
    "},Timesheet.prototype.parseDate=function(date){return-1===date.indexOf("/")?(date=new Date(parseInt(date,10),0,1),date.hasMonth=!1):(date=date.split("/"),date=new Date(parseInt(date[1],10),parseInt(date[0],10)-1,1),date.hasMonth=!0),date},Timesheet.prototype.parse=function(data){for(var n=0,m=data.length;m>n;n++){var beg=this.parseDate(data[n][0]),end=4===data[n].length?this.parseDate(data[n][1]):null,lbl=4===data[n].length?data[n][2]:data[n][1],cat=data[n][3]||"default";beg.getFullYear()this.year.max?this.year.max=end.getFullYear():beg.getFullYear()>this.year.max&&(this.year.max=beg.getFullYear()),this.data.push({start:beg,end:end,label:lbl,type:cat})}},window.Timesheet=Timesheet}(); \ No newline at end of file +!function(){"use strict";var Timesheet=function(container,min,max,data){this.data=[],this.year={min:min,max:max},this.parse(data||[]),"undefined"!=typeof document&&(this.container="string"==typeof container?document.querySelector("#"+container):container,this.drawSections(),this.insertData())};Timesheet.prototype.insertData=function(){for(var html=[],widthMonth=this.container.querySelector(".scale section").offsetWidth,n=0,m=this.data.length;m>n;n++){var cur=this.data[n],bubble=new Bubble(widthMonth,this.year.min,cur.start,cur.end),line=['',''+bubble.getDateLabel()+" ",''+cur.label+""].join("");html.push("
  • "+line+"
  • ")}this.container.innerHTML+='
      '+html.join("")+"
    "},Timesheet.prototype.drawSections=function(){for(var html=[],c=this.year.min;c<=this.year.max;c++)html.push("
    "+c+"
    ");this.container.className="timesheet color-scheme-default",this.container.innerHTML='
    '+html.join("")+"
    "},Timesheet.prototype.parseDate=function(date){return-1===date.indexOf("/")?(date=new Date(parseInt(date,10),0,1),date.hasMonth=!1):(date=date.split("/"),date=new Date(parseInt(date[1],10),parseInt(date[0],10)-1,1),date.hasMonth=!0),date},Timesheet.prototype.parse=function(data){for(var n=0,m=data.length;m>n;n++){var beg=this.parseDate(data[n][0]),end=4===data[n].length?this.parseDate(data[n][1]):null,lbl=4===data[n].length?data[n][2]:data[n][1],cat=4===data[n].length?data[n][3]:3===data[n].length?data[n][2]:"default";beg.getFullYear()this.year.max?this.year.max=end.getFullYear():beg.getFullYear()>this.year.max&&(this.year.max=beg.getFullYear()),this.data.push({start:beg,end:end,label:lbl,type:cat})}};var Bubble=function(wMonth,min,start,end){this.min=min,this.start=start,this.end=end,this.widthMonth=wMonth};Bubble.prototype.formatMonth=function(num){return num=parseInt(num,10),num>=10?num:"0"+num},Bubble.prototype.getStartOffset=function(){return this.widthMonth/12*(12*(this.start.getFullYear()-this.min)+this.start.getMonth())},Bubble.prototype.getFullYears=function(){return(this.end&&this.end.getFullYear()||this.start.getFullYear())-this.start.getFullYear()},Bubble.prototype.getMonths=function(){var fullYears=this.getFullYears(),months=0;return this.end?this.end.hasMonth?(months+=this.end.getMonth()+1,months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1)):(months+=12-(this.start.hasMonth?this.start.getMonth():0),months+=12*(fullYears-1>0?fullYears-1:0)):months+=this.start.hasMonth?1:12,months},Bubble.prototype.getWidth=function(){return this.widthMonth/12*this.getMonths()},Bubble.prototype.getDateLabel=function(){return[(this.start.hasMonth?this.formatMonth(this.start.getMonth()+1)+"/":"")+this.start.getFullYear(),this.end?"-"+((this.end.hasMonth?this.formatMonth(this.end.getMonth()+1)+"/":"")+this.end.getFullYear()):""].join("")},window.Timesheet=Timesheet}(); \ No newline at end of file diff --git a/test/timesheet.js b/test/timesheet.js index 5d84105..231a205 100644 --- a/test/timesheet.js +++ b/test/timesheet.js @@ -3,27 +3,26 @@ */ window = {}; require(__dirname + '/../source/javascripts/timesheet.js'); -require(__dirname + '/../source/javascripts/timesheet.bubble.js'); var assert = require('assert'); suite('Timesheet', function() { test('Calculation', function(done) { var TS = new window.Timesheet(); - assert.equal(12, (new window.TimesheetBubble(60, 2012, TS.parseDate('2002'), TS.parseDate('2002'))).getMonths()); - assert.equal(12, (new window.TimesheetBubble(60, 2012, TS.parseDate('2002'), TS.parseDate('2003'))).getMonths()); - assert.equal(24, (new window.TimesheetBubble(60, 2012, TS.parseDate('2002'), TS.parseDate('2004'))).getMonths()); + assert.equal(12, (new window.Bubble(60, 2012, TS.parseDate('2002'), TS.parseDate('2002'))).getMonths()); + assert.equal(12, (new window.Bubble(60, 2012, TS.parseDate('2002'), TS.parseDate('2003'))).getMonths()); + assert.equal(24, (new window.Bubble(60, 2012, TS.parseDate('2002'), TS.parseDate('2004'))).getMonths()); - assert.equal(9, (new window.TimesheetBubble(60, 2012, TS.parseDate('04/2002'), TS.parseDate('2002'))).getMonths()); - assert.equal(9, (new window.TimesheetBubble(60, 2012, TS.parseDate('04/2002'), TS.parseDate('2003'))).getMonths()); - assert.equal(21, (new window.TimesheetBubble(60, 2012, TS.parseDate('04/2002'), TS.parseDate('2004'))).getMonths()); + assert.equal(9, (new window.Bubble(60, 2012, TS.parseDate('04/2002'), TS.parseDate('2002'))).getMonths()); + assert.equal(9, (new window.Bubble(60, 2012, TS.parseDate('04/2002'), TS.parseDate('2003'))).getMonths()); + assert.equal(21, (new window.Bubble(60, 2012, TS.parseDate('04/2002'), TS.parseDate('2004'))).getMonths()); - assert.equal(13, (new window.TimesheetBubble(60, 2012, TS.parseDate('04/2002'), TS.parseDate('04/2003'))).getMonths()); + assert.equal(13, (new window.Bubble(60, 2012, TS.parseDate('04/2002'), TS.parseDate('04/2003'))).getMonths()); - assert.equal(25, (new window.TimesheetBubble(60, 2012, TS.parseDate('04/2002'), TS.parseDate('04/2004'))).getMonths()); + assert.equal(25, (new window.Bubble(60, 2012, TS.parseDate('04/2002'), TS.parseDate('04/2004'))).getMonths()); - assert.equal(1, (new window.TimesheetBubble(60, 2012, TS.parseDate('04/2002'))).getMonths()); - assert.equal(12, (new window.TimesheetBubble(60, 2012, TS.parseDate('2002'))).getMonths()); + assert.equal(1, (new window.Bubble(60, 2012, TS.parseDate('04/2002'))).getMonths()); + assert.equal(12, (new window.Bubble(60, 2012, TS.parseDate('2002'))).getMonths()); done(); });