Skip to content

Commit

Permalink
Display holidays
Browse files Browse the repository at this point in the history
  • Loading branch information
duybuffet authored and duyth4993 committed Feb 15, 2016
1 parent 6e0a864 commit a427a16
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 5 deletions.
45 changes: 45 additions & 0 deletions app/assets/javascripts/calendar/custom.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
$(document).ready(function() {
$(window).trigger("resize");
var schedule_query_url = "/api/schedules.json";
var holiday_query_url = "/api/holidays.json";
var MyCalendar = $("#calendar");
var MyMiniCalendar = $("#mini-calendar");
var EventPopup = $("#quick-event-popup");
Expand Down Expand Up @@ -168,6 +169,8 @@ $(document).ready(function() {

localStorage.setItem("view_type", view.type);

cloneHeaderRow(view);

try {
setTimeLine();
setInterval(function(){ setTimeLine(); }, clock*1000);
Expand All @@ -177,6 +180,8 @@ $(document).ready(function() {
setElementBackground(lastSelectedDay, "");
MyCalendar.find(".fc-left").append($("#room_selector"));
MyCalendar.find(".fc-right").append($("#other_dropdown"));

renderHolidays(view);
},
loading: function(isLoading, view){
$("label.loading").attr("class", isLoading ? "loading" : "loading hidden");
Expand Down Expand Up @@ -337,6 +342,46 @@ $(document).ready(function() {
}
}

function cloneHeaderRow(view) {
if (view.type != "month" && $(".list-holidays").length == 0){
MyCalendar.find(".fc-head").clone().addClass("list-holidays").insertAfter(".fc-head");
$(".list-holidays .fc-day-header").each(function(){
if (view.type == "agendaDay"){
var currentDate = new Date(MyCalendar.fullCalendar("getView").start);
$(this).attr("value", dateFormat(currentDate, "mm-dd")).html(" ");
} else {
$(this).attr("value", dateFormat(new Date($(this).text()), "mm-dd")).html(" ");
}
});
}
}

function renderHolidays(view) {
var fcStartRange = new Date(MyCalendar.fullCalendar("getView").start);
var fcEndRange = new Date(MyCalendar.fullCalendar("getView").end);
$.ajax({
url: holiday_query_url,
type: "GET",
data: {start_date_range: fcStartRange, end_date_range: fcEndRange},
success: function(response) {
if(response.holidays){
$.map(response.holidays, function(holiday) {
holidayDate = dateFormat(new Date(holiday.date), "mm-dd");
if (view.type == "month"){
$(".fc-day-number").filter(function(){
return dateFormat($(this).data("date"), "mm-dd") == holidayDate;
}).addClass("holiday").prepend("<span class='holiday-name-month'>" + holiday.name + "</span>");
} else {
$(".list-holidays .fc-day-header").filter(function(){
return $(this).attr("value") == holidayDate;
}).addClass("holiday").text(holiday.name);
}
});
}
}
});
}

$(document).on("click", ".fc-body, #quick-event-popup", function(e){
e.stopPropagation();
});
Expand Down
125 changes: 125 additions & 0 deletions app/assets/javascripts/date_format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Date Format 1.2.3
* (c) 2007-2009 Steven Levithan <stevenlevithan.com>
* MIT license
*
* Includes enhancements by Scott Trenda <scott.trenda.net>
* and Kris Kowal <cixar.com/~kris.kowal/>
*
* Accepts a date, a mask, or a date and a mask.
* Returns a formatted version of the given date.
* The date defaults to the current date/time.
* The mask defaults to dateFormat.masks.default.
*/

var dateFormat = function () {
var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
timezoneClip = /[^-+\dA-Z]/g,
pad = function (val, len) {
val = String(val);
len = len || 2;
while (val.length < len) val = "0" + val;
return val;
};

// Regexes and supporting functions are cached through closure
return function (date, mask, utc) {
var dF = dateFormat;

// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
mask = date;
date = undefined;
}

// Passing date through Date applies Date.parse, if necessary
date = date ? new Date(date) : new Date;
if (isNaN(date)) throw SyntaxError("invalid date");

mask = String(dF.masks[mask] || mask || dF.masks["default"]);

// Allow setting the utc argument via the mask
if (mask.slice(0, 4) == "UTC:") {
mask = mask.slice(4);
utc = true;
}

var _ = utc ? "getUTC" : "get",
d = date[_ + "Date"](),
D = date[_ + "Day"](),
m = date[_ + "Month"](),
y = date[_ + "FullYear"](),
H = date[_ + "Hours"](),
M = date[_ + "Minutes"](),
s = date[_ + "Seconds"](),
L = date[_ + "Milliseconds"](),
o = utc ? 0 : date.getTimezoneOffset(),
flags = {
d: d,
dd: pad(d),
ddd: dF.i18n.dayNames[D],
dddd: dF.i18n.dayNames[D + 7],
m: m + 1,
mm: pad(m + 1),
mmm: dF.i18n.monthNames[m],
mmmm: dF.i18n.monthNames[m + 12],
yy: String(y).slice(2),
yyyy: y,
h: H % 12 || 12,
hh: pad(H % 12 || 12),
H: H,
HH: pad(H),
M: M,
MM: pad(M),
s: s,
ss: pad(s),
l: pad(L, 3),
L: pad(L > 99 ? Math.round(L / 10) : L),
t: H < 12 ? "a" : "p",
tt: H < 12 ? "am" : "pm",
T: H < 12 ? "A" : "P",
TT: H < 12 ? "AM" : "PM",
Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
};

return mask.replace(token, function ($0) {
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
});
};
}();

// Some common format strings
dateFormat.masks = {
"default": "ddd mmm dd yyyy HH:MM:ss",
shortDate: "m/d/yy",
mediumDate: "mmm d, yyyy",
longDate: "mmmm d, yyyy",
fullDate: "dddd, mmmm d, yyyy",
shortTime: "h:MM TT",
mediumTime: "h:MM:ss TT",
longTime: "h:MM:ss TT Z",
isoDate: "yyyy-mm-dd",
isoTime: "HH:MM:ss",
isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
dayNames: [
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
],
monthNames: [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
]
};

// For convenience...
Date.prototype.format = function (mask, utc) {
return dateFormat(this, mask, utc);
};
1 change: 1 addition & 0 deletions app/assets/javascripts/full_calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
//= require schedules
//= require underscore
//= require header-dropdown
//= require date_format
24 changes: 21 additions & 3 deletions app/assets/stylesheets/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -576,15 +576,33 @@ footer {

#load {
position: absolute;
margin-left: 275px;
margin-left: 265px;
padding: 3.6px;
padding-left: 10px;
padding-right: 10px;
padding-left: 5px;
padding-right: 5px;
}

.holiday {
border: 1px solid #16A765 !important;
color: #1d1d1d !important;
background-color: #16A765 !important;
margin: 2px;
}

.holiday-name-month {
position: relative;
right: 40%;
}

.glyphicon-refresh-animate {
-animation: spin .7s infinite linear;
-webkit-animation: spin2 .7s infinite linear;
-moz-animation: spin3 .7s infinite linear;
}

@-moz-keyframes spin3 {
from { -moz-transform: rotate(0deg);}
to { -moz-transform: rotate(360deg);}
}

@-webkit-keyframes spin2 {
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/api/holidays_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Api::HolidaysController < ApplicationController
def index
load_holidays
respond_to do |format|
format.json {render json: {holidays: @holidays}, status: :ok}
end
end

private
def load_holidays
from = Date.parse params[:start_date_range]
to = Date.parse params[:end_date_range]
@holidays = Holiday.in_range from, to
end
end
2 changes: 1 addition & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize user
when user.admin?
can :access, :rails_admin
can :dashboard
can :manage, [User, Repeat, Room]
can :manage, [User, Repeat, Room, Holiday]
can [:create, :read, :destroy], Schedule
can :update, Schedule do |schedule|
schedule.finish_time > Time.zone.now
Expand Down
20 changes: 20 additions & 0 deletions app/models/concerns/rails_admin_holiday.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module RailsAdminHoliday
extend ActiveSupport::Concern

included do
rails_admin do
list do
field :id
field :name
field :date
field :is_annual
end

edit do
field :name
field :date
field :is_annual
end
end
end
end
12 changes: 12 additions & 0 deletions app/models/holiday.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Holiday < ActiveRecord::Base
include RailsAdminHoliday

QUERY = "(is_annual = 0 AND date BETWEEN :from AND :to)
OR (is_annual = 1 AND DATE_FORMAT(date,'%m-%d') >= DATE_FORMAT(:from,'%m-%d')
AND DATE_FORMAT(date,'%m-%d') <= DATE_FORMAT(:to,'%m-%d'))"

validates :name, presence: true
validates :date, presence: true

scope :in_range, -> (from, to){where(QUERY, from: from, to: to)}
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
resources :my_schedules, only: :index
resources :shared_schedules, only: :index
resources :schedules, only: :index
resources :holidays, only: :index
end
end
9 changes: 9 additions & 0 deletions db/migrate/20160204155953_create_holidays.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateHolidays < ActiveRecord::Migration
def change
create_table :holidays do |t|
t.string :name
t.date :date
t.boolean :is_annual, default: true
end
end
end
8 changes: 7 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160201035201) do
ActiveRecord::Schema.define(version: 20160204155953) do

create_table "creators", force: :cascade do |t|
t.string "email", limit: 255
t.string "display_name", limit: 255
end

create_table "holidays", force: :cascade do |t|
t.string "name", limit: 255
t.date "date"
t.boolean "is_annual", limit: 1, default: true
end

create_table "repeats", force: :cascade do |t|
t.integer "repeat_type", limit: 4
t.datetime "created_at", null: false
Expand Down

0 comments on commit a427a16

Please sign in to comment.