From bb626470e77ad5d4ab0123607011dda92391f615 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Thu, 27 Sep 2012 13:44:22 +0800 Subject: [PATCH] week view updates --- .../app/assets/javascripts/calendarAPI.js.erb | 119 ++++++++++++++---- .../calendar/back_end/cals_controller.rb | 26 +++- .../panel/calendar/back_end/cals_helper.rb | 48 ++++++- .../calendar/back_end/cals/week_view.html.erb | 19 ++- .../calendar/back_end/events/create.js.erb | 2 +- 5 files changed, 174 insertions(+), 40 deletions(-) diff --git a/vendor/built_in_modules/calendar/app/assets/javascripts/calendarAPI.js.erb b/vendor/built_in_modules/calendar/app/assets/javascripts/calendarAPI.js.erb index 076e44b36..aca68f07a 100644 --- a/vendor/built_in_modules/calendar/app/assets/javascripts/calendarAPI.js.erb +++ b/vendor/built_in_modules/calendar/app/assets/javascripts/calendarAPI.js.erb @@ -1,16 +1,47 @@ //created on sep 14 2012 +Date.prototype.getWeek = function (dowOffset) { +/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.epoch-calendar.com */ + + dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero + var newYear = new Date(this.getFullYear(),0,1); + var day = newYear.getDay() - dowOffset; //the day of week the year begins on + day = (day >= 0 ? day : day + 7); + var daynum = Math.floor((this.getTime() - newYear.getTime() - + (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1; + var weeknum; + //if the year starts before the middle of a week + if(day < 4) { + weeknum = Math.floor((daynum+day-1)/7) + 1; + if(weeknum > 52) { + nYear = new Date(this.getFullYear() + 1,0,1); + nday = nYear.getDay() - dowOffset; + nday = nday >= 0 ? nday : nday + 7; + /*if the next year starts before the middle of + the week, it is week #1 of that year*/ + weeknum = nday < 4 ? 1 : 53; + } + } + else { + weeknum = Math.floor((daynum+day-1)/7); + } + return weeknum; +}; + var calendarAPI = function(){ c = this; this.event_create_div = $("#event_create"); this.event_quick_view_div = $("#event_quick_view"); - this.cur_month = null; - this.cur_year = null; this.today = new Date(); + this.cur_month = c.today.getMonth()+1; + this.cur_year = c.today.getFullYear(); + this.cur_week = c.today.getWeek(); + 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.loadMonthView(c.cur_month,c.cur_year); + c.loadWeekView(51,c.cur_year); bindHandlers(); }) var bindHandlers = function(){ @@ -39,7 +70,7 @@ var calendarAPI = function(){ c.loadMonthView(c.cur_month,c.cur_year); break; case 'week': - c.loadWeekView(); + c.loadWeekView(c.cur_week,c.cur_year); break; case 'day': c.loadDayView(); @@ -50,26 +81,53 @@ var calendarAPI = function(){ } }) $("button#prev_month_btn").click(function(){ - var m,y; - if(c.cur_month == 1){ - m = 12; - y = c.cur_year-1; - }else{ - m = c.cur_month-1; - y = c.cur_year; + switch (c.view){ + case "month": + var m,y; + if(c.cur_month == 1){ + m = 12; + y = c.cur_year-1; + }else{ + m = c.cur_month-1; + y = c.cur_year; + } + c.loadMonthView(m,y); + break; + case "week": + var w,y; + w = c.cur_week - 1; + y = c.cur_year; + c.loadWeekView(w,y); + break; } - c.loadMonthView(m,y); + }) $("button#next_month_btn").click(function(){ - var m,y; - if(c.cur_month == 12){ - m = 1; - y = c.cur_year+1; - }else{ - m = c.cur_month+1; - y = c.cur_year; + switch (c.view){ + case "month": + var m,y; + if(c.cur_month == 12){ + m = 1; + y = c.cur_year+1; + }else{ + m = c.cur_month+1; + y = c.cur_year; + } + c.loadMonthView(m,y); + break; + case "week": + var w,y; + + if(c.cur_week == 52){ + w = 2; + y = c.cur_year + 1; + }else{ + w = c.cur_week + 1; + y = c.cur_year; + } + c.loadWeekView(w,y); + break; } - c.loadMonthView(m,y); }) $("button#today_btn").click(function(){ c.loadMonthView(); @@ -77,6 +135,7 @@ var calendarAPI = function(){ } } this.loadMonthView = function(month,year){ + c.view = "month"; if(!month){ var dt = new Date(); month = dt.getMonth()+1; @@ -87,7 +146,7 @@ var calendarAPI = function(){ c.cur_month = month; c.cur_year = year; $('#view_holder').load("cals/month_view?month="+month+"&year="+year, function() { - c.getEvents(month,year); + c.getEventsForMonth(month,year); $('.current_day_title').text(c.monthlist[c.cur_month]+" "+c.cur_year); if($('#calendar_month').length > 0){ var $c_table = $('#calendar_month'); @@ -114,7 +173,7 @@ var calendarAPI = function(){ } - this.getEvents = function(month,year){ + this.getEventsForMonth = function(month,year){ $.getJSON("cals/getEvents",{"type":"monthview","month":month,"year":year},function(events){ makerow(events); }) @@ -214,9 +273,18 @@ var calendarAPI = function(){ } } - this.loadWeekView = function(){ - $('#view_holder').load("cals/week_view", function() { - $('.current_day_title').text('September 2 - 8, 2012'); + this.loadWeekView = function(week,year){ + c.view = "week"; + if(!week){ + var dt = new Date(); + week = dt.getWeek(); + year = dt.getFullYear(); + } + + c.cur_week = week; + c.cur_year = year; + $('#view_holder').load("cals/week_view?week="+week+"&year="+year, function() { + $('.current_day_title').text($("#week_range").text()); }) } this.loadDayView = function(){ @@ -267,6 +335,7 @@ var calendarAPI = function(){ }) }) } + c.initialize(); } diff --git a/vendor/built_in_modules/calendar/app/controllers/panel/calendar/back_end/cals_controller.rb b/vendor/built_in_modules/calendar/app/controllers/panel/calendar/back_end/cals_controller.rb index 38e3c18f4..f6f128ef5 100644 --- a/vendor/built_in_modules/calendar/app/controllers/panel/calendar/back_end/cals_controller.rb +++ b/vendor/built_in_modules/calendar/app/controllers/panel/calendar/back_end/cals_controller.rb @@ -30,6 +30,10 @@ class Panel::Calendar::BackEnd::CalsController < OrbitBackendController end def week_view + week = params[:week] + year = params[:year] + @dates = week_dates(week,year) + @range = week_range(week,year) render :layout => false end @@ -47,7 +51,6 @@ class Panel::Calendar::BackEnd::CalsController < OrbitBackendController cur_month_days = getMonthDays(year) @post_disabled_days = @pre_disabled_days + cur_month_days[month] @dateset = getDateSet(month,year) - render :layout => false end @@ -60,11 +63,30 @@ class Panel::Calendar::BackEnd::CalsController < OrbitBackendController year = params[:year] 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 = 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 + + 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 + + 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 diff --git a/vendor/built_in_modules/calendar/app/helpers/panel/calendar/back_end/cals_helper.rb b/vendor/built_in_modules/calendar/app/helpers/panel/calendar/back_end/cals_helper.rb index 3f9375525..380632654 100644 --- a/vendor/built_in_modules/calendar/app/helpers/panel/calendar/back_end/cals_helper.rb +++ b/vendor/built_in_modules/calendar/app/helpers/panel/calendar/back_end/cals_helper.rb @@ -61,10 +61,56 @@ module Panel::Calendar::BackEnd::CalsHelper monthsdays << 31 return monthsdays end + def getDayName(date,month,year) + date = date.to_i + month = month.to_i + year = year.to_i dt = Date.new(year,month,date) name = Date::ABBR_DAYNAMES[dt.wday] return name end -end \ No newline at end of file + def week_dates(week_num,year) + year = year.to_i + week_num = week_num.to_i + week_start = Date.commercial(year, week_num-1, 7) + month = week_start.strftime("%m") + month_days = getMonthDays(year) + date = week_start.strftime("%d") + dates = Array.new + x = date.to_i + for i in 0..6 + d = x + i + if d > month_days[month.to_i] + d = d - month_days[month.to_i] + end + dates << Date::ABBR_DAYNAMES[i] + " " + month + "/" + d.to_s + end + dates + end + + def week_range(week_num,year) + year = year.to_i + week_num = week_num.to_i + week_start = Date.commercial(year, week_num-1, 7) + week_end = Date.commercial(year, week_num, 7) + week_end = week_end - 1 + start_date = week_start.strftime("%d") + end_date = week_end.strftime("%d") + start_month = week_start.strftime("%m") + end_month = week_end.strftime("%m") + + if end_month == start_month + range = Date::ABBR_MONTHNAMES[start_month.to_i] + " " + start_date.to_s + " - " + end_date.to_s + ", " + week_start.strftime("%Y").to_s + else + range = Date::ABBR_MONTHNAMES[start_month.to_i] + " " + start_date.to_s + " - " + Date::ABBR_MONTHNAMES[end_month.to_i] + " " + end_date.to_s + ", " + week_start.strftime("%Y").to_s + end + range + end +end + + + + + diff --git a/vendor/built_in_modules/calendar/app/views/panel/calendar/back_end/cals/week_view.html.erb b/vendor/built_in_modules/calendar/app/views/panel/calendar/back_end/cals/week_view.html.erb index a2d6dfbea..dc27b7362 100644 --- a/vendor/built_in_modules/calendar/app/views/panel/calendar/back_end/cals/week_view.html.erb +++ b/vendor/built_in_modules/calendar/app/views/panel/calendar/back_end/cals/week_view.html.erb @@ -1,35 +1,32 @@ +
- - - - - - - + <% @dates.each do |day| %> + + <% end %> diff --git a/vendor/built_in_modules/calendar/app/views/panel/calendar/back_end/events/create.js.erb b/vendor/built_in_modules/calendar/app/views/panel/calendar/back_end/events/create.js.erb index eee4491b9..98703270c 100644 --- a/vendor/built_in_modules/calendar/app/views/panel/calendar/back_end/events/create.js.erb +++ b/vendor/built_in_modules/calendar/app/views/panel/calendar/back_end/events/create.js.erb @@ -1,4 +1,4 @@ $("#event_create").empty().hide(); $("#create_event_btn").removeClass("active"); $("#calendar_month tr.event_row").remove(); -calendar.getEvents(<%= @m %>,<%= @y %>); \ No newline at end of file +calendar.getEventsForMonth(<%= @m %>,<%= @y %>); \ No newline at end of file
Sun 9/11Mon 9/12Tue 9/13Wed 9/14Thu 9/15Fri 9/16Sat 9/17<%= day %>
-
+
-
+