PageSourceSearch

https://www.braceriabava.com/cgi-bin/mailform/gcalendar-holidays.js

js braceriabava.com collected 2026-10-08 19:26:15 UTC 7,128 bytes, 190 lines download raw bytes

1/**
2 *  GCalendar Holidays - Googleカレンダーから日本の祝日を取得
3 *  @see       http://0-oo.net/sbox/javascript/google-calendar-holidays
4 *  @version   0.4.0
5 *  @copyright 2008-2011 [email protected]
6 *  @license   http://0-oo.net/pryn/MIT_license.txt (The MIT license)
7 *
8 *  See also
9 *  @see http://code.google.com/intl/ja/apis/calendar/data/2.0/reference.html
10 *  @see http://code.google.com/intl/ja/apis/gdata/docs/json.html
11 */
12
13var GCalHolidays = {
14    /** GoogleカレンダーのID(複数でも可) */
15    userIds: [
16        "[email protected]"  //Google公式版
17        //"[email protected]"    //Google公式(英語)版
18        //"[email protected]" //mozilla.org版
19    ],
20
21    /** 取得するイベント(スケジュール)の最大件数 */
22    maxResults: 31,
23
24    // @see http://code.google.com/intl/ja/apis/calendar/data/2.0/reference.html#Visibility
25    visibility: "public",
26
27    // @see http://code.google.com/intl/ja/apis/calendar/data/2.0/reference.html#Projection
28    projection: "full-noattendees",
29
30    /** jQuery UI Datepicker用のstyle(Themeに合わせてお好みで上書きして使う) */
31    datepickerStyles: {
32        sunday:   "background-image: none; background-color: #f99", //日曜日
33        saturday: "background-image: none; background-color: #6cf", //土曜日
34        holiday:  "background-image: none; background-color: #f9f"  //祝日
35    }
36};
37/**
38 *  祝日を取得する
39 *  @param  Function    callback    データ取得時に呼び出されるfunction
40 *  @param  Number      year        (optional) 年(指定しなければ今年)
41 *  @param  Number      month       (optional) 月(1~12 指定しなければ1年の全て)
42 */
43GCalHolidays.get = function(callback, year, month) {
44    //日付範囲
45    var padZero = function(value) { return ("0" + value).slice(-2); };
46    var y = year || new Date().getFullYear();
47    var start = [y, padZero(month || 1), "01"].join("-");
48    var m = month || 12;
49    var end = [y, padZero(m), padZero(new Date(y, m, 0).getDate())].join("-");
50
51    this._caches = (this._caches || {});
52    this._userCallback = callback;
53
54    for (var i = 0, len = this.userIds.length; i < len; i++) {
55        var cache = this._caches[i + ":" + start + ".." + end];
56
57        if (cache) {    //取得済みの場合はそれを使う
58            callback(cache, i);
59            continue;
60        }
61
62        //URL作成
63        var url = location.protocol + "//www.google.com/calendar/feeds/";
64        url += encodeURIComponent(this.userIds[i]) + "/";
65        url += this.visibility + "/" + this.projection;
66        url += "?alt=json-in-script&callback=GCalHolidays.decode";
67        url += "&max-results=" + this.maxResults;
68        url += "&start-min=" + start + "&start-max=" + end + "T23:59:59";
69
70        //scriptタグ生成
71        var script = document.createElement("script");
72        script.type = "text/javascript";
73        script.src = url;
74        script.charset = "UTF-8";
75        document.body.appendChild(script);
76    }
77};
78/**
79 *  JSONPによりGoogle Calendar API経由で呼び出されるfunction
80 *  @param  Object  gdata   カレンダーデータ
81 */
82GCalHolidays.decode = function(gdata) {
83    var days = GCalHolidays._entries2days(gdata.feed.entry);
84
85    var links = gdata.feed.link;
86    var href = "";
87
88    for (var j = 0, len2 = links.length; j < len2; j++) {
89        if (links[j].rel == "self") {
90            href = links[j].href;
91        }
92    }
93
94    var userId = decodeURIComponent(href.split("/")[5]);
95    var index = null;
96
97    //どのカレンダーか特定する
98    for (var i = 0, len = this.userIds.length; i < len; i++) {
99        if (this.userIds[i] == userId) {
100            index = i;
101            break;
102        }
103    }
104
105    //キャッシュする
106    var range = href.match(/\d{4}-\d{2}-\d{2}/g);   //日付範囲の最初の日と最後の日
107    this._caches[index + ":" + range[0] + ".." + range[1]] = days;
108
109    //コールバック
110    this._userCallback(days, index);
111};
112/**
113 *  JSONPで取得したデータから日付情報を取り出す
114 *  @param  Array   entries スケジュール
115 *  @return Array   日付情報(year, month, date, title)
116 */
117GCalHolidays._entries2days = function(entries) {
118    var days = [];
119
120    if (!entries) {
121        return days;
122    }
123
124    //日付順にソート
125    entries.sort(function(a, b) {
126        return (a.gd$when[0].startTime > b.gd$when[0].startTime) ? 1 : -1;
127    });
128
129    //シンプルな器に移す
130    for (var i = 0, len = entries.length; i < len; i++) {
131        var ymd = entries[i].gd$when[0].startTime.split("T")[0].split("-");
132        var title = entries[i].title.$t;
133        //年月日は使いやすいように数値にする
134        days[i] = { year: ymd[0] * 1, month: ymd[1] * 1, date: ymd[2] * 1, title: title };
135    }
136
137    return days;
138};
139
140/**
141 *  jQuery UI Datepickerのカレンダーに祝日を表示する
142 *  @param  Number  year    表示する年
143 *  @param  Number  month   表示する月
144 *  @param  Object  inst    Datepicker
145 *  @see http://jqueryui.com/demos/datepicker/
146 */
147GCalHolidays.datepicker = function(year, month, inst) {
148    setTimeout(function() { //処理後に対象のdivが再構築されるケースを回避
149        GCalHolidays.get(function(holidays) {
150            for (var i = 0, len = holidays.length; i < len; i++) {
151                var holiday = holidays[i];
152
153                if (holiday.year != year || holiday.month != month) {
154                    return; //既に別の月を表示している場合は何もしない
155                }
156
157                inst.dpDiv.find("a").each(function() {
158                    if ($(this).text() == holiday.date) {
159                        $(this).addClass("gcal-holiday").attr("title", holiday.title);
160                        return false;
161                    }
162                });
163            }
164        }, year, month);
165    }, 1);
166};
167/**
168 *  jQuery UI Datepickerが有効な場合はイベントハンドラとstyleをセットする
169 */
170if (window.$ && $.datepicker && $.datepicker.setDefaults) {
171    $.datepicker.setDefaults({
172        beforeShow: function(input, inst) {
173            var date = $(input).datepicker("getDate") || new Date();
174            GCalHolidays.datepicker(date.getFullYear(), date.getMonth() + 1, inst);
175        },
176        beforeShowDay: function(date) { //土日のclass属性
177            return [true, { 0: "gcal-sunday", 6: "gcal-saturday" }[date.getDay()] || ""];
178        },
179        onChangeMonthYear: GCalHolidays.datepicker
180    });
181
182    $(function() {  //ページ表示後に土日・祝日用のstyleをセット
183        var styles = GCalHolidays.datepickerStyles;
184        var css = "";
185        css += ".gcal-sunday   .ui-state-default { " + styles.sunday + " } ";
186        css += ".gcal-saturday .ui-state-default { " + styles.saturday + " } ";
187        css += ".ui-widget-content .gcal-holiday { " + styles.holiday + " }";
188        $("head").append($('<style type="text/css">' + css + "</style>"));
189    });
190}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.