Advertisement
Guest User

Untitled

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