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

Implementing a custom 'moment' on 'options' definition #304

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions coffee/angular-daterangepicker.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pickerModule = angular.module('daterangepicker', [])

pickerModule.constant('dateRangePickerConfig',
cancelOnOutsideClick: true
moment: window.moment
locale:
separator: ' - '
format: 'YYYY-MM-DD'
Expand Down Expand Up @@ -68,8 +69,8 @@ pickerModule.directive 'dateRangePicker', ($compile, $timeout, $parse, dateRange

_setDatePoint = (setter) ->
(newValue) ->
if (newValue && (!moment.isMoment(newValue) || newValue.isValid()))
newValue = moment(newValue)
if (newValue && (!opts.moment.isMoment(newValue) || newValue.isValid()))
newValue = opts.moment(newValue)
else
# keep previous value if invalid
# set newValue = {} to default it
Expand Down Expand Up @@ -102,8 +103,8 @@ pickerModule.directive 'dateRangePicker', ($compile, $timeout, $parse, dateRange

getViewValue =(model) ->
f = (date) ->
if not moment.isMoment(date)
then moment(date).format(opts.locale.format)
if not opts.moment.isMoment(date)
then opts.moment(date).format(opts.locale.format)
else date.format(opts.locale.format)

if opts.singleDatePicker and model
Expand Down Expand Up @@ -140,7 +141,7 @@ pickerModule.directive 'dateRangePicker', ($compile, $timeout, $parse, dateRange
modelCtrl.$parsers.push (viewValue) ->
# Parse the string value
f = (value) ->
date = moment(value, opts.locale.format)
date = opts.moment(value, opts.locale.format)
return (date.isValid() && date) || null

objValue = if opts.singleDatePicker then null else
Expand Down Expand Up @@ -284,7 +285,7 @@ pickerModule.directive 'dateRangePicker', ($compile, $timeout, $parse, dateRange
if date and (min or max)
[date, min, max] = [date, min, max].map (d) ->
if (d)
moment(d)
opts.moment(d)
else d
return (!min or min.isBefore(date) or min.isSame(date, 'day')) and
(!max or max.isSame(date, 'day') or max.isAfter(date))
Expand All @@ -308,7 +309,7 @@ pickerModule.directive 'dateRangePicker', ($compile, $timeout, $parse, dateRange

if attrs[field]
$scope.$watch field, (date) ->
opts[optName] = if date then moment(date) else false
opts[optName] = if date then opts.moment(date) else false
if (_picker)
_picker[optName] = opts[optName]
$timeout -> modelCtrl.$validate()
Expand Down
14 changes: 7 additions & 7 deletions js/angular-daterangepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
};
_setDatePoint = function(setter) {
return function(newValue) {
if (newValue && (!moment.isMoment(newValue) || newValue.isValid())) {
newValue = moment(newValue);
if (newValue && (!opts.moment.isMoment(newValue) || newValue.isValid())) {
newValue = opts.moment(newValue);
} else {
return;
}
Expand Down Expand Up @@ -109,8 +109,8 @@
getViewValue = function(model) {
var f, viewValue;
f = function(date) {
if (!moment.isMoment(date)) {
return moment(date).format(opts.locale.format);
if (!opts.moment.isMoment(date)) {
return opts.moment(date).format(opts.locale.format);
} else {
return date.format(opts.locale.format);
}
Expand Down Expand Up @@ -146,7 +146,7 @@
var f, objValue, x;
f = function(value) {
var date;
date = moment(value, opts.locale.format);
date = opts.moment(value, opts.locale.format);
return (date.isValid() && date) || null;
};
objValue = opts.singleDatePicker ? null : {
Expand Down Expand Up @@ -272,7 +272,7 @@
if (date && (min || max)) {
ref = [date, min, max].map(function(d) {
if (d) {
return moment(d);
return opts.moment(d);
} else {
return d;
}
Expand All @@ -299,7 +299,7 @@
};
if (attrs[field]) {
return $scope.$watch(field, function(date) {
opts[optName] = date ? moment(date) : false;
opts[optName] = date ? opts.moment(date) : false;
if (_picker) {
_picker[optName] = opts[optName];
return $timeout(function() {
Expand Down