Baremetrics / calendar

Date range picker for Baremetrics
MIT License
681 stars 79 forks source link

Earliest date not respected in presets #14

Closed greenafrican closed 9 years ago

greenafrican commented 9 years ago

Hello there,

Firstly, thanks for sharing an awesome calendar picker!

I've noticed that if I set the earliest_date to something like 1st Dec 2014 it isn't respected by the 1 Year preset (and others), which sets the start_date to 27 July 2014, for example.

Ciao ;)

kalepail commented 9 years ago

Seems to work for me, are you sure you're formatting it correctly? It needs to be an actual date object.

...
earliest_date: new Date('December 1, 2014'),`
...
greenafrican commented 9 years ago
...
earliest_date: new Date("October 1, 2014"),
...

Screenshot - https://cloudup.com/c1zKpw7-jdH

kalepail commented 9 years ago

Ahhh I see what you're saying. Yep that's an issue. Sorry 'bout that.

greenafrican commented 8 years ago

Just wondering what happened to this? Do you prefer to leave the presets unchanged?

kalepail commented 8 years ago

@greenafrican Is it still an issue? I closed it forever ago. Are you still experiencing the problem in the latest update?

greenafrican commented 8 years ago

@tyvdh Sorry, I know this issue is updated. I'm now looking to update earliest_date on the fly and have the presets update. I guess a method is needed. Would you like a PR?

kalepail commented 8 years ago

@greenafrican I wouldn't merge it into the main repo since this isn't a core feature. All you really need to do is update the instance variable and then refresh the calendar. All of which you can already do.

greenafrican commented 8 years ago

@tyvdh Ok, thanks. What is the best way to refresh? .presetsCreate() looks like it'll work.

kalepail commented 8 years ago

Yep. What we use @Baremetrics is something to the flavor of:

calendar.start_date = moment().subtract(1, 'month');
calendar.end_date = moment();
calendar.calendarSetDates();

var range = moment(calendar.end_date).diff(calendar.start_date, 'days');

calendar.presets = presetArray(calendar, range);
calendar.presetCreate();

function presetArray(CalBase, range) {
  return [{
    label: 'Previous Period',
    start: moment(CalBase.start_date).subtract(range + 1, 'days'),
    end: moment(CalBase.end_date).subtract(range + 1, 'days')
  },{
    label: '3 months previous',
    start: moment(CalBase.start_date).subtract(3, 'months'),
    end: moment(CalBase.end_date).subtract(3, 'months')
  },{
    label: '6 months previous',
    start: moment(CalBase.start_date).subtract(6, 'months'),
    end: moment(CalBase.end_date).subtract(6, 'months')
  },{
    label: 'Previous year',
    start: moment(CalBase.start_date).subtract(1, 'year'),
    end: moment(CalBase.end_date).subtract(1, 'year')
  }]
}

Good luck!

greenafrican commented 8 years ago

Do you mean:

calendar.settings.presets = presetArray(calendar, range);

Otherwise, .presetsCreate() defaults the hard-coded presets?

kalepail commented 8 years ago

Yeah. It would update your dates in the preset list I think though. Honestly I'm not 100% on exactly what does what. Been too long since I worked on that piece of it. Just fool around with a couple of the functions and see what they do. I do know you'll at least need to updat your default dates before redrawing any calendar HTML