OSDN Git Service

next.
[hmh/wiki.git] / i / wiki.js
1 // JavaScript Document
2 var LoginOp;
3 //
4 function insertTab(ev,o){
5         var kc;
6         if (ev.which)
7                 kc = ev.which;
8         else
9                 kc = ev.keyCode;
10         if(kc==9){
11                 if(ev.type=="keydown"){
12                         if(o.setSelectionRange){
13                                 var s=o.selectionStart;
14                                 var e=o.selectionEnd;
15                                 o.value=o.value.substring(0,s)+"        "+o.value.substr(e);
16                                 o.setSelectionRange(s+1,s+1);
17                                 o.focus();
18                         }else if(o.createTextRange){
19                                 document.selection.createRange().text=" ";
20                                 o.onblur=function(){this.focus();this.onblur=null;};
21                         }
22                 }
23                 if(ev.returnValue)
24                         ev.returnValue=false;
25                 if(ev.preventDefault)
26                         ev.preventDefault();
27                 return false;
28         }
29         return true;
30 }
31 //
32 function gotoPage(page,data){
33         if(page==undefined) page=Page;
34         if(page==undefined) page='Main';
35         if(data){
36                 window.location.href='../../wiki.hml/'+encodeURIComponent(Proj)+'/'+encodeURIComponent(page)+'?'+$H(data).toQueryString();
37         }else{
38                 window.location.href='../../wiki.hml/'+encodeURIComponent(Proj)+'/'+encodeURIComponent(page);
39         }
40 }
41 //
42 function callURL(url,data,func){
43         var relogin=function(r){
44                 showLogin();
45                 LoginOp=Array(url,data,func);
46         };
47         new Ajax.Request(url,
48                          {
49                                 parameters:data,
50                                 onSuccess:func,
51                                 on403:relogin,
52                          }
53                         );
54 }
55 function callWiki(page,data,func){
56         callURL('../../op-wiki.hml/'+encodeURIComponent(Proj)+'/'+encodeURIComponent(page),data,func);
57 }
58 //
59 function readWiki(page,data,target){
60         var f=function(r){
61 //              $(target).update(r.responseText);
62                 showWikiPane(r.responseXML.documentElement,target);
63         };
64         callWiki(page,data,f);
65 }
66 function showWiki(xml){
67         var ctx=new ExprContext(xml);
68         var title=xpathParse('/WikiText/Title/text()').evaluate(ctx);
69         var html=xpathParse('/WikiText/Html/text()').evaluate(ctx);
70         var lu=xpathParse('/WikiText/LastUpdate/text()').evaluate(ctx);
71         if(title) document.title=title.stringValue();
72         if(html) $('WikiPane').innerHTML=html.stringValue();
73         if(lu&&lu.value.size()>0) $('LastUpdate').innerHTML=lu.stringValue();
74 }
75 function showWikiPane(xml,target){
76         var ctx=new ExprContext(xml);
77         var html=xpathParse('/WikiText/Html/text()').evaluate(ctx);
78         if(target) $(target).innerHTML=html.stringValue();
79 }
80 function showWikiHandler(r){
81         showWiki(r.responseXML.documentElement);
82 }
83 function reloadHander(r){
84         gotoPage();
85 }
86 //
87 function SubmitForm(form,page,data,func){
88         if(func){
89                 callWiki(page,$H($(form).serialize(true)).update(data),func);
90         }else{
91                 callWiki(page,$H($(form).serialize(true)).update(data),showWikiHandler);
92         }
93 }
94 //
95 Element.prototype.showDialog=function(){
96         var d=document.viewport.getDimensions();
97         var pos=this.getDimensions();
98         var h=d.height/2-pos.height/2-100;
99         if(h<0)h=0;
100         this.style.top=String(h)+"px";
101         this.style.left=String(d.width/2-pos.width/2)+"px";
102         this.style.display='block';
103 }
104 Element.prototype.hideDialog=function(){
105         this.style.display='none';
106 }
107 Element.prototype.removeChildren=function(){
108         while(this.hasChildNodes()) this.removeChild(this.lastChild);
109 }
110 Element.prototype.appendText=function(txt){
111         if(txt==undefined) txt='';
112         this.appendChild(document.createTextNode(txt));
113 }
114 Element.prototype.replaceText=function(txt){
115         if(txt==undefined) txt='';
116         if(this){
117                 this.removeChildren();
118                 this.appendText(txt);
119         }
120 }
121 //
122 function showLogin(){
123         var f=$('LoginForm');
124         var m=$('LoginMsgPane');
125         $(f.Login).clear();
126         $(f.Password).clear();
127         while(m.hasChildNodes()) m.removeChild(m.lastChild);
128         $('LoginPane').showDialog();
129         $('Login').activate();
130 }
131 function hideLogin(){
132         $('LoginPane').hideDialog();
133         LoginOp=undefined;
134 }
135 //
136 function loadURL(url,data,target,f2){
137         var f=function(r){
138                 $(target).update(r.responseText);
139                 if(f2)f2();
140         };
141         callURL(url,data,f);
142 }
143 function opLogin(form){
144         var f=function(r){
145                 if(r.responseText=='OK'){
146                         var f2=function(r){
147                                 $('LoginPane').hideDialog();
148                                 if(LoginOp){
149                                         callURL(LoginOp[0],LoginOp[1],LoginOp[2]);
150                                         LoginOp=undefined;
151                                 }
152                         };
153                         loadURL('../../op-top.hml',undefined,'TopMenuPane',f2);
154                 }else{
155                         $('LoginMsgPane').replaceText('IDまたはパスワードが間違っています。');
156                 }
157         };
158         callURL('../../op-login.hml',$(form).serialize(true),f);
159 }
160 //
161 function opLogout(){
162         callURL('../../op-logout.hml',null,reloadHander);
163 }
164 //
165 function opEditSel(sel){
166         switch($F(sel)){
167         case '1':
168                 $('EditWikiPane').style.display='block';
169                 $('EditOprPane').style.display='none';
170                 break;
171         case '2':
172                 $('EditWikiPane').style.display='none';
173                 $('EditOprPane').style.display='block';
174                 break;
175         case '3':
176                 $('EditWikiPane').style.display='none';
177                 $('EditOprPane').style.display='block';
178                 readWiki('special-Diff',{'Target':Page},'EditOprPane');
179                 break;
180         }
181 }
182 //
183 function postWiki(form,data,func){
184         callURL('../../op-post.hml',$H($(form).serialize(true)).update(data),func);
185 }
186 function opPreview(form){
187         postWiki(form,null,showWikiHandler);
188 }
189 function opSave(form){
190         postWiki(form,{'Save':'1'},showWikiHandler);
191 }
192 function opSaveClose(form){
193         postWiki(form,{'Save':'1'},reloadHander);
194 }