PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/root/static.js

https://github.com/paul999/ajax-shoutbox
JavaScript | 735 lines | 611 code | 99 blank | 25 comment | 71 complexity | bd89b1272c201a1028bfda6963da8acb MD5 | raw file
  1. /**
  2. *
  3. * @package Ajax Shoutbox
  4. * @version $Id: static.js 278 2008-04-13 08:42:03Z paul $
  5. * @copyright (c) 2007 Paul Sohier
  6. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  7. *
  8. */
  9. var div, hin, huit, hin2, hsmilies, hinfo, hdelete, hnr;
  10. var config = new Array();
  11. var post_info = timer_in = last = null;
  12. var display_shoutbox = false;
  13. var start = first = true;
  14. var smilies = false;
  15. var count = 0;
  16. var form_name = 'chat_form';
  17. var text_name = 'chat_message';
  18. var bbcode = new Array();
  19. var bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]', '[img]','[/img]', '[url]', '[/url]');
  20. var one_open = false;
  21. var lang = new Array();
  22. var edit_button, edit_form = null;
  23. var error_number = 0;
  24. function err_msg(title, not_reload_complete)
  25. {
  26. var err = new Error(title);
  27. if (!err.message)
  28. {
  29. err.message = title;
  30. }
  31. if (!not_reload_complete)
  32. {
  33. load_shout()
  34. }
  35. err.name = "E_USER_ERROR";// Php error?!? :D
  36. return err;
  37. }
  38. function handle(e)
  39. {
  40. switch (e.name)
  41. {
  42. //Is it our error? :)
  43. case "E_USER_ERROR":
  44. case "E_CORE_ERROR":
  45. message(e.message, true);
  46. return;
  47. break;
  48. default:
  49. {
  50. tmp = lang['JS_ERR'];
  51. tmp += e.message;
  52. if (e.lineNumber)
  53. {
  54. tmp += '\n' + lang['LINE'] + ': ';
  55. tmp += e.lineNumber;
  56. }
  57. if (e.fileName)
  58. {
  59. tmp += '\n' + lang['FILE'] + ' : ';
  60. tmp += e.fileName;
  61. }
  62. message(tmp, true);
  63. return;
  64. }
  65. }
  66. }
  67. function parse_xml_to_html(xml)
  68. {
  69. try
  70. {
  71. if (xml.childNodes.length == 0)
  72. {
  73. return tn('');
  74. }
  75. else if (xml.childNodes.length == 1 && xml.childNodes[0].nodeValue != null)
  76. {
  77. // With a tag in it, its bigger as 1?
  78. return tn(xml.childNodes[0].nodeValue);
  79. }
  80. else
  81. {
  82. var div = ce('span');
  83. loop:
  84. for (var i = 0; i < xml.childNodes.length; i++)
  85. {
  86. switch (xml.childNodes[i].nodeType)
  87. {
  88. case 3:
  89. div.appendChild(document.createTextNode(xml.childNodes[i].nodeValue));
  90. break;
  91. case 9:
  92. case 8:
  93. case 10:
  94. case 11:
  95. // continue;
  96. break;
  97. case 1:
  98. if (xml.childNodes[i].childNodes.length == 0 && xml.childNodes[i].nodeName != 'br' && xml.childNodes[i].nodeName != 'img' && xml.childNodes[i].nodeName != 'hr')
  99. {
  100. break;
  101. }
  102. // This is a difficult one :)
  103. switch (xml.childNodes[i].nodeName)
  104. {
  105. case 'blockquote':
  106. var q = ce('blockquote');
  107. q.className = 'quote';
  108. q.appendChild(parse_xml_to_html(xml.childNodes[i]));
  109. add_style(xml.childNodes[i], q);
  110. div.appendChild(q);
  111. break;
  112. case 'a':
  113. var a = ce('a');
  114. a.href = xml.childNodes[i].getAttribute('href');
  115. a.appendChild(parse_xml_to_html(xml.childNodes[i]));
  116. add_style(xml.childNodes[i], a);
  117. div.appendChild(a);
  118. break;
  119. case 'img':
  120. var img = ce('img');
  121. img.alt = xml.childNodes[i].getAttribute('alt');
  122. img.src = xml.childNodes[i].getAttribute('src');
  123. img.border = 0;
  124. add_style(xml.childNodes[i], img);
  125. div.appendChild(img);
  126. break;
  127. case 'script':
  128. // Bad boy, die.
  129. return;
  130. break;
  131. default:
  132. {
  133. try
  134. {
  135. var e = ce(xml.childNodes[i].nodeName);
  136. }
  137. catch (e)
  138. {
  139. break;
  140. }
  141. e.appendChild(parse_xml_to_html(xml.childNodes[i]));
  142. add_style(xml.childNodes[i], e);
  143. div.appendChild(e)
  144. }
  145. }
  146. break;
  147. }
  148. }
  149. }
  150. return div;
  151. }
  152. catch (e)
  153. {
  154. handle(e);
  155. return div;
  156. }
  157. }
  158. function add_style(element, html)
  159. {
  160. var Class = element.getAttribute('class');
  161. if (Class != null)
  162. {
  163. html.className = Class;
  164. }
  165. var styles = element.getAttribute('style');
  166. if (styles == null)
  167. {
  168. return;
  169. }
  170. if (styles.indexOf(';') == -1)
  171. {
  172. styles += ';';
  173. }
  174. styles = styles.split(';');
  175. for (var j = 0; j < styles.length; j++)
  176. {
  177. var style = styles[j].split(':');
  178. if (style[0])
  179. {
  180. style[0] = trim(style[0]);
  181. }
  182. if (style[1])
  183. {
  184. style[1] = trim(style[1]);
  185. }
  186. switch (style[0])
  187. {
  188. case 'font-style':
  189. html.style.fontStyle = style[1];
  190. break;
  191. case 'font-weight':
  192. html.style.fontWeight = style[1];
  193. break;
  194. case 'font-size':
  195. try
  196. {
  197. html.style.fontSize = style[1];
  198. }
  199. catch (e){}
  200. break;
  201. case 'line-height':
  202. html.style.lineHeigt = style[1];
  203. break;
  204. case 'color':
  205. html.style.color = style[1];
  206. break;
  207. case 'text-decoration':
  208. html.style.textDecoration = style[1];
  209. break;
  210. }
  211. }
  212. }
  213. function trim(value)
  214. {
  215. value = value.replace(/^\s+/,'');
  216. value = value.replace(/\s+$/,'');
  217. return value;
  218. }
  219. function http()
  220. {
  221. try
  222. {
  223. var http_request = false;
  224. if (window.XMLHttpRequest)
  225. {
  226. // Mozilla, Safari,...
  227. http_request = new XMLHttpRequest();
  228. if (http_request.overrideMimeType)
  229. {
  230. http_request.overrideMimeType('text/xml');
  231. }
  232. }
  233. else if (window.ActiveXObject)
  234. { // IE
  235. try
  236. {
  237. http_request = new ActiveXObject('Msxml2.XMLHTTP');
  238. }
  239. catch (e)
  240. {
  241. try
  242. {
  243. http_request = new ActiveXObject('Microsoft.XMLHTTP');
  244. }
  245. catch (e)
  246. {
  247. }
  248. }
  249. }
  250. if (!http_request)
  251. {
  252. throw err_msg(lang['no_ajax']);
  253. }
  254. return http_request;
  255. }
  256. catch (e)
  257. {
  258. handle(e);
  259. return false;
  260. }
  261. }
  262. function message(msg, color, no_reload)
  263. {
  264. try
  265. {
  266. if (document.getElementById('msg_txt') != null)
  267. {
  268. document.getElementById('msg_txt').innerHTML = '';
  269. var tmp = ce('p');
  270. tmp.appendChild(tn(msg));
  271. if (color)
  272. {
  273. tmp.style.color = 'red';
  274. }
  275. document.getElementById('msg_txt').appendChild(tmp);
  276. }
  277. else
  278. {
  279. div.innerHTML = '';
  280. var tmp = ce('p');
  281. tmp.appendChild(tn(msg));
  282. if (color)
  283. {
  284. tmp.style.color = 'red';
  285. }
  286. div.appendChild(tmp);
  287. }
  288. // We reload everything after 5 seconds when an error happens, to prevent errors that are happening, and
  289. // the shoutbox dont work anymore without a reload.
  290. if (!no_reload)
  291. {
  292. last = 0;
  293. one_open = false;
  294. hin = http(); // Reset HTTP data.
  295. if (error_number <= 10)
  296. {
  297. timer_in = setTimeout('reload_post();',5000);
  298. }
  299. else
  300. {
  301. clearTimeout(timer_in);
  302. return;
  303. }
  304. error_number++;
  305. }
  306. }
  307. catch (e)
  308. {
  309. handle(e);
  310. return false;
  311. }
  312. }
  313. function cp()
  314. {
  315. var sep = ce('span');
  316. sep.className = 'page-sep';
  317. sep.appendChild(tn(lang['COMMA_SEPARATOR']));
  318. return sep;
  319. }
  320. function bbcode_buttons(posting_box)
  321. {
  322. // BBcode buttons ;)
  323. var bbcode = ce('input');
  324. bbcode.type = 'button';
  325. bbcode.className = 'button2 btnmain';
  326. bbcode.accesskey = 'b';
  327. bbcode.name = bbcode.id = 'addbbcode0';
  328. bbcode.value = bbcode.defaultValue = ' B ';
  329. bbcode.style.fontWeight = 'bold';
  330. bbcode.style.width = '33px';
  331. bbcode.onclick = function()
  332. {
  333. var tfn = form_name;
  334. form_name = 'chat_form';
  335. var ttn = text_name;
  336. text_name = 'chat_message';
  337. bbstyle(0);
  338. form_name = tfn;
  339. text_name = ttn;
  340. }
  341. posting_box.appendChild(bbcode);
  342. posting_box.appendChild(tn(' '));
  343. var bbcode = ce('input');
  344. bbcode.type = 'button';
  345. bbcode.className = 'button2 btnmain';
  346. bbcode.accesskey = 'i';
  347. bbcode.name = bbcode.id = 'addbbcode2';
  348. bbcode.value = bbcode.defaultValue = ' I ';
  349. bbcode.style.fontStyle = 'italic';
  350. bbcode.style.width = '33px';
  351. bbcode.onclick = function()
  352. {
  353. var tfn = form_name;
  354. form_name = 'chat_form';
  355. var ttn = text_name;
  356. text_name = 'chat_message';
  357. bbstyle(2);
  358. form_name = tfn;
  359. text_name = ttn;
  360. }
  361. posting_box.appendChild(bbcode);
  362. posting_box.appendChild(tn(' '));
  363. var bbcode = ce('input');
  364. bbcode.type = 'button';
  365. bbcode.className = 'button2 btnmain';
  366. bbcode.accesskey = 'u';
  367. bbcode.name = bbcode.id = 'addbbcode4';
  368. bbcode.value = bbcode.defaultValue = 'U';
  369. bbcode.style.textDecoration = 'underline';
  370. bbcode.style.width = '33px';
  371. bbcode.onclick = function()
  372. {
  373. var tfn = form_name;
  374. form_name = 'chat_form';
  375. var ttn = text_name;
  376. text_name = 'chat_message';
  377. bbstyle(4);
  378. form_name = tfn;
  379. text_name = ttn;
  380. }
  381. posting_box.appendChild(bbcode);
  382. posting_box.appendChild(tn(' '));
  383. var bbcode = ce('input');
  384. bbcode.type = 'button';
  385. bbcode.className = 'button2 btnmain';
  386. bbcode.accesskey = 'p';
  387. bbcode.name = bbcode.id = 'addbbcode6';
  388. bbcode.value = bbcode.defaultValue = ' IMG ';
  389. bbcode.style.width = '43px';
  390. bbcode.onclick = function()
  391. {
  392. var tfn = form_name;
  393. form_name = 'chat_form';
  394. var ttn = text_name;
  395. text_name = 'chat_message';
  396. bbstyle(6);
  397. form_name = tfn;
  398. text_name = ttn;
  399. }
  400. posting_box.appendChild(bbcode);
  401. posting_box.appendChild(tn(' '));
  402. var bbcode = ce('input');
  403. bbcode.type = 'button';
  404. bbcode.className = 'button2 btnmain';
  405. bbcode.accesskey = 'w';
  406. bbcode.name = bbcode.id = 'addbbcode8';
  407. bbcode.value = bbcode.defaultValue = ' URL ';
  408. bbcode.style.width = '43px';
  409. bbcode.onclick = function()
  410. {
  411. var tfn = form_name;
  412. form_name = 'chat_form';
  413. var ttn = text_name;
  414. text_name = 'chat_message';
  415. bbstyle(8);
  416. form_name = tfn;
  417. text_name = ttn;
  418. }
  419. posting_box.appendChild(bbcode);
  420. }
  421. function delete_message(post_id)
  422. {
  423. if (hdelete.readyState == 4 || hdelete.readyState == 0)
  424. {
  425. // Lets got some nice things :D
  426. hdelete.open('GET',delete_url + '&id=' + post_id + '&rand='+Math.floor(Math.random() * 1000000),true);
  427. hdelete.onreadystatechange = function()
  428. {
  429. try
  430. {
  431. if (hdelete.readyState != 4)
  432. {
  433. return;
  434. }
  435. if (hdelete.readyState == 4)
  436. {
  437. xml = hdelete.responseXML;
  438. if (typeof xml != 'object')
  439. {
  440. throw err_msg(lang['SERVER_ERR']);
  441. return;
  442. }
  443. if (xml.getElementsByTagName('error') && xml.getElementsByTagName('error').length != 0)
  444. {
  445. err = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;
  446. message(err, true);
  447. return;
  448. }
  449. else
  450. {
  451. message(lang['MSG_DEL_DONE']);
  452. }
  453. setTimeout("document.getElementById('msg_txt').innerHTML = ''",3000);
  454. last = 0;// Reset last, because if we delete the last message, the next messages cannot load correctly.
  455. clearTimeout(timer_in);
  456. reload_post();
  457. reload_page();
  458. }
  459. }
  460. catch (e)
  461. {
  462. handle(e);
  463. return;
  464. }
  465. }
  466. hdelete.send(null);
  467. }
  468. }
  469. function handle_edit(dd, inh, i)
  470. {
  471. msg3 = ce('span');
  472. msg3.id = 'text' + i;
  473. msg3.style.display = 'none';
  474. dd.appendChild(msg3);
  475. // User can edit this shout.
  476. edit_form = ce('form');
  477. edit_form.id = 'form' + i;
  478. edit_form.i = i;
  479. edit_form.style.display = 'none';
  480. edit_form.onsubmit = function()
  481. {
  482. return false
  483. }
  484. var input = ce('input');
  485. input.id = 'input' + i;
  486. input.i = i;
  487. input.value = input.defaultValue = inh.getElementsByTagName('msg_plain')[0].childNodes[0].nodeValue;
  488. input.style.width = '125px';
  489. input.onkeypress = function(evt)
  490. {
  491. try
  492. {
  493. evt = (evt) ? evt : event;
  494. var c = (evt.wich) ? evt.wich : evt.keyCode;
  495. if (c == 13)
  496. {
  497. document.getElementById('submit' + this.i).click();
  498. evt.returnValue = false;
  499. this.returnValue = false;
  500. return false;
  501. }
  502. return true;
  503. }
  504. catch (e)
  505. {
  506. handle(e);
  507. return;
  508. }
  509. }
  510. edit_form.appendChild(input);
  511. var input = ce('input');
  512. input.type = 'button';
  513. input.id = 'submit' + i;
  514. input.value = lang['EDIT'];
  515. input.i = i;
  516. input.shout_id = inh.getElementsByTagName('shout_id')[0].childNodes[0].nodeValue;
  517. input.onclick = function()
  518. {
  519. //one_open = false;
  520. i = this.i;
  521. document.getElementById('form' + i).style.display = 'none';
  522. //
  523. document.getElementById('text' + i).style.display = 'block';
  524. document.getElementById('text' + i).innerHTML = '';
  525. document.getElementById('text' + i).appendChild(tn(lang['SENDING_EDIT']));
  526. var hedit = http();
  527. if (hedit.readyState == 4 || hedit.readyState == 0)
  528. {
  529. hedit.open('POST', edit_url + '&last=' + last + '&rand='+Math.floor(Math.random() * 1000000),true);
  530. hedit.i = i;
  531. hedit.onreadystatechange = function()
  532. {
  533. try
  534. {
  535. if (hedit.readyState != 4)
  536. {
  537. return;
  538. }
  539. i = hedit.i;
  540. one_open = false;
  541. document.getElementById('text' + i).style.display = 'none';
  542. document.getElementById('shout' + i).style.display = 'block';
  543. document.getElementById('edit_button' + i).style.display = 'inline';
  544. setTimeout("document.getElementById('msg_txt').innerHTML = ''", 3000);
  545. last = 0;
  546. reload_post();
  547. try
  548. {
  549. document.getElementById("post_message").style.display = "block";
  550. }
  551. catch(e){}
  552. var xml = hedit.responseXML;
  553. if (typeof xml != 'object')
  554. {
  555. throw err_msg(lang['SERVER_ERR']);
  556. return;
  557. }
  558. if (xml.getElementsByTagName('error') && xml.getElementsByTagName('error').length != 0)
  559. {
  560. var msg = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;
  561. throw err_msg(msg, true);
  562. return;
  563. }
  564. else
  565. {
  566. message(lang['EDIT_DONE']);
  567. }
  568. }
  569. catch (e)
  570. {
  571. handle(e);
  572. return;
  573. }
  574. }
  575. post = 'chat_message=';
  576. post += encodeURIComponent(document.getElementById('input' + i).value);
  577. post += '&shout_id=' + this.shout_id;
  578. document.getElementById('input' + i).value = '';
  579. hedit.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  580. hedit.send(post);
  581. }
  582. }
  583. edit_form.appendChild(input);
  584. var input = ce('input');
  585. input.type = 'button';
  586. input.value = lang['CANCEL'];
  587. input.i = i;
  588. input.onclick = function()
  589. {
  590. one_open = false;
  591. try
  592. {
  593. document.getElementById("post_message").style.display = "block";
  594. }
  595. catch(e){}
  596. i = this.i;
  597. document.getElementById('form' + i).style.display = 'none';
  598. document.getElementById('shout' + i).style.display = 'block';
  599. document.getElementById('edit_button' + i).style.display = 'inline';
  600. }
  601. edit_form.appendChild(input);
  602. edit_button.style.display = 'inline';
  603. edit_button.i = i;
  604. edit_button.id = 'edit_button' + i;
  605. edit_button.onclick = function()
  606. {
  607. if (one_open)
  608. {
  609. alert(lang['ONLY_ONE_OPEN']);
  610. return;
  611. }
  612. one_open = true;
  613. try
  614. {
  615. document.getElementById("post_message").style.display = "none";
  616. }
  617. catch(e){}
  618. i = this.i;
  619. document.getElementById('form' + i).style.display = 'block';
  620. document.getElementById('shout' + i).style.display = 'none';
  621. document.getElementById('edit_button' + i).style.display = 'none';
  622. }
  623. return dd;
  624. }
  625. /**
  626. * Lazyness ftw
  627. *
  628. */
  629. function ce(e)
  630. {
  631. return document.createElement(e);
  632. }
  633. function tn(e)
  634. {
  635. return document.createTextNode(e);
  636. }