Skip to content

Instantly share code, notes, and snippets.

@lusentis
Created June 27, 2012 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lusentis/3005037 to your computer and use it in GitHub Desktop.
Save lusentis/3005037 to your computer and use it in GitHub Desktop.
Plasticpanda Date Picker
# PlasticPanda.com - Date Picker
## cofigurable parameters
cssPrefix = 'pandapicker-'
template_html = """
<table role="datepicker">
<thead>
<tr>
<th class="#{cssPrefix}prev" role="prevMonth">&laquo;</th>
<th class="#{cssPrefix}title" colspan="5" role="title"></th>
<th class="#{cssPrefix}next" role="nextMonth">&raquo;</th>
</tr>
<tr>
<th>#{dayNames[0]}</th>
<th>#{dayNames[1]}</th>
<th>#{dayNames[2]}</th>
<th>#{dayNames[3]}</th>
<th>#{dayNames[4]}</th>
<th>#{dayNames[5]}</th>
<th>#{dayNames[6]}</th>
</tr>
</thead>
<tbody>
</tbody>
</table> """
$.fn.pandaPicker = (options) ->
options = $.extend {
format: 'DD-MM-YYYY'
}, options
updateCalendar = (me, template, startDate) ->
daysInMonth = startDate.daysInMonth()
startWeekday = startDate.clone().date(1).day()
currentMonth = startDate.month()+1
weeksNum = Math.ceil (startWeekday + daysInMonth) / 7
# Coffee rocks! <3
$('tbody', template).html('')
($('tbody', template).append "<tr>"+("#{(if 7*week+day-startWeekday > 0 and 7*week+day-startWeekday <= daysInMonth then "<td>#{7*week+day-startWeekday}</td>" else "<td></td>")}" for day in [1..7])+"</tr>") for week in [0..weeksNum-1]
$('td:not(:empty)', template).click (ev) ->
startDate.date parseInt $(@).html()
me.val startDate.format options.format
$('[role="title"]').html startDate.format 'MMMM YYYY'
init = () =>
me = $(@)
template = $(template_html)
template.insertAfter me
if me.val()
startDate = moment me.val(), options.format
else
startDate = moment()
updateCalendar me, template, startDate
$('[role="nextMonth"]').click (ev) ->
startDate.add 'M', 1
updateCalendar me, template, startDate
$('[role="prevMonth"]').click (ev) ->
startDate.subtract 'M', 1
updateCalendar me, template, startDate
# bind
$(@).click (evt) ->
$('table[role="datepicker"]').first().remove()
init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment