Skip to content
Browse files

[enhance] CCalendar: Adding displayed date range in controls.

Calendar controls now include a label displaying the range of
dates visible in the calendar. The range depends on the mode
of the calendar so in weekly mode in can look something like:

  15 Aug -- 1 Sep 2011

(month & year visible in the beginning date only if different
than in the end date) and in the monthly view:

  September 2011
  • Loading branch information...
1 parent 5b79024 commit 955ab50185a4d1d11588651dc9ff9ef70df0983b @akoprow akoprow committed Nov 1, 2011
Showing with 45 additions and 7 deletions.
  1. +32 −4 stdlib/components/calendar/calendar.opa
  2. +13 −3 stdlib/components/calendar/calendar_controls.opa
View
36 stdlib/components/calendar/calendar.opa
@@ -171,6 +171,9 @@ type CCalendar.msg('event) =
/* shutting down the calendar component */
/ { Shutdown }
+ // ------------------ callbacks -----------------
+ / { UpdateCallbacks : CCalendar.callbacks('event) -> CCalendar.callbacks('event) }
+
type CCalendar.callbacks('event) =
{
/* the viewing mode or the visible date range has changed */
@@ -294,23 +297,26 @@ type CCalendar.state('event) =
update_state_and_refresh(state, new_state)
@private on_message(state : CCalendar.state, msg, channel) =
+ upgrade = update_state_and_refresh(state, _)
match msg with
| {Next} -> on_message(state, {Move = 1}, channel)
| {Prev} -> on_message(state, {Move = -1}, channel)
| {Move = by} -> move_by(by, state)
| {GoToday} -> on_message(state, {SetDate = Date.now()}, channel)
- | {SetDate = date} -> update_state_and_refresh(state, set_date(state, date))
+ | {SetDate = date} -> upgrade(set_date(state, date))
| {SetMode = mode} ->
new_state = {state with ~mode}
change_mode_to(state, new_state)
| {ChangeMode = mode} ->
new_state = change_mode(state, mode)
change_mode_to(state, new_state)
- | {ChangeConfig = config} -> update_state_and_refresh(state, { state with ~config })
- | {Refresh} -> update_state_and_refresh(state, state)
+ | {ChangeConfig = config} -> upgrade({ state with ~config })
+ | {Refresh} -> upgrade(state)
| {Startup ~redraw_handler} ->
- update_state_and_refresh(state, {state with redraw_handler=some(redraw_handler)})
+ upgrade({state with redraw_handler=some(redraw_handler)})
| {Shutdown} -> calendar_shutdown(state)
+ | {UpdateCallbacks=f} ->
+ upgrade({state with callbacks=f(state.callbacks)})
| {AddEvent=_}
| {RemoveEvent=_}
| {ModifyEvent=_}
@@ -828,11 +834,33 @@ type CCalendar.state('event) =
| {~SetDate} -> {~SetDate}
| {~Refresh} -> {~Refresh}
| {~Shutdown} -> {~Shutdown}
+ | {~UpdateCallbacks} -> {~UpdateCallbacks}
Session.send(c, msg)
redraw(c : CCalendar.instance) : void =
perform(c, {Refresh})
+// ***************************************************************************************
+ /**
+ * {2 Auxilary functions}
+ **/
+// ***************************************************************************************
+ date_range_string(mode : CCalendar.mode) : string =
+ match mode with
+ | ~{month} -> "{month.month} {month.year}"
+ | {weeks=~{no start_at}} ->
+ end_at = Date.advance(start_at, Duration.weeks(no))
+ |> Date.advance(_, Duration.days(-1))
+ start_day = Date.get_day(start_at)
+ dont_repeat(f) =
+ if f(start_at) == f(end_at) then
+ ""
+ else
+ "{f(start_at)} "
+ start_month = dont_repeat(Date.get_month)
+ start_year = dont_repeat(Date.get_year)
+ "{start_day} {start_month}{start_year}– {Date.to_string_date_only(end_at)}"
+
}}
// ***************************************************************************************
View
16 stdlib/components/calendar/calendar_controls.opa
@@ -21,10 +21,10 @@
* @author Adam Koprowski, 2011
*/
-
import stdlib.widgets.core
import stdlib.widgets.button
import stdlib.widgets.datepicker
+import stdlib.components.fragment
// ***************************************************************************************
/**
@@ -60,9 +60,18 @@ type CCalendarControls.config('event) =
toggled_style = mk_button_stl("toggled")
}
+ @private register_date_range_view(cal) =
+ update_date(_state, {NewMode=mode}) =
+ {re_render=<>{CCalendar.date_range_string(mode)}</>}
+ (date_range_xhtml, date_range_fragment) = CFragment.create(void, <></>, update_date)
+ ViewChanged(mode) = CFragment.notify(date_range_fragment, {NewMode=mode})
+ do CCalendar.perform(cal, {UpdateCallbacks=(callbacks -> {callbacks with ~ViewChanged })})
+ date_range_xhtml
+
extensible_style_config =
{
generate(id, cal) =
+ date_range_xhtml = register_date_range_view(cal)
// buttons
button = generate_button(cal, make_buttons_style("ccalendar_ext_ctrl_btn"), _, _, _)
// left panel
@@ -91,7 +100,7 @@ type CCalendarControls.config('event) =
// html
<div id={id} style={panel_style}>
<span style={left_panel_style}>
- {today}{prev}{next}{goto}
+ {today}{prev}{next}{date_range_xhtml}{goto}
</>
<span style={right_panel_style}>
{two_weeks_view}{month_view}
@@ -102,6 +111,7 @@ type CCalendarControls.config('event) =
google_style_config =
{
generate(id, cal) =
+ date_range_xhtml = register_date_range_view(cal)
// buttons
buttons_config = make_buttons_style("ccalendar_ext_ctrl_btn")
blue_button = generate_button(cal, buttons_config, _, _, _)
@@ -131,7 +141,7 @@ type CCalendarControls.config('event) =
// xhtml
<div style={panel_style} id={id}>
<span style={left_panel_style}>
- {today}{prev}{next}
+ {today}{prev}{next}{date_range_xhtml}
</>
<span style={right_panel_style}>
{month_view}{two_weeks_view}

0 comments on commit 955ab50

Please sign in to comment.
Something went wrong with that request. Please try again.