change in algo of event generation plus now multi week events enabled

This commit is contained in:
Harry Bomrah 2012-09-24 21:26:15 +08:00
parent 84205379c1
commit 16c51a77a7
8 changed files with 145 additions and 70 deletions

View File

@ -82,12 +82,12 @@ var calendarAPI = function(){
month = dt.getMonth()+1;
year = dt.getFullYear();
}
// month = 2;
// month = 10;
// year = 2008;
c.cur_month = month;
c.cur_year = year;
$('#view_holder').load("cals/month_view?month="+month+"&year="+year, function() {
getEvents(month,year);
c.getEvents(month,year);
$('.current_day_title').text(c.monthlist[c.cur_month]+" "+c.cur_year);
if($('#calendar_month').length > 0){
var $c_table = $('#calendar_month');
@ -111,71 +111,109 @@ var calendarAPI = function(){
});
}
})
var getEvents = function(month,year){
$.getJSON("cals/getEvents",{"type":"monthview","month":month,"year":year},function(events){
var $eventrow = $("<tr></tr>");
var nos = new Array();
var currow = 0;
var lastno = 0;
nos.push(0);
$.each(events,function(i,evnt){
}
this.getEvents = function(month,year){
$.getJSON("cals/getEvents",{"type":"monthview","month":month,"year":year},function(events){
makerow(events);
})
var doneEventArray = new Array();
var makerow = function(events){
var $eventrow =null;
var currow = 0;
var curdate = 0;
var allow = false;
var curparent = null;
var lastno = 0;
var indexcount = events.length;
$.each(events,function(i,evnt){
indexcount++;
if($.inArray(evnt.index,doneEventArray) == -1){
var daydom = $("#calendar_month td[date="+evnt.start_date+"]");
var curparent = daydom.parent().parent().parent();
var thisrow = curparent.attr("row");
var pos = daydom.attr("position");
var thisparent = daydom.parent().parent().parent();
var thisrow = thisparent.attr("row");
var pos = parseInt(daydom.attr("position"));
var thisno = daydom.attr("no");
if(thisrow != currow){
if(curparent){
curparent.append($eventrow);
}
$eventrow = null;
$eventrow = $("<tr class='event_row'></tr>");
allow = true;
}else if(evnt.start_date > curdate){
allow = true;
}
var recordcurdate = true;
var index = "new";
var smallcount = 0;
for(x in nos){
if(thisno > nos[x]){
smallcount++;
}
if(smallcount > 0){
index = x;
break;
}
}
if(index == "new"){
nos=[]
$eventrow = null;
$eventrow = $("<tr></tr>");
}
if(thisrow!=currow){
$eventrow = null;
$eventrow = $("<tr></tr>");
}
if(index!="new"){
nos[index] = (parseInt(thisno) + parseInt(evnt.total_days)) - 1;
}else{
nos.push((parseInt(thisno) + parseInt(evnt.total_days)) - 1);
}
if(pos == 1){
$eventrow.html('<td colspan="'+evnt.total_days+'" class="main_td" pos="'+pos+'"><div class="event" link="'+evnt.show_link+'" style="background-color: '+evnt.color+';">'+evnt.title+'</div></td>');
}else{
if($eventrow.html()==""){
$eventrow.append('<td colspan="'+(pos-1)+'">');
}else{
if((lastno+1)!=thisno){
var inposition = $eventrow.find("td.main_td").attr("pos");
var colspan = parseInt(pos) - parseInt(inposition);
colspan--;
$eventrow.append('<td colspan="'+colspan+'">');
if(allow){
if(pos == 1){
var colspan = 0;
if(evnt.total_days>7){
colspan = 7;
var totaldays = parseInt(evnt.total_days) - colspan;
var stardate = parseInt(evnt.start_date) + colspan;
var index = i + 1;
var tempArray = {"index":indexcount,"id":evnt.id,"start_date":stardate,"total_days":totaldays,"title":evnt.title,"color":evnt.color,"show_link":evnt.show_link};
events.splice(index,0,tempArray);
//recordcurdate = false;
}else{
colspan = evnt.total_days;
}
$eventrow.html('<td colspan="'+colspan+'" class="main_td" pos="'+pos+'"><div class="event" link="'+evnt.show_link+'" style="background-color: '+evnt.color+';">'+evnt.title+'</div></td>');
}else{
if($eventrow.html()==""){
$eventrow.append('<td colspan="'+(pos-1)+'">');
}else{
if((lastno+1)!=thisno){
var inposition = parseInt($eventrow.find("td.main_td:last").attr("pos"));
var curcolspan = $eventrow.find("td.main_td:last").attr("colspan");
var colspan = pos - (inposition+parseInt(curcolspan));
$eventrow.append('<td colspan="'+colspan+'">');
}
}
var colspan = 0;
if((pos + parseInt(evnt.total_days))-1 > 7){
colspan = 7-(pos-1);
var totaldays = parseInt(evnt.total_days) - colspan;
var stardate = parseInt(evnt.start_date) + colspan;
var index = i + 1;
var tempArray = {"index":indexcount,"id":evnt.id,"start_date":stardate,"total_days":totaldays,"title":evnt.title,"color":evnt.color,"show_link":evnt.show_link};
events.splice(index,0,tempArray);
// recordcurdate = false;
}else{
colspan = evnt.total_days;
}
// if(evnt.title == "Again")
// console.log(colspan);
$eventrow.append('<td colspan="'+colspan+'" class="main_td" pos="'+pos+'"><div link="'+evnt.show_link+'" class="event" style="background-color: '+evnt.color+';">'+evnt.title+'</div></td>');
}
$eventrow.append('<td colspan="'+evnt.total_days+'" class="main_td" pos="'+pos+'"><div link="'+evnt.show_link+'" class="event" style="background-color: '+evnt.color+';">'+evnt.title+'</div></td>');
lastno = (parseInt(thisno) + parseInt(evnt.total_days)) - 1;
currow = thisrow;
curdate =(evnt.start_date + evnt.total_days) - 1;
allow = false;
curparent = thisparent;
if(recordcurdate)
doneEventArray.push(evnt.index);
}
lastno = (parseInt(thisno) + parseInt(evnt.total_days)) - 1;
currow = thisrow;
curparent.append($eventrow);
})
}
})
if(curparent){
curparent.append($eventrow);
}
if(events.length != doneEventArray.length){
makerow(events);
}
}
}
this.loadWeekView = function(){
$('#view_holder').load("cals/week_view", function() {
$('.current_day_title').text('September 2 - 8, 2012');

View File

@ -58,17 +58,19 @@ class Panel::Calendar::BackEnd::CalsController < OrbitBackendController
def get_events
month = params[:month]
year = params[:year]
events = Event.where(:start_month => month).and(:start_year => year).asc(:start_date)
month = month.to_i
year = year.to_i
events = Event.where(:start_month => month).and(:start_year => year).asc(:start_date).desc(:total_days)
@events = Array.new
events.each do |event|
events.each_with_index do |event,i|
# @temp = Array.new
no_of_days = Date.new(event.end_year,event.end_month,event.end_date) - Date.new(event.start_year,event.start_month,event.start_date)
no_of_days = event.total_days
no_of_days = no_of_days.to_i
no_of_days += 1
color = Cal.find(event.cal_id).color
@events << {"start_date"=>event.start_date, "total_days" => no_of_days, "title" => event.title,"color"=>color,"show_link"=>panel_calendar_back_end_event_path(event)}
@events << {"id"=>event.id,"index"=>i,"start_date"=>event.start_date, "total_days" => no_of_days, "title" => event.title,"color"=>color,"show_link"=>panel_calendar_back_end_event_path(event)}
end
@events = @events.sort{|k,v| v[:total_days] <=> k[:total_days]}
render :json => @events.to_json
end

View File

@ -23,8 +23,9 @@ class Panel::Calendar::BackEnd::EventsController < OrbitBackendController
temp = $start.split("/")
start_date = temp[0]
start_month = temp[1]
star_year = temp[2]
start_year = temp[2]
@m = start_month
@y = start_year
temp = $end.split("/")
end_date = temp[0]
end_month = temp[1]
@ -35,10 +36,20 @@ class Panel::Calendar::BackEnd::EventsController < OrbitBackendController
cal_id = params[:event][:cal_id]
# if start_am_pm == "PM"
# temp_start_time = $starttime + 12
# end
# if end_am_pm == "PM"
# temp_end_time = $endtime + 12
# end
final_start_time = Date.new(start_year.to_i,start_month.to_i,start_date.to_i)
final_end_time = Date.new(end_year.to_i,end_month.to_i,end_date.to_i)
total_days = final_end_time - final_start_time
@event = Event.new
@event.title = title
@event.note = note
@event.start_year = star_year
@event.start_year = start_year
@event.end_year = end_year
@event.start_month = start_month
@event.end_month = end_month
@ -49,6 +60,9 @@ class Panel::Calendar::BackEnd::EventsController < OrbitBackendController
@event.end_time = $endtime
@event.end_am_pm = end_am_pm
@event.cal_id = cal_id
@event.final_start_time = final_start_time
@event.final_end_time = final_end_time
@event.total_days = total_days
@event.save!
respond_to do |h|

View File

@ -14,6 +14,9 @@ class Event
field :start_am_pm
field :end_time, type: Float
field :end_am_pm
field :final_start_time, type: Date
field :final_end_time, type: Date
field :total_days, type: Integer
belongs_to :cal

View File

@ -1,2 +1,4 @@
$("#event_create").empty().hide();
$("#create_event_btn").removeClass("active");
$("#create_event_btn").removeClass("active");
$("#calendar_month tr.event_row").remove();
calendar.getEvents(<%= @m %>,<%= @y %>);

View File

@ -1,3 +1,4 @@
zh_tw:
calendar:
calendars: Calendars
calendars: Calendars
create: Create

View File

@ -7,7 +7,20 @@ class Panel::Gprs::BackEnd::LocationsController < OrbitBackendController
def create
debugger
@newlocation = Location.new(params[:location])
render :json => {"success"=>"true"}.to_json
@newlocation.save!
render :action => "index"
end
def get_locations
locations = Location.all
@data = Array.new
locations.each do |location|
picurl = "http://"+request.host + location.file.url
thumburl = "http://"+request.host + location.file.thumb.url
@data << {"name"=>location.name,"pic_url"=>picurl,"thumb_url"=>thumburl,"description"=>location.description}
end
render :json => @data.to_json
end
end

View File

@ -3,7 +3,9 @@ Rails.application.routes.draw do
namespace :gprs do
namespace :back_end do
resources :locations
match "locations/get_locations" => "locations#get_locations"
resources :locations
end
end