Calendar updates for day view

This commit is contained in:
Harry Bomrah 2012-10-24 18:55:06 +08:00 committed by chris
parent 8c6e5deb64
commit a5722af315
13 changed files with 335 additions and 152 deletions

View File

@ -27,6 +27,13 @@ Date.prototype.getWeek = function (dowOffset) {
return weeknum;
};
Date.prototype.daysInMonth = function(){
var daysArray = [31,28,31,30,31,30,31,31,30,31,30,31];
if(this.getFullYear()%4 == 0)
daysArray[1] = 29;
return daysArray[this.getMonth()];
}
var calendarAPI = function(){
c = this;
@ -36,17 +43,20 @@ var calendarAPI = function(){
this.cur_month = c.today.getMonth()+1;
this.cur_year = c.today.getFullYear();
this.cur_week = c.today.getWeek();
this.cur_date = c.today.getDate();
this.view = null;
this.monthlist = ["","January","February","March","April","May","June","July","August","September","October","November","December"];
this.initialize = function(){
$(window).load(function(){
// c.loadMonthView(c.cur_month,c.cur_year);
c.loadWeekView(c.cur_week,c.cur_year);
// c.loadWeekView(c.cur_week,c.cur_year);
c.loadDayView(c.cur_date,c.cur_month,c.cur_year);
bindHandlers();
})
var bindHandlers = function(){
$(".event").live("click",function(){
c.displayEvent($(this));
$(".event").live("click",function(e){
var pos = {"x":e.clientX,"y":e.clientY};
c.displayEvent($(this),pos);
})
$("#create_event_btn").click(function(){
@ -73,7 +83,7 @@ var calendarAPI = function(){
c.loadWeekView(c.cur_week,c.cur_year);
break;
case 'day':
c.loadDayView();
c.loadDayView(c.cur_date,c.cur_month,c.cur_year);
break;
case 'agenda':
c.loadAgendaView();
@ -104,6 +114,22 @@ var calendarAPI = function(){
}
c.loadWeekView(w,y);
break;
case "day":
var d,w,y;
d = c.cur_date - 1;
m = c.cur_month;
y = c.cur_year;
if(d == 0){
var dx = new Date(c.cur_year,c.cur_month-2);
d = dx.daysInMonth();
m--;
}
if(m == 0){
m = 12;
y = y - 1;
}
c.loadDayView(d,m,y);
break;
}
})
@ -132,6 +158,26 @@ var calendarAPI = function(){
}
c.loadWeekView(w,y);
break;
case "day":
var d,w,y;
var dx = new Date(c.cur_year,c.cur_month-1);
if(c.cur_date == dx.daysInMonth()){
d = 1;
m = c.cur_month + 1;
}else{
d = c.cur_date + 1;
m = c.cur_month;
}
if(m == 13){
m = 1;
y = c.cur_year + 1;
}else{
y = c.cur_year;
}
c.loadDayView(d,m,y);
break;
}
})
$("button#today_btn").click(function(){
@ -142,6 +188,9 @@ var calendarAPI = function(){
case "month":
c.loadMonthView();
break;
case "day":
c.loadDayView();
break;
}
})
}
@ -222,6 +271,8 @@ var calendarAPI = function(){
var recordcurdate = true;
if(allow){
if(evnt.title=="Navratri")
console.log(pos);
if(pos == 1){
var colspan = 0;
if(evnt.total_days>7){
@ -238,7 +289,7 @@ var calendarAPI = function(){
$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)+'"></td>');
$eventrow.append('<td colspan="'+(pos-1)+'"></td>');
}else{
if((lastno+1)!=thisno){
var inposition = parseInt($eventrow.find("td.main_td:last").attr("pos"));
@ -312,7 +363,6 @@ var calendarAPI = function(){
$.each(events,function(i,evnt){
// console.log("rowcount: "+rowcount);
// console.log("start:"+evnt.start);
console.log(rowcount);
if(rowcount >= (7 - evnt.total_days)){
if(tr.html()!=""){
if(rowcount != 7){
@ -345,11 +395,7 @@ var calendarAPI = function(){
}
rowcount = precountspan + colcount;
if(evnt.title == "Yeah"){
console.log("rowcount: "+rowcount);
console.log("start:"+evnt.total_days);
}
tr.append('<td colspan="'+colcount+'" class="all_day_event week_day" ><div class="event half" style="background-color: '+evnt.color+' ;color:#fff;" link="'+evnt.show_link+'"><dl><dt>'+evnt.title+'</dt><dd></dd></dl></div></td>');
// $(".week_day_header[date="+evnt.start_date+"]").append('<div class="event half" link="'+evnt.show_link+'" style="background-color: '+evnt.color+';color:#fff;"><dl><dt>'+evnt.title+'</dt><dd></dd></dl></div>');
@ -411,9 +457,76 @@ var calendarAPI = function(){
}
})
}
this.loadDayView = function(){
$('#view_holder').load("cals/day_view", function() {
$('.current_day_title').text('September 2, 2012');
this.loadDayView = function(day,month,year){
c.view = "day";
if(!day){
var dt = new Date();
week = dt.getWeek();
year = dt.getFullYear();
day = dt.getDate();
month = dt.getMonth()+1;
}
c.cur_date = day;
c.cur_month = month;
c.cur_year = year;
$('#view_holder').load("cals/day_view?date="+day+"&month="+month+"&year="+year, function() {
$('.current_day_title').text($("#day_header").text());
c.getEventsForDay(day,month,year);
})
}
this.getEventsForDay = function(day,month,year){
$.getJSON("cals/getDayEvents",{"date":day,"month":month,"year":year},function(events){
$.each(events,function(i,evnt){
if(evnt.all_day){
$(".all_day_event").append('<div class="event" link="'+evnt.show_link+'" style="background-color: '+evnt.color+';color:#fff;">'+evnt.title+'</div>')
}else{
var starttime,endtime,displaystarttime,displayendtime;
if(evnt.start_am_pm == "AM"){
starttime = evnt.start_time;
if(starttime == 12)
starttime = 0;
if(starttime == 12.5)
starttime = 0.5;
}else
starttime = evnt.start_time + 12;
if(evnt.end_am_pm == "AM"){
endtime = evnt.end_time;
if(endtime == 12)
endtime = 0;
if(endtime == 12.5)
endtime = 0.5;
}else
endtime = evnt.end_time + 12;
var temp = parseInt(evnt.start_time);
if (evnt.start_time > temp)
displaystarttime = temp + ":30 " + evnt.start_am_pm;
else
displaystarttime = temp + ":00 " + evnt.start_am_pm;
temp = parseInt(evnt.end_time);
if (evnt.end_time > temp)
displayendtime = temp + ":30 " + evnt.end_am_pm;
else
displayendtime = temp + ":00 " + evnt.end_am_pm;
var toppx = ((starttime * 2) * 20) + 1;
var h = endtime - starttime;
var halfhour = "";
if(h == 0.5)
halfhour = "half";
h = 17 + 20 + (((h-1) * 2) * 20) + 1;
var eventdom = $('<div class="inner"><div class="event '+halfhour+'" link="'+evnt.show_link+'" style="background-color: '+evnt.color+';color:#fff; top:'+toppx+'px; height:'+h+'px;"><dl><dt>'+displaystarttime+' - '+displayendtime+'</dt><dd>'+evnt.title+'</dd></dl></div></div>');
$(".event_holder").append(eventdom);
}
})
})
}
this.loadAgendaView = function(){
@ -449,11 +562,27 @@ var calendarAPI = function(){
var domfor = $(this).attr("data-content");
$("tr[for="+domfor+"]").remove();
})
$(".btn-edit-a").live("ajax:success",function(evt,form){
$("#edit_area_" + $(this).attr("for")).html(form).slideDown();
$("#edit_area_" + $(this).attr("for")).find(".color-picker").miniColors();
})
$(".bt-cancel").live("click",function(){
$("#edit_area_" + $(this).attr("for")).html("").slideUp();
})
}
this.displayEvent = function(dom){
this.displayEvent = function(dom,pos){
var url = dom.attr("link");
c.event_quick_view_div.load(url,function(){
c.event_quick_view_div.show()
var x = pos.x;
var y = pos.y;
var winheight = $(window).height()
if((x+c.event_quick_view_div.width()) > $(window).width()){
x = x - c.event_quick_view_div.width();
}
if((y+c.event_quick_view_div.height()) > winheight){
y = y - c.event_quick_view_div.height();
}
c.event_quick_view_div.css({"left":x+"px","top":y+"px"}).show()
c.event_quick_view_div.find(".event-close-btn").click(function(){
c.event_quick_view_div.empty().hide();
})

View File

@ -32,6 +32,7 @@
border-radius: 3px;
cursor: pointer;
padding: 1px 3px;
font-weight: bold;
box-shadow: inset 0 0 1px black;
-webkit-box-shadow: inset 0 0 1px black;
-moz-box-shadow: inset 0 0 1px black;

View File

@ -11,6 +11,19 @@ class Panel::Calendar::BackEnd::CalsController < OrbitBackendController
@calendars = Cal.all
end
def edit
@calendar = Cal.find(params[:id])
render :layout=>false
end
def update
@calendar = Cal.find(params[:id])
@calendar.update_attributes(params[:cal])
respond_to do |h|
h.js
end
end
def create
@calendar = Cal.new(params[:cal])
@calendar.save!
@ -26,7 +39,12 @@ class Panel::Calendar::BackEnd::CalsController < OrbitBackendController
end
def day_view
render :layout => false
date = params[:date].to_i
month = params[:month].to_i
year = params[:year].to_i
@cur_day = getDayName(date,month,year) + " " + month.to_s + "/" + date.to_s + " - " + year.to_s
@hours = getHours(12)
render :layout => false
end
def week_view
@ -70,38 +88,84 @@ class Panel::Calendar::BackEnd::CalsController < OrbitBackendController
end
def get_month_events
month = params[:month]
year = params[:year]
month = month.to_i
year = year.to_i
events = Event.where(:start_month.lt => month).and(:start_year => year).and(:end_month.gte => month).asc(:start_date).desc(:total_days)
month = params[:month].to_i
year = params[:year].to_i
# events = Event.where(:start_month.lt => month).and(:start_year => year).and(:end_month.gte => month).asc(:start_date).desc(:total_days)
@events = Array.new
no_of_days_in_month = getMonthDays(year)
events.each_with_index do |event,i|
no_of_days = event.total_days
# events.each_with_index do |event,i|
# no_of_days = event.total_days
if event.end_month > month
no_of_days = no_of_days_in_month[month]
elsif event.end_month == month
no_of_days = event.end_date
end
# if event.end_month > month
# no_of_days = no_of_days_in_month[month]
# elsif event.end_month == month
# no_of_days = event.end_date
# end
no_of_days = no_of_days.to_i
# no_of_days = no_of_days.to_i
color = Cal.find(event.cal_id).color
@events << {"id"=>event.id,"index"=>i,"start_date"=>"1", "total_days" => no_of_days, "title" => event.title,"color"=>color,"show_link"=>panel_calendar_back_end_event_path(event)}
end
events = Event.where(:start_month => month).and(:start_year => year).asc(:start_date).desc(:total_days)
# color = Cal.find(event.cal_id).color
# @events << {"id"=>event.id,"index"=>i,"start_date"=>"1", "total_days" => no_of_days, "title" => event.title,"color"=>color,"show_link"=>panel_calendar_back_end_event_path(event)}
# end
# events = Event.where(:start_month => month).and(:start_year => year).asc(:start_date).desc(:total_days)
# events.each_with_index do |event,i|
# # @temp = Array.new
# no_of_days = event.total_days
# if event.end_month > month
# no_of_days = no_of_days_in_month[month] - event.start_date
# end
# no_of_days = no_of_days.to_i
# no_of_days += 1
# color = Cal.find(event.cal_id).color
# @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 = Event.all.asc(:start_date).desc(:total_days)
events.each_with_index do |event,i|
# @temp = Array.new
no_of_days = event.total_days
if event.end_month > month
no_of_days = no_of_days_in_month[month] - event.start_date
end
no_of_days = no_of_days.to_i
no_of_days += 1
color = Cal.find(event.cal_id).color
@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)}
startdt = Date.new(event.start_year,event.start_month)
enddt = Date.new(event.end_year,event.end_month)
range = startdt..enddt
dt = Date.new(year,month)
if range === dt
no_of_days = event.total_days
start_date = event.start_date
if event.start_year < year
no_of_days = no_of_days_in_month[month] - event.start_date
no_of_days += 1
temp = month + 12
if event.start_month < temp
start_date = 1
end
end
if event.end_year > year
no_of_days = no_of_days_in_month[month] - event.start_date
no_of_days += 1
temp = month + 12
end
if event.end_month > month
no_of_days = no_of_days_in_month[month] - event.start_date
no_of_days += 1
elsif event.end_month == month
no_of_days = event.end_date
end
if event.start_month == month && event.end_month == month
no_of_days = event.total_days
no_of_days += 1
end
if event.start_month < month
start_date = 1
end
color = Cal.find(event.cal_id).color
@events << {"start_year"=>event.start_year, "id"=>event.id,"index"=>i,"start_date"=>start_date, "total_days" => no_of_days, "title" => event.title,"color"=>color,"show_link"=>panel_calendar_back_end_event_path(event)}
end
end
render :json => @events.to_json
@ -162,6 +226,36 @@ class Panel::Calendar::BackEnd::CalsController < OrbitBackendController
render :json => @events.to_json
end
def get_day_events
day = params[:date].to_i
month = params[:month].to_i
year = params[:year].to_i
dt = Date.new(year,month,day)
week = dt.strftime("%U")
events = Event.where(:start_month.lte => month).and(:start_year => year).and(:end_month.gte => month)
@events = Array.new
events.each_with_index do |event,i|
days = event.total_days.to_i + 1
colcount = 0
all_day = event.all_day
if days > 1
all_day = true
end
startdt = Date.new(event.start_year,event.start_month,event.start_date)
enddt = Date.new(event.end_year,event.end_month,event.end_date)
range = startdt..enddt
if range === dt
color = Cal.find(event.cal_id).color
@events << {"id"=>event.id,"index"=>i,"start_date"=>event.start_date,"end_date"=>event.end_date ,"all_day"=>all_day, "start_week" => event.start_week, "end_week" => event.end_week, "total_days" => days, "title" => event.title,"color"=>color,"show_link"=>panel_calendar_back_end_event_path(event),"start_time"=>event.start_time,"end_time"=>event.end_time,"start_am_pm"=>event.start_am_pm,"end_am_pm"=>event.end_am_pm}
end
end
render :json => @events.to_json
end
def week_number_test
events = Event.all

View File

@ -1,10 +1,12 @@
<tr class="with_action" for="<%= calendar.id.to_s %>">
<td>
<span class="calendars_color_tag" style="background-color: <%= calendar.color %>"></span>
<div id="edit_area_<%= calendar.id %>" style="display:hidden;"></div>
<div class="quick-edit">
<ul class="nav nav-pills hide">
<li><a href="">Edit</a></li>
<li><%= link_to t("calendar.delete"), panel_calendar_back_end_cal_path(calendar), :method => "delete",:remote => true, :confirm => t("gallery.sure?"),:class=>"btn-del-a", "data-content"=>calendar.id.to_s %></li>
<li><%= link_to t("calendar.edit"), edit_panel_calendar_back_end_cal_path(calendar), :remote => true, :class=>"btn-edit-a", :for => calendar.id %></li>
<li><%= link_to t("calendar.delete"), panel_calendar_back_end_cal_path(calendar), :method => "delete",:remote => true, :confirm => t("calendar.sure?"),:class=>"btn-del-a", "data-content"=>calendar.id.to_s %></li>
</ul>
</div>
</td>

View File

@ -1,4 +1,4 @@
<!-- <div id="calendar_agenda">
<div id="calendar_agenda">
<table class="table">
<thead>
<tr>
@ -45,8 +45,8 @@
</tr>
</tbody>
</table>
</div> -->
<div id="calendar_week">
</div>
<!-- <div id="calendar_week">
<table class="table header">
<tr>
<th style="width: 44px;"></th>
@ -264,3 +264,4 @@
</table>
</div>
</div>
-->

View File

@ -3,14 +3,14 @@
<tr>
<th class="span1"></th>
<th>
<h2>Sunday 9/2</h2>
<h2 id="day_header" style="display:none;"><%= @cur_day %></h2>
</th>
</tr>
<tr>
<td></td>
<td class="all_day_event">
<div class="event" style="background-color: #ffcc00;">event1</div>
<div class="event" style="background-color: #ffcc00;">event2</div>
<!-- <div class="event" style="background-color: #ffcc00;">event1</div>
<div cla ss="event" style="background-color: #ffcc00;">event2</div> -->
</td>
</tr>
</table>
@ -18,107 +18,25 @@
<table class="table">
<tr>
<td style="width: 60px;">
<div class="day_time">12am</div>
<div class="day_time">1am</div>
<div class="day_time">2am</div>
<div class="day_time">3am</div>
<div class="day_time">4am</div>
<div class="day_time">5am</div>
<div class="day_time">6am</div>
<div class="day_time">7am</div>
<div class="day_time">8am</div>
<div class="day_time">9am</div>
<div class="day_time">10am</div>
<div class="day_time">11am</div>
<div class="day_time">12am</div>
<div class="day_time">1pm</div>
<div class="day_time">2pm</div>
<div class="day_time">3pm</div>
<div class="day_time">4pm</div>
<div class="day_time">5pm</div>
<div class="day_time">6pm</div>
<div class="day_time">7pm</div>
<div class="day_time">8pm</div>
<div class="day_time">9pm</div>
<div class="day_time">10pm</div>
<div class="day_time">11pm</div>
<% @hours.each do |hour| %>
<div class="day_time"><%= hour %>am</div>
<% end %>
<% @hours.each do |hour| %>
<div class="day_time"><%= hour %>pm</div>
<% end %>
</td>
<td>
<div class="event_list_wrapper">
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<% @hours.each do |hour| %>
<div class="cell">
<div class="divide"></div>
</div>
<div class="cell">
<div class="divide"></div>
</div>
<% end %>
<div class="event_holder">
<div class="inner">
<!-- <div class="inner">
<div class="event" style="background-color: #66cc00; top: 0;">
<dl>
<dt>10:30am - 11:00am</dt>
@ -149,7 +67,7 @@
<dd>template</dd>
</dl>
</div>
</div>
</div> -->
</div>
</div>
</td>

View File

@ -0,0 +1,26 @@
<%= form_for @calendar, :url => panel_calendar_back_end_cal_path(@calendar), :remote => true do |f| %>
<h4>Edit</h4>
<div class="row-fluid">
<div class="span2">
<%= label_tag("color", t("calendar.color")) %>
<%= f.text_field :color, :class => "color-picker miniColors", :size => "5", :maxlength => "5", :autocomplete=>"off",:value=>@calendar.color %>
</div>
</div>
<div>
<%= f.fields_for :name_translations do |name| %>
<% @site_valid_locales.each_with_index do |locale, i| %>
<div class="control-group">
<%= label_tag(locale, t("calendar.name")+"-"+I18nVariable.from_locale(locale)) %>
<div class="controls">
<%= name.text_field locale, :class => "input-xxlarge", :size=>"30", :value=>@calendar.name_translations[locale] %>
</div>
</div>
<% end %>
<% end %>
</div>
<div class="">
<%= f.submit t("calendar.save"), :class=>"btn btn-primary" %>
<button class="btn btn-primary bt-cancel" for="<%= @calendar.id %>">Cancel</button>
</div>
<% end %>

View File

@ -64,7 +64,7 @@
</div>
<div id="event_create" class="modal" style="right: 8px; bottom: 50px; left: auto; top: auto; width: 380px; margin: 0;display:none;"></div>
<div id="event_quick_view" class="modal" style="width: 300px; top: 60%;display:none"></div>
<div id="event_quick_view" class="modal" style="width: 300px; display:none; margin:0 0 0 0;"></div>
<script type="text/javascript">
var calendar = new calendarAPI();

View File

@ -0,0 +1,2 @@
var tr = $("<%= j render :partial=>'calendar', :object=>@calendar %>");
$("#calendar_list tr[for=<%= @calendar.id %>]").empty().html(tr.html());

View File

@ -6,4 +6,6 @@ en:
save: Save
delete: Delete
select_calendar: "Select Calendar"
create: Create
create: Create
sure?: "Are you sure?"
edit: Edit

View File

@ -1,4 +1,11 @@
zh_tw:
calendar:
calendars: Calendars
create: Create
calendars: Calendars
color: Color
name: Name
save: Save
delete: Delete
select_calendar: "Select Calendar"
create: Create
sure?: "Are you sure?"
edit: Edit

View File

@ -9,6 +9,7 @@ Rails.application.routes.draw do
match 'cals/week_view' => 'cals#week_view', :via => :get
match 'cals/getMonthEvents' => 'cals#get_month_events', :via => :get
match 'cals/getWeekEvents' => 'cals#get_week_events', :via => :get
match 'cals/getDayEvents' => 'cals#get_day_events', :via => :get
resources :cals
resources :events