/* * quick-reply.js * https://github.com/savetheinternet/Tinyboard/blob/master/js/quick-reply.js * * Released under the MIT license * Copyright (c) 2013 Michael Save * Copyright (c) 2013-2014 Marcin Łabanowski * * Usage: * $config['additional_javascript'][] = 'js/jquery.min.js'; * $config['additional_javascript'][] = 'js/jquery-ui.custom.min.js'; // Optional; if you want the form to be draggable. * $config['additional_javascript'][] = 'js/quick-reply.js'; * */ (function() { var settings = new script_settings('quick-reply'); var show_quick_reply = function(){ if($('div.banner').length == 0) return; if($('#quick-reply').length != 0) return; var $postForm = $('form[name="post"]').clone(); $postForm.clone(); $dummyStuff = $('
').appendTo($postForm); $postForm.find('table tr').each(function() { var $th = $(this).children('th:first'); var $td = $(this).children('td:first'); if ($th.length && $td.length) { $td.attr('colspan', 2); if ($td.find('input[type="text"]').length) { // Replace with input placeholders $td.find('input[type="text"]') .removeAttr('size') .attr('placeholder', $th.clone().children().remove().end().text()); } // Move anti-spam nonsense and remove $th.contents().filter(function() { return this.nodeType == 3; // Node.TEXT_NODE }).remove(); $th.contents().appendTo($dummyStuff); $th.remove(); if ($td.find('input[name="password"]').length) { // Hide password field $(this).hide(); } // Fix submit button if ($td.find('input[type="submit"]').length) { $td.removeAttr('colspan'); $('').append($td.find('input[type="submit"]')).insertAfter($td); } // Upload section if ($td.find('input[type="file"]').length) { if ($td.find('input[name="file_url"]').length) { $file_url = $td.find('input[name="file_url"]'); $file_url.parent().remove(); $td.find('label').remove(); $td.contents().filter(function() { return this.nodeType == 3; // Node.TEXT_NODE }).remove(); $td.find('input[name="file_url"]').removeAttr('id'); } if ($(this).find('input[name="spoiler"]').length) { $td.removeAttr('colspan'); } } // Remove upload selection if ($td.is('#upload_selection')) { $(this).remove(); } $td.find('small').hide(); } }); $postForm.find('textarea[name="body"]').removeAttr('id').removeAttr('cols').attr('placeholder', _('Comment')); $postForm.find('textarea:not([name="body"]),input[type="hidden"]:not(.captcha_cookie)').removeAttr('id').appendTo($dummyStuff); $postForm.find('br').remove(); $postForm.find('table').prepend('\ \ ×\ ' + _('Quick Reply') + '\ \ '); $postForm.attr('id', 'quick-reply'); $postForm.appendTo($('body')).hide(); $origPostForm = $('form[name="post"]:first'); // Synchronise body text with original post form $origPostForm.find('textarea[name="body"]').on('change input propertychange', function() { $postForm.find('textarea[name="body"]').val($(this).val()); }); $postForm.find('textarea[name="body"]').on('change input propertychange', function() { $origPostForm.find('textarea[name="body"]').val($(this).val()); }); $postForm.find('textarea[name="body"]').focus(function() { $origPostForm.find('textarea[name="body"]').removeAttr('id'); $(this).attr('id', 'body'); }); $origPostForm.find('textarea[name="body"]').focus(function() { $postForm.find('textarea[name="body"]').removeAttr('id'); $(this).attr('id', 'body'); }); // Synchronise other inputs $origPostForm.find('input[type="text"],select').on('change input propertychange', function() { $postForm.find('[name="' + $(this).attr('name') + '"]').val($(this).val()); }); $postForm.find('input[type="text"],select').on('change input propertychange', function() { $origPostForm.find('[name="' + $(this).attr('name') + '"]').val($(this).val()); }); if (localStorage.quickReplyPosition) { var offset = JSON.parse(localStorage.quickReplyPosition); if (offset.top < 0) offset.top = 0; if (offset.right > $(window).width() - $postForm.width()) offset.right = $(window).width() - $postForm.width(); if (offset.top > $(window).height() - $postForm.height()) offset.top = $(window).height() - $postForm.height(); $postForm.css('right', offset.right).css('top', offset.top); } $postForm.draggable({ handle: 'th .handle', containment: 'window', distance: 10, scroll: false, stop: function() { var offset = { top: $(this).offset().top - $(window).scrollTop(), right: $(window).width() - $(this).offset().left - $(this).width(), }; localStorage.quickReplyPosition = JSON.stringify(offset); $postForm.css('right', offset.right).css('top', offset.top).css('left', 'auto'); } }); $postForm.find('th .handle').css('cursor', 'move'); $postForm.find('th .close-btn').click(function() { $origPostForm.find('textarea[name="body"]').attr('id', 'body'); $postForm.remove(); floating_link(); }); // Fix bug when table gets too big for form. Shouldn't exist, but crappy CSS etc. $postForm.show(); $postForm.width($postForm.find('table').width()); $postForm.hide(); $(window).trigger('quick-reply'); $(window).ready(function() { $(window).scroll(function() { if ($(this).width() <= 400) return; if ($(this).scrollTop() < $origPostForm.offset().top + $origPostForm.height() - 100) $postForm.hide(); else $postForm.show(); }).scroll(); $(window).on('stylesheet', function() { do_css(); if ($('link#stylesheet').attr('href')) { $('link#stylesheet')[0].onload = do_css; } }); }); }; $(window).on('cite', function(e, id, with_link) { if ($(this).width() <= 400) return; show_quick_reply(); if (with_link) { $(document).ready(function() { if ($('#' + id).length) { highlightReply(id); $(document).scrollTop($('#' + id).offset().top); } // Honestly, I'm not sure why we need setTimeout() here, but it seems to work. // Same for the "tmp" variable stuff you see inside here: setTimeout(function() { var tmp = $('#quick-reply textarea[name="body"]').val(); $('#quick-reply textarea[name="body"]').val('').focus().val(tmp); }, 1); }); } }); })();