Created
April 17, 2012 20:47
-
-
Save mcwqy9/2408919 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Wolfpack.Views.LandingPage ||= {} | |
class Wolfpack.Views.LandingPage.EventsView extends Backbone.View | |
template: JST["backbone/templates/landing_page/events"] | |
tagName: "section" | |
id: "events" | |
initialize: () -> | |
@term = '' | |
window.context = { | |
event_search_day: '', | |
current_week: 0 | |
} | |
@collection = window.Events | |
@collection.on "reset", @render, @ | |
@collection.fetch() if @collection.length is 0 | |
updateDateRangeDisplay: ()=> | |
startDate = @$("li.day a:first").attr("data-date") | |
endDate = @$("li.day a:last").attr("data-date") | |
@$("#week_span").text("#{startDate} - #{endDate}") | |
advanceAWeek: () => | |
@setCurrentWeek(1) if @currentWeek() == 0 | |
moment().day(1).add('weeks', 1) | |
declineAWeek: () => | |
@setCurrentWeek(0) if @currentWeek() > 0 | |
moment().day(1) | |
buildDayNavigation: () -> | |
$navigation = @.$("#day_navigation ul") | |
current_week = @currentWeek() | |
$navigation.append @buildPreviousLink() | |
for day_of_week in [1..7] | |
day = moment().day(day_of_week) | |
day = day.add('days', 7) if current_week > 0 # Next week | |
view = new Wolfpack.Views.Shared.DayView({ day: day }) | |
$navigation.append view.render().el | |
$navigation.append @buildNextLink() | |
@updateSelectedDay() | |
$('#day_navigation ul li').bind 'click', @getEventsByDay | |
return this | |
buildNextLink: () -> | |
$link = $('<li></li>') | |
if @currentWeek() > 0 | |
$link.attr('style', 'display: none') | |
$link.html("<a href='#' id='next'>NEXT <i class='icon-arrow-right icon-white'></i></a>") | |
buildPreviousLink: () -> | |
$link = $('<li></li>') | |
if @currentWeek() == 0 | |
$link.attr('style', 'display: none') | |
$link.html("<a href='#' id='previous'><i class='icon-arrow-left icon-white'></i> PREV</a>") | |
currentDay: => | |
$("#day_navigation ul li.selected a").data 'date' | |
currentWeek: => | |
window.context['current_week'] | |
getEventsByDay: (e) => | |
e.preventDefault() | |
target = e.target | |
day = if target.id == 'next' | |
@advanceAWeek().format("M/D/YYYY") | |
else if target.id == 'previous' | |
@declineAWeek().format("M/D/YYYY") | |
else | |
$(target).data('date') | |
@setSelectedDay(target) | |
@updateSelectedDay(target) | |
$.getJSON '/events', { day: day }, (data) => | |
@collection.reset data | |
render: -> | |
$(@el).html @template( { term: @term, hasNoEvents: (@collection.length == 0) } ) | |
$event_list = @.$("#events_table") | |
@collection.each (event) -> | |
view = new Wolfpack.Views.Shared.EventView({ event: event }) | |
$event_list.append view.render().el | |
$('#event_search_button').bind 'click', @search | |
$('#event_search').bind 'keydown', @searchKeydown | |
@buildDayNavigation() | |
@updateDateRangeDisplay() | |
return this | |
searchKeydown: (e) => | |
if e.which == 13 | |
window.notify "test" | |
@search | |
search: (e) => | |
e.preventDefault() | |
term = $('#event_search').val() | |
current_day = @currentDay() | |
$.getJSON '/events/search', { term: term, day: current_day }, (data) => | |
@collection.reset data | |
getSelectedDay: () => | |
window.context['event_search_day'] | |
setCurrentWeek: (week) => | |
window.context['current_week'] = week | |
setSelectedDay: (day) => | |
window.context['event_search_day'] = unless day.id == 'next' || day.id == 'previous' | |
$(day).parent().prevAll().length | |
else | |
1 | |
updateSelectedDay: () => | |
day = @getSelectedDay() | |
unless day == '' | |
$("#day_navigation ul li").removeClass 'selected' | |
$("#day_navigation ul li:eq(#{day})").addClass 'selected' | |
else | |
$("#day_navigation ul li:eq(#{moment().day()})").addClass 'selected' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment