PageSourceSearch

https://tree-s.co.jp/assets_web/js/jquery.holiday.js

js tree-s.co.jp collected 2026-10-04 19:11:26 UTC 9,446 bytes, 325 lines download raw bytes

1/* 
2 * jquery.holiday.js
3 * jQuery UI datepicker 用日本の祝祭日ツールチップ表示
4 * 
5 * 機能
6 * 		datepicker で表示されるダイアログに祝祭日の表示を付加します。
7 * 備考
8 *		西暦2007年以降から2015年現在までの祝祭日に対応
9 *		1つのWebページ内は同じ祝祭日の表示になります。
10 *		(ページ内の複数のdatepickerごとに表示内容は切り替えられません。)
11 * 使い方
12 *		<link rel="stylesheet" href="jquery.holiday.css">
13 *		<script src="jquery.holiday.js"></script>
14 * 
15 *		$('input.xxx').datepicker().holiday();
16 * 
17 * 地域・言語の対応
18 *		他の地域や言語のために表示データを差し替えることができます。
19 *		サンプル:
20 *		<script src="jquery.holiday_en-JP.js"></script>
21 * 
22 * 更新履歴
23 *	2016-10-22	holiday("init")の重複実行の抑止。動的なinput要素の生成に対応。
24 */
25
26(function($) {
27	var settings = {};
28	var css = {};
29	var holidays = {};
30	var prepared = {};
31	
32	var methods = {
33		//初期処理
34		init : function( options ) {
35			//datepicker ロードチェック
36			if (!$.fn['datepicker']) {
37				return this;
38			}
39
40			//オプション設定
41			settings = $.extend( {
42				//曜日、祝祭日のCSSスタイルクラス
43				'css': [
44					{'day':0, 'class':'day-sunday'}, //日曜日
45					{'day':6, 'class':'day-saturday'}, //土曜日
46					{'day':'holiday', 'class':'day-holiday'}, //祝祭日
47					{'day':'anniversary', 'class':'day-anniversary'} //記念日
48				],
49				//追加する記念日 [{date:年月日, name:名称, class:CSSクラス}]
50				'anniversary': []
51			}, options);
52			
53			//スタイルの登録
54			$.each(settings.css, function(key, val) {
55				if (typeof val['class']!="undefined") {
56					css[val['day']] = val['class'];
57				}
58			});
59			
60			//記念日の追加
61			var attr;
62			$.each (settings.anniversary, function(key, val){
63				attr = {};
64				attr['name'] = val['name'];
65				if (typeof val['class']!="undefined") {
66					attr['class'] = val['class'];
67				} else if (typeof css['anniversary']!="undefined") {
68					attr['class'] = css['anniversary'];
69				}
70				holidays[val['date']] = attr;
71			});
72			
73			return this.each(function() {
74				if (typeof $(this).datepicker!="function") {
75					return true;
76				}
77				//初期化済みチェック
78				if ($(this).data("holiday")) {
79					return true;
80				}
81				//初期化データ設定
82				$(this).data("holiday", {init: true});
83			
84				//祝祭日と日付スタイルの設定
85				$(this).datepicker("option", "beforeShowDay", function(day) {
86					var $self = $.fn.holiday;
87					var attr = $self("attr", day);
88					var dow = day.getDay();
89					var title = "";
90					if (typeof attr['name']!="undefined") {
91						title = attr['name'];
92					}
93					var style = "";
94					if (title && typeof attr['class']!="undefined") {
95    style = attr['class']; //祝祭日、記念日のスタイル
96    return false; //選択を無効化←追加箇所
97} else if (dow==6) { //日曜日か判定←追加箇所
98    return false; //選択を無効化←追加箇所
99} else if (dow==0) { //日曜日か判定←追加箇所
100    return false; //選択を無効化←追加箇所
101} else if (typeof css[dow]!="undefined") {
102    style = css[dow]; //曜日のスタイル
103}
104
105					return [true, style, title];
106				});
107			});
108
109		},
110		
111		//終了処理
112		destroy : function( ) {
113			return this.each(function() {
114				if (typeof $(this).datepicker!="function") {
115					return true;
116				}
117				//祝祭日と日付スタイルの除去
118				$(this).datepicker("option", "beforeShowDay", null);
119				//初期化済みデータ消去
120				$(this).removeData("holiday");
121			});
122		},
123		
124		//祝祭日の属性の取得
125		attr : function( day ) {
126			//日付が文字の場合はDate型に変換する。
127			if (typeof day=="string") {
128				var match = day.match(/^(20\d{2})(\d{2})(\d{2})$/);
129				if (match) {
130					match.shift();
131					day = new Date(match.join("/"));
132				} else {
133					day = new Date(day);
134				}
135			}
136			//祝祭日の計算
137			var Y = day.getFullYear();
138			var M = padzero(day.getMonth()+1);
139			if (typeof prepared[Y+M]=="undefined") {
140				prepare(Y, M);
141			}
142			//祝祭日のチェック
143			var YMD = day.getFullYear()+padzero(day.getMonth()+1)+padzero(day.getDate());
144			if (typeof holidays[YMD]!="undefined") {
145				return holidays[YMD];
146			} else {
147				return {};
148			}
149		},
150		
151		//祝祭日名の取得
152		name : function( day ) {
153			var attr = $.fn.holiday('attr', day);
154			if (typeof attr['name']=="undefined") {
155				return "";
156			} else {
157				return attr['name'];
158			}
159		},
160		
161		//祝祭日一覧
162		list : function() {
163			return holidays;
164		},
165		
166		//祝祭日データ設定
167		setRegionData : function(obj) {
168			if (typeof obj=="object") {
169				if (typeof obj['annual']=="object") {
170					annual = obj['annual'];
171				}
172				if (typeof obj['individual']=="object") {
173					individual = obj['individual'];
174				}
175				if (typeof obj['words']=="object") {
176					words = obj['words'];
177				}
178			}
179		}
180	};
181
182	$.fn.holiday = function( method ) {
183		if ( methods[method] ) {
184			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
185		} else if ( typeof method === 'object' || ! method ) {
186			return methods.init.apply( this, arguments );
187		} else {
188			$.error( 'Method ' +  method + ' does not exist on jQuery.holiday' );
189		}
190	};
191	
192	//ゼロサプレス
193	var padzero = function(val) {
194		return ('0'+val).slice(-2);
195	}
196	
197	//祝祭日計算
198	var prepare = function(Y, M) {
199		//作成済みチェック
200		if (typeof prepared[Y+M]!="undefined") {
201			return true;
202		}
203		
204		//年間の祝祭日の計算
205		var attr, substitutes = {};
206		if (typeof annual[M]!="undefined") {
207			var d, dayyear;
208			$.each(annual[M], function(key, val){
209				d = null;
210				if (isFinite(val['day'])) {
211					//日付固定
212					d = val['day'];
213				} else if (val['day']=="spring equinox") {
214					//春分の日
215					d = parseInt(20.69115 + (Y - 2000) * 0.2421904 - parseInt((Y - 2000) / 4));
216				} else if (val['day']=="autumnal equinox") {
217					//秋分の日
218					d = parseInt(23.09 + (Y - 2000) * 0.2421904 - parseInt((Y - 2000) / 4));
219				} else if (val['day'].match(/\//)) {
220					//ハッピーマンデー
221					d = padzero(nthday(Y, M, val['day']));
222				} else if (val.day.match(/\>/)) {
223					//特定年以降
224					dayyear = val['day'].split('>');
225					if (Y>=dayyear[1]) {
226						d = padzero(dayyear[0]);
227					}
228				}
229				
230				if (d==null) {
231					return true; //continue;
232				}
233
234				//祝祭日をリストに設定
235				attr = {};
236				attr['name'] = val['name'];
237				if (typeof val['class']!="undefined") {
238					attr['class'] = val['class'];
239				} else if (typeof css['holiday']!="undefined") {
240					attr['class'] = css['holiday'];
241				} else {
242					attr['class'] = "";
243				}
244
245				holidays[Y + M + d] = attr;
246				
247				//振替休日のチェック
248				d = new Date(Y, M-1, d);
249				if (d.getDay()==0) {
250					d = padzero(d.getDate() + 1);
251					attr['name'] = words['substitute']+"("+val['name']+")";
252					substitutes[Y +"/"+ M +"/"+ d] = attr;
253				}
254			});
255			
256			//振替休日の補正
257			$.each (substitutes, function(key, val) {
258				key = new Date(key);
259				d = key.getFullYear()+padzero(key.getMonth()+1)+padzero(key.getDate());
260				while (typeof holidays[d]!="undefined") {
261					key.setDate(key.getDate() + 1);
262					d = key.getFullYear()+padzero(key.getMonth()+1)+padzero(key.getDate());
263				}
264				holidays[d] = val;
265			});
266		}
267		
268		//個別の祝祭日の追加
269		if (typeof individual[Y+M]!="undefined") {
270			$.each(individual[Y+M], function(key, val) {
271				attr = {};
272				attr['name'] = val['name'];
273				if (typeof val['class']!="undefined") {
274					attr['class'] = val['class'];
275				} else if (typeof css['holiday']!="undefined") {
276					attr['class'] = css['holiday'];
277				} else {
278					attr['class'] = "";
279				}
280				holidays[Y+M+val.day] = attr;
281			});
282		};
283		
284		prepared[Y+M] = true;
285		return true;
286	}
287
288	//月の第n番目のm曜日
289	var nthday = function(y, m, val) {
290		var dayOfWeek = val.split('/');
291		var firstDay = new Date(y, m - 1, 1);
292		var adjust = dayOfWeek[1] - firstDay.getDay();
293		if (adjust < 0)
294			adjust += 7;
295		//第n番目のm曜日
296		return 1 + adjust + (dayOfWeek[0] -1 ) * 7;
297	}
298	
299	//年間の祝祭日データ
300	var annual = {
301		'01': [{'day':'01','name':'元旦'}, {'day':'2/1','name':'成人の日'}],
302		'02': [{'day':'11','name':'建国記念日'}],
303		'03': [{'day':'spring equinox','name':'春分の日'}],
304		'04': [{'day':'29','name':'昭和の日'}],
305		'05': [{'day':'03','name':'憲法記念日'}, {'day':'04','name':'みどりの日'}, {'day':'05','name':'こどもの日'}],
306		'07': [{'day':'3/1','name':'海の日'}],
307		'08': [{'day':'11>
3072016','name':'山の日'}], //2016年以降
308		'09': [{'day':'3/1','name':'敬老の日'}, {'day':'autumnal equinox','name':'秋分の日'}],
309		'10': [{'day':'2/1','name':'体育の日'}],
310		'11': [{'day':'03','name':'文化の日'}, {'day':'23','name':'勤労感謝の日'}],
311		'12': [{'day':'23','name':'天皇誕生日'}]
312	};
313	//個別の祝祭日データ
314	var individual = {
315		'201509': [{'day':'22','name':'国民の休日'}],
316		'202609': [{'day':'22','name':'国民の休日'}], //2015年時点の推定
317		'203209': [{'day':'21','name':'国民の休日'}], //2015年時点の推定
318		'203709': [{'day':'22','name':'国民の休日'}] //2015年時点の推定
319	};
320	//言語対応
321	var words = {
322		'substitute': '振替休日'
323	};
324		
325})(jQuery);

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.