dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #29694
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15037: add new PeriodGenerator, uses a simple js api similar to old PeriodType class. var pg = new dhis2...
------------------------------------------------------------
revno: 15037
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-04-28 10:57:25 +0545
message:
add new PeriodGenerator, uses a simple js api similar to old PeriodType class. var pg = new dhis2.period.PeriodGenerator(); pg.get('Daily').generatePeriods() (optional offset). Also exposes a global generator in dhis2.period.generator which uses the system calendar.
modified:
dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.period.js
dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.period.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.period.js 2014-04-27 16:50:15 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.period.js 2014-04-28 05:12:25 +0000
@@ -30,219 +30,356 @@
dhis2.period.DATE_FORMAT = "yyyy-mm-dd";
-dhis2.period.generateDailyPeriods = function( cal, offset ) {
- var year = cal.today().year() - offset;
-
- var periods = [];
-
- var startDate = cal.newDate(year, 1, 1);
-
- for( var day = 1; day <= cal.daysInYear(year); day++ ) {
- var period = {};
- period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
- period['endDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
- period['name'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
- period['id'] = 'Daily_' + period['startDate'];
- period['iso'] = startDate.formatDate("yyyymmdd");
-
- periods.push(period);
-
- startDate.add(1, 'd');
- }
-
- return periods;
-};
-
-dhis2.period.generateWeeklyPeriods = function( cal, offset ) {
- var year = cal.today().year() - offset;
-
- var periods = [];
-
- var startDate = cal.newDate(year, 1, 1);
- startDate.add(-(startDate.dayOfWeek() - 1), 'd'); // rewind to start of week, might cross year boundary
-
- // no reliable way to figure out number of weeks in a year (can differ in different calendars)
- // goes up to 200, but break when week is back to 1
- for( var week = 1; week < 200; week++ ) {
- var period = {};
- period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
-
- // not very elegant, but seems to be best way to get week end, adds a week, then minus 1 day
- var endDate = cal.newDate(startDate).add(1, 'w').add(-1, 'd');
-
- period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
- period['name'] = 'W' + week + ' - ' + period['startDate'] + ' - ' + period['endDate'];
- period['id'] = 'Weekly_' + period['startDate'];
- period['iso'] = year + 'W' + week;
-
- periods.push(period);
-
- startDate.add(1, 'w');
-
- if( startDate.weekOfYear() == 1 ) {
- break;
- }
- }
-
- return periods;
-};
-
-dhis2.period.generateMonthlyPeriods = function( cal, offset ) {
- var year = cal.today().year() - offset;
-
- var periods = [];
-
- for( var month = 1; month <= cal.monthsInYear(year); month++ ) {
- var startDate = cal.newDate(year, month, 1);
- var endDate = cal.newDate(startDate).set(startDate.daysInMonth(month), 'd');
-
- var period = {};
- period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
- period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
- period['name'] = startDate.formatDate("MM yyyy");
- period['id'] = 'Monthly_' + period['startDate'];
- period['iso'] = startDate.formatDate("yyyymm");
-
- periods.push(period);
- }
-
- return periods;
-};
-
-dhis2.period.generateBiMonthlyPeriods = function( cal, offset ) {
- var year = cal.today().year() - offset;
-
- var periods = [];
-
- for( var month = 1; month <= cal.monthsInYear(year); month += 2 ) {
- var startDate = cal.newDate(year, month, 1);
- var endDate = cal.newDate(startDate).set(month + 1, 'm');
- endDate.set(endDate.daysInMonth(month + 1), 'd');
-
- var period = {};
- period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
- period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
- period['name'] = startDate.formatDate("MM") + '-' + endDate.formatDate('MM') + ' ' + year;
- period['id'] = 'BiMonthly_' + period['startDate'];
- period['iso'] = startDate.formatDate("yyyymm") + 'B';
-
- periods.push(period);
- }
-
- return periods;
-};
-
-dhis2.period.generateQuarterlyPeriods = function( cal, offset ) {
- var year = cal.today().year() - offset;
-
- var periods = [];
-
- for( var month = 1, idx = 1; month <= cal.monthsInYear(year); month += 3, idx++ ) {
- var startDate = cal.newDate(year, month, 1);
- var endDate = cal.newDate(startDate).set(month + 2, 'm');
- endDate.set(endDate.daysInMonth(month + 2), 'd');
-
- var period = {};
- period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
- period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
- period['name'] = startDate.formatDate("MM") + '-' + endDate.formatDate('MM') + ' ' + year;
- period['id'] = 'Quarterly_' + period['startDate'];
- period['iso'] = startDate.formatDate("yyyy") + 'Q' + idx;
-
- periods.push(period);
- }
-
- return periods;
-};
-
-dhis2.period.generateSixMonthlyPeriods = function( cal, offset ) {
- var year = cal.today().year() - offset;
-
- var periods = [];
-
- var startDate = cal.newDate(year, 1, 1);
- var endDate = cal.newDate(startDate).set(6, 'm');
- endDate.set(endDate.daysInMonth(6), 'd');
-
- var period = {};
- period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
- period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
- period['name'] = startDate.formatDate("MM") + '-' + endDate.formatDate('MM') + ' ' + year;
- period['id'] = 'SixMonthly_' + period['startDate'];
- period['iso'] = startDate.formatDate("yyyy") + 'S1';
-
- periods.push(period);
-
- startDate = cal.newDate(year, 7, 1);
- endDate = cal.newDate(startDate).set(cal.monthsInYear(year), 'm');
- endDate.set(endDate.daysInMonth(12), 'd');
-
- period = {};
- period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
- period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
- period['name'] = startDate.formatDate("MM") + '-' + endDate.formatDate('MM') + ' ' + year;
- period['id'] = 'SixMonthly_' + period['startDate'];
- period['iso'] = startDate.formatDate("yyyy") + 'S2';
-
- periods.push(period);
-
- return periods;
-};
-
-dhis2.period.generateSixMonthlyAprilPeriods = function( cal, offset ) {
- var year = cal.today().year() - offset;
-
- var periods = [];
-
- var startDate = cal.newDate(year, 4, 1);
- var endDate = cal.newDate(startDate).set(9, 'm');
- endDate.set(endDate.daysInMonth(9), 'd');
-
- var period = {};
- period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
- period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
- period['name'] = startDate.formatDate("MM") + ' - ' + endDate.formatDate('MM') + ' ' + year;
- period['id'] = 'SixMonthlyApril_' + period['startDate'];
- period['iso'] = startDate.formatDate("yyyy") + 'AprilS1';
-
- periods.push(period);
-
- startDate = cal.newDate(year, 10, 1);
- endDate = cal.newDate(startDate).set(startDate.year() + 1, 'y').set(2, 'm');
- endDate.set(endDate.daysInMonth(endDate.month()), 'd');
-
- period = {};
- period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
- period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
- period['name'] = startDate.formatDate("MM yyyy") + ' - ' + endDate.formatDate('MM yyyy');
- period['id'] = 'SixMonthlyApril_' + period['startDate'];
- period['iso'] = startDate.formatDate("yyyy") + 'AprilS2';
-
- periods.push(period);
-
- return periods;
-};
-
-dhis2.period.generateYearlyPeriods = function( cal, offset ) {
- var year = cal.today().year() - offset;
-
- var periods = [];
-
- // generate 11 years, thisYear +/- 5 years
- for( var i = -5; i < 6; i++ ) {
- var startDate = cal.newDate(year + i, 1, 1);
- var endDate = cal.newDate(startDate).set(cal.monthsInYear(year + i), 'm');
+dhis2.period.PeriodGenerator = function( calendar ) {
+ if( typeof calendar === 'undefined' ) {
+ calendar = dhis2.period.calendar;
+ }
+
+ this.calendar = calendar;
+
+ var periodTypes = {
+ "Daily": dhis2.period.makeDailyPeriodGenerator(this.calendar),
+ "Weekly": dhis2.period.makeWeeklyPeriodGenerator(this.calendar),
+ "Monthly": dhis2.period.makeMonthlyPeriodGenerator(this.calendar),
+ "BiMonthly": dhis2.period.makeBiMonthlyPeriodGenerator(this.calendar),
+ "Quarterly": dhis2.period.makeQuarterlyPeriodGenerator(this.calendar),
+ "SixMonthly": dhis2.period.makeSixMonthlyPeriodGenerator(this.calendar),
+ "SixMonthlyApril": dhis2.period.makeSixMonthlyAprilPeriodGenerator(this.calendar),
+ "Yearly": dhis2.period.makeYearlyPeriodGenerator(this.calendar),
+ "FinancialOct": dhis2.period.makeMonthlyPeriodGenerator(this.calendar),
+ "FinancialJuly": dhis2.period.makeMonthlyPeriodGenerator(this.calendar),
+ "FinancialApril": dhis2.period.makeMonthlyPeriodGenerator(this.calendar)
+ };
+
+ this.getAll = function() {
+ return periodTypes;
+ };
+
+ this.get = function( generator ) {
+ return periodTypes[generator];
+ };
+
+ this.daily = function( offset ) {
+ return this.get('Daily').generatePeriods(offset);
+ };
+
+ this.weekly = function( offset ) {
+ return this.get('Weekly').generatePeriods(offset);
+ };
+
+ this.monthly = function( offset ) {
+ return this.get('Monthly').generatePeriods(offset);
+ };
+
+ this.biMonthly = function( offset ) {
+ return this.get('BiMonthly').generatePeriods(offset);
+ };
+
+ this.quarterly = function( offset ) {
+ return this.get('Quarterly').generatePeriods(offset);
+ };
+
+ this.sixMonthly = function( offset ) {
+ return this.get('SixMonthly').generatePeriods(offset);
+ };
+
+ this.sixMonthlyApril = function( offset ) {
+ return this.get('SixMonthlyApril').generatePeriods(offset);
+ };
+
+ this.financialOct = function( offset ) {
+ return this.get('FinancialOct').generatePeriods(offset);
+ };
+
+ this.financialJuly = function( offset ) {
+ return this.get('FinancialJuly').generatePeriods(offset);
+ };
+
+ this.financialApril = function( offset ) {
+ return this.get('FinancialApril').generatePeriods(offset);
+ };
+};
+
+dhis2.period.makeDailyPeriodGenerator = function( cal ) {
+ var self = {};
+ self.generatePeriods = function( offset ) {
+ if( typeof offset === 'undefined' ) {
+ offset = 0;
+ }
+
+ var year = cal.today().year() - offset;
+ var periods = [];
+
+ var startDate = cal.newDate(year, 1, 1);
+
+ for( var day = 1; day <= cal.daysInYear(year); day++ ) {
+ var period = {};
+ period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['endDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['name'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['id'] = 'Daily_' + period['startDate'];
+ period['iso'] = startDate.formatDate("yyyymmdd");
+
+ periods.push(period);
+
+ startDate.add(1, 'd');
+ }
+
+ return periods;
+ };
+
+ return self;
+};
+
+dhis2.period.makeWeeklyPeriodGenerator = function( cal ) {
+ var self = {};
+ self.generatePeriods = function( offset ) {
+ if( typeof offset === 'undefined' ) {
+ offset = 0;
+ }
+
+ var year = cal.today().year() - offset;
+
+ var periods = [];
+
+ var startDate = cal.newDate(year, 1, 1);
+ startDate.add(-(startDate.dayOfWeek() - 1), 'd'); // rewind to start of week, might cross year boundary
+
+ // no reliable way to figure out number of weeks in a year (can differ in different calendars)
+ // goes up to 200, but break when week is back to 1
+ for( var week = 1; week < 200; week++ ) {
+ var period = {};
+ period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
+
+ // not very elegant, but seems to be best way to get week end, adds a week, then minus 1 day
+ var endDate = cal.newDate(startDate).add(1, 'w').add(-1, 'd');
+
+ period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['name'] = 'W' + week + ' - ' + period['startDate'] + ' - ' + period['endDate'];
+ period['id'] = 'Weekly_' + period['startDate'];
+ period['iso'] = year + 'W' + week;
+
+ periods.push(period);
+
+ startDate.add(1, 'w');
+
+ if( startDate.weekOfYear() == 1 ) {
+ break;
+ }
+ }
+
+ return periods;
+ };
+
+ return self;
+};
+
+dhis2.period.makeMonthlyPeriodGenerator = function( cal ) {
+ var self = {};
+ self.generatePeriods = function( offset ) {
+ if( typeof offset === 'undefined' ) {
+ offset = 0;
+ }
+
+ var year = cal.today().year() - offset;
+
+ var periods = [];
+
+ for( var month = 1; month <= cal.monthsInYear(year); month++ ) {
+ var startDate = cal.newDate(year, month, 1);
+ var endDate = cal.newDate(startDate).set(startDate.daysInMonth(month), 'd');
+
+ var period = {};
+ period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['name'] = startDate.formatDate("MM yyyy");
+ period['id'] = 'Monthly_' + period['startDate'];
+ period['iso'] = startDate.formatDate("yyyymm");
+
+ periods.push(period);
+ }
+
+ return periods;
+ };
+
+ return self;
+};
+
+dhis2.period.makeBiMonthlyPeriodGenerator = function( cal ) {
+ var self = {};
+ self.generatePeriods = function( offset ) {
+ if( typeof offset === 'undefined' ) {
+ offset = 0;
+ }
+
+ var year = cal.today().year() - offset;
+ var periods = [];
+
+ for( var month = 1; month <= cal.monthsInYear(year); month += 2 ) {
+ var startDate = cal.newDate(year, month, 1);
+ var endDate = cal.newDate(startDate).set(month + 1, 'm');
+ endDate.set(endDate.daysInMonth(month + 1), 'd');
+
+ var period = {};
+ period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['name'] = startDate.formatDate("MM") + '-' + endDate.formatDate('MM') + ' ' + year;
+ period['id'] = 'BiMonthly_' + period['startDate'];
+ period['iso'] = startDate.formatDate("yyyymm") + 'B';
+
+ periods.push(period);
+ }
+
+ return periods;
+ };
+
+ return self;
+};
+
+dhis2.period.makeQuarterlyPeriodGenerator = function( cal ) {
+ var self = {};
+ self.generatePeriods = function( offset ) {
+ if( typeof offset === 'undefined' ) {
+ offset = 0;
+ }
+
+ var year = cal.today().year() - offset;
+ var periods = [];
+
+ for( var month = 1, idx = 1; month <= cal.monthsInYear(year); month += 3, idx++ ) {
+ var startDate = cal.newDate(year, month, 1);
+ var endDate = cal.newDate(startDate).set(month + 2, 'm');
+ endDate.set(endDate.daysInMonth(month + 2), 'd');
+
+ var period = {};
+ period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['name'] = startDate.formatDate("MM") + '-' + endDate.formatDate('MM') + ' ' + year;
+ period['id'] = 'Quarterly_' + period['startDate'];
+ period['iso'] = startDate.formatDate("yyyy") + 'Q' + idx;
+
+ periods.push(period);
+ }
+
+ return periods;
+ };
+
+ return self;
+};
+
+dhis2.period.makeSixMonthlyPeriodGenerator = function( cal ) {
+ var self = {};
+ self.generatePeriods = function( offset ) {
+ if( typeof offset === 'undefined' ) {
+ offset = 0;
+ }
+
+ var year = cal.today().year() - offset;
+
+ var periods = [];
+
+ var startDate = cal.newDate(year, 1, 1);
+ var endDate = cal.newDate(startDate).set(6, 'm');
+ endDate.set(endDate.daysInMonth(6), 'd');
+
+ var period = {};
+ period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['name'] = startDate.formatDate("MM") + '-' + endDate.formatDate('MM') + ' ' + year;
+ period['id'] = 'SixMonthly_' + period['startDate'];
+ period['iso'] = startDate.formatDate("yyyy") + 'S1';
+
+ periods.push(period);
+
+ startDate = cal.newDate(year, 7, 1);
+ endDate = cal.newDate(startDate).set(cal.monthsInYear(year), 'm');
+ endDate.set(endDate.daysInMonth(12), 'd');
+
+ period = {};
+ period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['name'] = startDate.formatDate("MM") + '-' + endDate.formatDate('MM') + ' ' + year;
+ period['id'] = 'SixMonthly_' + period['startDate'];
+ period['iso'] = startDate.formatDate("yyyy") + 'S2';
+
+ periods.push(period);
+
+ return periods;
+ };
+
+ return self;
+};
+
+dhis2.period.makeSixMonthlyAprilPeriodGenerator = function( cal ) {
+ var self = {};
+ self.generatePeriods = function( offset ) {
+ if( typeof offset === 'undefined' ) {
+ offset = 0;
+ }
+
+ var year = cal.today().year() - offset;
+ var periods = [];
+
+ var startDate = cal.newDate(year, 4, 1);
+ var endDate = cal.newDate(startDate).set(9, 'm');
+ endDate.set(endDate.daysInMonth(9), 'd');
+
+ var period = {};
+ period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['name'] = startDate.formatDate("MM") + ' - ' + endDate.formatDate('MM') + ' ' + year;
+ period['id'] = 'SixMonthlyApril_' + period['startDate'];
+ period['iso'] = startDate.formatDate("yyyy") + 'AprilS1';
+
+ periods.push(period);
+
+ startDate = cal.newDate(year, 10, 1);
+ endDate = cal.newDate(startDate).set(startDate.year() + 1, 'y').set(2, 'm');
endDate.set(endDate.daysInMonth(endDate.month()), 'd');
- var period = {};
+ period = {};
period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
- period['name'] = startDate.formatDate("yyyy");
- period['id'] = 'Yearly_' + period['startDate'];
- period['iso'] = startDate.formatDate("yyyy");
+ period['name'] = startDate.formatDate("MM yyyy") + ' - ' + endDate.formatDate('MM yyyy');
+ period['id'] = 'SixMonthlyApril_' + period['startDate'];
+ period['iso'] = startDate.formatDate("yyyy") + 'AprilS2';
periods.push(period);
- }
-
- return periods;
+
+ return periods;
+ };
+
+ return self;
+};
+
+dhis2.period.makeYearlyPeriodGenerator = function( cal ) {
+ var self = {};
+ self.generatePeriods = function( offset ) {
+ if( typeof offset === 'undefined' ) {
+ offset = 0;
+ }
+
+ var year = cal.today().year() - offset;
+ var periods = [];
+
+ // generate 11 years, thisYear +/- 5 years
+ for( var i = -5; i < 6; i++ ) {
+ var startDate = cal.newDate(year + i, 1, 1);
+ var endDate = cal.newDate(startDate).set(cal.monthsInYear(year + i), 'm');
+ endDate.set(endDate.daysInMonth(endDate.month()), 'd');
+
+ var period = {};
+ period['startDate'] = startDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['endDate'] = endDate.formatDate(dhis2.period.DATE_FORMAT);
+ period['name'] = startDate.formatDate("yyyy");
+ period['id'] = 'Yearly_' + period['startDate'];
+ period['iso'] = startDate.formatDate("yyyy");
+
+ periods.push(period);
+ }
+
+ return periods;
+ };
+
+ return self;
};
=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm 2014-04-27 16:59:04 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/main.vm 2014-04-28 05:12:25 +0000
@@ -90,6 +90,7 @@
#else
dhis2.period.calendar = $.calendars.instance('$keyCalendar');
#end
+ dhis2.period.generator = new dhis2.period.PeriodGenerator( dhis2.period.calendar );
</script>
</head>