Develop and Download Open Source Software

Browse Subversion Repository

Diff of /js/domain.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 9 by berupon, Tue Dec 8 10:46:33 2009 UTC revision 10 by berupon, Tue Dec 8 11:41:01 2009 UTC
# Line 2  Line 2 
2  // Copyright (c) 2009 Katsuhisa Yuasa <berupon [at] gmail.com>  // Copyright (c) 2009 Katsuhisa Yuasa <berupon [at] gmail.com>
3  // License http://www.opensource.org/licenses/mit-license.html  // License http://www.opensource.org/licenses/mit-license.html
4    
5    // このファイルにはシステム固有の処理を配置する。
6    
7  if (!behaviourAssignors) {  if (!behaviourAssignors) {
8          var behaviourAssignors = {};          var behaviourAssignors = {};
9  }  }
10    
11  behaviourAssignors['number'] = function (elem) {  behaviourAssignors['number'] = function (elem) {
12          addListener(          Event.observe(
13                  elem,                  elem,
14                  'keypress',                  'keypress',
15                  // nonnegative_integers                  // nonnegative_integers
# Line 20  behaviourAssignors['number'] = function Line 22  behaviourAssignors['number'] = function
22  }  }
23    
24  behaviourAssignors['integer'] = function (elem) {  behaviourAssignors['integer'] = function (elem) {
25          addListener(          Event.observe(
26                  elem,                  elem,
27                  'keypress',                  'keypress',
28                  function (event) {                  function (event) {
# Line 32  behaviourAssignors['integer'] = function Line 34  behaviourAssignors['integer'] = function
34  }  }
35    
36  behaviourAssignors['word'] = function (elem) {  behaviourAssignors['word'] = function (elem) {
37          addListener(          Event.observe(
38                  elem,                  elem,
39                  'keypress',                  'keypress',
40                  function (event) {                  function (event) {
# Line 44  behaviourAssignors['word'] = function (e Line 46  behaviourAssignors['word'] = function (e
46  }  }
47    
48  behaviourAssignors['date'] = function (elem) {  behaviourAssignors['date'] = function (elem) {
49          addListener(          Event.observe(
50                  elem,                  elem,
51                  'keypress',                  'keypress',
52                  function (event) {                  function (event) {
# Line 57  behaviourAssignors['date'] = function (e Line 59  behaviourAssignors['date'] = function (e
59  }  }
60    
61  behaviourAssignors['slip_code'] = function (elem) {  behaviourAssignors['slip_code'] = function (elem) {
62          addListener(          Event.observe(
63                  elem,                  elem,
64                  'keypress',                  'keypress',
65                  function(event) {                  function(event) {
# Line 69  behaviourAssignors['slip_code'] = functi Line 71  behaviourAssignors['slip_code'] = functi
71  }  }
72    
73  behaviourAssignors['sales_code'] = function (elem) {  behaviourAssignors['sales_code'] = function (elem) {
74          addListener(          Event.observe(
75                  elem,                  elem,
76                  'keypress',                  'keypress',
77                  function(event) {                  function(event) {
# Line 131  function toDateFormat(num) { Line 133  function toDateFormat(num) {
133          }          }
134  }  }
135    
 function windowEvent() {  
         if (window.event) return window.event;  
         var caller = arguments.callee.caller;  
         while (caller) {  
                 var ob = caller.arguments[0];  
                 if (ob && ob.constructor == MouseEvent) return ob;  
                 caller = caller.caller;  
         }  
         return null;  
 }  
   
136  function clearForm(id) {  function clearForm(id) {
137          var form = Event.findElement(windowEvent()).form;          var form = Event.findElement(windowEvent()).form;
138          clearFormElementsValues(form, id);          clearFormElementsValues(form, id);
# Line 182  function ajaxRequest(url, method, parame Line 173  function ajaxRequest(url, method, parame
173          );          );
174  }  }
175    
 // 列の表示切り替え&並び替え用、DOMにする前に templateの文字列時点で入れ替えを行う  
 //  
 // 以下概要  
 //   # で始まる行を対象として処理を行う。  
 //   #hoge{ でhogeグループ開始  
 //   グループ中の#key1 で始まる行頭から次の#key2 で始まる行頭までを key1 要素とする。  
 //   #} でグループ終了。  
 //   引数の data 中の hoge 配列の値どうりに、key要素を出現させていく。  
 //  
 function modifyTemplate(src, data) {  
         var r = /^#/gm;  
         var r2 = /^[^\s]+/g;  
         var indexes = [];  
           
         while (r.test(src)) {  
                 indexes.push(r.lastIndex);  
         }  
           
         if (indexes.length == 0) {  
                 return src;  
         }  
           
         function attachParts(pieces, parts, setting) {  
                 if (!setting) {  
                         for (var j=0; j<parts.length; ++j) {  
                                 var part = parts[j];  
                                 pieces.push(part.text);  
                         }  
                 }else {  
                         for (var i=0; i<setting.length; ++i) {  
                                 var name = setting[i];  
                                 for (var j=0; j<parts.length; ++j) {  
                                         var part = parts[j];  
                                         if (part.name == name) {  
                                                 pieces.push(part.text);  
                                                 break;  
                                         }  
                                 }  
                         }  
                 }  
         }  
           
         var defName = null;  
         var pos = 0;  
         var pieces = [];  
         var piece = src.substr(0, indexes[0] - 1);  
         pieces.push(piece);  
         var parts = [];  
         for (var i=0; i<indexes.length-1; ++i) {  
                 var start = indexes[i];  
                 var end = indexes[i+1] - 1;  
                 piece = src.substr(start, end-start);  
                 r2.lastIndex = 0;  
                 var ret = r2.exec(piece);  
                 if (!ret || !ret.length) {  
                         return false;  
                 }  
                 var key = ret[0];  
                 if (!defName) {  
                         if (key.charAt(key.length-1) != '{') {  
                                 return false;  
                         }  
                         defName = key.substr(0, key.length-1);  
                 }else {  
                         if (key != '}') {  
                                 var part = piece.substr(r2.lastIndex, piece.length-1);  
                                 parts.push({name:key, text:part});  
                         }else {  
                                 attachParts(pieces, parts, data[defName]);  
                                 parts.clear();  
                                 defName = null;  
                                 pieces.push(piece.substr(1,piece.length));  
                         }  
                 }  
         }  
         if (parts.length) {  
                 attachParts(pieces, parts, data[defName]);  
         }  
         var start = indexes[indexes.length-1] + 1;  
         var piece = src.substr(start, src.length-start);  
         pieces.push(piece);  
         return pieces.join("");  
 }  
   
176  // cookie中の指定した名前の値をObjectとして取り出し。JSON形式で格納されている事前提。  // cookie中の指定した名前の値をObjectとして取り出し。JSON形式で格納されている事前提。
177  function digestCookie(key) {  function digestCookie(key) {
178          var pieces = document.cookie.split('; ');          var pieces = document.cookie.split('; ');
# Line 325  function getGridSetting(visibles) { Line 232  function getGridSetting(visibles) {
232          return collectMember(visibles.options, 'value');          return collectMember(visibles.options, 'value');
233  }  }
234    
235  // Application クラス  var app = (function () {
 // グローバル変数 app の予約。  
 if (app != undefined) {  
         alert("error: global variable 'app' is already defined.");  
 }  
 var app = null;  
 Application = function () {  
         if (app) {  
                 return;  
         }  
236                    
237          /// private ///          /// private ///
238            Application = function () {
239                    
240                    this.title = "SimpleLightWeb";
241                    
242                    // 擬似ページの読込時に呼び出すコールバック。
243                    this.pageLoadHandler = null;
244                    
245                    // 擬似ページの破棄時に呼び出すコールバック。戻り値が有る場合はcookieに保存
246                    this.pageUnloadHandler = null;
247                    
248                    // Modal PopUp 用の処理
249                    this.showPopup = function () {
250                            popup.show();
251                    };
252                    
253                    this.hidePopup = function () {
254                            popup.hide();
255                    };
256                    
257                    this.setPopup = function (elem) {
258                            popup.elem.appendChild(elem);
259                    };
260                    
261                    // 入力補助等のevent handlers設定
262                    this.setHelpers = function () {
263                            setAutoFocus();
264                            draggable.attach();
265                            ResizableColumns();
266                            assignBehaviours();
267                    };
268                    
269                    this.getPathFromLocationHash = function () {
270                            var url = location.hash;
271                            return url.substr(1, url.length);
272                    };
273                    
274                    // hash指定によるページ切り替え処理
275                    // this routine understands relative path
276                    this.moveLocation = function (path) {
277                            var curHash = location.hash;
278                            if (path.substr(0,1) == '/') {
279                                    location.hash = path.substr(1,path.length);
280                            }else {
281                                    var upLevel = 0;
282                                    while (1) {
283                                            if (path.substr(0, 3) == '../') {
284                                                    ++upLevel;
285                                                    path = path.substr(3, path.length);
286                                            }else {
287                                                    break;
288                                            }
289                                    }
290                                    var curPath = location.hash;
291                                    curPath = curPath.substr(1, curPath.length);
292                                    var parts = curPath.split('/');
293                                    parts = parts.slice(0, parts.length - (upLevel+1));
294                                    var newPath = parts.join('/');
295                                    if (newPath) {
296                                            newPath += '/';
297                                    }
298                                    newPath += path;
299                                    location.hash = newPath;
300                            }
301                    };
302                    
303                    // postによるデータ登録処理。エラー時にメッセージ表示を行うElementを用意する事。
304                    var okToPost = true;
305                    this.postRequest = function (params) {
306                            if (!okToPost) {
307                                    alert("ただいま結果待ちな為送信出来ません。");
308                                    return;
309                            }
310                            if (params instanceof HTMLFormElement) {
311                                    params = Form.serialize(params, true);
312                            }
313                    //      alert(Object.toJSON(params));
314                            ajaxRequest(
315                                    this.getPathFromLocationHash(),
316                                    'post',
317                                    params,
318                                    function(transport) {
319                                            var ret = transport.responseJSON;
320                                            if (!ret) {
321                                                    alert(transport.responseText);
322                                            }
323                                            if (ret.errors.length) {
324                                                    $("messages").innerHTML = ret.errors.join("<BR>");
325                                                    $("error").appear({ duration: 1.0 });
326                                            }else {
327                                                    this.moveLocation("list");
328                                            }
329                                            okToPost = true;
330                                    },
331                                    function () {
332                                            alert('Something went wrong...')
333                                            okToPost = true;
334                                    }
335                            );
336                            okToPost = false;
337                    };
338                    
339                    // template文字列は指定要素の最初の要素であるHTML comment中にあるという前提。
340                    this.templateUpdate = function (templateEl, updateEl, data) {
341                            var comment = templateEl.firstChild;
342                            var src = comment.data;
343                            src = modifyTemplate(src, data);
344                            var template = new EJS({text: src});
345                            template.update(updateEl, data);
346                    };
347    
348            };
349            var self = new Application();
350                    
351          var box = null;          var box = null;
352          var contents = null;          var contents = null;
# Line 353  Application = function () { Line 363  Application = function () {
363                          'get',                          'get',
364                          params,                          params,
365                          function(transport) {                          function(transport) {
366                                  app.pageLoadHandler = null;                                  self.pageLoadHandler = null;
367                                  box = document.createElement("div");                                  box = document.createElement("div");
368                                  box.innerHTML = transport.responseText;                                  box.innerHTML = transport.responseText;
369                                  contents.appendChild(box);                                  contents.appendChild(box);
370                                  var title = $("pageTitle").innerHTML;                                  var title = $("pageTitle").innerHTML;
371                                  document.title = "#{APP_TITLE}" + ' ' + title;                                  document.title = self.title + ' ' + title;
372                                  var loc = app.getPathFromLocationHash();                                  var loc = self.getPathFromLocationHash();
373                                  $("title").innerHTML = title;                                  $("title").innerHTML = title;
374                          //      $("breadcrumbs").innerHTML = loc.split('/').join(' / ');                          //      $("breadcrumbs").innerHTML = loc.split('/').join(' / ');
375                                  if (Prototype.Browser.IE || Prototype.Browser.WebKit || Prototype.Browser.Opera) {                                  if (Prototype.Browser.IE || Prototype.Browser.WebKit || Prototype.Browser.Opera) {
# Line 373  Application = function () { Line 383  Application = function () {
383                                          script.text = texts.join("\r");                                          script.text = texts.join("\r");
384                                          box.appendChild(script);                                          box.appendChild(script);
385                                  }                                  }
386                                  if (app.pageLoadHandler) {                                  if (self.pageLoadHandler) {
387                                          app.pageLoadHandler();                                          self.pageLoadHandler();
388                                  }                                  }
389                                  app.setHelpers();                                  self.setHelpers();
390                          },                          },
391                          function () {                          function () {
392                                  alert("ページ読み込み失敗");                                  alert("ページ読み込み失敗");
# Line 392  Application = function () { Line 402  Application = function () {
402                          if (box) {                          if (box) {
403                                  popup.hide();                                  popup.hide();
404                                  popup.clear();                                  popup.clear();
405                                  if (app.pageUnloadHandler) {                                  if (self.pageUnloadHandler) {
406                                          var ret = app.pageUnloadHandler();                                          var ret = self.pageUnloadHandler();
407                                          if (ret) {                                          if (ret) {
408                                                  dumpCookie(prevHash, ret);                                                  dumpCookie(prevHash, ret);
409                                          }                                          }
410                                  }                                  }
411                                  contents.removeChild(box);                                  contents.removeChild(box);
412                                  app.pageUnloadHandler = null;                                  self.pageUnloadHandler = null;
413                          }                          }
414                          prevHash = location.hash;                          prevHash = location.hash;
415                          if (location.hash == "" || location.hash == "#") {                          if (location.hash == "" || location.hash == "#") {
# Line 411  Application = function () { Line 421  Application = function () {
421                  }                  }
422          }          }
423                    
424          /// public ///          return self;
           
         // 擬似ページの読込時に呼び出すコールバック。  
         this.pageLoadHandler = null;  
           
         // 擬似ページの破棄時に呼び出すコールバック。戻り値が有る場合はcookieに保存  
         this.pageUnloadHandler = null;  
           
         // Modal PopUp 用の処理  
         this.showPopup = function () {  
                 popup.show();  
         };  
           
         this.hidePopup = function () {  
                 popup.hide();  
         };  
           
         this.setPopup = function (elem) {  
                 popup.elem.appendChild(elem);  
         };  
           
         // 入力補助等のevent handlers設定  
         this.setHelpers = function () {  
                 setAutoFocus();  
                 draggable.attach();  
                 ResizableColumns();  
                 assignBehaviours();  
         };  
           
         this.getPathFromLocationHash = function () {  
                 var url = location.hash;  
                 return url.substr(1, url.length);  
         };  
           
         // hash指定によるページ切り替え処理  
         // this routine understands relative path  
         this.moveLocation = function (path) {  
                 var curHash = location.hash;  
                 if (path.substr(0,1) == '/') {  
                         location.hash = path.substr(1,path.length);  
                 }else {  
                         var upLevel = 0;  
                         while (1) {  
                                 if (path.substr(0, 3) == '../') {  
                                         ++upLevel;  
                                         path = path.substr(3, path.length);  
                                 }else {  
                                         break;  
                                 }  
                         }  
                         var curPath = location.hash;  
                         curPath = curPath.substr(1, curPath.length);  
                         var parts = curPath.split('/');  
                         parts = parts.slice(0, parts.length - (upLevel+1));  
                         var newPath = parts.join('/');  
                         if (newPath) {  
                                 newPath += '/';  
                         }  
                         newPath += path;  
                         location.hash = newPath;  
                 }  
         };  
           
         // postによるデータ登録処理。エラー時にメッセージ表示を行うElementを用意する事。  
         var okToPost = true;  
         this.postRequest = function (params) {  
                 if (!okToPost) {  
                         alert("ただいま結果待ちな為送信出来ません。");  
                         return;  
                 }  
                 if (params instanceof HTMLFormElement) {  
                         params = Form.serialize(params, true);  
                 }  
         //      alert(Object.toJSON(params));  
                 ajaxRequest(  
                         this.getPathFromLocationHash(),  
                         'post',  
                         params,  
                         function(transport) {  
                                 var ret = transport.responseJSON;  
                                 if (!ret) {  
                                         alert(transport.responseText);  
                                 }  
                                 if (ret.errors.length) {  
                                         $("messages").innerHTML = ret.errors.join("<BR>");  
                                         $("error").appear({ duration: 1.0 });  
                                 }else {  
                                         this.moveLocation("list");  
                                 }  
                                 okToPost = true;  
                         },  
                         function () {  
                                 alert('Something went wrong...')  
                                 okToPost = true;  
                         }  
                 );  
                 okToPost = false;  
         };  
           
         // template文字列は指定要素の最初の要素であるHTML comment中にあるという前提。  
         this.templateUpdate = function (templateEl, updateEl, data) {  
                 var comment = templateEl.firstChild;  
                 var src = comment.data;  
                 src = modifyTemplate(src, data);  
                 var template = new EJS({text: src});  
                 template.update(updateEl, data);  
         };  
425    
426  };  })();
427    
428  app = new Application();  // debug用のalert関数
429    function a(p) {
430            if (typeof p == "object") {
431                    alert(Object.toJSON(p));
432            }else {
433                    alert(p);
434            }
435    }
436    

Legend:
Removed from v.9  
changed lines
  Added in v.10

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