PageSourceSearch

https://tsumugiya.net/ec/skin/frontend/rwd/tsumugiya/js/jquery.jpostal.js

js tsumugiya.net collected 2026-10-05 19:13:13 UTC 9,927 bytes, 396 lines download raw bytes

1/**
2 * jquery.jpostal.js ver2.2
3 * 
4 * Copyright 2014, Aoki Makoto, Ninton G.K. http://www.ninton.co.jp
5 * 
6 * Released under the MIT license - http://en.wikipedia.org/wiki/MIT_License
7 * 
8 * Requirements
9 * jquery.js
10 */
11
12(function($) {
13	function JpostalDatabase(i_options) {
14		this.address = [];	// database cache
15		this.map = {};
16		this.url = {
17			'http': 'https://jpostal.googlecode.com/svn/trunk/json/',
18			'https': 'https://jpostal.googlecode.com/svn/trunk/json/',
19		};
20
21		this.find = function (i_postcode) {
22			var address = [];
23
24			for (var i = 0; i < this.address.length; ++i) {
25				if (this.address[i][0] == '_' + i_postcode) {
26					address = this.address[i];
27				}
28			}
29
30			return address;
31		};
32
33		this.get = function (i_postcode) {
34			//	--------------------------------------------------
35			//	i_postcode	find()	find()	result
36			//				1234567	123
37			//	--------------------------------------------------
38			//	1			-		-		defaults
39			//	12			-		-		defaults
40			//	123			-		Y		find( '123' )
41			//	123			-		N		defaults
42			//	1234		-		Y		find( '123' )
43			//	1234		-		N		defaults
44			//	1234567		Y		-		find( '1234567' )
45			//	1234567		N		Y		find( '123' )
46			//	1234567		N		N		defaults
47			//	--------------------------------------------------
48			var defaults = ['', '', '', '', '', '', '', '', ''];
49			var address;
50			var head3;
51
52			switch (i_postcode.length) {
53				case 3:
54				case 4:
55				case 5:
56				case 6:
57					head3 = i_postcode.substr(0, 3);
58					address = this.find(head3);
59					address = $.extend(defaults, address);
60					break;
61
62				case 7:
63					address = this.find(i_postcode);
64					if (!address) {
65						head3 = i_postcode.substr(0, 3);
66						address = this.find(head3);
67					}
68					address = $.extend(defaults, address);
69					break;
70
71				default:
72					address = defaults;
73					break;
74			}
75
76			return address;
77		};
78
79		this.getUrl = function (i_head3) {
80			var url = '';
81
82			switch (window.location.protocol) {
83				case 'http:':
84					url = this.url['http'];
85					break;
86
87				case 'https:':
88					url = this.url['https'];
89					break;
90			}
91			url = url + i_head3 + '.json';
92
93			return url;
94		};
95
96		this.request = function (i_postcode, i_callback) {
97			var _this = this;
98			var head3 = i_postcode.substr(0, 3);
99
100			if (i_postcode.length <= 2 || this.getStatus(head3) != 'none' || head3.match(/[^0-9]/)) {
101				return false;
102			}
103			this.setStatus(head3, 'waiting');
104
105			var url = this.getUrl(head3);
106
107			var options = {
108				async: false,
109				dataType: 'jsonp',
110				jsonpCallback: 'jQuery_jpostal_callback',
111				type: 'GET',
112				url: url,
113				success: function (i_data, i_dataType) {
114					JposDb.save(i_data);
115					i_callback();
116				},
117				error: function (i_XMLHttpRequest, i_textStatus, i_errorThrown) {
118				},
119				timeout: 5000	// msec
120			};
121			$.ajax(options);
122			return true;
123		};
124
125		this.save = function (i_data) {
126			for (var i = 0; i < i_data.length; ++i) {
127				var rcd = i_data[i];
128				var postcode = rcd[0];
129
130				if (typeof this.map[postcode] == 'undefined') {
131					this.address.push(rcd);
132					this.map[postcode] = {state: 'complete', time: 0};
133				} else if (this.map[postcode].state == 'waiting') {
134					this.address.push(rcd);
135					this.map[postcode].state = 'complete';
136				}
137			}
138		};
139
140		this.getStatus = function (i_postcode) {
141			//	--------------------------------------------------
142			//	#	['_001']	..state		.time		result
143			//	--------------------------------------------------
144			//	1	 =undefined	-			-			none
145			//	2	!=undefined	'complete'	-			complete
146			//	3	!=undefined	'waiting'	<5sec		waiting
147			//	4	!=undefined	'waiting'	>=5sec		none
148			//	--------------------------------------------------
149			var st = '';
150			var postcode = '_' + i_postcode;
151
152			if (typeof this.map[postcode] == 'undefined') {
153				// # 1
154				st = 'none';
155
156			} else if ('complete' == this.map[postcode].state) {
157				// # 2
158				st = 'complete';
159
160			} else {
161				var t_ms = (new Date()).getTime() - this.map[postcode].time;
162				if (t_ms < 5000) {
163					// # 3
164					st = 'waiting';
165
166				} else {
167					// # 4
168					st = 'none';
169				}
170			}
171
172			return st;
173		};
174
175		this.setStatus = function (i_postcode) {
176			var postcode = '_' + i_postcode;
177
178			if (typeof this.map[postcode] == 'undefined') {
179				this.map[postcode] = {
180					state: 'waiting',
181					time: 0
182				};
183			}
184
185			this.map[postcode].time = (new Date()).getTime();
186		};
187
188	}
189
190	function Jpostal(i_JposDb) {
191		this.address = '';
192		this.jposDb = i_JposDb;
193		this.options = {};
194		this.postcode = '';
195		this.minLen = 3;
196		this.addressCallback = null;
197
198		this.displayAddress = function () {
199			if (this.postcode == '000info') {
200				this.address[2] += ' ' + this.getScriptSrc();
201			}
202
203			console.log(this.address);
204			for (var key in this.options.address) {
205				if (this.addressCallback != null) {
206					var s = this.formatAddress(this.options.address[key], this.address);
207					if (!this.addressCallback(key, s))  {
208						$(key).val(s);
209					}
210				} else {
211					var s = this.formatAddress(this.options.address[key], this.address);
212					$(key).val(s);
213				}
214			}
215		};
216
217		this.formatAddress = function (i_fmt, i_address) {
218			var s = i_fmt;
219
220			s = s.replace(/%3|%p|%prefecture/, i_address[1]);
221			s = s.replace(/%4|%c|%city/, i_address[2]);
222			s = s.replace(/%5|%t|%town/, i_address[3]);
223			s = s.replace(/%6|%a|%address/, i_address[4]);
224			s = s.replace(/%7|%n|%name/, i_address[5]);
225
226			s = s.replace(/%8/, i_address[6]);
227			s = s.replace(/%9/, i_address[7]);
228			s = s.replace(/%10/, i_address[8]);
229
230			return s;
231		};
232
233		this.getScriptSrc = function () {
234			var src = '';
235
236			var el_arr = document.getElementsByTagName('script');
237			for (var i = 0; i < el_arr.length; ++i) {
238				if (0 <= el_arr[i].src.search(/jquery.jpostal.js/)) {
239					src = el_arr[i].src;
240				}
241			}
242
243			return src;
244		}
245
246		this.init = function (i_options) {
247
248			if (typeof i_options.postcode == 'undefined') {
249				throw new Error('postcode undefined');
250			}
251			if (typeof i_options.address == 'undefined') {
252				throw new Error('address undefined');
253			}
254
255			this.options.postcode = [];
256			if (typeof i_options.postcode == 'string') {
257				this.options.postcode.push(i_options.postcode);
258			} else {
259				this.options.postcode = i_options.postcode;
260			}
261
262			this.options.address = i_options.address;
263
264			if (typeof i_options.url != 'undefined') {
265				this.jposDb.url = i_options.url;
266			}
267
268			this.addressCallback = i_options.addressCallback;
269		};
270
271		this.main = function () {
272			this.scanPostcode();
273			if (this.postcode.length < this.minLen) {
274				// git hub issue #4: 郵便番号欄が0~2文字のとき、住所欄を空欄にせず、入力内容を維持してほしい
275				return;
276			}
277
278			var _this = this;
279			var f = this.jposDb.request(this.postcode, function () {
280				_this.callback();
281			});
282			if (!f) {
283				this.callback();
284			}
285		};
286
287		this.callback = function () {
288			this.address = this.jposDb.get(this.postcode);
289			this.displayAddress();
290		};
291
292		this.scanPostcode = function () {
293			var s = '';
294
295			switch (this.options.postcode.length) {
296				case 0:
297					break;
298
299				case 1:
300					//	github issue #8: 1つ目を空欄、2つ目を「001」としても、「001」として北海道札幌市を表示してしまう
301					//	----------------------------------------
302					//	case	postcode	result
303					//	----------------------------------------
304					//	1		''			''
305					//	1		12			''
306					//	2		123			123
307					//	2		123-		123
308					//	2		123-4		123
309					//	3		123-4567	1234567
310					//	2		1234		123
311					//	4		1234567		1234567
312					//	----------------------------------------
313					s = String($(this.options.postcode[0]).val());
314					if (0 <= s.search(/^([0-9]{3})([0-9A-Za-z]{4})/)) {
315						// case 4
316						s = RegExp.$1 + '' + RegExp.$2;
317					} else if (0 <= s.search(/^([0-9]{3})-([0-9A-Za-z]{4})/)) {
318						// case 3
319						s = RegExp.$1 + '' + RegExp.$2;
320					} else if (0 <= s.search(/^([0-9]{3})/)) {
321						// case 2
322						s = RegExp.$1;
323					} else {
324						// case 1
325						s = '';
326					}
327					break;
328
329				case 2:
330					//	github issue #8: 1つ目を空欄、2つ目を「001」としても、「001」として北海道札幌市を表示してしまう
331					//	----------------------------------------
332					//	case	post1	post2	result
333					//	----------------------------------------
334					//	1		''		---		''
335					//	1		12		---		''
336					//	2		123		''		123
337					//	2		123		4		123
338					//	3		123		4567	1234567
339					//	----------------------------------------
340					var s3 = String($(this.options.postcode[0]).val());
341					var s4 = String($(this.options.postcode[1]).val());
342					if (0 <= s3.search(/^[0-9]{3}$/)) {
343						if (0 <= s4.search(/^[0-9A-Za-z]{4}$/)) {
344							// case 3
345							s = s3 + s4;
346						} else {
347							// case 2
348							s = s3;
349						}
350					} else {
351						// case 1
352						s = '';
353					}
354					break;
355			}
356
357			this.postcode = s;
358		};
359	}
360
361//	MEMO: For the following reason, JposDb was put on the global scope, not local scope. 
362//	---------------------------------------------------------------------
363// 	data file	callback			JposDb scope
364//	---------------------------------------------------------------------
365//	001.js		JposDb.save			global scope
366//	001.js.php	$_GET['callback']	local scopde for function($){}
367//	---------------------------------------------------------------------
368	var JposDb = new JpostalDatabase();
369
370	/*
371	//どうやらjsonpがうまく呼び出されていないようだ。
372	function jQuery_jpostal_callback(i_data) {
373		JposDb.save(i_data);
374	}
375	*/
376
377	//(function ($) {
378		$.fn.jpostal = function (i_options) {
379			var Jpos = new Jpostal(JposDb);
380			Jpos.init(i_options);
381
382			if (typeof i_options.click == 'string' && i_options.click != '') {
383				$(i_options.click).bind('click', function (e) {
384					Jpos.main();
385				});
386			} else {
387				for (var i = 0; i < Jpos.options.postcode.length; ++i) {
388					var selector = Jpos.options.postcode[i];
389					$(selector).bind('keyup change', function (e) {
390						Jpos.main();
391					});
392				}
393			}
394		};
395	//})(jQuery);
396})($j);

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.