|
1 | 1 | <script type="text/javascript"> |
2 | 2 | (function () { |
| 3 | + |
| 4 | + var events = 'DOMContentLoaded load resize scroll'; |
| 5 | + var handler = null; |
| 6 | + var didCallback = false; |
| 7 | + |
3 | 8 | $('#reply-control').on('show.bs.collapse', function () { |
4 | 9 | // Show spacer div |
5 | 10 | $('#show-reply-form-when-visible').css({ |
|
18 | 23 | }); |
19 | 24 |
|
20 | 25 | $('.btn-comment-reply').click(function (aE) { |
21 | | - $('#reply-control').collapse('show'); |
22 | | - |
23 | 26 | var $comment = $(aE.target).closest('.topic-post'); |
24 | 27 | var $author = $comment.find('.topic-meta-data .names .username').first(); |
25 | 28 | var $replyTextarea = $('#reply-control textarea[name="comment-content"]'); |
| 29 | + var value = null; |
| 30 | + |
| 31 | + $('#reply-control').collapse('show'); |
26 | 32 |
|
27 | 33 | document.location.hash = $comment.attr('id'); |
28 | 34 |
|
29 | | - var value = $replyTextarea.val(); |
| 35 | + value = $replyTextarea.val(); |
30 | 36 | if (!/^\s*$/.test(value)) { |
31 | | - value += '\n\n'; // Add linebreaks if reply already started. |
| 37 | + // Add linebreaks if reply already started. |
| 38 | + value += '\n\n'; |
32 | 39 | } |
33 | 40 | value += '[Re](' + document.location.href.replace(/\(/g, '%28').replace(/\)/g, '%29') + '): '; |
34 | 41 | value += '[@' + $author.text() + '](' + $author.attr('href') + '): '; |
|
37 | 44 | $replyTextarea.focus(); |
38 | 45 | }); |
39 | 46 |
|
40 | | - function isElementInViewport(aEl) { |
41 | | - if (aEl instanceof jQuery) { |
42 | | - aEl = aEl[0]; |
43 | | - } |
44 | | - |
45 | | - var rect = aEl.getBoundingClientRect(); |
46 | | - |
47 | | - return ( |
48 | | - rect.top >= 0 && |
49 | | - rect.left >= 0 && |
50 | | - rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */ |
51 | | - rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */ |
52 | | - ); |
53 | | - } |
54 | | - |
55 | | - var hasShownReplyForm = false; |
56 | 47 | function callback(aEl) { |
57 | | - if (!hasShownReplyForm) { |
58 | | - $('#reply-control').collapse('show'); |
59 | | - hasShownReplyForm = true; |
| 48 | + if (!didCallback) { |
| 49 | + didCallback = true; |
60 | 50 |
|
| 51 | + $('#reply-control').collapse('show'); |
61 | 52 | $('#reply-control textarea[name="comment-content"]').focus(); |
62 | 53 | } |
63 | 54 | } |
64 | 55 |
|
| 56 | + {{> includes/scripts/isElementInViewport.js }} |
| 57 | + |
65 | 58 | function fireIfElementVisible(aEl, aCallback) { |
66 | 59 | return function () { |
67 | 60 | if (isElementInViewport(aEl)) { |
| 61 | + $(window).off(events, handler); |
| 62 | + |
68 | 63 | aCallback(aEl); |
69 | 64 | } |
70 | 65 | } |
71 | 66 | } |
72 | 67 |
|
73 | | - var handler = fireIfElementVisible($('#show-reply-form-when-visible'), callback); |
74 | | - |
75 | | - // jQuery |
76 | | - $(window).on('DOMContentLoaded load resize scroll', handler); |
| 68 | + handler = fireIfElementVisible($('#show-reply-form-when-visible'), callback); |
| 69 | + $(window).on(events, handler); |
77 | 70 |
|
78 | 71 | })(); |
79 | 72 | </script> |
0 commit comments