Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some minor issues #198

Merged
merged 15 commits into from
Nov 6, 2015
Merged
2 changes: 1 addition & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ <h5>Min + max ({{demoForm.pickerBothDates.$error.min ? 'Min: invalid, ' : 'Min:
<div>
<h2>Range</h2>

<div date-range id="pickerRange" start="minDate" end="maxDate" date-change="changeDate" view="date" min-view="date"></div>
<div date-range id="pickerRange" start="dates.minDate" end="dates.maxDate" date-change="changeDate" view="date" min-view="date" timezone="UTC"></div>

</div>

Expand Down
34 changes: 18 additions & 16 deletions app/scripts/dateRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,24 @@ Module.directive('dateRange', ['$compile', 'datePickerUtils', 'dateTimeConfig',
eventIsForPicker = datePickerUtils.eventIsForPicker;

scope.dateChange = function (modelName, newDate) {
//Received updated data from one of the pickers. Update the max/min date of the other picker.
var data = {},
pickerID;

if (modelName === 'start') {
//Start changed
data.minDate = newDate;
pickerID = pickerIDs[1];
} else {
//End changed
data.maxDate = newDate;
pickerID = pickerIDs[0];
}

scope.$broadcast('pickerUpdate', pickerID, data);

//Notify user if callback exists.
if (dateChange) {
dateChange(modelName, newDate);
}
};

function setMax(date) {
scope.$broadcast('pickerUpdate', pickerIDs[0], {
maxDate: date
});
}

function setMin(date) {
scope.$broadcast('pickerUpdate', pickerIDs[1], {
minDate: date
});
}

if (pickerRangeID) {
scope.$on('pickerUpdate', function (event, targetIDs, data) {
if (eventIsForPicker(targetIDs, pickerRangeID)) {
Expand All @@ -65,6 +61,12 @@ Module.directive('dateRange', ['$compile', 'datePickerUtils', 'dateTimeConfig',
scope.start = createMoment(scope.start);
scope.end = createMoment(scope.end);

scope.$watchGroup(['start', 'end'], function (dates) {
//Scope data changed, update picker min/max
setMin(dates[0]);
setMax(dates[1]);
});

if (angular.isDefined(attrs.dateChange)) {
dateChange = datePickerUtils.findFunction(scope, attrs.dateChange);
}
Expand Down
40 changes: 24 additions & 16 deletions app/scripts/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfi
position = attrs.position || dateTimeConfig.position,
container = null,
minDate = null,
minValid = null,
maxDate = null,
maxValid = null,
timezone = attrs.timezone || false,
eventIsForPicker = datePickerUtils.eventIsForPicker,
dateChange = null,
Expand All @@ -85,24 +87,35 @@ Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfi
return undefined;
}

function setMin(date) {
minDate = date;
attrs.minDate = date ? date.format() : date;
minValid = moment.isMoment(date);
}

function setMax(date) {
maxDate = date;
attrs.maxDate = date ? date.format() : date;
maxValid = moment.isMoment(date);
}

ngModel.$formatters.push(formatter);
ngModel.$parsers.unshift(parser);

if (angular.isDefined(attrs.minDate)) {
minDate = datePickerUtils.findParam(scope, attrs.minDate);
attrs.minDate = minDate ? minDate.format() : minDate;
setMin(datePickerUtils.findParam(scope, attrs.minDate));

ngModel.$validators.min = function (value) {
return moment.isMoment(value) && (minDate.isSame(value) || minDate.isBefore(value));
//If we don't have a min / max value, then any value is valid.
return minValid ? moment.isMoment(value) && (minDate.isSame(value) || minDate.isBefore(value)) : true;
};
}

if (angular.isDefined(attrs.maxDate)) {
maxDate = datePickerUtils.findParam(scope, attrs.maxDate);
attrs.maxDate = maxDate ? maxDate.format() : maxDate;
setMax(datePickerUtils.findParam(scope, attrs.maxDate));

ngModel.$validators.max = function (value) {
return moment.isMoment(value) && (maxDate.isSame(value) || maxDate.isAfter(value));
return maxValid ? moment.isMoment(value) && (maxDate.isSame(value) || maxDate.isAfter(value)) : true;
};
}

Expand All @@ -114,6 +127,7 @@ Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfi
template = dateTimeConfig.template(attrs);
}


function updateInput(event) {
event.stopPropagation();
if (ngModel.$pristine) {
Expand Down Expand Up @@ -143,22 +157,16 @@ Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfi
if (eventIsForPicker(pickerIDs, pickerID)) {
if (picker) {
//Need to handle situation where the data changed but the picker is currently open.
//However, this directive is not guaranteed to be present, as the date-picker directive can be used by itself.
//Therefore, we need to somehow catch this situation and update the inner picker. Perhaps we can use the same event
//for inner updates. If this directive exists, it will be caught here first, and then we can eat the event. Otherwise,
//the inner directive can catch the pickerUpdate event and update appropriately. We just need to pass the name
//of the model to the inner picker (or get it there somehow else if this directive doesn't exist) to determine
//which picker is being updated.
//To handle this, we can create the inner picker with a random ID, then forward
//any events received to it.
} else {
var validateRequired = false;
if (angular.isDefined(data.minDate)) {
minDate = data.minDate;
attrs.minDate = minDate ? minDate.format() : false;
setMin(data.minDate);
validateRequired = true;
}
if (angular.isDefined(data.maxDate)) {
maxDate = data.maxDate;
attrs.maxDate = maxDate ? maxDate.format() : false;
setMax(data.maxDate);
validateRequired = true;
}

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angular-datepicker",
"license": "MIT",
"version": "2.0.0",
"version": "2.0.1",
"main": [
"./dist/angular-datepicker.js",
"./dist/angular-datepicker.css"
Expand Down
74 changes: 42 additions & 32 deletions dist/angular-datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,28 +604,24 @@ Module.directive('dateRange', ['$compile', 'datePickerUtils', 'dateTimeConfig',
eventIsForPicker = datePickerUtils.eventIsForPicker;

scope.dateChange = function (modelName, newDate) {
//Received updated data from one of the pickers. Update the max/min date of the other picker.
var data = {},
pickerID;

if (modelName === 'start') {
//Start changed
data.minDate = newDate;
pickerID = pickerIDs[1];
} else {
//End changed
data.maxDate = newDate;
pickerID = pickerIDs[0];
}

scope.$broadcast('pickerUpdate', pickerID, data);

//Notify user if callback exists.
if (dateChange) {
dateChange(modelName, newDate);
}
};

function setMax(date) {
scope.$broadcast('pickerUpdate', pickerIDs[0], {
maxDate: date
});
}

function setMin(date) {
scope.$broadcast('pickerUpdate', pickerIDs[1], {
minDate: date
});
}

if (pickerRangeID) {
scope.$on('pickerUpdate', function (event, targetIDs, data) {
if (eventIsForPicker(targetIDs, pickerRangeID)) {
Expand All @@ -640,6 +636,12 @@ Module.directive('dateRange', ['$compile', 'datePickerUtils', 'dateTimeConfig',
scope.start = createMoment(scope.start);
scope.end = createMoment(scope.end);

scope.$watchGroup(['start', 'end'], function (dates) {
//Scope data changed, update picker min/max
setMin(dates[0]);
setMax(dates[1]);
});

if (angular.isDefined(attrs.dateChange)) {
dateChange = datePickerUtils.findFunction(scope, attrs.dateChange);
}
Expand Down Expand Up @@ -718,7 +720,9 @@ Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfi
position = attrs.position || dateTimeConfig.position,
container = null,
minDate = null,
minValid = null,
maxDate = null,
maxValid = null,
timezone = attrs.timezone || false,
eventIsForPicker = datePickerUtils.eventIsForPicker,
dateChange = null,
Expand All @@ -742,24 +746,35 @@ Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfi
return undefined;
}

function setMin(date) {
minDate = date;
attrs.minDate = date ? date.format() : date;
minValid = moment.isMoment(date);
}

function setMax(date) {
maxDate = date;
attrs.maxDate = date ? date.format() : date;
maxValid = moment.isMoment(date);
}

ngModel.$formatters.push(formatter);
ngModel.$parsers.unshift(parser);

if (angular.isDefined(attrs.minDate)) {
minDate = datePickerUtils.findParam(scope, attrs.minDate);
attrs.minDate = minDate ? minDate.format() : minDate;
setMin(datePickerUtils.findParam(scope, attrs.minDate));

ngModel.$validators.min = function (value) {
return moment.isMoment(value) && (minDate.isSame(value) || minDate.isBefore(value));
//If we don't have a min / max value, then any value is valid.
return minValid ? moment.isMoment(value) && (minDate.isSame(value) || minDate.isBefore(value)) : true;
};
}

if (angular.isDefined(attrs.maxDate)) {
maxDate = datePickerUtils.findParam(scope, attrs.maxDate);
attrs.maxDate = maxDate ? maxDate.format() : maxDate;
setMax(datePickerUtils.findParam(scope, attrs.maxDate));

ngModel.$validators.max = function (value) {
return moment.isMoment(value) && (maxDate.isSame(value) || maxDate.isAfter(value));
return maxValid ? moment.isMoment(value) && (maxDate.isSame(value) || maxDate.isAfter(value)) : true;
};
}

Expand All @@ -771,6 +786,7 @@ Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfi
template = dateTimeConfig.template(attrs);
}


function updateInput(event) {
event.stopPropagation();
if (ngModel.$pristine) {
Expand Down Expand Up @@ -800,22 +816,16 @@ Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfi
if (eventIsForPicker(pickerIDs, pickerID)) {
if (picker) {
//Need to handle situation where the data changed but the picker is currently open.
//However, this directive is not guaranteed to be present, as the date-picker directive can be used by itself.
//Therefore, we need to somehow catch this situation and update the inner picker. Perhaps we can use the same event
//for inner updates. If this directive exists, it will be caught here first, and then we can eat the event. Otherwise,
//the inner directive can catch the pickerUpdate event and update appropriately. We just need to pass the name
//of the model to the inner picker (or get it there somehow else if this directive doesn't exist) to determine
//which picker is being updated.
//To handle this, we can create the inner picker with a random ID, then forward
//any events received to it.
} else {
var validateRequired = false;
if (angular.isDefined(data.minDate)) {
minDate = data.minDate;
attrs.minDate = minDate ? minDate.format() : false;
setMin(data.minDate);
validateRequired = true;
}
if (angular.isDefined(data.maxDate)) {
maxDate = data.maxDate;
attrs.maxDate = maxDate ? maxDate.format() : false;
setMax(data.maxDate);
validateRequired = true;
}

Expand Down
Loading