Advertisement
Guest User

Untitled

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