widget added for calendar and also some engine settings

This commit is contained in:
Harry Bomrah 2015-06-02 17:22:51 +08:00
parent 37e58c00d0
commit a7a91c4be6
5 changed files with 261 additions and 41 deletions

View File

@ -357,8 +357,8 @@ var AgendaView = function(calendar){
type : "get", type : "get",
url : url, url : url,
data : {"agenda_start":sd.toLocaleString(),"agenda_end":ed.toLocaleString(),"page_id" : _calendar.page_id,"unix_start":usd,"unix_end":ued}, data : {"agenda_start":sd.toLocaleString(),"agenda_end":ed.toLocaleString(),"page_id" : _calendar.page_id,"unix_start":usd,"unix_end":ued},
success : function(events){ success : function(data){
$.each(events,function(i,e){ $.each(data.events,function(i,e){
var ed = eventDom(e), var ed = eventDom(e),
s = new Date(e.start), s = new Date(e.start),
e = new Date(e.end), e = new Date(e.end),

View File

@ -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 = $("<div data-year='"+year+"' data-month='"+month+"'></div>");
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 = $("<tr></tr>");
switch (position){
case "first":
for(var i = 0;i < 7;i++){
var td = $("<td></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 = $("<td></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 = $("<td></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);
}
}

View File

@ -22,16 +22,10 @@ class CalendarsController < ApplicationController
page = Page.find_by(:page_id => params[:page_id]) rescue nil page = Page.find_by(:page_id => params[:page_id]) rescue nil
events =[] events =[]
if !page.nil? 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? if params[:start].present? && params[:end].present?
sdt = Time.at(params[:start].to_i) sdt = Time.at(params[:start].to_i)
edt = Time.at(params[:end].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
end end
respond_to do |format| respond_to do |format|
@ -41,23 +35,34 @@ class CalendarsController < ApplicationController
end end
def agenda def agenda
# re = Event.recurring_event(Time.at(params[:unix_start].to_i), Time.at(params[:unix_end].to_i)) if !params[:subpart_id].nil?
# @events = @events.inject(re, :<<) subpartid = params[:subpart_id]
page = Page.find_by(:page_id => params[:page_id]) rescue nil widget = SubPart.find(subpartid)
events =[] calendar_types = widget.custom_array_field
if !page.nil?
categories = page.categories
if categories.first == "all"
calendar_types = CalendarType.all.collect{|ct| ct.id.to_s }
else else
calendar_types = CalendarType.where(:category_id.in => categories).collect{|ct| ct.id.to_s } rescue [] calendar_types = []
end end
if params[:unix_start].present? && params[:unix_end].present? if params[:unix_start].present? && params[:unix_end].present?
agenda_start = Time.at(params[:unix_start].to_i).to_s agenda_start = Time.at(params[:unix_start].to_i).to_s
agenda_end = Time.at(params[:unix_end].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) if !calendar_types.blank?
events = Event.where(:calendar_type_id.in => calendar_types).agenda_events(agenda_start,agenda_end)
else
events = Event.agenda_events(agenda_start,agenda_end)
end end
end end
render json: events.to_json render json: {"events" => events}.to_json({"frontend" => true})
end end
end end

View File

@ -36,6 +36,19 @@ class Event
] ]
def as_json(options = {}) def as_json(options = {})
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, :id => self.id.to_s,
:title => self.title, :title => self.title,
@ -49,6 +62,7 @@ class Event
:edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>self.id), :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) :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>self.id)
} }
end
end end
@ -112,6 +126,9 @@ class Event
end end
def self.agenda_events(agenda_start, agenda_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
end end

View File

@ -5,7 +5,7 @@ module Calendar
module_label "calendar.calendar" module_label "calendar.calendar"
base_url File.expand_path File.dirname(__FILE__) base_url File.expand_path File.dirname(__FILE__)
widget_methods ["widget"] widget_methods ["widget"]
widget_settings [{"data_count"=>10}] widget_settings [{"override_category_with"=>"calendar_type","multiselect"=>true,"display_field"=>"title"}]
taggable "Event" taggable "Event"
categorizable categorizable
authorizable authorizable