← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15030: support Weekly periods in js generator

 

------------------------------------------------------------
revno: 15030
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2014-04-27 22:35:15 +0545
message:
  support Weekly periods in js generator
modified:
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.period.js


--
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:04:05 +0000
+++ 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
@@ -30,52 +30,6 @@
 
 dhis2.period.DATE_FORMAT = "yyyy-mm-dd";
 
-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');
-    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;
-};
-
-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.generateDailyPeriods = function( cal, offset ) {
   var year = cal.today().year() - offset;
 
@@ -99,6 +53,62 @@
   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;
 
@@ -212,3 +222,27 @@
 
   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');
+    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;
+};