OSDN Git Service

3aae300f3c7bcb93e8c010ae3d411b322f85be35
[trpgtools-onweb/AjaxChat.git] / lib / js / index.js
1 /*
2  Ajax Chat for TRPG ver.2.6
3  (c)2007-2009 Cake All Rights Reserved.
4  Mail : cake_67@users.sourceforge.jp
5  Home : http://trpgtools-onweb.sourceforge.jp/
6  */
7 /*****************
8  *  JaveScriptライブラリ:index.php用
9  *****************/
10 // 変数
11 columnId = 'pc_input_0';
12 formObjId = '0';
13 var cookieSaveScript = './cookie_save.php';
14 var cookieLoadScript = './cookie_load.php';
15
16 // 発言成形
17 function comment(Obj) {
18     // 発言取得
19     var pc_input = Obj.pc_input.value;
20     if (pc_input) {
21         //入力チェック
22         var name = convert($("#name_on").attr("value"));
23         var pc_name = Obj.pc_name.value;
24         if (!pc_name) {
25             alert("PC名を入力してください。");
26             return false;
27         }
28         var pc_color = Obj.pc_color.value;
29
30         //データ成形
31         var params =
32             '?n='+name//名前
33             +'&c='+convert(pc_name)//PC
34             +'&l='+pc_color//色
35             +'&m='+convert(pc_input);//本文
36
37         // ささやき
38         var whisper_to_name = Obj.whisper_to.value;
39         var whisper_to_hash = Obj.whisper_to_hash.value;
40         if(whisper_to_name && whisper_to_name != '全員' && whisper_to_hash) {
41             params = params
42                 +'&w='+convert(whisper_to_name)//whisperto
43                 +'&h='+whisper_to_hash;//whisperto_hash
44         }
45
46         //状態表示
47         if (Obj.show_status.checked) {
48             params = params+'&s='+'1';
49         } else {
50             params = params+'&s='+'0';
51         }
52
53         //状態
54         if (Obj.status.value) {
55             params = params+'&t='+convert(Obj.status.value);
56         }
57
58         //他
59 //        params = params+'&a='+Obj.status.value;
60
61         if ($("#pwd_on").attr("value")) {
62             var pwd = convert($("#pwd_on").attr("value"));
63         }
64         if (pwd) {
65             var params =
66             params+'&p='+pwd;//PWD
67         }
68         var params =
69             params+'&y=com'
70             +'&e='+(new Date()).getTime();//cacheの更新
71
72         writeComment(params);
73
74         //フォームチェンジ
75         Obj.pc_input.value = "";
76     }
77     Obj.pc_input.focus();
78
79     return false;
80 }
81
82 // 書き込み
83 function writeComment(params)
84 {
85     $.ajax({
86         type: "GET",
87         url: writescript+params,
88         success: function(data) {
89             // 受信完了イベント
90             window.mainframe.getLog(true);
91         }
92     });
93 }
94
95 // 在室を確認して指定の処理を実行
96 function isMember(name, action, params, pwd) {
97     var flg = false;
98     $.ajax({
99         type: "get",
100         url: now_member,
101         ifModified: false,
102         cache: false,
103         dataType: "text",
104         success: function(data) {
105             var name_array = new Array();
106             var memberData = data.split("\n");
107             $.each(memberData, function(i){
108                 var memberDataDetail = memberData[i].split("<>");
109                 if (obfuscateLog == '1') {
110                     var plName = deObfLog(memberDataDetail[0]);
111                 } else {
112                     var plName = memberDataDetail[0];
113                 }
114
115                 if (name==plName) {
116                     flg = true;
117                 }
118             });
119
120             if (flg) {
121                 if (action=="logout") {
122                 $("#logout").click();
123                 return true;
124                 }
125                 if (action=="login") {
126                     alert('そのお名前でログインはできません。');
127                     return false;
128                 }
129             }
130
131             if (action=="login") {
132                 writeComment(params);
133                 //フォームチェンジ
134                 $("#online1").show();
135                 $("#online2").show();
136                 $("#offline").hide();
137                 $("#pc_0").show();
138                 $("#name_on").val(name);
139                 if (!$("#pc_name_0").val()) {
140                     $("#pc_name_0").val(name);
141                 }
142                 $("#pwd_on").val(pwd);
143                 $("#pc_input_0").focus();
144                 if (userWhisper) {
145                     $(".whisper_to").each(function(idx, Obj){
146                         Obj.disabled = true;
147                         Obj.value ="全員";
148                     });
149                     $(".whisper_to_hash").each(function(idx, Obj){
150                         Obj.value ="";
151                     });
152                 }
153
154                 // 更新
155                 window.mainframe.login();
156
157             }
158         },
159         error: function(data) {
160             alert('参加者情報取得に失敗しました');
161             return false;
162         }
163     });
164 }
165
166 // ウインドウサイズ変更でフォームサイズ変更
167 function changeSize() {
168     var winWidth = getWinWidth();
169     var winHeight = getWinHeight() - 80;
170     $(".input_comment").css({width: winWidth-275});
171     $(".status").css({width: winWidth-313});
172     $("#mainframe").css({height: winHeight-40});
173     $("#offline_win").css({left: winWidth-280});
174 }
175
176
177 // カーソル位置に指定文字列を挿入
178 function insert(Obj, selfId, text, backlength) {
179     // 自分自身には挿入できない(ダイスショートカット対応)
180     if (Obj.id == selfId) {
181         return false;
182     }
183
184     Obj.focus();
185
186     var textLength = text.length;
187     //FireFoxなど
188     if (Obj.selectionStart) {
189         var position = Obj.selectionStart;
190         Obj.value = Obj.value.substring(0, position)+text+Obj.value.substring(position, Obj.value.length);
191         Obj.setSelectionRange(position + textLength, position + textLength - backlength);
192     } else {
193         // IE
194         if (document.selection) {
195             var sel = document.selection.createRange();
196             sel.text = text;
197             sel.move('character', -backlength);
198             sel.select();
199         // その他
200         } else {
201             Obj.value = Obj.value+text;
202         }
203     }
204 }
205
206 // 名前文字色を反映
207 function changeColor(Obj, Ev) {
208     if(Ev != "37" && Ev != "39" && Ev != "8" && Ev != "16" && Ev != "17" && Ev != "18"){
209         Obj.form.pc_color.style.color = Obj.value;
210         Obj.form.pc_color.value = Obj.value;
211         Obj.form.pc_color_select.style.color = Obj.value;
212         Obj.form.pc_name.style.color = Obj.value;
213     }
214 }
215
216 // タグ挿入
217 function insertTag(tag, columnId) {
218     insert(document.getElementById(columnId), '','<'+tag+'></'+tag+'>', tag.length+3)
219 }
220
221 // フォントタグ挿入
222 function insertFontTag(columnId) {
223     var size = prompt("フォントサイズを指定してください(1~7または-4~+4)", "");
224     var color = prompt("フォント色を指定してください", "");
225
226     if (!size && !color) {
227         return false;
228     } else {
229         var tag = "<font";
230         if (size) {
231             tag= tag+" size="+size;
232         }
233         if (color) {
234             tag= tag+" color="+color;
235         }
236         tag = tag+"></font>";
237         insert(document.getElementById(columnId), '', tag, 7)
238     }
239 }
240
241 // 入室中にリロード・ページ遷移で自動退室
242 function UnLoad() {
243     var name = $("#name_on").attr("value");
244     if (name) {
245         isMember(name, 'logout', '', '');
246     }
247 }
248
249 // 起動時表示系処理
250 function pageLoad() {
251     // フォームON/OFF
252     $("#online1").hide();
253     $("#online2").hide();
254     $("#cfchange").hide();
255     if ($("#cfinput")) { 
256         $("#cfinput").checked = false;
257     }
258     $("#name_on").disabled = true;
259     $("#pwd_on").disabled = true;
260     $("#my_hash").disabled = true;
261     $("#name_on").val("");
262     $("#pwd_on").val("");
263     $("#my_hash").val("");
264     if (userWhisper) {
265         $(".whisper_to").each(function(idx, Obj){
266             Obj.disabled = true;
267             Obj.value ="全員";
268         });
269         $(".whisper_to_hash").each(function(idx, Obj){
270             Obj.value ="";
271         });
272     }
273     $("#cfchange").hide();
274     if ($("#cfinput")) { 
275         $("#cfinput").checked = false;
276     }
277
278     // コンソール窓設定
279     $(".console").draggable({
280         cursor:'move',
281         helper:'original'
282     });
283
284     $(".console").draggable('enable');
285     // ヘルプ窓設定
286     $("#help_win").css({left: document.body.clientWidth-380});
287     $("#help_win_acc_list").accordion({
288         event: 'click',
289         alwaysOpen: false,
290         autoHeight: false,
291         header: 'dt'
292     });
293 }
294
295 // ログイン
296 function doLogin(name, pwd) {
297     if (!name || name == noname) {
298         alert("お名前を入力してください。");
299         return false;
300     }
301
302     // 一部記号はNG
303     if (name.search(/[\'\"#\$\%&<>\/\\\`\=]/i) != -1) {
304         alert("一部の半角記号をお名前に使う事はできません。削除するか全角にしてみてください。");
305         return false;
306     }
307
308     //特定の名前ではログインできない
309     var ngNames = new Array(noname, systemName, '現在のカード', 'deckCardSet', 'deck', 'my', 'plName');
310     var ngName = false;
311     for(i=0; i<ngNames.length; i++){
312         if (name==ngNames[i]){
313             var ngName = true;
314             break;
315         }
316     }
317     if (ngName) {
318         alert("そのお名前ではログインできません");
319         return false;
320     }
321
322     // パラメータセット
323     //データ成形
324     var params =
325         '?n='+convert(name)//名前
326         +'&s='+'0';//状態表示
327     var params =
328         params+'&p='+convert(pwd);//PWD
329
330     var params =
331         params+'&y=in'
332         +'&e='+(new Date()).getTime();//cacheの更新
333
334     // 現在の入室者と同じ名前かどうかチェックしてログイン
335     isMember(name, 'login', params, pwd);
336 }
337
338 // ログアウト
339 function doLogout(name) {
340     //データ成形
341     var params =
342         '?n='+name//名前
343         +'&s='+'0'//状態表示
344     if($("#pwd_on").attr("value")) {
345         var params =
346         params
347         +'&p='+convert($("#pwd_on").attr("value"));//PWD
348     }
349     var params =
350         params+'&y=out'
351         +'&e='+(new Date()).getTime();//cacheの更新
352
353     writeComment(params);
354 }
355
356 // 直前に選択していた欄を記録
357 function selectedColumnId(Obj, isPc) {
358     if (Obj) {
359         if (columnId!=Obj.id) {
360             columnId = Obj.id;
361         }
362         if (isPc) {
363             formObjId = Obj.form.id;
364         }
365     }
366 }
367
368 // ささやき設定変更(mainから)
369 function changeWhisperConfig(name, whisperTo) {
370     var whisper_to_id = '#whisper_to_'+formObjId;
371     var whisper_to_hash_id = '#whisper_to_hash_'+formObjId;
372     var whisper_mark_id = '#whisper_mark_'+formObjId;
373     var formId = "#"+columnId;
374
375     if (whisper_to_hash_id) {
376         var whisperIcon = whisperOnIcon;
377     } else {
378         if ($(whisper_mark_id).attr("src")==whisperOnIcon) {
379         var whisperIcon = whisperOnIcon;
380         }
381     }
382     if (whisper_to_hash_id) {
383         $(whisper_to_id).val(name);
384         $(whisper_to_hash_id).val(whisperTo);
385         $(whisper_mark_id).attr("src", whisperIcon);
386         $(whisper_mark_id).attr("title", "クリックでささやき解除");
387         $(formId).focus();
388     }
389 }
390
391 // ささやき設定変更クリア(indexから)
392 function changeWhisperConfigClear(Obj) {
393     var whisper_to_id = '#whisper_to_'+Obj.form.id;
394     var whisper_to_hash_id = '#whisper_to_hash_'+Obj.form.id;
395     var whisper_mark_id = '#whisper_mark_'+Obj.form.id;
396
397     $(whisper_to_id).val("全員");
398     $(whisper_to_hash_id).val("");
399     $(whisper_mark_id).attr("src", whisperOffIcon);
400     $(whisper_mark_id).attr("title", "「ささやき」状態を表示");
401 }
402
403 // ダイスショートカット欄にカーソルを当てるとその欄拡張
404 function expandDiceShortcut(Obj) {
405     var winWidth = document.body.clientWidth;
406
407     var diceShortCut = $(".diceShortCut");
408     diceShortCut.css('width', '40px');
409
410     Obj.style.width = winWidth-380;
411 ;
412 }
413 // ダイスショートカット欄からカーソルを離したら元通り
414 function shrinkDiceShortcut(Obj) {
415     var diceShortCut = $(".diceShortCut");
416     diceShortCut.css('width', '');
417 }
418
419 // クッキー関連
420 // クッキーの書き込み
421 function cookieWrite() {
422     if (!checkCookie()) {
423         return false;
424     }
425
426     var Expires = (60 * 60 * 24 * 1000 * CookieExpire);
427
428     // 現在のCOOKIE取得
429     var cookieValue = get_cookie_content();
430
431     var str = setCookie(cookieValue, Expires);
432
433     document.cookie = str;
434
435     alert("クッキーを記録しました");
436     return true;
437 }
438
439 // クッキーの削除
440 function cookieDelete() {
441     if (!checkCookie()) {
442         return false;
443     }
444
445     var str = setCookie("", "");
446
447     document.cookie = str;
448
449     alert("クッキーを削除しました");
450     return true;
451 }
452
453 // クッキー有効か否か
454 function checkCookie() {
455     if (!navigator.cookieEnabled) {
456         alert("クッキーが無効になっています。ブラウザの設定を変更してください。");
457         return false;
458     }
459     // クッキー名あるいは有効期限:どちらかがない場合クッキー使用しない
460     if (!CookieName || CookieExpire=="0") {
461         alert('チャット側の設定により、クッキー機能は無効です。');
462         return false;
463     }
464
465     return true;
466 }
467
468 // クッキーValueセット
469 function setCookie(cookieValue, Expires) {
470     var str = CookieName+"="+encodeURIComponent(cookieValue);
471
472     // ドメイン
473     domain = location.hostname;
474     str += "; domain="+domain;
475     // パス
476     path = location.pathname.replace("index.php", "");
477     str += "; path="+path;
478     // 有効期限
479     if (CookieExpire!='') {
480         var nowtime = new Date().getTime();
481         expires = new Date(nowtime + Expires);
482         expires = expires.toGMTString();
483         str += "; expires="+expires;
484     } else {
485         str += "; expires="+0;
486     }
487
488     return str;
489 }
490
491 // クッキーをファイルで保管
492 function cookiesaveFile() {
493     if (!checkCookie()) {
494         return false;
495     }
496
497     // ファイル名の指定
498     var filename = prompt('現在のクッキーをファイルで保管します。\nファイル名を指定して下さい。','');
499     if (filename == null) {
500         return false;
501     }
502
503     // 現在のCOOKIE取得
504     var cookieValue = get_cookie_content();
505
506     var url = cookieSaveScript
507                 +'?'
508                 +'f='+encodeURIComponent(filename)
509                 +'&c='+encodeURIComponent(cookieValue);
510
511     location.href = url;
512
513 }
514
515 // ファイルから読み込む
516 function isFile(id) {
517     var fileName = $(id).val();
518     if (!fileName) {
519         alert('ファイルを指定して下さい');
520         return false;
521     }
522     return true;
523 }
524
525 // 指定URLを別窓で開く
526 function openUrl(url, target) {
527     if (!target) {
528         target = '_blank';
529     }
530     window.open(url, target);
531 }
532
533 // 別窓で開く
534 function openNewWindow(location, target, noBar, width, height, scroll) {
535     if (!location) {
536         return false; 
537     }
538     if (!target) {
539         target = _blank;
540     }
541     if (!width || width <= 0) {
542         width = 600;
543     }
544     if (!height || height <= 0) {
545         height = document.body.clientHeight - 80;
546     }
547     if (noBar) {
548         showBar = 'toolbar=no,location=no,directorybottuns=no,menubar=no';
549     } else {
550         showBar = 'toolbar=yes,location=yes,directorybottuns=yes,menubar=yes';
551     }
552     if (scroll) {
553         setScroll = 'scrollbars=yes';
554     } else {
555         setScroll = 'scrollbars=no';
556     }
557     var options = 'width='+width
558                  +',height='+height
559                  +','+showBar
560                  +','+setScroll
561                  +',status=yes,resizable=yes';
562     return window.open(location, target, options);
563 }