##// END OF EJS Templates
Setting focus to text area after reply.
Pavel Ryapolov -
r107:6af33f1b default
parent child Browse files
Show More
@@ -1,94 +1,96 b''
1 var image_mode = 0;
1 var image_mode = 0;
2 var normal_dom, table_dom;
2 var normal_dom, table_dom;
3
3
4 function add_panel(after)
4 function add_panel(after)
5 {
5 {
6 var nav_top = $(after);
6 var nav_top = $(after);
7 if (nav_top.length === 0) return;
7 if (nav_top.length === 0) return;
8 nav_top = nav_top[0];
8 nav_top = nav_top[0];
9
9
10 var tab_bar = $('<div class="image-mode-tab" role="radiogroup" aria-label="Image mode"></div>');
10 var tab_bar = $('<div class="image-mode-tab" role="radiogroup" aria-label="Image mode"></div>');
11
11
12 var tab;
12 var tab;
13
13
14 tab = $('<input type="radio" class="image-mode-normal" name="image-mode" value="0" checked="checked"/>');
14 tab = $('<input type="radio" class="image-mode-normal" name="image-mode" value="0" checked="checked"/>');
15 tab.on("change", tab_handler);
15 tab.on("change", tab_handler);
16 tab = $('<label>Normal</label>').prepend(tab);
16 tab = $('<label>Normal</label>').prepend(tab);
17 tab_bar.append(tab);
17 tab_bar.append(tab);
18
18
19 tab = $('<input type="radio" class="image-mode-table" name="image-mode" value="1"/>');
19 tab = $('<input type="radio" class="image-mode-table" name="image-mode" value="1"/>');
20 tab.on("change", tab_handler);
20 tab.on("change", tab_handler);
21 tab = $('<label>Gallery</label>').prepend(tab);
21 tab = $('<label>Gallery</label>').prepend(tab);
22 tab_bar.append(tab);
22 tab_bar.append(tab);
23
23
24 tab_bar.insertAfter(nav_top);
24 tab_bar.insertAfter(nav_top);
25 }
25 }
26
26
27 function tab_handler(ev)
27 function tab_handler(ev)
28 {
28 {
29 var current_el = $(this);
29 var current_el = $(this);
30
30
31 if (!current_el.prop('checked')) return;
31 if (!current_el.prop('checked')) return;
32
32
33 var new_mode = parseInt(current_el.val(), 10);
33 var new_mode = parseInt(current_el.val(), 10);
34 if (new_mode === image_mode) return;
34 if (new_mode === image_mode) return;
35 image_mode = new_mode;
35 image_mode = new_mode;
36
36
37 make_normal_dom();
37 make_normal_dom();
38 make_table_dom();
38 make_table_dom();
39
39
40 switch(new_mode) {
40 switch(new_mode) {
41 case 0:
41 case 0:
42 $('#posts-table').replaceWith(normal_dom);
42 $('#posts-table').replaceWith(normal_dom);
43 break;
43 break;
44 case 1:
44 case 1:
45 $('#posts').replaceWith(table_dom);
45 $('#posts').replaceWith(table_dom);
46 break;
46 break;
47 }
47 }
48 }
48 }
49
49
50 function make_normal_dom()
50 function make_normal_dom()
51 {
51 {
52 if (typeof normal_dom === 'undefined') {
52 if (typeof normal_dom === 'undefined') {
53 normal_dom = $('#posts').clone(true);
53 normal_dom = $('#posts').clone(true);
54 }
54 }
55 }
55 }
56
56
57 function make_table_dom()
57 function make_table_dom()
58 {
58 {
59 if (typeof table_dom !== 'undefined') return;
59 if (typeof table_dom !== 'undefined') return;
60
60
61 table_dom = $('<div id="posts-table"></div>');
61 table_dom = $('<div id="posts-table"></div>');
62 $('#posts > .post > .image > a').each(
62 $('#posts > .post > .image > a').each(
63 function(){
63 function(){
64 table_dom.append(
64 table_dom.append(
65 $(this).clone().attr('target', '_blank')
65 $(this).clone().attr('target', '_blank')
66 );
66 );
67 }
67 }
68 );
68 );
69 }
69 }
70
70
71 function moveCaretToEnd(el) {
71 function moveCaretToEnd(el) {
72 if (typeof el.selectionStart == "number") {
72 if (typeof el.selectionStart == "number") {
73 el.selectionStart = el.selectionEnd = el.value.length;
73 el.selectionStart = el.selectionEnd = el.value.length;
74 } else if (typeof el.createTextRange != "undefined") {
74 } else if (typeof el.createTextRange != "undefined") {
75 el.focus();
75 el.focus();
76 var range = el.createTextRange();
76 var range = el.createTextRange();
77 range.collapse(false);
77 range.collapse(false);
78 range.select();
78 range.select();
79 }
79 }
80 }
80 }
81
81
82 function addQuickReply(postId) {
82 function addQuickReply(postId) {
83 var textToAdd = '>>' + postId + '\n\n';
83 var textToAdd = '>>' + postId + '\n\n';
84 $('#id_text').val($('#id_text').val()+ textToAdd);
84 var textAreaId = '#id_text';
85 $(textAreaId).val($(textAreaId).val()+ textToAdd);
85
86
86 var textarea = document.getElementById('id_text');
87 var textarea = document.getElementById('id_text');
88 $(textAreaId).focus();
87 moveCaretToEnd(textarea);
89 moveCaretToEnd(textarea);
88
90
89 $("html, body").animate({ scrollTop: $('#id_text').offset().top }, "slow");
91 $("html, body").animate({ scrollTop: $(textAreaId).offset().top }, "slow");
90 }
92 }
91
93
92 $(document).ready(function(){
94 $(document).ready(function(){
93 add_panel('.navigation_panel');
95 add_panel('.navigation_panel');
94 });
96 });
General Comments 0
You need to be logged in to leave comments. Login now