From a7a91c4be621e13cb5672dbb55a0ace3ba3f8c0f Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 2 Jun 2015 17:22:51 +0800 Subject: [PATCH] widget added for calendar and also some engine settings --- app/assets/javascripts/calendar_frontend.js | 4 +- app/assets/javascripts/calendar_widget.js | 198 ++++++++++++++++++++ app/controllers/calendars_controller.rb | 53 +++--- app/models/event.rb | 45 +++-- lib/calendar/engine.rb | 2 +- 5 files changed, 261 insertions(+), 41 deletions(-) create mode 100644 app/assets/javascripts/calendar_widget.js diff --git a/app/assets/javascripts/calendar_frontend.js b/app/assets/javascripts/calendar_frontend.js index 31de1d6..88ec964 100644 --- a/app/assets/javascripts/calendar_frontend.js +++ b/app/assets/javascripts/calendar_frontend.js @@ -357,8 +357,8 @@ var AgendaView = function(calendar){ type : "get", url : url, data : {"agenda_start":sd.toLocaleString(),"agenda_end":ed.toLocaleString(),"page_id" : _calendar.page_id,"unix_start":usd,"unix_end":ued}, - success : function(events){ - $.each(events,function(i,e){ + success : function(data){ + $.each(data.events,function(i,e){ var ed = eventDom(e), s = new Date(e.start), e = new Date(e.end), diff --git a/app/assets/javascripts/calendar_widget.js b/app/assets/javascripts/calendar_widget.js new file mode 100644 index 0000000..5e0c64e --- /dev/null +++ b/app/assets/javascripts/calendar_widget.js @@ -0,0 +1,198 @@ + var CalendarModuleMonth = function(date,dom,subpart){ + _this = this; + var template = dom.find(".month_template"), + monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December'], + initialDate = date, + subpartid = subpart, + fetchInterval = null, + month = date.getMonth(), + year = date.getFullYear(), + firstDay = new Date(year,month,1), + lastDay = new Date(year,month+1,0), + today = date.getDate(), + last_inserted_date = 1, + monthDom = $("
"); + monthDom.html(template); + + var renderMonth = function(){ + var num_of_rows = getNumberOfRows(year,month), + head_title = monthDom.find("h4 span.text"), + table_body = monthDom.find("table.table tbody"); + table_body.html(""); + + for(var i = 0; i < num_of_rows; i++){ + var tr = null; + if(i == 0){ + tr = makeRow("first"); + }else if(i == (num_of_rows - 1)){ + tr = makeRow("last"); + }else{ + tr = makeRow("middle"); + } + if(tr == null){ + break; + } + table_body.append(tr); + head_title.text(monthNames[firstDay.getMonth()] + " " + firstDay.getFullYear()); + } + + } + + var getNumberOfRows = function(year, month) { + var day = 1, + sat_counter = 0, + sunday_counter = 0, + date = new Date(year, month, day); + + while(date.getMonth() === month) { + if(date.getDay() === 0) { + sunday_counter++; + }else if(date.getDay() === 6) { + sat_counter++; + } + day++; + date = new Date(year, month, day); + } + return (sunday_counter == 5 && sat_counter == 5 ? 6 : 5); + } + + var makeRow = function(position){ + if(last_inserted_date <= lastDay.getDate()){ + var row = $(""); + switch (position){ + case "first": + for(var i = 0;i < 7;i++){ + var td = $(""); + if(i >= firstDay.getDay()){ + if(today != 0 && last_inserted_date == today){ + td.attr("class","w-calendar-today"); + } + td.text(last_inserted_date); + td.attr("data-date-node",last_inserted_date+"-"+firstDay.getMonth()+"-"+firstDay.getFullYear()); + last_inserted_date++; + } + row.append(td); + } + break; + case "middle": + for(var i = 0;i < 7;i++){ + var td = $(""); + if(today != 0 && last_inserted_date == today){ + td.attr("class","w-calendar-today"); + } + td.text(last_inserted_date); + td.attr("data-date-node",last_inserted_date+"-"+firstDay.getMonth()+"-"+firstDay.getFullYear()); + last_inserted_date++; + row.append(td); + } + break; + case "last": + for(var i = 0;i < 7;i++){ + var td = $(""); + if(i <= lastDay.getDay()){ + if(today != 0 && last_inserted_date == today){ + td.attr("class","w-calendar-today"); + } + td.text(last_inserted_date); + td.attr("data-date-node",last_inserted_date+"-"+firstDay.getMonth()+"-"+firstDay.getFullYear()); + last_inserted_date++; + } + row.append(td); + } + break; + } + }else{ + var row = null; + } + return row; + } + + var fetchEvents = function(){ + var usd = Math.round(firstDay/1000), + ued = Math.round(lastDay/1000); + $.ajax({ + url : "/xhr/calendars/agenda", + data : {"unix_start" : usd, "unix_end" : ued, "subpart_id" : subpartid}, + dataType : "json", + type : "get" + }).done(function(data){ + $.each(data.events,function(index,eve){ + var sd = new Date(eve.start), + ed = new Date(eve.end), + timeDiff = Math.abs(ed.getTime() - sd.getTime()), + dayDiff = Math.ceil(timeDiff / (1000 * 3600 * 24)); + if(dayDiff > 0){ + var inserting_date = (sd < firstDay ? 1 : sd.getDate()); + for(i = 0;i <= dayDiff; i++){ + var dt = inserting_date + "-" + month + "-" + year, + td = dom.find("td[data-date-node=" + dt + "]"); + td.addClass("w-calendar-event"); + inserting_date++; + if(inserting_date > lastDay.getDate() || (ed.getMonth() == month && inserting_date > ed.getDate())){ + break; + } + } + }else{ + var dt = sd.getDate() + "-" + sd.getMonth() + "-" + sd.getFullYear(); + td = dom.find("td[data-date-node=" + dt + "]"); + td.addClass("w-calendar-event"); + } + }) + monthDom.find("i.loading").addClass("hide"); + }) + } + + this.currentMonth = function(){ + renderMonth(); + dom.html(monthDom); + monthDom.find("i.loading").removeClass("hide"); + fetchInterval = setTimeout(fetchEvents,300); + } + + this.nextMonth = function(){ + clearTimeout(fetchInterval); + monthDom.find("i.loading").removeClass("hide"); + month++; + if(month == 12){ + year++; + month = 0; + } + firstDay = new Date(year,month,1); + lastDay = new Date(year,month+1,0); + today = (initialDate.getMonth() == month && initialDate.getFullYear() == year ? initialDate.getDate() : 0); + last_inserted_date = 1; + renderMonth(); + dom.find("table.w-calendar-table tbody").html(monthDom.find("tbody").html()); + fetchInterval = setTimeout(fetchEvents,1000); + } + + this.prevMonth = function(){ + clearTimeout(fetchInterval); + monthDom.find("i.loading").removeClass("hide"); + month--; + if(month == -1){ + year--; + month = 11; + } + firstDay = new Date(year,month,1); + lastDay = new Date(year,month+1,0); + today = (initialDate.getMonth() == month && initialDate.getFullYear() == year ? initialDate.getDate() : 0); + last_inserted_date = 1; + renderMonth(); + dom.find("table.w-calendar-table tbody").html(monthDom.find("tbody").html()); + fetchInterval = setTimeout(fetchEvents,1000); + + } +} + + + + + + + + + + + + diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index 67df027..8aaed13 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -22,16 +22,10 @@ class CalendarsController < ApplicationController page = Page.find_by(:page_id => params[:page_id]) rescue nil events =[] if !page.nil? - categories = page.categories - if categories.first == "all" - calendar_types = CalendarType.all.collect{|ct| ct.id.to_s } - else - calendar_types = CalendarType.where(:category_id.in => categories).collect{|ct| ct.id.to_s } rescue [] - end if params[:start].present? && params[:end].present? sdt = Time.at(params[:start].to_i) edt = Time.at(params[:end].to_i) - events = Event.monthly_event(sdt,edt).where(:calendar_type_id.in => calendar_types) + events = Event.monthly_event(sdt,edt) end end respond_to do |format| @@ -41,23 +35,34 @@ class CalendarsController < ApplicationController end def agenda - # re = Event.recurring_event(Time.at(params[:unix_start].to_i), Time.at(params[:unix_end].to_i)) - # @events = @events.inject(re, :<<) - page = Page.find_by(:page_id => params[:page_id]) rescue nil - events =[] - if !page.nil? - categories = page.categories - if categories.first == "all" - calendar_types = CalendarType.all.collect{|ct| ct.id.to_s } + if !params[:subpart_id].nil? + subpartid = params[:subpart_id] + widget = SubPart.find(subpartid) + calendar_types = widget.custom_array_field + else + calendar_types = [] + end + if params[:unix_start].present? && params[:unix_end].present? + agenda_start = Time.at(params[:unix_start].to_i).to_s + agenda_end = Time.at(params[:unix_end].to_i).to_s + if !calendar_types.blank? + events = Event.where(:calendar_type_id.in => calendar_types).agenda_events(agenda_start,agenda_end) else - calendar_types = CalendarType.where(:category_id.in => categories).collect{|ct| ct.id.to_s } rescue [] + events = Event.agenda_events(agenda_start,agenda_end) end - if params[:unix_start].present? && params[:unix_end].present? - agenda_start = Time.at(params[:unix_start].to_i).to_s - agenda_end = Time.at(params[:unix_end].to_i).to_s - events = Event.agenda_events(agenda_start,agenda_end).where(:calendar_type_id.in => calendar_types) - end - end - render json: events.to_json + end + render json: {"events" => events}.to_json({"frontend" => true}) end -end \ No newline at end of file +end + + + + + + + + + + + + diff --git a/app/models/event.rb b/app/models/event.rb index 05ea7f0..35b88e6 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -36,19 +36,33 @@ class Event ] def as_json(options = {}) - { - :id => self.id.to_s, - :title => self.title, - :note => self.note || "", - :start => self.start.rfc822, - :end => self.end.rfc822, - :allDay => self.all_day, - :recurring => self.recurring, - :calendar => self.calendar_type_id.to_s, - :color => (self.calendar_type.color rescue nil), - :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>self.id), - :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>self.id) - } + if options["frontend"] + { + :id => self.id.to_s, + :title => self.title, + :note => self.note || "", + :start => self.start.rfc822, + :end => self.end.rfc822, + :allDay => self.all_day, + :recurring => self.recurring, + :calendar => self.calendar_type_id.to_s, + :color => (self.calendar_type.color rescue nil) + } + else + { + :id => self.id.to_s, + :title => self.title, + :note => self.note || "", + :start => self.start.rfc822, + :end => self.end.rfc822, + :allDay => self.all_day, + :recurring => self.recurring, + :calendar => self.calendar_type_id.to_s, + :color => (self.calendar_type.color rescue nil), + :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>self.id), + :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>self.id) + } + end end @@ -112,6 +126,9 @@ class Event end def self.agenda_events(agenda_start, agenda_end) - @all_events = self.any_of(:start.gte => agenda_start, :end.gte => agenda_start).and(:start.lte => agenda_end).asc(:start) + recurring = self.recurring_event(agenda_start,agenda_end) + events = self.any_of(:start.gte => agenda_start).or(:end.gte => agenda_start).and(:start.lte => agenda_end).asc(:start) + all_events = recurring.concat(events) + recurring end end \ No newline at end of file diff --git a/lib/calendar/engine.rb b/lib/calendar/engine.rb index e30b826..f709fd9 100644 --- a/lib/calendar/engine.rb +++ b/lib/calendar/engine.rb @@ -5,7 +5,7 @@ module Calendar module_label "calendar.calendar" base_url File.expand_path File.dirname(__FILE__) widget_methods ["widget"] - widget_settings [{"data_count"=>10}] + widget_settings [{"override_category_with"=>"calendar_type","multiselect"=>true,"display_field"=>"title"}] taggable "Event" categorizable authorizable