Skip to content

Commit

Permalink
move logic to calendarmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
fiskus committed Oct 21, 2015
1 parent d04549a commit 9bbd1fa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
48 changes: 5 additions & 43 deletions lib/calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import moment from 'moment';

require('moment/locale/ru.js');

import * as CalendarModel from './calendarmodel.js';
import Controls from './controls.jsx';
import Helpers from './helpers.js';
import Month from './month.jsx';
Expand All @@ -16,7 +17,7 @@ export default class Calendar extends React.Component {
moment.locale(props.LANG);

this.state = {
date: getInitialDate(props)
date: CalendarModel.getInitialDate(props)
};
}

Expand All @@ -28,7 +29,7 @@ export default class Calendar extends React.Component {

onNext () {
var currentMoment = this.state.date;
if (isAbleToScrollRigtht(this.props, currentMoment)) {
if (CalendarModel.isAbleToScrollRigtht(this.props, currentMoment)) {
this.setState({
date: currentMoment.add(1, 'months')
});
Expand All @@ -37,7 +38,7 @@ export default class Calendar extends React.Component {

onPrev () {
var currentMoment = this.state.date;
if (isAbleToScrollLeft(this.props, currentMoment)) {
if (CalendarModel.isAbleToScrollLeft(this.props, currentMoment)) {
this.setState({
date: currentMoment.subtract(1, 'months')
});
Expand All @@ -48,7 +49,7 @@ export default class Calendar extends React.Component {
let months = Helpers.getRange(1, this.props.UI_MONTHS_NUMBER).map(i => {
let monthProps = {
DATE: this.state.date.clone().add(i - 1, 'month'),
DATE_SELECTS: parseSelects(this.props),
DATE_SELECTS: CalendarModel.parseSelects(this.props),
key: i
}
return <Month {...this.props} {...monthProps} />;
Expand Down Expand Up @@ -93,42 +94,3 @@ Calendar.defaultProps = {
UI_TEXT_PREV: 'Prev',
WEEK_OFFSET: 0
};

function getInitialDate (props) {
// NOTE: there is DEFAUT_DATE and DATE
// TODO: leave only one date DEFAUT_DATE or DATE
return props.DATE_CURRENT ? moment(props.DATE_CURRENT) : moment();
}

function parseSelects (props) {
var internalSelects = {};
if (props.DATE_SELECTS) {
props.DATE_SELECTS.forEach(select => {
var ymd = moment(select.DATE).format('YYYY-MM-DD');
internalSelects[ymd] = select.CLASSNAME;
});
}
return internalSelects;
}

function isAbleToScrollLeft (props, currentMoment) {
var minDate = props.DATE_MIN;
if (minDate) {
return currentMoment.isAfter(minDate, 'month');
} else {
return true;
}
}

function isAbleToScrollRigtht (props, currentMoment) {
var maxDate = props.DATE_MAX;
if (props.UI_MONTHS_NUMBER && props.UI_MONTHS_NUMBER > 1) {
currentMoment = currentMoment.clone().add(
props.UI_MONTHS_NUMBER - 1, 'month');
}
if (maxDate) {
return currentMoment.isBefore(maxDate, 'month');
} else {
return true;
}
}
38 changes: 38 additions & 0 deletions lib/calendarmodel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import moment from 'moment';

export function getInitialDate (props) {
// NOTE: there is DEFAUT_DATE and DATE
// TODO: leave only one date DEFAUT_DATE or DATE
return props.DATE_CURRENT ? moment(props.DATE_CURRENT) : moment();
}

export function parseSelects (props) {
let internalSelects = {};
if (props.DATE_SELECTS) {
props.DATE_SELECTS.forEach(select => {
let ymd = moment(select.DATE).format('YYYY-MM-DD');
internalSelects[ymd] = select.CLASSNAME;
});
}
return internalSelects;
}

export function isAbleToScrollLeft (props, currentMoment) {
if (props.DATE_MIN) {
return currentMoment.isAfter(props.DATE_MIN, 'month');
} else {
return true;
}
}

export function isAbleToScrollRigtht (props, currentMoment) {
if (props.UI_MONTHS_NUMBER && props.UI_MONTHS_NUMBER > 1) {
currentMoment = currentMoment.clone().add(
props.UI_MONTHS_NUMBER - 1, 'month');
}
if (props.DATE_MAX) {
return currentMoment.isBefore(props.DATE_MAX, 'month');
} else {
return true;
}
}
12 changes: 6 additions & 6 deletions specs/calendar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
require('../lib/bindshim.js');

var moment = require('moment');
var C = require('../lib/calendar.jsx');
var CalendarModel = require('../lib/calendarmodel.js');

describe('Calendar', function () {
xit('getInitialDate, default', function () {
expect(C.getInitialDate({})._isAMomentObject).toBe(true);
expect(C.getInitialDate({}).isValid()).toBe(true);
it('getInitialDate, default', function () {
expect(CalendarModel.getInitialDate({})._isAMomentObject).toBe(true);
expect(CalendarModel.getInitialDate({}).isValid()).toBe(true);
});

xit('getInitialDate, set', function () {
expect(C.getInitialDate({
it('getInitialDate, set', function () {
expect(CalendarModel.getInitialDate({
DATE_CURRENT: new Date('2015-04-30')
}).isSame(moment('30-04-2015', 'DD-MM-YYYY'), 'day')).toBe(true);
});
Expand Down

0 comments on commit 9bbd1fa

Please sign in to comment.