Develop and Download Open Source Software

Browse Subversion Repository

Contents of /js/domain.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (show annotations) (download) (as text)
Tue Dec 22 15:50:45 2009 UTC (14 years, 5 months ago) by berupon
File MIME type: application/x-javascript
File size: 14344 byte(s)
表示方式の変更
1
2 // Copyright (c) 2009 Katsuhisa Yuasa <berupon [at] gmail.com>
3 // License http://www.opensource.org/licenses/mit-license.html
4
5 // このファイルにはシステム固有の処理を配置する。
6
7 if (!behaviourAssignors) {
8 var behaviourAssignors = {};
9 }
10
11 behaviourAssignors['number'] = function (elem) {
12 Event.observe(
13 elem,
14 'keypress',
15 // nonnegative_integers
16 function (event) {
17 var char = pressedChar(event);
18 var unmatched = (char && !char.match(/\d/));
19 cancelEvent(event, unmatched);
20 }
21 );
22 }
23
24 behaviourAssignors['integer'] = function (elem) {
25 Event.observe(
26 elem,
27 'keypress',
28 function (event) {
29 var char = pressedChar(event);
30 var unmatched = (char && !char.match(/[\-0-9]/));
31 cancelEvent(event, unmatched);
32 }
33 );
34 }
35
36 behaviourAssignors['word'] = function (elem) {
37 Event.observe(
38 elem,
39 'keypress',
40 function (event) {
41 var char = pressedChar(event);
42 var unmatched = (char && !char.match(/\w/));
43 cancelEvent(event, unmatched);
44 }
45 );
46 }
47
48 behaviourAssignors['date'] = function (elem) {
49 Event.observe(
50 elem,
51 'keypress',
52 function (event) {
53 var char = pressedChar(event);
54 var unmatched = (char && !char.match(/\d/) && char != '/');
55 cancelEvent(event, unmatched);
56 }
57 );
58 elem.maxLength = 10;
59 }
60
61 behaviourAssignors['slip_code'] = function (elem) {
62 Event.observe(
63 elem,
64 'keypress',
65 function(event) {
66 var char = pressedChar(event);
67 var unmatched = (char && !char.match(/[a-zA-Z0-9\-]/));
68 cancelEvent(event, unmatched);
69 }
70 );
71 }
72
73 behaviourAssignors['sales_code'] = function (elem) {
74 Event.observe(
75 elem,
76 'keypress',
77 function(event) {
78 var char = pressedChar(event);
79 var unmatched = (char && !char.match(/[a-zA-Z0-9]/));
80 cancelEvent(event, unmatched);
81 }
82 );
83 }
84
85 function setAutoFocus() {
86 var cnt = document.forms.length;
87 for (var i=0; i<cnt; ++i) {
88 var form = document.forms[i];
89 var cnt2 = form.elements.length;
90 for (var j=0; j<cnt2; ++j) {
91 var elem = form.elements[j];
92 if (elem.attributes.getNamedItem("autofocus")) {
93 if (document.activeElement == elem) {
94 return;
95 }
96 var e = elem;
97 do {
98 if (e.style) {
99 if (0
100 || (e.style.display && e.style.display == 'none')
101 || (e.style.visibility && e.style.visibility == 'hidden')
102 ) {
103 return;
104 }
105 }
106 }while (e = e.parentNode);
107 elem.focus();
108 elem.select();
109 return;
110 }
111 }
112 }
113 }
114
115 Event.observe(window, "load", setAutoFocus);
116
117 function toDateFormat(num) {
118 if (num.match(/\d{8,8}/)) {
119 var year = "00" + Math.floor(num / 10000);
120 var remain = num % 10000;
121 var month = "00" + Math.floor(remain / 100);
122 var day = "00" + remain % 100;
123 var ret = "" // damn Automatic Semicolon Insertion ;;
124 + year.substr(year.length-4, 4)
125 + "/"
126 + month.substr(month.length-2, 2)
127 + "/"
128 + day.substr(day.length-2, 2)
129 ;
130 return ret;
131 }else {
132 return num;
133 }
134 }
135
136 function clearForm(id) {
137 var form = Event.findElement(windowEvent()).form;
138 clearFormElementsValues(form, id);
139 }
140
141 Ajax.Responders.register({
142 onCreate: function() {
143 $("loading").appear({ duration: 0.1 });
144 },
145 onComplete: function() {
146 $("loading").fade({ duration: 0.5 });
147 }
148 });
149
150 // cookie中の指定した名前の値をObjectとして取り出し。JSON形式で格納されている事前提。
151 function digestCookie(key) {
152 var pieces = document.cookie.split('; ');
153 for (var i=0; i<pieces.length; ++i) {
154 var piece = pieces[i];
155 var pair = piece.split('=');
156 if (pair[0] == key) {
157 return unescape(pair[1]).evalJSON();
158 }
159 }
160 return {};
161 }
162
163 // cookieに指定した名前で値を保存。JSON形式で格納。
164 function dumpCookie(key, value) {
165 if (!Object.isString(value)) {
166 value = Object.toJSON(value);
167 }
168 var str = key + '=' + escape(value) + ";";
169 var d = new Date();
170 // future yet unknown XD
171 d.setYear(2012);
172 d.setMonth(12);
173 d.setDate(21);
174 str += "expires=" + d.toGMTString() + ";";
175 document.cookie = str;
176 }
177
178 // TODO: rename to DualSelect routines
179 function resetGridSetting(visibles, hiddens, cols) {
180 visibles.options.length = 0;
181 hiddens.options.length = 0;
182 for (var i=0; i<cols.length; ++i) {
183 var col = cols[i];
184 visibles.options[i] = new Option(col.text, col.value);
185 }
186 }
187
188 function setGridSetting(visibles, hiddens, defaultCols, cols) {
189 visibles.options.length = 0;
190 hiddens.options.length = 0;
191 for (var i=0; i<defaultCols.length; ++i) {
192 var col = defaultCols[i];
193 var found = false;
194 for (var j=0; j<cols.length; ++j) {
195 if (cols[j] == col.value) {
196 found = true;
197 break;
198 }
199 }
200 var el = found ? visibles : hiddens;
201 el.options[el.options.length] = new Option(col.text, col.value);
202 }
203 }
204
205 function getGridSetting(visibles) {
206 return collectMember(visibles.options, 'value');
207 }
208
209 renderDoubleList = (function () {
210 var tmp = new EJS({url: 'js/doublelist.ejs'});
211 return function (lhs, rhs) {
212 var str = tmp.render(
213 {
214 'lhs' : lhs,
215 'rhs' : rhs
216 }
217 );
218 return str;
219 }
220 })();
221
222 var app = (function () {
223
224 /// private ///
225 Application = function () {
226
227 this.title = "SimpleLightWeb";
228 this.pageTitle = "";
229
230 // 擬似ページの読込時に呼び出すコールバック。
231 this.pageLoadHandler = null;
232
233 // 擬似ページの破棄時に呼び出すコールバック。戻り値が有る場合はcookieに保存
234 this.pageUnloadHandler = null;
235
236 // Modal PopUp 用の処理
237 this.showPopup = function () {
238 popup.show();
239 };
240
241 this.hidePopup = function () {
242 popup.hide();
243 };
244
245 this.setPopup = function (elem) {
246 popup.elem.appendChild(elem);
247 };
248
249 // 入力補助等のevent handlers設定
250 this.setHelpers = function () {
251 setAutoFocus();
252 draggable.attach();
253 ResizableColumns();
254 assignBehaviours();
255 };
256
257 this.getPathFromLocationHash = function () {
258 var url = location.hash;
259 return url.substr(1, url.length);
260 };
261
262 // prototype.js の String.toQueryParams は = で右辺に値を設定していないとパラメータとして拾ってくれないので自作。
263 this.getHashParams = function () {
264 var hash = this.getPathFromLocationHash();
265 var paraPos = hash.indexOf('?');
266 if (paraPos < 1) {
267 return {}
268 }else {
269 var paraStr = hash.substr(paraPos+1, hash.length);
270 var params = paraStr.split('&');
271 var ret = {};
272 for (var i=0; i<params.length; ++i) {
273 var para = params[i];
274 var pair = para.split('=');
275 for (var j=0; j<pair.length; ++j) {
276 pair[j] = decodeURIComponent(pair[j]);
277 }
278 if (pair.length == 1) {
279 ret[pair[0]] = null;
280 }else if (pair.length == 2) {
281 ret[pair[0]] = pair[1];
282 }
283 }
284 return ret;
285 }
286 }
287
288 // hash指定によるページ切り替え処理
289 // this routine understands relative path
290 this.moveLocation = function (path) {
291 var curHash = location.hash;
292 if (path.substr(0,1) == '/') {
293 location.hash = path.substr(1,path.length);
294 }else {
295 var upLevel = 0;
296 while (1) {
297 if (path.substr(0, 3) == '../') {
298 ++upLevel;
299 path = path.substr(3, path.length);
300 }else {
301 break;
302 }
303 }
304 var curPath = location.hash;
305 curPath = curPath.substr(1, curPath.length);
306 var parts = curPath.split('/');
307 parts = parts.slice(0, parts.length - (upLevel+1));
308 var newPath = parts.join('/');
309 if (newPath) {
310 newPath += '/';
311 }
312 newPath += path;
313 location.hash = newPath;
314 }
315 };
316
317 this.ajaxRequest = function (url, method, parameters, onSuccess, onFailure) {
318 ajaxRequest(url, method, parameters, onSuccess, onFailure);
319 };
320
321 // postによるデータ登録処理。エラー時にメッセージ表示を行うElementを用意する事。
322 var okToPost = true;
323 this.postRequest = function (params, newLocation) {
324 if (!okToPost) {
325 alert("ただいま結果待ちな為送信出来ません。");
326 return;
327 }
328 if (params instanceof HTMLFormElement) {
329 params = Form.serialize(params, true);
330 }
331 // alert(Object.toJSON(params));
332 this.ajaxRequest(
333 null,
334 'post',
335 params,
336 function(transport) {
337 var ret = transport.responseJSON;
338 if (!ret)
339 {
340 alert(transport.responseText);
341 }
342 if (ret.errors.length) {
343 $("messages").innerHTML = ret.errors.join("<BR>");
344 $("error").appear({ duration: 0.6 });
345 }else {
346 if (Object.isFunction(newLocation)) {
347 newLocation(ret);
348 }else {
349 if (newLocation == undefined) {
350 newLocation = "list";
351 }
352 this.moveLocation(newLocation);
353 }
354 }
355 okToPost = true;
356 }.bind(this),
357 function () {
358 alert('Something went wrong...')
359 okToPost = true;
360 }
361 );
362 okToPost = false;
363 };
364
365 // template文字列は指定要素の最初の要素であるHTML comment中にあるという前提。
366 this.templateUpdate = function (templateEl, updateEl, data, setupFunc) {
367 if (!templateEl || !updateEl) {
368 return;
369 }
370 var st = new Date();
371 // to workaround IE and WebKit bug. do not directly set innerHTML.
372 while (updateEl.firstChild) {
373 updateEl.removeChild(updateEl.firstChild);
374 }
375 var comment = templateEl.firstChild;
376 var src = comment.data;
377 src = modifyTemplate(src, data);
378 var template = new EJS({text: src});
379 var newdiv = document.createElement("div");
380 newdiv.innerHTML = template.render(data);
381 // setupFunc can process newly created elements before rendering. (this speeds up page redndering)
382 if (setupFunc) {
383 setupFunc(newdiv);
384 }
385 updateEl.appendChild(newdiv);
386 // updateEl.appear({ duration: 0.1 });
387 var et = new Date();
388 //a(et-st);
389
390 };
391
392 };
393 var self = new Application();
394
395 var box = null;
396 var contents = null;
397 Event.observe(window, "load", function () {
398 contents = $('contents');
399 var timerID = setInterval(checkLocationHash, 10);
400 });
401 var draggable = new Draggable();
402 var popup = new ModalPopUp();
403
404 var pageUrl = null;
405 function ajaxRequest(url, method, parameters, onSuccess, onFailure) {
406 if (url == null) {
407 url = self.getPathFromLocationHash();
408 }
409 var launchURL = pageUrl;
410 new Ajax.Request(
411 url,
412 {
413 "method" : method,
414 "parameters" : parameters,
415 "onSuccess": function(transport) {
416 try {
417 if (pageUrl == launchURL) {
418 onSuccess(transport);
419 }
420 }catch (e) {
421 a(e);
422 }
423 },
424 "onFailure": function () {
425 if (onFailure) {
426 try {
427 onFailure();
428 }catch (e) {
429 alert(e);
430 }
431 }
432 }
433 }
434 );
435 }
436
437 // 画面設定処理
438 function setPage(html) {
439 self.pageLoadHandler = null;
440 box = document.createElement("div");
441 box.innerHTML = html;
442 contents.appendChild(box);
443 var loc = self.getPathFromLocationHash();
444 if (Prototype.Browser.IE || Prototype.Browser.WebKit || Prototype.Browser.Opera) {
445 var texts = new Array();
446 var scripts = box.getElementsByTagName("script");
447 for (var i=0; i<scripts.length; ++i) {
448 var script = scripts[i];
449 texts.push(script.text);
450 }
451 var script = document.createElement("script");
452 script.text = texts.join("\r");
453 box.appendChild(script);
454 }
455 if (self.pageLoadHandler) {
456 self.pageLoadHandler();
457 }
458 document.title = self.title + ' ' + self.pageTitle;
459 $("title").innerHTML = self.pageTitle;
460 // $("breadcrumbs").innerHTML = loc.split('/').join(' / ');
461 var navisrc = $("navisrc");
462 $("navi").innerHTML = navisrc.innerHTML;
463 navisrc.innerHTML = '';
464 self.setHelpers();
465 }
466
467 // AJAXによる画面データ読込処理
468 var pageCache = {};
469 function loadPage(dest, params) {
470 if (dest in pageCache) {
471 var html = pageCache[dest];
472 pageUrl = dest;
473 setPage(html);
474 }else {
475 self.ajaxRequest(
476 dest,
477 'get',
478 params,
479 function(transport) {
480 var html = transport.responseText;
481 pageCache[dest] = html;
482 pageUrl = dest;
483 setPage(html);
484 },
485 function () {
486 alert("ページ読み込み失敗");
487 }
488 );
489 }
490 }
491
492 // location.hash を監視して変更されていたらpathに相当する画面のデータを設定する。
493 var prevHash = "nullpo";
494 function checkLocationHash() {
495 if (location.hash != prevHash) {
496 if (box) {
497 popup.hide();
498 popup.clear();
499 if (self.pageUnloadHandler) {
500 var ret = self.pageUnloadHandler();
501 if (ret) {
502 dumpCookie(prevHash, ret);
503 }
504 }
505 contents.removeChild(box);
506 self.pageUnloadHandler = null;
507 }
508 prevHash = location.hash;
509 if (location.hash == "" || location.hash == "#") {
510 loadPage("menu" + ".html", {});
511 }else {
512 var pagePath = location.hash.substr(1, location.hash.length);
513 var paraPos = pagePath.indexOf('?');
514 if (paraPos >= 1) {
515 pagePath = pagePath.substr(0, paraPos);
516 }
517 loadPage(pagePath + ".html", {});
518 }
519 }
520 }
521
522 return self;
523
524 })();
525
526 // debug用のalert関数
527 function a(p) {
528 if (typeof p == "object") {
529 alert(Object.toJSON(p));
530 }else {
531 alert(p);
532 }
533 }
534
535 var prefectures = {
536 1 : '北海道',
537 2 : '青森県',
538 3 : '岩手県',
539 4 : '宮城県',
540 5 : '秋田県',
541 6 : '山形県',
542 7 : '福島県',
543 8 : '茨城県',
544 9 : '栃木県',
545 10 : '群馬県',
546 11 : '埼玉県',
547 12 : '千葉県',
548 13 : '東京都',
549 14 : '神奈川県',
550 15 : '新潟県',
551 16 : '富山県',
552 17 : '石川県',
553 18 : '福井県',
554 19 : '山梨県',
555 20 : '長野県',
556 21 : '岐阜県',
557 22 : '静岡県',
558 23 : '愛知県',
559 24 : '三重県',
560 25 : '滋賀県',
561 26 : '京都府',
562 27 : '大阪府',
563 28 : '兵庫県',
564 29 : '奈良県',
565 30 : '和歌山県',
566 31 : '鳥取県',
567 32 : '島根県',
568 33 : '岡山県',
569 34 : '広島県',
570 35 : '山口県',
571 36 : '徳島県',
572 37 : '香川県',
573 38 : '愛媛県',
574 39 : '高知県',
575 40 : '福岡県',
576 41 : '佐賀県',
577 42 : '長崎県',
578 43 : '熊本県',
579 44 : '大分県',
580 45 : '宮崎県',
581 46 : '鹿児島県',
582 47 : '沖縄県'
583 };

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26