Skip to content

Commit

Permalink
Merge #2525
Browse files Browse the repository at this point in the history
2525: feat(bhPeriodSelect): preserve custom dates r=jniles a=jniles

This commit ensures that the bhPeriodSelect remembers custom dates if they are set in the same session.

Closes #2516.
  • Loading branch information
bors[bot] committed Feb 8, 2018
2 parents 898f3f4 + 010e9e2 commit ee79b19
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions client/src/js/components/bhPeriodSelect.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
angular.module('bhima.components')
.component('bhPeriodSelect', {
bindings : {
defaultPeriod : '@',
onSelectCallback : '&',
},
templateUrl : 'modules/templates/bhPeriodSelect.tmpl.html',
controller : PeriodSelect,
});
.component('bhPeriodSelect', {
bindings : {
defaultPeriod : '@',
onSelectCallback : '&',
},
templateUrl : 'modules/templates/bhPeriodSelect.tmpl.html',
controller : PeriodSelect,
});

PeriodSelect.$inject = ['PeriodService', 'bhConstants'];

function PeriodSelect(Periods, bhConstants) {
var ctrl = this;
var DEFAULT_PERIOD = 'today';
const ctrl = this;
const DEFAULT_PERIOD = 'today';

ctrl.NO_PERIOD_LIMIT_KEY = 'allTime';
ctrl.CUSTOM_PERIOD_KEY = 'custom';
Expand All @@ -32,6 +32,12 @@ function PeriodSelect(Periods, bhConstants) {
ctrl.$onInit = function onInit() {
ctrl.periodKey = ctrl.defaultPeriod || DEFAULT_PERIOD;
ctrl.period = Periods.definition(ctrl.periodKey);

// if custom is already defined, use it
if (ctrl.periodKey === ctrl.CUSTOM_PERIOD_KEY) {
ctrl.customSelection.from = ctrl.period.customPeriodStart || new Date();
ctrl.customSelection.to = ctrl.period.customPeriodEnd || new Date();
}
};

ctrl.toggleSelectionOptions = function toggleSelectionOptions() {
Expand All @@ -53,14 +59,13 @@ function PeriodSelect(Periods, bhConstants) {
*/
ctrl.selectPeriod(ctrl.NO_PERIOD_LIMIT_KEY, true);
}

};


ctrl.selectPeriod = function selectPeriod(key, togglable) {
var period = Periods.definition(key);
ctrl.onSelectCallback({ period : period });
// shoud not toggle in custom mode , as explained in at this point : ctrl.toggleCustomSelection()
const period = Periods.definition(key);
ctrl.onSelectCallback({ period });
// should not toggle in custom mode , as explained in at this point : ctrl.toggleCustomSelection()
if (!togglable) {
ctrl.toggleSelectionOptions();
}
Expand All @@ -69,22 +74,22 @@ function PeriodSelect(Periods, bhConstants) {
};

// custom dates changed, current period should be updated
ctrl.customPeriodChanges = function () {
var _period = Periods.index.custom;
ctrl.customPeriodChanges = () => {
const _period = Periods.index.custom;
_period.customPeriodStart = ctrl.customSelection.from;
_period.customPeriodEnd = ctrl.customSelection.to;
ctrl.onSelectCallback({ period : _period });
};


ctrl.selectCustomPeriod = function selectCustomPeriod(selection) {
var period = Periods.index.custom;
const period = Periods.index.custom;

// alias start and
// alias start and end;
period.customPeriodStart = selection.from;
period.customPeriodEnd = selection.to;

ctrl.onSelectCallback({ period : period });
ctrl.onSelectCallback({ period });
ctrl.toggleSelectionOptions();

ctrl.period = period;
Expand Down

0 comments on commit ee79b19

Please sign in to comment.