##// END OF EJS Templates
Additinal JS refactoring
neko259 -
r1082:66eb05a4 default
parent child Browse files
Show More
@@ -1,101 +1,103 b''
1 /*
1 /*
2 @licstart The following is the entire license notice for the
2 @licstart The following is the entire license notice for the
3 JavaScript code in this page.
3 JavaScript code in this page.
4
4
5
5
6 Copyright (C) 2013 neko259
6 Copyright (C) 2013 neko259
7
7
8 The JavaScript code in this page is free software: you can
8 The JavaScript code in this page is free software: you can
9 redistribute it and/or modify it under the terms of the GNU
9 redistribute it and/or modify it under the terms of the GNU
10 General Public License (GNU GPL) as published by the Free Software
10 General Public License (GNU GPL) as published by the Free Software
11 Foundation, either version 3 of the License, or (at your option)
11 Foundation, either version 3 of the License, or (at your option)
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 without even the implied warranty of MERCHANTABILITY or FITNESS
13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15
15
16 As additional permission under GNU GPL version 3 section 7, you
16 As additional permission under GNU GPL version 3 section 7, you
17 may distribute non-source (e.g., minimized or compacted) forms of
17 may distribute non-source (e.g., minimized or compacted) forms of
18 that code without the copy of the GNU GPL normally required by
18 that code without the copy of the GNU GPL normally required by
19 section 4, provided you include this license notice and a URL
19 section 4, provided you include this license notice and a URL
20 through which recipients can access the Corresponding Source.
20 through which recipients can access the Corresponding Source.
21
21
22 @licend The above is the entire license notice
22 @licend The above is the entire license notice
23 for the JavaScript code in this page.
23 for the JavaScript code in this page.
24 */
24 */
25
25
26 function addImgPreview() {
26 function addImgPreview() {
27 var margin = 20; //..change
27 var margin = 20; //..change
28
28
29 //keybind
29 //keybind
30 $(document).on('keyup.removepic', function(e) {
30 $(document).on('keyup.removepic', function(e) {
31 if(e.which === 27) {
31 if(e.which === 27) {
32 $('.img-full').remove();
32 $('.img-full').remove();
33 }
33 }
34 });
34 });
35
35
36 $('body').on('click', '.thumb', function() {
36 $('body').on('click', '.thumb', function() {
37 var el = $(this);
37 var el = $(this);
38 var thumb_id = 'full' + el.find('img').attr('alt');
38 var thumb_id = 'full' + el.find('img').attr('alt');
39
39
40 if(!$('#'+thumb_id).length) {
40 var existingPopups = $('#' + thumb_id);
41 if(!existingPopups.length) {
41 var imgElement= el.find('img');
42 var imgElement= el.find('img');
42
43
43 var img_w = imgElement.attr('data-width');
44 var img_w = imgElement.attr('data-width');
44 var img_h = imgElement.attr('data-height');
45 var img_h = imgElement.attr('data-height');
45
46
46 var win = $(window);
47 var win = $(window);
47
48
48 var win_w = win.width();
49 var win_w = win.width();
49 var win_h = win.height();
50 var win_h = win.height();
50 //new image size
51 //new image size
51 if (img_w > win_w) {
52 if (img_w > win_w) {
52 img_h = img_h * (win_w/img_w) - margin;
53 img_h = img_h * (win_w/img_w) - margin;
53 img_w = win_w - margin;
54 img_w = win_w - margin;
54 }
55 }
55 if (img_h > win_h) {
56 if (img_h > win_h) {
56 img_w = img_w * (win_h/img_h) - margin;
57 img_w = img_w * (win_h/img_h) - margin;
57 img_h = win_h - margin;
58 img_h = win_h - margin;
58 }
59 }
59
60
60 var img_pv = new Image();
61 var img_pv = new Image();
61 var newImage = $(img_pv);
62 var newImage = $(img_pv);
62 newImage.addClass('img-full')
63 newImage.addClass('img-full')
63 .attr('id', thumb_id)
64 .attr('id', thumb_id)
64 .attr('src', $(el).attr('href'))
65 .attr('src', $(el).attr('href'))
65 .appendTo($(el))
66 .appendTo($(el))
66 .css({
67 .css({
67 'width': img_w,
68 'width': img_w,
68 'height': img_h,
69 'height': img_h,
69 'left': (win_w - img_w) / 2,
70 'left': (win_w - img_w) / 2,
70 'top': ((win_h - img_h) / 2)
71 'top': ((win_h - img_h) / 2)
71 })
72 })
72 //scaling preview
73 //scaling preview
73 .mousewheel(function(event, delta) {
74 .mousewheel(function(event, delta) {
74 var cx = event.originalEvent.clientX,
75 var cx = event.originalEvent.clientX,
75 cy = event.originalEvent.clientY,
76 cy = event.originalEvent.clientY,
76 i_w = parseFloat(newImage.width()),
77 i_w = parseFloat(newImage.width()),
77 i_h = parseFloat(newImage.height()),
78 i_h = parseFloat(newImage.height()),
78 newIW = i_w * (delta > 0 ? 1.25 : 0.8),
79 newIW = i_w * (delta > 0 ? 1.25 : 0.8),
79 newIH = i_h * (delta > 0 ? 1.25 : 0.8);
80 newIH = i_h * (delta > 0 ? 1.25 : 0.8);
80
81
81 newImage.width(newIW);
82 newImage.width(newIW);
82 newImage.height(newIH);
83 newImage.height(newIH);
83 //set position
84 //set position
84 newImage.css({
85 newImage.css({
85 left: parseInt(cx - (newIW/i_w) * (cx - parseInt($(img_pv).position().left, 10)), 10),
86 left: parseInt(cx - (newIW/i_w) * (cx - parseInt($(img_pv).position().left, 10)), 10),
86 top: parseInt(cy - (newIH/i_h) * (cy - parseInt($(img_pv).position().top, 10)), 10)
87 top: parseInt(cy - (newIH/i_h) * (cy - parseInt($(img_pv).position().top, 10)), 10)
87 });
88 });
88
89
89 return false;
90 return false;
90 }
91 }
91 ).draggable({
92 )
92 addClasses: false,
93 .draggable({
93 stack: '.img-full'
94 addClasses: false,
94 })
95 stack: '.img-full'
96 });
95 } else {
97 } else {
96 $('#'+thumb_id).remove();
98 existingPopups.remove();
97 }
99 }
98 //prevent default
100 //prevent default
99 return false;
101 return false;
100 });
102 });
101 }
103 }
@@ -1,102 +1,60 b''
1 /*
1 /*
2 @licstart The following is the entire license notice for the
2 @licstart The following is the entire license notice for the
3 JavaScript code in this page.
3 JavaScript code in this page.
4
4
5
5
6 Copyright (C) 2013 neko259
6 Copyright (C) 2013 neko259
7
7
8 The JavaScript code in this page is free software: you can
8 The JavaScript code in this page is free software: you can
9 redistribute it and/or modify it under the terms of the GNU
9 redistribute it and/or modify it under the terms of the GNU
10 General Public License (GNU GPL) as published by the Free Software
10 General Public License (GNU GPL) as published by the Free Software
11 Foundation, either version 3 of the License, or (at your option)
11 Foundation, either version 3 of the License, or (at your option)
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 without even the implied warranty of MERCHANTABILITY or FITNESS
13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15
15
16 As additional permission under GNU GPL version 3 section 7, you
16 As additional permission under GNU GPL version 3 section 7, you
17 may distribute non-source (e.g., minimized or compacted) forms of
17 may distribute non-source (e.g., minimized or compacted) forms of
18 that code without the copy of the GNU GPL normally required by
18 that code without the copy of the GNU GPL normally required by
19 section 4, provided you include this license notice and a URL
19 section 4, provided you include this license notice and a URL
20 through which recipients can access the Corresponding Source.
20 through which recipients can access the Corresponding Source.
21
21
22 @licend The above is the entire license notice
22 @licend The above is the entire license notice
23 for the JavaScript code in this page.
23 for the JavaScript code in this page.
24 */
24 */
25
25
26 /**
26 /**
27 * Add the desired characters to the start and end of selection.
27 * Add the desired characters to the start and end of selection.
28 *
28 *
29 * @param start Start (left) text
29 * @param start Start (left) text
30 * @param end End (right) text
30 * @param end End (right) text
31 */
31 */
32 function addMarkToMsg(start, end) {
32 function addMarkToMsg(start, end) {
33 if (end.length == 0) {
34 return addTextToEachLineOfSelection(start);
35 }
36
37 var textareas = $('textarea');
33 var textareas = $('textarea');
38
34
39 for (var i = 0; i < textareas.length; i++) {
35 for (var i = 0; i < textareas.length; i++) {
40 var textarea = textareas[i];
36 var textarea = textareas[i];
41
37
42 if (document.selection) {
38 if (document.selection) {
43 textarea.focus();
39 textarea.focus();
44
40
45 var sel = document.selection.createRange();
41 var sel = document.selection.createRange();
46 sel.text = start + sel.text + end;
42 sel.text = start + sel.text + end;
47 } else if (textarea.selectionStart || textarea.selectionStart == '0') {
43 } else if (textarea.selectionStart || textarea.selectionStart == '0') {
48 textarea.focus();
44 textarea.focus();
49
45
50 var startPos = textarea.selectionStart;
46 var startPos = textarea.selectionStart;
51 var endPos = textarea.selectionEnd;
47 var endPos = textarea.selectionEnd;
52
48
53 var oldValue = textarea.value;
49 var oldValue = textarea.value;
54 textarea.value = oldValue.substring(0, startPos) + start +
50 textarea.value = oldValue.substring(0, startPos) + start +
55 oldValue.substring(startPos, endPos) + end +
51 oldValue.substring(startPos, endPos) + end +
56 oldValue.substring(endPos, oldValue.length);
52 oldValue.substring(endPos, oldValue.length);
57 } else {
53 } else {
58 textarea.value += start + end;
54 textarea.value += start + end;
59 }
55 }
60 }
56 }
61
57
62 return false;
58 return false;
63 }
59 }
64
60
65 /**
66 * Add text to the beginning of each selected line. Partially selected lines
67 * are included
68 *
69 * @param textToAdd Text to add to the each line
70 */
71 function addTextToEachLineOfSelection(textToAdd) {
72 var textareas = $('textarea');
73
74 for (var i = 0; i < textareas.length; i++) {
75 var textarea = textareas[i];
76
77 if (document.selection) {
78 textarea.focus();
79
80 var sel = document.selection.createRange();
81 sel.text = start + sel.text + end;
82 } else if (textarea.selectionStart || textarea.selectionStart == '0') {
83 textarea.focus();
84
85 var startPos = textarea.selectionStart;
86 var endPos = textarea.selectionEnd;
87
88 var oldValue = textarea.value;
89
90 var textBeforeSelection = oldValue.substring(0, startPos);
91 var selectionText = oldValue.substring(startPos, endPos)
92 .replace(/\n/g, '\n' + textToAdd);
93 textarea.value = textBeforeSelection + textToAdd +
94 selectionText +
95 oldValue.substring(endPos, oldValue.length);
96 } else {
97 textarea.value += textToAdd;
98 }
99 }
100
101 return false;
102 }
General Comments 0
You need to be logged in to leave comments. Login now