mattlewis92 / angular-bootstrap-calendar

A port of the bootstrap calendar widget to AngularJS (no jQuery required!)
https://mattlewis92.github.io/angular-bootstrap-calendar/
MIT License
798 stars 369 forks source link

Is there a way to prevent dragging events to past days? #175

Closed jmsherry closed 9 years ago

jmsherry commented 9 years ago

Hi Matt, I know this isn't a bug, more an enhancement (to be able to specify the option), but it'd be a handy option to be able to put in (although I'm not sure how atm!)

Thanks

James

mattlewis92 commented 9 years ago

On your on-event-times-changed expression pass in a function which checks if calendarNewEventStart or calendarNewEventEnd is in the past and shows a modal or something similar alerting the user that the event is in the past (and then don't change the event times)

jmsherry commented 9 years ago

Yes, I did that, but it leaves the dot where the user left it, so the UI is then in an incorrect state. I even took note of the pre-drag position and after the check tried to revert it but the drag puts it back in the wrong place.

mattlewis92 commented 9 years ago

Oh that might be a bug then, it should snap back to where it was originally whether you change the dates or not.

jmsherry commented 9 years ago

Best I've come up with at the moment is blocking the server-side save and then reloading the view in UI-Router (or you could do it with $window.location.reload()) - but that's a temporary fix, obviously.

mattlewis92 commented 9 years ago

I can't seem to replicate the issue you're having, can you reproduce it in a plunker or something similar?

jmsherry commented 9 years ago

Hey Matt, Do you want to close the issue whilst I rig up a plunkr and we can have a look (only because it might take me a few days atm)?? If it is an issue you can always re-open it...

mattlewis92 commented 9 years ago

Sure, no problem :smile:

jmsherry commented 9 years ago

Matt, I've just cloned the project demo and I can move things into the past and they don't get put back...?? Just before I go make plunkrs can you confirm whether this happens for you?

Thanks Matt :)

mattlewis92 commented 9 years ago

Yeah that's the expected behaviour - you need to implement the business logic yourself as I explained here: https://github.com/mattlewis92/angular-bootstrap-calendar/issues/175#issuecomment-143659639

e.g. on-event-times-changed="vm.eventTimesChanged(calendarEvent, calendarNewEventStart, calendarNewEventEnd)"

In your controller:

vm.eventTimesChanged = function(event, newStart, newEnd) {
  if (moment(newStart).isAfter(moment())) {
    event.startsAt = newStart;
    event.endsAt = newEnd;
  } else {
    //Show modal etc saying the event is in the past
  }
}

Hope this helps :smile:

jmsherry commented 9 years ago

Geezas!! Sorry Matt! I've got my mother visiting - I could just stop there. I may have even less hair than my avatar by the end of this... :D

Yes, you're absolutely right. That's OBVIOUSLY the case. :D SO, I've set up a plunkr:

http://plnkr.co/edit/cpzJUE4MwuT0WoT94Xg7

I'll edit the eventTimesChanged method appropriately...

//if date is in the past return and error toast.
if (moment(newStartTime).isBefore(now, 'day')) {
  toastr.warning('That date is in the past and will be disregarded; Resetting calendar...');
  return scheduledMeal;
}

...that was what I had in my code, just to prove I'm not a total numpty!

jmsherry commented 9 years ago

OK, plunkr is ready.

mattlewis92 commented 9 years ago

Sorry about the late reply on this, been a really busy few days. If you look at the expression you're passing to on-event-times-changed you left in the bit from the demo which changes the calendar dates. So it was:

on-event-times-changed="vm.eventTimesChanged(calendarEvent, calendarNewEventStart); calendarEvent.startsAt = calendarNewEventStart; calendarEvent.endsAt = calendarNewEventEnd"

and it should be

on-event-times-changed="vm.eventTimesChanged(calendarEvent, calendarNewEventStart);"

:smile:

jmsherry commented 9 years ago

Hi Matt, No need to apologise - I'm very grateful you've taken the time - very kind of you. :)

That seems to have fixed it, which is great news.

I wondered, before I go, if I might ask your opinion on a quick matter?

I've forked the calendar for my own purposes (I can always solidify it later and offer it as a PR, should you wish) to give it a date-picking ability and I'm stumped by a binding issue...

You pass vm.events through the calendar controller and its child controllers using the '=' two way bind. I've matched what you've done with events with another collection (vm.meals, in my case) but it comes up as undefined by the time it reaches the child controller.

Repo is: https://github.com/jmsherry/angular-bootstrap-calendar

I wondered if you could take a quick look? The only thing I can think is that something is executing with the watch statements and a race condition is occurring...

Thanks Matt.

mattlewis92 commented 9 years ago

Sorry I couldn't work out quite what you meant, could you highlight whereabouts in the code you're getting the undefined variable.

jmsherry commented 9 years ago

AH! Got it! I didn't see that the cells were sub directives - I thought it just passed down the scope chain!

Thanks for your help and patience, Matt. :+1: