PageSourceSearch

https://tubesq.jp/wp-content/themes/tube_sq_ver.03/js/libs/gcalendar-holidays.js?ver=4.7.29

js tubesq.jp collected 2026-10-02 19:25:39 UTC 7,408 bytes, 196 lines download raw bytes

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

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.