Last active
August 29, 2015 14:06
-
-
Save sheldonbaker/b880d79f15938c20dc62 to your computer and use it in GitHub Desktop.
recurrence-form component - for usage with https://github.com/jakubroztocil/rrule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`import Ember from 'ember'` | |
RecurrenceFormComponent = Ember.Component.extend | |
tagName: 'fieldset' | |
minEndsOn: null | |
maxEndsOn: null | |
value: ((key, value) -> | |
if arguments.length > 1 | |
if value | |
value = (new RRule(RRule.parseString(value))) | |
@set('frequency', value.options.freq) | |
@set('interval', value.options.interval) | |
@set('weekDays', value.options.byweekday) | |
if value.options.until | |
@set('endsOnMoment', moment.utc(value.options.until)) | |
else | |
# false-y value - clear all | |
@set('frequency', null) | |
@set('interval', null) | |
@set('weekDays', null) | |
@set('endsOnMoment', null) | |
value | |
else | |
return null unless freq = @get('frequency') | |
options = { | |
freq: freq | |
interval: @get('interval') | |
byweekday: @get('weekDays') | |
} | |
if endsOn = @get('endsOnMoment') | |
options.until = endsOn.utc().toDate() | |
new RRule(options).toString() | |
).property('frequency', 'interval', 'weekDays', 'endsOnMoment') | |
frequencies: (-> | |
never = [{ value: null, label: 'Never' }] | |
others = ['Daily', 'Weekly'].map (frequency) -> { value: RRule[frequency.toUpperCase()], label: frequency } | |
never.concat(others) | |
).property() | |
intervals: (-> | |
if @get('frequencyIsWeekly') | |
[1..4] | |
else | |
[1..30] | |
).property('frequencyIsWeekly') | |
frequency: null | |
interval: null | |
frequencyIsNever: Ember.computed.not 'frequency' | |
frequencyIsWeekly: Ember.computed.equal 'frequency', RRule.WEEKLY | |
onMonday: null | |
onTuesday: null | |
onWednesday: null | |
onThursday: null | |
onFriday: null | |
onSaturday: null | |
onSunday: null | |
# Sync between array of days and onMonday, onTuesday, etc. | |
# (useful for multiple checkboxes like [] S [] M [] T [] W [] T [] F [] S) | |
weekDays: ((key, value) -> | |
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] | |
if arguments.length > 1 | |
if value | |
value.forEach (dayIdx) => | |
@set('on' + days[dayIdx], true) | |
value | |
else | |
ret = [] | |
return ret unless @get('frequencyIsWeekly') | |
days.forEach (day) => | |
ret.push RRule[day.substr(0, 2).toUpperCase()] if @get('on' + day) | |
ret | |
).property('frequencyIsWeekly', 'onMonday', 'onTuesday', 'onWednesday', 'onThursday', 'onFriday', 'onSaturday', 'onSunday') | |
endsOnMoment: Ember.computed.defaultTo 'defaultEndsOnMoment' | |
defaultEndsOnMoment: (-> | |
(@get('minEndsOn') || moment()).clone().add('weeks', 2) | |
).property('endsOnMoment') | |
actions: | |
toggle: -> | |
@toggleProperty('isVisible') | |
`export default RecurrenceFormComponent` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment