Skip to content

Commit

Permalink
Merge branch 'master' of github.com:camdub/SAP
Browse files Browse the repository at this point in the history
  • Loading branch information
kcalmes committed Dec 6, 2011
2 parents bef50d6 + a1aacb6 commit 591e1b2
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 35 deletions.
Binary file removed .DS_Store
Binary file not shown.
29 changes: 15 additions & 14 deletions app/assets/javascripts/app.js.coffee
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
window.App =
Models: {}
Collections: {}
Views: {}
Routers: {}
init: (events) -> # called inside document.ready
@events = new App.Collections.Events(events) # this var can be accessed with App.events
@advisors = new App.Collections.Advisors() # this var can be accessed with App.events
@advisors.fetch({async:false})
@current_user = new App.Models.User(JSON.parse($("#current_user").attr("user")))
new App.Routers.Events
Backbone.history.start();

# END INIT FUNC

window.App =
Models: {}
Collections: {}
Views: {}
Routers: {}
init: (events) -> # called inside document.ready
@events = new App.Collections.Events(events) # this var can be accessed with App.events
@advisors = new App.Collections.Advisors() # this var can be accessed with App.events
@advisors.fetch({async:false})
@current_user = new App.Models.User(JSON.parse($("#current_user").attr("user")))
new App.Routers.Events
Backbone.history.start();

# END INIT FUNC


9 changes: 3 additions & 6 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//

//= require jquery
//= require jquery_ujs

//= require_self

//= require underscore
//= require backbone
//= require backbone.authtokenadapter
Expand Down
4 changes: 4 additions & 0 deletions app/assets/javascripts/collections/appointments.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class App.Collections.Appointments extends Backbone.Collection

model: App.Models.Appointment
url: '/appointments'
3 changes: 3 additions & 0 deletions app/assets/javascripts/models/appointment.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class App.Models.Appointment extends Backbone.Model

urlRoot: '/appointments'
6 changes: 6 additions & 0 deletions app/assets/javascripts/models/event.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ class App.Models.Event extends Backbone.Model

defaults:
'title' : 'Open'
#'appointments' : []

initialize: -> # set a collection of appointments
appointments = new App.Collections.Appointments
appointments.reset(@get('appointments')) if @get('appointments') != undefined
@appointments = appointments

validate: ->
errors = []
if @get 'start' > @get 'end'
Expand Down
44 changes: 39 additions & 5 deletions app/assets/javascripts/views/event_detail.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ class App.Views.EventDetailView extends Backbone.View

events:
"click" : "popover"
"click .book" : "edit_appointment"

initialize: ->
# bind events from this model's appointment collection
@model.appointments.bind('add', @render_appointment)

render: ->
@el.popover('show')
@

popover: ->
popover: (e) ->
$('.popover').remove()
@render()
$('.close').live 'click', =>
@el.popover('hide')
$('input[name="student_name"]').focus()

#edit.attr('event',"#{@model.get('id')}")
$('.close').click =>
@el.popover('hide')

$('.edit').click =>
console.log 'clicked'
if $('.edit').html() == 'edit'
view = new App.Views.NewEventView(model: @model, collection: @collection, edit: true)
view.render()
Expand All @@ -27,8 +31,38 @@ class App.Views.EventDetailView extends Backbone.View
if c
@model.destroy()
@collection.remove(@model)

text = $('#new_apt')
text.keypress (e) =>
if(!text || e.keyCode != 13)
return
else
model = new App.Models.Appointment(title: text.val(), status: 'Scheduled', user_id: App.current_user.get('id'), event_id: @model.id)
model.save({},
success: (m, r) =>
@el.popover('hide')
)
@model.appointments.add(model)
@model.save(title: text.val())

render_appointment: (apt) =>
@collection.trigger('change', @collection.get(@model.id))

edit_appointment: =>
value = @$("#book-#{@model.id} a")
console.log value
if value.html() == 'remove'
@el.popover('hide')
c = confirm("Are you sure you want to remove #{@model.get('title')} from this appointment?")
if c
@model.save(title: 'Open', color: 'green')
@model.appointments.remove(@model.appointments.at(0))
@collection.trigger('change', @collection.get(@model.id))

###
focus_name_field: ->
$('input[name="student_name"]').focus()
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/views/event_new.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class App.Views.NewEventView extends Backbone.View
start = @extract_date('start')
end = @extract_date('end')

@collection.create(new App.Models.Event( start: start, end: end, event_type: 'Busy', title: @$('[name="apt_title"]').val()),
@collection.create(new App.Models.Event( start: start, end: end, event_type: 'Busy', title: @$('[name="apt_title"]', appointments: []).val()),
success: (model, response) =>
$('.modal').modal('hide')
return
Expand Down
25 changes: 19 additions & 6 deletions app/assets/javascripts/views/events_index.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ class App.Views.EventIndex extends Backbone.View
# Loads all events in the collection into the calendar (creates event obj)
# based on the current user and which user's calendar is currently selected
addAll: (user) =>
# add colors for events
@collection.each((event) ->
event.color = 'red' if event.get('event_type') == 'Busy'
event.color = 'green' if event.get('event_type') == 'Open'
)

this_users_events = @collection.filter( (event)->
return event.get("user_data").netid == user.get("netid")
return event.get("user").netid == user.get("netid")
)
this_users_events = _.map(this_users_events, (event)->
event.color = 'red' if event.get('event_type') == 'Busy'
event.color = 'green' if event.get('event_type') == 'Open'
return event.toJSON()
)
)

@el.fullCalendar('addEventSource', this_users_events)
$('.fc-event').fadeIn(1000);

Expand Down Expand Up @@ -83,7 +92,6 @@ class App.Views.EventIndex extends Backbone.View
# check to see if this user can edit current calendar
if App.current_user.get('netid') == App.advisors.getByCid($("#calendar_selection").val()).get('netid')
view = new App.Views.NewEventView(collection: @collection)
console.log view
view.model = new App.Models.Event( start: startdate.toString(), end: enddate.toString() )
view.render()
else
Expand All @@ -98,9 +106,15 @@ class App.Views.EventIndex extends Backbone.View
);

after_render: (event, element)=>
if event.event_type == 'Open'
if event.event_type == 'Open'
if event.title == 'Open'
link_name = 'book'
else
link_name = 'remove'

console.log link_name
# set up to add book link to opend events
element.find('.fc-event-time').append("<span id='book-#{event.id}' class='book' style='float:right'><a href='#'>book</a></span>")
element.find('.fc-event-time').append("<span id='book-#{event.id}' class='book' style='float:right'><a href='#'>" + link_name + "</a></span>")

element.popover # set popover options
placement: 'left'
Expand All @@ -109,7 +123,6 @@ class App.Views.EventIndex extends Backbone.View
trigger: 'manual'
html: true # allows template to be rendered for content

console.log "after render %o", event
# the following must be called here. All events in the Detail view are bound to the popover
# any popover related events needing to be bound to the event happens here
element.attr('title', event.title) # popover takes title from title attr
Expand Down
1 change: 0 additions & 1 deletion app/assets/templates/events/detail.jst.eco
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
<div class="clearfix form-stacked" id="book_name" style="display:<%= 'none' if @model.title != 'Open' %>">
<label for="">Enter Name or NetID to book this slot</label>
<input type="text" class="xlarge" id="new_apt" name="student_name" size="30" event="<%= @model.id %>" />
<button class="btn">Submit</button>
</div>

1 change: 0 additions & 1 deletion app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class EventsController < ApplicationController
# GET
def index
@events = Event.all
@events.map{|event| event[:user_data] = event.user.user_data}
respond_with @events
end

Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def user_data
:firstname => self.firstname,
:lastname => self.lastname,
:netid => self.netid,
:id => self.id,
:can_read_all_events => true,
:can_read_own_events => true,
:can_write_events => true,
Expand Down
2 changes: 1 addition & 1 deletion app/views/events/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// This loads some initial data for backbone
$(function() {
App.init(<%== @events.to_json %>);
App.init(<%== @events.to_json(include: [:appointments, :user], except: [:created_at, :updated_at, :password_digest]) %>);
});

<% end %>
Expand Down
11 changes: 11 additions & 0 deletions lib/assets/javascripts/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
this.module = function(names, fn) {
var space, _name;
if (typeof names === 'string') names = names.split('.');
space = this[_name = names.shift()] || (this[_name] = {});
space.module || (space.module = this.module);
if (names.length) {
return space.module(names, fn);
} else {
return fn.call(space);
}
};

0 comments on commit 591e1b2

Please sign in to comment.