Develop and Download Open Source Software

Browse Subversion Repository

Contents of /js/domain.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (show annotations) (download) (as text)
Tue Dec 8 10:46:33 2009 UTC (14 years, 5 months ago) by berupon
File MIME type: application/x-javascript
File size: 12944 byte(s)
Application用の処理によるglobalな名前空間の使用を削減。
User登録処理が正常に動作するように修正。
1
2 // Copyright (c) 2009 Katsuhisa Yuasa <berupon [at] gmail.com>
3 // License http://www.opensource.org/licenses/mit-license.html
4
5 if (!behaviourAssignors) {
6 var behaviourAssignors = {};
7 }
8
9 behaviourAssignors['number'] = function (elem) {
10 addListener(
11 elem,
12 'keypress',
13 // nonnegative_integers
14 function (event) {
15 var char = pressedChar(event);
16 var unmatched = (char && !char.match(/\d/));
17 cancelEvent(event, unmatched);
18 }
19 );
20 }
21
22 behaviourAssignors['integer'] = function (elem) {
23 addListener(
24 elem,
25 'keypress',
26 function (event) {
27 var char = pressedChar(event);
28 var unmatched = (char && !char.match(/[\-0-9]/));
29 cancelEvent(event, unmatched);
30 }
31 );
32 }
33
34 behaviourAssignors['word'] = function (elem) {
35 addListener(
36 elem,
37 'keypress',
38 function (event) {
39 var char = pressedChar(event);
40 var unmatched = (char && !char.match(/\w/));
41 cancelEvent(event, unmatched);
42 }
43 );
44 }
45
46 behaviourAssignors['date'] = function (elem) {
47 addListener(
48 elem,
49 'keypress',
50 function (event) {
51 var char = pressedChar(event);
52 var unmatched = (char && !char.match(/\d/) && char != '/');
53 cancelEvent(event, unmatched);
54 }
55 );
56 elem.maxLength = 10;
57 }
58
59 behaviourAssignors['slip_code'] = function (elem) {
60 addListener(
61 elem,
62 'keypress',
63 function(event) {
64 var char = pressedChar(event);
65 var unmatched = (char && !char.match(/[a-zA-Z0-9\-]/));
66 cancelEvent(event, unmatched);
67 }
68 );
69 }
70
71 behaviourAssignors['sales_code'] = function (elem) {
72 addListener(
73 elem,
74 'keypress',
75 function(event) {
76 var char = pressedChar(event);
77 var unmatched = (char && !char.match(/[a-zA-Z0-9]/));
78 cancelEvent(event, unmatched);
79 }
80 );
81 }
82
83
84 function setAutoFocus() {
85 var cnt = document.forms.length;
86 for (var i=0; i<cnt; ++i) {
87 var form = document.forms[i];
88 var cnt2 = form.elements.length;
89 for (var j=0; j<cnt2; ++j) {
90 var elem = form.elements[j];
91 if (elem.attributes.getNamedItem("autofocus")) {
92 if (document.activeElement == elem) {
93 return;
94 }
95 var e = elem;
96 do {
97 if (e.style) {
98 if (0
99 || (e.style.display && e.style.display == 'none')
100 || (e.style.visibility && e.style.visibility == 'hidden')
101 ) {
102 return;
103 }
104 }
105 }while (e = e.parentNode);
106 elem.focus();
107 elem.select();
108 return;
109 }
110 }
111 }
112 }
113
114 Event.observe(window, "load", setAutoFocus);
115
116 function toDateFormat(num) {
117 if (num.match(/\d{8,8}/)) {
118 var year = Math.floor(num / 10000);
119 var remain = num % 10000;
120 var month = Math.floor(remain / 100);
121 var day = remain % 100;
122 return "" // damn Automatic Semicolon Insertion ;;
123 + ("00" + year).substr(-4)
124 + "/"
125 + ("00" + month).substr(-2)
126 + "/"
127 + ("00" + day).substr(-2)
128 ;
129 }else {
130 return num;
131 }
132 }
133
134 function windowEvent() {
135 if (window.event) return window.event;
136 var caller = arguments.callee.caller;
137 while (caller) {
138 var ob = caller.arguments[0];
139 if (ob && ob.constructor == MouseEvent) return ob;
140 caller = caller.caller;
141 }
142 return null;
143 }
144
145 function clearForm(id) {
146 var form = Event.findElement(windowEvent()).form;
147 clearFormElementsValues(form, id);
148 }
149
150 Ajax.Responders.register({
151 onCreate: function() {
152 $("loading").appear({ duration: 0.1 });
153 },
154 onComplete: function() {
155 $("loading").fade({ duration: 0.5 });
156 }
157 });
158
159 function ajaxRequest(url, method, parameters, onSuccess, onFailure) {
160 new Ajax.Request(
161 url,
162 {
163 "method" : method,
164 "parameters" : parameters,
165 "onSuccess": function(transport) {
166 try {
167 onSuccess(transport);
168 }catch (e) {
169 alert(e);
170 }
171 },
172 "onFailure": function () {
173 if (onFailure) {
174 try {
175 onFailure();
176 }catch (e) {
177 alert(e);
178 }
179 }
180 }
181 }
182 );
183 }
184
185 // ���������������������������������������������DOM��������������� template������������������������������������������
186 //
187 // ������������
188 // # ���������������������������������������������������
189 // #hoge{ ���hoge������������������
190 // ������������������#key1 ������������������������������#key2 ��������������������������� key1 ������������������
191 // #} ������������������������
192 // ��������� data ������ hoge ���������������������������key���������������������������������
193 //
194 function modifyTemplate(src, data) {
195 var r = /^#/gm;
196 var r2 = /^[^\s]+/g;
197 var indexes = [];
198
199 while (r.test(src)) {
200 indexes.push(r.lastIndex);
201 }
202
203 if (indexes.length == 0) {
204 return src;
205 }
206
207 function attachParts(pieces, parts, setting) {
208 if (!setting) {
209 for (var j=0; j<parts.length; ++j) {
210 var part = parts[j];
211 pieces.push(part.text);
212 }
213 }else {
214 for (var i=0; i<setting.length; ++i) {
215 var name = setting[i];
216 for (var j=0; j<parts.length; ++j) {
217 var part = parts[j];
218 if (part.name == name) {
219 pieces.push(part.text);
220 break;
221 }
222 }
223 }
224 }
225 }
226
227 var defName = null;
228 var pos = 0;
229 var pieces = [];
230 var piece = src.substr(0, indexes[0] - 1);
231 pieces.push(piece);
232 var parts = [];
233 for (var i=0; i<indexes.length-1; ++i) {
234 var start = indexes[i];
235 var end = indexes[i+1] - 1;
236 piece = src.substr(start, end-start);
237 r2.lastIndex = 0;
238 var ret = r2.exec(piece);
239 if (!ret || !ret.length) {
240 return false;
241 }
242 var key = ret[0];
243 if (!defName) {
244 if (key.charAt(key.length-1) != '{') {
245 return false;
246 }
247 defName = key.substr(0, key.length-1);
248 }else {
249 if (key != '}') {
250 var part = piece.substr(r2.lastIndex, piece.length-1);
251 parts.push({name:key, text:part});
252 }else {
253 attachParts(pieces, parts, data[defName]);
254 parts.clear();
255 defName = null;
256 pieces.push(piece.substr(1,piece.length));
257 }
258 }
259 }
260 if (parts.length) {
261 attachParts(pieces, parts, data[defName]);
262 }
263 var start = indexes[indexes.length-1] + 1;
264 var piece = src.substr(start, src.length-start);
265 pieces.push(piece);
266 return pieces.join("");
267 }
268
269 // cookie���������������������������������Object������������������������JSON������������������������������������������
270 function digestCookie(key) {
271 var pieces = document.cookie.split('; ');
272 for (var i=0; i<pieces.length; ++i) {
273 var piece = pieces[i];
274 var pair = piece.split('=');
275 if (pair[0] == key) {
276 return unescape(pair[1]).evalJSON();
277 }
278 }
279 return {};
280 }
281
282 // cookie���������������������������������������JSON������������������
283 function dumpCookie(key, value) {
284 if (!Object.isString(value)) {
285 value = Object.toJSON(value);
286 }
287 var str = key + '=' + escape(value) + ";";
288 var d = new Date();
289 // future yet unknown XD
290 d.setYear(2012);
291 d.setMonth(12);
292 d.setDate(21);
293 str += "expires=" + d.toGMTString() + ";";
294 document.cookie = str;
295 }
296
297 // TODO: rename to DualSelect routines
298 function resetGridSetting(visibles, hiddens, cols) {
299 visibles.options.length = 0;
300 hiddens.options.length = 0;
301 for (var i=0; i<cols.length; ++i) {
302 var col = cols[i];
303 visibles.options[i] = new Option(col.text, col.value);
304 }
305 }
306
307 function setGridSetting(visibles, hiddens, defaultCols, cols) {
308 visibles.options.length = 0;
309 hiddens.options.length = 0;
310 for (var i=0; i<defaultCols.length; ++i) {
311 var col = defaultCols[i];
312 var found = false;
313 for (var j=0; j<cols.length; ++j) {
314 if (cols[j] == col.value) {
315 found = true;
316 break;
317 }
318 }
319 var el = found ? visibles : hiddens;
320 el.options[el.options.length] = new Option(col.text, col.value);
321 }
322 }
323
324 function getGridSetting(visibles) {
325 return collectMember(visibles.options, 'value');
326 }
327
328 // Application ���������
329 // ��������������������� app ������������
330 if (app != undefined) {
331 alert("error: global variable 'app' is already defined.");
332 }
333 var app = null;
334 Application = function () {
335 if (app) {
336 return;
337 }
338
339 /// private ///
340
341 var box = null;
342 var contents = null;
343 Event.observe(window, "load", function () {
344 contents = $('contents');
345 });
346 var draggable = new Draggable();
347 var popup = new ModalPopUp();
348
349 // AJAX������������������������������������
350 function loadPage(dest, params) {
351 ajaxRequest(
352 dest,
353 'get',
354 params,
355 function(transport) {
356 app.pageLoadHandler = null;
357 box = document.createElement("div");
358 box.innerHTML = transport.responseText;
359 contents.appendChild(box);
360 var title = $("pageTitle").innerHTML;
361 document.title = "#{APP_TITLE}" + ' ' + title;
362 var loc = app.getPathFromLocationHash();
363 $("title").innerHTML = title;
364 // $("breadcrumbs").innerHTML = loc.split('/').join(' / ');
365 if (Prototype.Browser.IE || Prototype.Browser.WebKit || Prototype.Browser.Opera) {
366 var texts = new Array();
367 var scripts = box.getElementsByTagName("script");
368 for (var i=0; i<scripts.length; ++i) {
369 var script = scripts[i];
370 texts.push(script.text);
371 }
372 var script = document.createElement("script");
373 script.text = texts.join("\r");
374 box.appendChild(script);
375 }
376 if (app.pageLoadHandler) {
377 app.pageLoadHandler();
378 }
379 app.setHelpers();
380 },
381 function () {
382 alert("���������������������������");
383 }
384 );
385 }
386
387 // location.hash ���������������������������������������path���������������������������������AJAX������������������
388 var prevHash = "nullpo";
389 var timerID = setInterval(checkLocationHash, 10);
390 function checkLocationHash() {
391 if (location.hash != prevHash) {
392 if (box) {
393 popup.hide();
394 popup.clear();
395 if (app.pageUnloadHandler) {
396 var ret = app.pageUnloadHandler();
397 if (ret) {
398 dumpCookie(prevHash, ret);
399 }
400 }
401 contents.removeChild(box);
402 app.pageUnloadHandler = null;
403 }
404 prevHash = location.hash;
405 if (location.hash == "" || location.hash == "#") {
406 loadPage("menu", {});
407 }else {
408 var pagePath = location.hash.substr(1, location.hash.length);
409 loadPage(pagePath, {});
410 }
411 }
412 }
413
414 /// public ///
415
416 // ���������������������������������������������������������������
417 this.pageLoadHandler = null;
418
419 // ������������������������������������������������������������������������������������������cookie���������
420 this.pageUnloadHandler = null;
421
422 // Modal PopUp ������������
423 this.showPopup = function () {
424 popup.show();
425 };
426
427 this.hidePopup = function () {
428 popup.hide();
429 };
430
431 this.setPopup = function (elem) {
432 popup.elem.appendChild(elem);
433 };
434
435 // ������������������event handlers������
436 this.setHelpers = function () {
437 setAutoFocus();
438 draggable.attach();
439 ResizableColumns();
440 assignBehaviours();
441 };
442
443 this.getPathFromLocationHash = function () {
444 var url = location.hash;
445 return url.substr(1, url.length);
446 };
447
448 // hash������������������������������������������
449 // this routine understands relative path
450 this.moveLocation = function (path) {
451 var curHash = location.hash;
452 if (path.substr(0,1) == '/') {
453 location.hash = path.substr(1,path.length);
454 }else {
455 var upLevel = 0;
456 while (1) {
457 if (path.substr(0, 3) == '../') {
458 ++upLevel;
459 path = path.substr(3, path.length);
460 }else {
461 break;
462 }
463 }
464 var curPath = location.hash;
465 curPath = curPath.substr(1, curPath.length);
466 var parts = curPath.split('/');
467 parts = parts.slice(0, parts.length - (upLevel+1));
468 var newPath = parts.join('/');
469 if (newPath) {
470 newPath += '/';
471 }
472 newPath += path;
473 location.hash = newPath;
474 }
475 };
476
477 // post������������������������������������������������������������������������������Element���������������������
478 var okToPost = true;
479 this.postRequest = function (params) {
480 if (!okToPost) {
481 alert("������������������������������������������������������");
482 return;
483 }
484 if (params instanceof HTMLFormElement) {
485 params = Form.serialize(params, true);
486 }
487 // alert(Object.toJSON(params));
488 ajaxRequest(
489 this.getPathFromLocationHash(),
490 'post',
491 params,
492 function(transport) {
493 var ret = transport.responseJSON;
494 if (!ret) {
495 alert(transport.responseText);
496 }
497 if (ret.errors.length) {
498 $("messages").innerHTML = ret.errors.join("<BR>");
499 $("error").appear({ duration: 1.0 });
500 }else {
501 this.moveLocation("list");
502 }
503 okToPost = true;
504 },
505 function () {
506 alert('Something went wrong...')
507 okToPost = true;
508 }
509 );
510 okToPost = false;
511 };
512
513 // template���������������������������������������������������HTML comment������������������������������
514 this.templateUpdate = function (templateEl, updateEl, data) {
515 var comment = templateEl.firstChild;
516 var src = comment.data;
517 src = modifyTemplate(src, data);
518 var template = new EJS({text: src});
519 template.update(updateEl, data);
520 };
521
522 };
523
524 app = new Application();
525

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