diff --git a/src/Calendar.jsx b/src/Calendar.jsx index ce472401a..58b3135d9 100644 --- a/src/Calendar.jsx +++ b/src/Calendar.jsx @@ -12,31 +12,14 @@ import { notify } from './utils/helpers'; import { navigate, views } from './utils/constants'; import dates from './utils/dates'; import defaultFormats from './formats'; - -import Month from './Month'; -import Day from './Day'; -import Week from './Week'; -import Agenda from './Agenda'; +import viewLabel from './utils/viewLabel'; +import moveDate from './utils/move'; +import VIEWS from './Views'; import Toolbar from './Toolbar'; import omit from 'lodash/object/omit'; import defaults from 'lodash/object/defaults'; -const VIEWS = { - [views.MONTH]: Month, - [views.WEEK]: Week, - [views.DAY]: Day, - [views.AGENDA]: Agenda -}; - -const Formats = { - [views.MONTH]: 'monthHeaderFormat', - [views.WEEK]: 'dayRangeHeaderFormat', - [views.DAY]: 'dayHeaderFormat', - [views.AGENDA]: 'agendaHeaderFormat' -} - - function viewNames(_views){ return !Array.isArray(_views) ? Object.keys(_views) : _views } @@ -232,18 +215,9 @@ let Calendar = React.createClass({ formats = defaultFormats(formats) let View = VIEWS[view]; - let headerSingle = view === views.MONTH || view === views.DAY - + let label = viewLabel(current, view, formats, culture) let names = viewNames(this.props.views) - let { start, end } = View.range(current, this.props); - - let headerFormat = formats[Formats[view]]; - - let label = headerSingle - ? localizer.format(current, headerFormat, culture) - : localizer.format({ start, end }, headerFormat, culture) - let elementProps = omit(this.props, Object.keys(Calendar.propTypes)) let viewComponents = defaults( @@ -286,16 +260,7 @@ let Calendar = React.createClass({ _navigate(action, newDate) { let { view, date, onNavigate } = this.props; - switch (action){ - case navigate.TODAY: - date = new Date() - break; - case navigate.DATE: - date = newDate - break; - default: - date = VIEWS[view].navigate(newDate || date, action) - } + date = moveDate(action, newDate || date, view) onNavigate(date, view) diff --git a/src/Views.js b/src/Views.js new file mode 100644 index 000000000..f3fb49e6a --- /dev/null +++ b/src/Views.js @@ -0,0 +1,14 @@ +import { views } from './utils/constants'; +import Month from './Month'; +import Day from './Day'; +import Week from './Week'; +import Agenda from './Agenda'; + +const VIEWS = { + [views.MONTH]: Month, + [views.WEEK]: Week, + [views.DAY]: Day, + [views.AGENDA]: Agenda +}; + +export default VIEWS; diff --git a/src/index.js b/src/index.js index f5810c0db..0effb1b7d 100644 --- a/src/index.js +++ b/src/index.js @@ -2,11 +2,15 @@ import Calendar from './Calendar'; import { set as setLocalizer } from './localizer'; import momentLocalizer from './moment-localizer'; import globalizeLocalizer from './globalize-localizer'; +import viewLabel from './utils/viewLabel'; +import move from './utils/move'; Object.assign(Calendar, { setLocalizer, globalizeLocalizer, - momentLocalizer + momentLocalizer, + label: viewLabel, + move }) export default Calendar diff --git a/src/utils/move.js b/src/utils/move.js new file mode 100644 index 000000000..2dc4f8058 --- /dev/null +++ b/src/utils/move.js @@ -0,0 +1,16 @@ +import { navigate } from './constants'; +import VIEWS from '../Views'; + +export default function moveDate(action, date, view){ + switch (action){ + case navigate.TODAY: + date = new Date() + break; + case navigate.DATE: + break; + default: + date = VIEWS[view].navigate(date, action) + } + + return date +} diff --git a/src/utils/viewLabel.js b/src/utils/viewLabel.js new file mode 100644 index 000000000..fb58760ae --- /dev/null +++ b/src/utils/viewLabel.js @@ -0,0 +1,25 @@ +import { views } from './constants'; +import defaultFormats from '../formats'; +import localizer from '../localizer'; + +import VIEWS from '../Views'; + +const Formats = { + [views.MONTH]: 'monthHeaderFormat', + [views.WEEK]: 'dayRangeHeaderFormat', + [views.DAY]: 'dayHeaderFormat', + [views.AGENDA]: 'agendaHeaderFormat' +} + +export default function viewLabel(date, view, formats, culture){ + let View = VIEWS[view]; + let headerSingle = view === views.MONTH || view === views.DAY + + formats = defaultFormats(formats || {}) + + let headerFormat = formats[Formats[view]]; + + return headerSingle + ? localizer.format(date, headerFormat, culture) + : localizer.format(View.range(date, { culture }), headerFormat, culture) +}