Skip to content

Commit

Permalink
[added] expose move and label methods for easier external toolbars
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Sep 13, 2015
1 parent 7e7bc17 commit ae039b9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 41 deletions.
45 changes: 5 additions & 40 deletions src/Calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions src/Views.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions src/utils/move.js
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions src/utils/viewLabel.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit ae039b9

Please sign in to comment.