Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         report: function(id) {
  339.         var reason = prompt('Reason');
  340.         if(reason.trim()) {
  341.             jQuery.post(
  342.                 'xmlhttp.php',
  343.                 {action: 'dvz_sb_report', id: id, key: my_post_key, reason: reason }, function(data){
  344.                     if(data == true) {
  345.                         alert('Shout is succesfully reported!');
  346.                     } else {
  347.                         alert('Something has gone wrong, try again.');
  348.                     }
  349.                 }
  350.             );
  351.             return;
  352.         }
  353.  
  354.         alert('You entered a invalid reason.');
  355.         return;
  356.     },
  357.  
  358.  
  359.     // functionality
  360.     toggle: function(status, remember) {
  361.  
  362.         if (status == true) {
  363.  
  364.             dvz_shoutbox.status = true;
  365.  
  366.             $('#shoutbox').removeClass('collapsed');
  367.             $('#shoutbox .body').fadeIn();
  368.  
  369.             if (dvz_shoutbox.frozen || !dvz_shoutbox.started) {
  370.                 dvz_shoutbox.frozen = false;
  371.                 dvz_shoutbox.loop();
  372.             }
  373.  
  374.         } else {
  375.  
  376.             dvz_shoutbox.status = false;
  377.  
  378.             $('#shoutbox .body').stop(1).fadeOut(function() {
  379.                 if (dvz_shoutbox.status == false) $('#shoutbox').stop(1).addClass('collapsed');
  380.             });
  381.  
  382.         }
  383.  
  384.         if (remember !== false) {
  385.             Cookie.set('dvz_sb_status', status ? '1' : '0');
  386.         }
  387.  
  388.         dvz_shoutbox.runCallbacks('toggle', { status: status });
  389.  
  390.     },
  391.  
  392.     // core
  393.     antifloodPass: function() {
  394.         var time = Math.floor((new Date).getTime() / 1000);
  395.         return (time - dvz_shoutbox.lastSent) >= dvz_shoutbox.antiflood;
  396.     },
  397.  
  398.     updateActivity: function() {
  399.         dvz_shoutbox.activity = (new Date).getTime();
  400.     },
  401.  
  402.     isAway: function() {
  403.         if (!dvz_shoutbox.awayTime) return false;
  404.         return (new Date).getTime() - dvz_shoutbox.activity > dvz_shoutbox.awayTime;
  405.     },
  406.  
  407.     onDisplay: function() {
  408.         var viewTop       = $(document).scrollTop(),
  409.             viewBottom    = viewTop + $(window).height(),
  410.             elementTop    = $('#shoutbox').offset().top,
  411.             elementBottom = elementTop + $('#shoutbox').height();
  412.  
  413.         return elementBottom >= (viewTop - dvz_shoutbox.lazyMargin) && elementTop <= (viewBottom + dvz_shoutbox.lazyMargin);
  414.     },
  415.  
  416.     checkVisibility: function() {
  417.         if (dvz_shoutbox.frozen && dvz_shoutbox.onDisplay()) {
  418.             dvz_shoutbox.frozen = false;
  419.             dvz_shoutbox.loop();
  420.         }
  421.     },
  422.  
  423.     lazyLoad: function() {
  424.         if (dvz_shoutbox.lazyMode && !dvz_shoutbox.onDisplay()) {
  425.             if (
  426.                 dvz_shoutbox.lazyMode == 'start' && !dvz_shoutbox.started ||
  427.                 dvz_shoutbox.lazyMode == 'always'
  428.             ) {
  429.                 return false;
  430.             }
  431.         }
  432.  
  433.         return true;
  434.     },
  435.  
  436.     handleErrors: function(response) {
  437.         if (response == 'A') {
  438.             alert(dvz_shoutbox.lang[1]);
  439.             return true;
  440.         } else
  441.         if (response == 'P') {
  442.             alert(dvz_shoutbox.lang[2]);
  443.             return true;
  444.         }
  445.         if (response == 'S') {
  446.             dvz_shoutbox.toggle(false);
  447.             return true;
  448.         }
  449.  
  450.         return false;
  451.     },
  452.  
  453.     runCallbacks: function(name, data) {
  454.         if (dvz_shoutbox.callbacks[name]) {
  455.             for (var i in dvz_shoutbox.callbacks[name]) {
  456.                 dvz_shoutbox.callbacks[name][i](data);
  457.             }
  458.         }
  459.     },
  460.  
  461.     // visual
  462.     call: function(username) {
  463.  
  464.         var $input = $('#shoutbox input.text'),
  465.             words = $input.val().split(' '),
  466.             appendix = username;
  467.  
  468.         // enclose in quotes if needed
  469.         if (username.match( /["'`\.:\-+=~@#$%^*!?()\[\]{}\s]+/g )) {
  470.  
  471.             var quotes = ['"', "'", '`'];
  472.  
  473.             for (var i in quotes) {
  474.                 if (username.indexOf(quotes[i]) == -1) {
  475.                     appendix = quotes[i] + username + quotes[i];
  476.                     break;
  477.                 }
  478.             }
  479.  
  480.         }
  481.  
  482.         // add a call sign
  483.         appendix = dvz_shoutbox.callSign + appendix;
  484.  
  485.         // add a leading space if suitable
  486.         if ($input.val() != '' && $input.val().slice(-1) != ' ') {
  487.             appendix = ' ' + appendix;
  488.         }
  489.  
  490.         // add a trailing space if suitable
  491.         for (var i in words) {
  492.             if (words[i] != '' && words[i].slice(0,1) != dvz_shoutbox.callSign) {
  493.                 break;
  494.             }
  495.             if (i == words.length-1) {
  496.                 appendix = appendix + ' ';
  497.             }
  498.         }
  499.  
  500.         $('#shoutbox input.text').focus();
  501.         $('#shoutbox input.text').val($input.val() + appendix);
  502.         $('#shoutbox input.text').focus();
  503.  
  504.         dvz_shoutbox.runCallbacks('call', { username: username });
  505.  
  506.     },
  507.  
  508.     toggleForm: function(status) {
  509.         if (status == false) {
  510.             $("#shoutbox input.text").attr('disabled', 'disabled');
  511.         } else {
  512.             $("#shoutbox input.text").removeAttr('disabled');
  513.             $("#shoutbox input.text").focus();
  514.         }
  515.     },
  516.  
  517.     clearForm: function() {
  518.         $('#shoutbox input.text').val('');
  519.     },
  520.  
  521.     parseEntries: function(areLatest) {
  522.         dvz_shoutbox.runCallbacks('entries');
  523.  
  524.         $('#shoutbox .entry:not([data-parsed])').each(function() {
  525.  
  526.             if (typeof $(this).attr('data-mod') !== 'undefined') {
  527.                 $(this).children('.info').prepend('<a href="" class="mod edit">E</a><a href="" class="mod del">X</a>');
  528.             }
  529.  
  530.             if (dvz_shoutbox.markUnread) {
  531.                 if ((areLatest === true ? dvz_shoutbox.firstId : $(this).attr('data-id')) > parseInt(Cookie.get('dvz_sb_last_read'))) {
  532.                     $(this).addClass('unread');
  533.                 }
  534.             }
  535.  
  536.             $(this).attr('data-parsed', '');
  537.  
  538.         });
  539.     },
  540.  
  541.     updateLastRead: function() {
  542.         if (dvz_shoutbox.markUnread) {
  543.             if (
  544.                 Cookie.get('dvz_sb_last_read') === undefined ||
  545.                 (dvz_shoutbox.firstId <= Cookie.get('dvz_sb_last_read') && Cookie.get('dvz_sb_last_read') != dvz_shoutbox.lastId))
  546.             {
  547.                 Cookie.set('dvz_sb_last_read', dvz_shoutbox.lastId);
  548.             }
  549.         }
  550.     },
  551.  
  552. };
  553.  
  554. $(document).on('click', '#shoutbox .head', function() {
  555.     dvz_shoutbox.toggle(!dvz_shoutbox.status);
  556. });
  557. $(document).on('click', '#shoutbox .head a', function(e) {
  558.     e.stopPropagation();
  559. });
  560. $(document).on('click', '#shoutbox .entry .avatar', function() {
  561.     dvz_shoutbox.call( $(this).parents('.entry').attr('data-username') );
  562.     return false;
  563. });
  564. $(document).on('click', '#shoutbox .entry .mod.edit', function() {
  565.     dvz_shoutbox.edit( $(this).parents('.entry').attr('data-id') );
  566.     return false;
  567. });
  568. $(document).on('mousedown', '#shoutbox .entry[data-mod] .text', function() {
  569.     dvz_shoutbox.holdTimeout = setTimeout($.proxy(function() {
  570.         dvz_shoutbox.edit( $(this).parents('.entry').attr('data-id') );
  571.     }, this), 500);
  572.  
  573. }).bind('mouseup mouseleave mousemove', function() {
  574.     clearTimeout(dvz_shoutbox.holdTimeout);
  575. });
  576. $(document).on('click', function(e) {
  577.     if (e.which == 2 && $(e.target).is('#shoutbox .entry .mod.del')) {
  578.         dvz_shoutbox.delete( $(e.target).parents('.entry').attr('data-id'), true );
  579.         e.preventDefault();
  580.     }
  581. });
  582. $(document).on('click', '#shoutbox .entry .mod.report', function() {
  583.     dvz_shoutbox.report(jQuery(this).parents('.entry').attr('data-id'));
  584.     return false;
  585. });
  586. $(document).on('click', '#shoutbox .entry .mod.del', function() {
  587.     dvz_shoutbox.delete( $(this).parents('.entry').attr('data-id') );
  588.     return false;
  589. });
  590. $('#shoutbox .window').scroll(function() {
  591.     if (dvz_shoutbox.recalling && $('#shoutbox .entry').length == dvz_shoutbox.maxShouts) {
  592.  
  593.         var scrollMax = $('#shoutbox .data').innerHeight() - $('#shoutbox .window').innerHeight(),
  594.             scroll    = $('#shoutbox .window').scrollTop();
  595.  
  596.         if (
  597.             !dvz_shoutbox.reversed && scroll >= scrollMax ||
  598.             dvz_shoutbox.reversed && scroll == 0
  599.         ) {
  600.             dvz_shoutbox.recall();
  601.         }
  602.     }
  603. });
  604. $(document).on('submit', '#shoutbox .panel form', function() {
  605.     dvz_shoutbox.shout();
  606.     return false;
  607. });
  608.  
  609. $(function(){
  610.     if (dvz_shoutbox.reversed) {
  611.         $('#shoutbox .window').scrollTop( $('#shoutbox .window')[0].scrollHeight );
  612.     } else {
  613.         $('#shoutbox .window').scrollTop(0);
  614.     }
  615. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement