The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.3KB

  1. /*
  2. * hide-images.js
  3. * https://github.com/savetheinternet/Tinyboard/blob/master/js/hide-images.js
  4. *
  5. * Hide individual images.
  6. *
  7. * Released under the MIT license
  8. * Copyright (c) 2013 Michael Save <savetheinternet@tinyboard.org>
  9. * Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
  10. *
  11. * Usage:
  12. * $config['additional_javascript'][] = 'js/jquery.min.js';
  13. * $config['additional_javascript'][] = 'js/hide-images.js';
  14. *
  15. */
  16. $(document).ready(function(){
  17. $('<style type="text/css"> img.hidden{ opacity: 0.1; background: grey; border: 1px solid #000; } </style>').appendTo($('head'));
  18. if (!localStorage.hiddenimages)
  19. localStorage.hiddenimages = '{}';
  20. // Load data from HTML5 localStorage
  21. var hidden_data = JSON.parse(localStorage.hiddenimages);
  22. var store_data = function() {
  23. localStorage.hiddenimages = JSON.stringify(hidden_data);
  24. };
  25. // Delete old hidden images (30+ days old)
  26. for (var key in hidden_data) {
  27. for (var id in hidden_data[key]) {
  28. if (hidden_data[key][id] < Math.round(Date.now() / 1000) - 60 * 60 * 24 * 30) {
  29. delete hidden_data[key][id];
  30. store_data();
  31. }
  32. }
  33. }
  34. var handle_images = function() {
  35. var index = $(this).parents('.file').index();
  36. var img = this;
  37. var fileinfo = $(this).parent().prev();
  38. var id = $(this).parents('div.post, div[id^="thread_"]').attr('id').split('_')[1];
  39. var board = $(this).parents('[id^="thread_"]').data("board");
  40. if (!hidden_data[board]) {
  41. hidden_data[board] = {}; // id : timestamp
  42. }
  43. var replacement = $('<span>'+_('File')+' <small>(<a class="hide-image-link" href="javascript:void(0)">'+_('hide')+'</a>)</small>: </span>');
  44. replacement.find('a').click(function() {
  45. if (hidden_data[board][id]) {
  46. hidden_data[board][id]['ts'] = Math.round(Date.now() / 1000);
  47. if (hidden_data[board][id]['index'].indexOf(index) === -1)
  48. hidden_data[board][id]['index'].push(index);
  49. } else {
  50. hidden_data[board][id] = {ts: Math.round(Date.now() / 1000), index: [index]};
  51. }
  52. store_data();
  53. var show_link = $('<a class="show-image-link" href="javascript:void(0)">'+_('show')+'</a>').click(function() {
  54. var i = hidden_data[board][id]['index'].indexOf(index);
  55. if (i > -1) hidden_data[board][id]['index'].splice(i,1);
  56. if (hidden_data[board][id]['index'].length === 0)
  57. delete hidden_data[board][id];
  58. store_data();
  59. $(img)
  60. .removeClass('hidden')
  61. .attr('src', $(img).data('orig'));
  62. $(this).prev().show();
  63. $(this).remove();
  64. });
  65. $(this).hide().after(show_link);
  66. if ($(img).parent().data('expanded') == 'true') {
  67. $(img).parent().click();
  68. }
  69. $(img)
  70. .data('orig', img.src)
  71. .attr('src', 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==')
  72. .addClass('hidden');
  73. });
  74. $(this).parent().prev().contents().first().replaceWith(replacement);
  75. if (hidden_data[board][id] && hidden_data[board][id]['index'].indexOf(index) !== -1)
  76. $(this).parent().prev().find('.hide-image-link').click();
  77. };
  78. $('div.post > a > img.post-image, div.post > a > video.post-image, div > a > img.post-image, div > a > video.post-image').each(handle_images);
  79. $(document).on('new_post', function(e, post) {
  80. $(post).find('a > img.post-image, a > video.post-image').each(handle_images);
  81. });
  82. });