Advertisement
Guest User

Untitled

a guest
May 10th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.23 KB | None | 0 0
  1. /* DVZ Shoutbox by Tomasz 'Devilshakerz' Mlynski [devilshakerz.com]; Copyright (C) 2014-2016 */
  2.  
  3. var dvz_shoutbox = {
  4.  
  5. // defaults
  6. interval: 5,
  7. antiflood: 0,
  8. maxShouts: 20,
  9. awayTime: 600000,
  10. lazyMode: false,
  11. lazyMargin: 0,
  12. callSign: '@',
  13. lang: [],
  14. status: true,
  15. reversed: false,
  16. markUnread: false,
  17. callbacks: {
  18. 'toggle': [],
  19. 'update': [],
  20. 'recall': [],
  21. 'entries': [],
  22. 'shout': [],
  23. 'edit': [],
  24. 'delete': [],
  25. 'call': [],
  26. },
  27.  
  28. // runtime
  29. recalling: false,
  30. timeout: false,
  31. holdTimeout: false,
  32. frozen: false,
  33. updating: false,
  34. started: false,
  35. lastSent: 0,
  36. firstId: 0,
  37. lastId: 0,
  38. activity: 0,
  39.  
  40. loop: function(forced) {
  41.  
  42. if (forced == true) {
  43. clearTimeout(dvz_shoutbox.timeout);
  44. } else {
  45.  
  46. if (dvz_shoutbox.isAway()) {
  47. dvz_shoutbox.toggle(false, false);
  48. dvz_shoutbox.frozen = true;
  49. return false;
  50. }
  51.  
  52. if (!dvz_shoutbox.lazyLoad()) {
  53. dvz_shoutbox.frozen = true;
  54. return false;
  55. }
  56.  
  57. if (dvz_shoutbox.status == false) {
  58. dvz_shoutbox.frozen = true;
  59. return false;
  60. }
  61.  
  62. }
  63.  
  64. dvz_shoutbox.update(function() {
  65.  
  66. dvz_shoutbox.started = true;
  67.  
  68. // next request
  69. if (dvz_shoutbox.interval) {
  70. dvz_shoutbox.timeout = setTimeout(dvz_shoutbox.loop, dvz_shoutbox.interval * 1000);
  71. }
  72.  
  73. });
  74.  
  75. },
  76.  
  77. // actions
  78. update: function(callback) {
  79.  
  80. if (dvz_shoutbox.updating) {
  81. return false;
  82. } else {
  83. dvz_shoutbox.updating = true;
  84. }
  85.  
  86. $.get(
  87. 'xmlhttp.php',
  88. { action: 'dvz_sb_get_updates', first: dvz_shoutbox.firstId, last: dvz_shoutbox.lastId },
  89. function(data) {
  90.  
  91. if (dvz_shoutbox.handleErrors(data)) {
  92. return false;
  93. }
  94.  
  95. if (data) {
  96.  
  97. var data = $.parseJSON(data);
  98.  
  99. // new shouts
  100. if (data.html) {
  101.  
  102. // insert new shouts
  103. if (dvz_shoutbox.reversed) {
  104.  
  105. var scrollMax = $('#shoutbox .data').innerHeight() - $('#shoutbox .window').innerHeight(),
  106. scroll = $('#shoutbox .window').scrollTop();
  107.  
  108. $('#shoutbox .data').append( $(data.html).fadeIn(function() {
  109.  
  110. // scroll to bottom again
  111. if (!dvz_shoutbox.started || scroll >= scrollMax) {
  112. $('#shoutbox .window').scrollTop( $('#shoutbox .window')[0].scrollHeight );
  113. }
  114.  
  115. }) );
  116.  
  117. } else {
  118. $('#shoutbox .data').prepend( $(data.html).hide().fadeIn() );
  119. }
  120.  
  121. // remove old shouts to fit the limit
  122. var old = $('#shoutbox .entry').length - dvz_shoutbox.maxShouts;
  123.  
  124. if (old > 0) {
  125. $('#shoutbox .entry:nth'+(dvz_shoutbox.reversed ? '' : '-last')+'-child(-n+'+old+')').remove();
  126. dvz_shoutbox.firstId = $('#shoutbox .entry:'+(dvz_shoutbox.reversed ? 'first' : 'last')+'-child').attr('data-id');
  127. }
  128.  
  129. // mark new shouts
  130. if (dvz_shoutbox.started) {
  131.  
  132. $('#shoutbox .entry').filter(function() {
  133. return parseInt($(this).attr('data-id')) > dvz_shoutbox.lastId && $(this).not('[data-own]').length;
  134. }).addClass('new');
  135.  
  136. setTimeout("$('#shoutbox .entry.new').removeClass('new')", 1000);
  137. }
  138.  
  139. dvz_shoutbox.lastId = data.last;
  140.  
  141. if (dvz_shoutbox.firstId == 0 && data.first !== undefined) {
  142. dvz_shoutbox.firstId = data.first;
  143. }
  144.  
  145. dvz_shoutbox.parseEntries(true);
  146. dvz_shoutbox.updateLastRead();
  147.  
  148. }
  149.  
  150. // sync updates
  151. if (data.sync) {
  152.  
  153. for (var i in data.sync) {
  154.  
  155. var entry = $('#shoutbox .entry[data-id='+i+']');
  156.  
  157. if (data.sync[i] === null) {
  158. entry.fadeOut(function() {
  159. $(this).remove();
  160. });
  161. } else {
  162. entry.children('.text').html(data.sync[i]);
  163. }
  164.  
  165. }
  166.  
  167. }
  168.  
  169. }
  170.  
  171. dvz_shoutbox.updating = false;
  172.  
  173. dvz_shoutbox.runCallbacks('update');
  174.  
  175. if (typeof(callback) == 'function') {
  176. callback();
  177. }
  178.  
  179. }
  180. );
  181.  
  182. },
  183.  
  184. recall: function() {
  185.  
  186. $.get(
  187. 'xmlhttp.php',
  188. { action: 'dvz_sb_recall', first: dvz_shoutbox.firstId },
  189. function(data) {
  190.  
  191. if (dvz_shoutbox.handleErrors(data)) {
  192. return false;
  193. }
  194.  
  195. if (data) {
  196.  
  197. var data = $.parseJSON(data);
  198.  
  199. // insert new shouts
  200. if (dvz_shoutbox.reversed) {
  201.  
  202. var heightBefore = $('#shoutbox .data').height();
  203.  
  204. $('#shoutbox .data').prepend( $(data.html) );
  205.  
  206. var heightAfter = $('#shoutbox .data').height();
  207.  
  208. if ($('#shoutbox .window').scrollTop() == 0) {
  209. $('#shoutbox .window').scrollTop(heightAfter - heightBefore);
  210. }
  211.  
  212. } else {
  213. $('#shoutbox .data').append( $(data.html) );
  214. }
  215.  
  216. // extend the limit
  217. dvz_shoutbox.maxShouts = $('#shoutbox .entry').length;
  218.  
  219. dvz_shoutbox.firstId = data.first;
  220.  
  221. dvz_shoutbox.parseEntries();
  222. dvz_shoutbox.updateLastRead();
  223.  
  224. if (data.end) {
  225. dvz_shoutbox.recalling = false;
  226. }
  227.  
  228. }
  229.  
  230. dvz_shoutbox.runCallbacks('recall');
  231.  
  232. }
  233. );
  234.  
  235. },
  236.  
  237. shout: function() {
  238.  
  239. var message = $('#shoutbox input.text').val();
  240.  
  241. if ($.trim(message) == '') {
  242. return false;
  243. }
  244.  
  245. if (!dvz_shoutbox.antifloodPass()) {
  246. dvz_shoutbox.handleErrors('A');
  247. return false;
  248. }
  249.  
  250. dvz_shoutbox.toggleForm(false);
  251.  
  252. $.post(
  253. 'xmlhttp.php',
  254. { action: 'dvz_sb_shout', text: message, key: my_post_key },
  255. function(data) {
  256.  
  257. if (!dvz_shoutbox.handleErrors(data)) {
  258.  
  259. dvz_shoutbox.lastSent = Math.floor((new Date).getTime() / 1000);
  260. dvz_shoutbox.clearForm();
  261. dvz_shoutbox.loop(true);
  262.  
  263. dvz_shoutbox.runCallbacks('shout', { message: message });
  264.  
  265. }
  266.  
  267. dvz_shoutbox.toggleForm(true);
  268.  
  269. }
  270. );
  271.  
  272. },
  273.  
  274. edit: function(id) {
  275.  
  276. // text request
  277. $.get(
  278. 'xmlhttp.php',
  279. { action: 'dvz_sb_get', id: id, key: my_post_key },
  280. function(data) {
  281.  
  282. if (dvz_shoutbox.handleErrors(data)) {
  283. return false;
  284. }
  285.  
  286. var data = $.parseJSON(data),
  287. newText = prompt('Shout #'+id+':', data.text);
  288.  
  289. if (newText && newText != data.text) {
  290.  
  291. // update request
  292. $.post(
  293. 'xmlhttp.php',
  294. { action: 'dvz_sb_update', text: newText, id: id, key: my_post_key },
  295. function(data) {
  296.  
  297. if (!dvz_shoutbox.handleErrors(data)) {
  298.  
  299. $('#shoutbox .entry[data-id="'+id+'"] .text').html(data);
  300.  
  301. dvz_shoutbox.runCallbacks('edit', { id: id, text: data });
  302.  
  303. }
  304.  
  305. }
  306. );
  307.  
  308. }
  309.  
  310. }
  311. );
  312. },
  313.  
  314. delete: function(id, noConfirm) {
  315.  
  316. if (noConfirm || confirm(dvz_shoutbox.lang[0])) {
  317.  
  318. $.post(
  319. 'xmlhttp.php',
  320. { action: 'dvz_sb_delete', id: id, key: my_post_key },
  321. function(data) {
  322.  
  323. if (!dvz_shoutbox.handleErrors(data)) {
  324.  
  325. $('#shoutbox .entry[data-id="'+id+'"]').fadeOut(function() { $(this).remove() });
  326.  
  327. dvz_shoutbox.runCallbacks('delete', { id: id });
  328.  
  329. }
  330.  
  331. }
  332. );
  333.  
  334. }
  335.  
  336. },
  337.  
  338. // functionality
  339. toggle: function(status, remember) {
  340.  
  341. if (status == true) {
  342.  
  343. dvz_shoutbox.status = true;
  344.  
  345. $('#shoutbox').removeClass('collapsed');
  346. $('#shoutbox .body').fadeIn();
  347.  
  348. if (dvz_shoutbox.frozen || !dvz_shoutbox.started) {
  349. dvz_shoutbox.frozen = false;
  350. dvz_shoutbox.loop();
  351. }
  352.  
  353. } else {
  354.  
  355. dvz_shoutbox.status = false;
  356.  
  357. $('#shoutbox .body').stop(1).fadeOut(function() {
  358. if (dvz_shoutbox.status == false) $('#shoutbox').stop(1).addClass('collapsed');
  359. });
  360.  
  361. }
  362.  
  363. if (remember !== false) {
  364. Cookie.set('dvz_sb_status', status ? '1' : '0');
  365. }
  366.  
  367. dvz_shoutbox.runCallbacks('toggle', { status: status });
  368.  
  369. },
  370.  
  371. // core
  372. antifloodPass: function() {
  373. var time = Math.floor((new Date).getTime() / 1000);
  374. return (time - dvz_shoutbox.lastSent) >= dvz_shoutbox.antiflood;
  375. },
  376.  
  377. updateActivity: function() {
  378. dvz_shoutbox.activity = (new Date).getTime();
  379. },
  380.  
  381. isAway: function() {
  382. if (!dvz_shoutbox.awayTime) return false;
  383. return (new Date).getTime() - dvz_shoutbox.activity > dvz_shoutbox.awayTime;
  384. },
  385.  
  386. onDisplay: function() {
  387. var viewTop = $(document).scrollTop(),
  388. viewBottom = viewTop + $(window).height(),
  389. elementTop = $('#shoutbox').offset().top,
  390. elementBottom = elementTop + $('#shoutbox').height();
  391.  
  392. return elementBottom >= (viewTop - dvz_shoutbox.lazyMargin) && elementTop <= (viewBottom + dvz_shoutbox.lazyMargin);
  393. },
  394.  
  395. checkVisibility: function() {
  396. if (dvz_shoutbox.frozen && dvz_shoutbox.onDisplay()) {
  397. dvz_shoutbox.frozen = false;
  398. dvz_shoutbox.loop();
  399. }
  400. },
  401.  
  402. lazyLoad: function() {
  403. if (dvz_shoutbox.lazyMode && !dvz_shoutbox.onDisplay()) {
  404. if (
  405. dvz_shoutbox.lazyMode == 'start' && !dvz_shoutbox.started ||
  406. dvz_shoutbox.lazyMode == 'always'
  407. ) {
  408. return false;
  409. }
  410. }
  411.  
  412. return true;
  413. },
  414.  
  415. handleErrors: function(response) {
  416. if (response == 'A') {
  417. alert(dvz_shoutbox.lang[1]);
  418. return true;
  419. } else
  420. if (response == 'P') {
  421. alert(dvz_shoutbox.lang[2]);
  422. return true;
  423. }
  424. if (response == 'S') {
  425. dvz_shoutbox.toggle(false);
  426. return true;
  427. }
  428.  
  429. return false;
  430. },
  431.  
  432. runCallbacks: function(name, data) {
  433. if (dvz_shoutbox.callbacks[name]) {
  434. for (var i in dvz_shoutbox.callbacks[name]) {
  435. dvz_shoutbox.callbacks[name][i](data);
  436. }
  437. }
  438. },
  439.  
  440. // visual
  441. call: function(username) {
  442.  
  443. var $input = $('#shoutbox input.text'),
  444. words = $input.val().split(' '),
  445. appendix = username;
  446.  
  447. // enclose in quotes if needed
  448. if (username.match( /["'`\.:\-+=~@#$%^*!?()\[\]{}\s]+/g )) {
  449.  
  450. var quotes = ['"', "'", '`'];
  451.  
  452. for (var i in quotes) {
  453. if (username.indexOf(quotes[i]) == -1) {
  454. appendix = quotes[i] + username + quotes[i];
  455. break;
  456. }
  457. }
  458.  
  459. }
  460.  
  461. // add a call sign
  462. appendix = dvz_shoutbox.callSign + appendix;
  463.  
  464. // add a leading space if suitable
  465. if ($input.val() != '' && $input.val().slice(-1) != ' ') {
  466. appendix = ' ' + appendix;
  467. }
  468.  
  469. // add a trailing space if suitable
  470. for (var i in words) {
  471. if (words[i] != '' && words[i].slice(0,1) != dvz_shoutbox.callSign) {
  472. break;
  473. }
  474. if (i == words.length-1) {
  475. appendix = appendix + ' ';
  476. }
  477. }
  478.  
  479. $('#shoutbox input.text').focus();
  480. $('#shoutbox input.text').val($input.val() + appendix);
  481. $('#shoutbox input.text').focus();
  482.  
  483. dvz_shoutbox.runCallbacks('call', { username: username });
  484.  
  485. },
  486.  
  487. toggleForm: function(status) {
  488. if (status == false) {
  489. $("#shoutbox input.text").attr('disabled', 'disabled');
  490. } else {
  491. $("#shoutbox input.text").removeAttr('disabled');
  492. $("#shoutbox input.text").focus();
  493. }
  494. },
  495.  
  496. clearForm: function() {
  497. $('#shoutbox input.text').val('');
  498. },
  499.  
  500. parseEntries: function(areLatest) {
  501. dvz_shoutbox.runCallbacks('entries');
  502.  
  503. $('#shoutbox .entry:not([data-parsed])').each(function() {
  504.  
  505. if (typeof $(this).attr('data-mod') !== 'undefined') {
  506. $(this).children('.info').prepend('<a href="" class="mod edit">E</a><a href="" class="mod del">X</a>');
  507. }
  508.  
  509. if (dvz_shoutbox.markUnread) {
  510. if ((areLatest === true ? dvz_shoutbox.firstId : $(this).attr('data-id')) > parseInt(Cookie.get('dvz_sb_last_read'))) {
  511. $(this).addClass('unread');
  512. }
  513. }
  514.  
  515. $(this).attr('data-parsed', '');
  516.  
  517. });
  518. },
  519.  
  520. updateLastRead: function() {
  521. if (dvz_shoutbox.markUnread) {
  522. if (
  523. Cookie.get('dvz_sb_last_read') === undefined ||
  524. (dvz_shoutbox.firstId <= Cookie.get('dvz_sb_last_read') && Cookie.get('dvz_sb_last_read') != dvz_shoutbox.lastId))
  525. {
  526. Cookie.set('dvz_sb_last_read', dvz_shoutbox.lastId);
  527. }
  528. }
  529. },
  530.  
  531. };
  532.  
  533. $(document).on('click', '#shoutbox .head', function() {
  534. dvz_shoutbox.toggle(!dvz_shoutbox.status);
  535. });
  536. $(document).on('click', '#shoutbox .head a', function(e) {
  537. e.stopPropagation();
  538. });
  539. $(document).on('click', '#shoutbox .entry .avatar', function() {
  540. if ($('#shoutbox input.text').length) {
  541. dvz_shoutbox.call( $(this).parents('.entry').attr('data-username') );
  542. }
  543. return false;
  544. });
  545. $(document).on('click', '#shoutbox .entry .mod.edit', function() {
  546. dvz_shoutbox.edit( $(this).parents('.entry').attr('data-id') );
  547. return false;
  548. });
  549. $(document).on('mousedown', '#shoutbox .entry[data-mod] .text', function() {
  550. dvz_shoutbox.holdTimeout = setTimeout($.proxy(function() {
  551. dvz_shoutbox.edit( $(this).parents('.entry').attr('data-id') );
  552. }, this), 500);
  553.  
  554. }).bind('mouseup mouseleave mousemove', function() {
  555. clearTimeout(dvz_shoutbox.holdTimeout);
  556. });
  557. $(document).on('click', function(e) {
  558. if (e.which == 2 && $(e.target).is('#shoutbox .entry .mod.del')) {
  559. dvz_shoutbox.delete( $(e.target).parents('.entry').attr('data-id'), true );
  560. e.preventDefault();
  561. }
  562. });
  563.  
  564. $(document).on('click', '#shoutbox .entry .mod.del', function() {
  565. dvz_shoutbox.delete( $(this).parents('.entry').attr('data-id') );
  566. return false;
  567. });
  568. $('#shoutbox .window').scroll(function() {
  569. if (dvz_shoutbox.recalling && $('#shoutbox .entry').length == dvz_shoutbox.maxShouts) {
  570.  
  571. var scrollMax = $('#shoutbox .data').innerHeight() - $('#shoutbox .window').innerHeight(),
  572. scroll = $('#shoutbox .window').scrollTop();
  573.  
  574. if (
  575. !dvz_shoutbox.reversed && scroll >= scrollMax ||
  576. dvz_shoutbox.reversed && scroll == 0
  577. ) {
  578. dvz_shoutbox.recall();
  579. }
  580. }
  581. });
  582. $(document).on('submit', '#shoutbox .panel form', function() {
  583. dvz_shoutbox.shout();
  584. return false;
  585. });
  586.  
  587. $(function(){
  588. if (dvz_shoutbox.reversed) {
  589. $('#shoutbox .window').scrollTop( $('#shoutbox .window')[0].scrollHeight );
  590. } else {
  591. $('#shoutbox .window').scrollTop(0);
  592. }
  593. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement