This repository was archived by the owner on May 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (96 loc) · 3.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _rx = require('rx');
var _cycleIsolate = require('@cycle/isolate');
var _cycleIsolate2 = _interopRequireDefault(_cycleIsolate);
var _cycleDom = require('@cycle/dom');
var _calendar = require('calendar');
var _lodash = require('lodash');
var calendar = new _calendar.Calendar();
var now = new Date();
var defaultProps = {
i18n: {
days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
},
year: now.getFullYear(),
month: now.getMonth(),
value: null
};
function intent(DOM) {
return _rx.Observable.merge(DOM.select('.prev').events('click').map(function () {
return -1;
}), DOM.select('.next').events('click').map(function () {
return +1;
}), DOM.select('.cal-date').events('click').map(function (x) {
return (0, _lodash.mapValues)((0, _lodash.toPlainObject)(x.target.dataset), Number);
})).startWith(null);
}
function model(props$, intent$) {
return _rx.Observable.combineLatest(props$, intent$, function (props, intent) {
if ((0, _lodash.isNumber)(intent)) {
props.month = props.month + intent;
if (props.month < 0) {
props.year--;
props.month = 11;
}
if (props.month > 11) {
props.year++;
props.month = 0;
}
}
if ((0, _lodash.isPlainObject)(intent)) {
props.value = intent;
}
return props;
});
}
function view(model$) {
return model$.map(function (props) {
return (0, _cycleDom.div)('.cal-wg', (0, _cycleDom.table)([(0, _cycleDom.tr)([(0, _cycleDom.th)((0, _cycleDom.input)('.prev', { type: 'button', value: '<' })), (0, _cycleDom.th)({ colSpan: 5 }, props.i18n.months[props.month].substring(0, 3) + ' ' + props.year), (0, _cycleDom.th)((0, _cycleDom.input)('.next', { type: 'button', value: '>' }))]), (0, _cycleDom.tr)(props.i18n.days.map(function (x) {
return (0, _cycleDom.th)(x.substring(0, 2));
}))].concat(calendar.monthDates(props.year, props.month).map(function (w) {
return (0, _cycleDom.tr)(w.map(function (d) {
return (0, _cycleDom.td)('.cal-date' + (d.getMonth() != props.month ? ' .minor' : '') + (d.getDate() == now.getDate() && d.getMonth() == now.getMonth() && d.getFullYear() == now.getFullYear() ? ' .today' : '') + ((0, _lodash.isPlainObject)(props.value) && d.getDate() === props.value.day && d.getMonth() === props.value.month && d.getFullYear() === props.value.year ? ' .selected' : ''), {
style: {
cursor: 'pointer'
},
dataset: {
year: d.getFullYear(),
month: d.getMonth(),
day: d.getDate()
}
}, d.getDate().toString());
}));
}))));
});
}
function CalendarWidget(_ref) {
var DOM = _ref.DOM;
var props$ = _ref.props$;
if (!props$) {
props$ = _rx.Observable.of({});
}
var initProps$ = props$.merge(_rx.Observable.of(defaultProps)).reduce(function (x, y) {
return (0, _lodash.defaultsDeep)(x, y);
});
var intent$ = intent(DOM);
var model$ = model(initProps$, intent$);
var view$ = view(model$);
var value$ = intent$.filter(function (intent) {
return (0, _lodash.isPlainObject)(intent);
}).map(function (value) {
return new Date(value.year, value.month, value.day);
}).startWith(null);
return {
DOM: view$,
value$: value$
};
}
exports['default'] = function (sources) {
return (0, _cycleIsolate2['default'])(CalendarWidget)(sources);
};
module.exports = exports['default'];