Completed
Pull Request — release-2.1 (#4823)
by
unknown
08:48
created

Themes/default/scripts/quotedText.js (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
// Traverse the DOM tree in our spinoff of jQuery's closest()
2
function getClosest(el, divID)
3
{
4
	if (typeof divID == 'undefined' || divID == false)
5
		return null;
6
7
	do
8
	{
9
		// End the loop if quick edit is detected.
10
		if (el.nodeName === 'TEXTAREA' || el.nodeName === 'INPUT' || el.id === 'error_box')
11
			break;
12
13
		if (el.id === divID)
14
		{
15
			return el;
16
		}
17
	}
18
	while (el = el.parentNode);
19
20
	// not found :(
21
	return null;
22
}
23
24
function getSelectedText(divID)
25
{
26
	if (typeof divID == 'undefined' || divID == false)
27
		return false;
28
29
	var text = '',
30
	selection,
31
	found = 0,
32
	container = document.createElement("div");
33
34
	if (window.getSelection)
35
	{
36
		selection = window.getSelection();
37
		text = selection.toString();
38
	}
39
	else if (document.selection && document.selection.type != 'Control')
40
	{
41
		selection = document.selection.createRange();
42
		text = selection.text;
43
	}
44
45
	// Need to be sure the selected text does belong to the right div.
46
	for (var i = 0; i < selection.rangeCount; i++) {
0 ignored issues
show
The variable selection does not seem to be initialized in case document.selection && do...ction.type != "Control" on line 39 is false. Are you sure this can never be the case?
Loading history...
47
			s = getClosest(selection.getRangeAt(i).startContainer, divID);
0 ignored issues
show
The variable s seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.s.
Loading history...
48
			e = getClosest(selection.getRangeAt(i).endContainer, divID);
0 ignored issues
show
The variable e seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.e.
Loading history...
49
50
			if (s !== null && e !== null)
51
			{
52
				found = 1;
53
				container.appendChild(selection.getRangeAt(i).cloneContents());
54
				text = container.innerHTML;
55
				break;
56
			}
57
		}
58
59
	return found === 1 ? text : false;
60
}
61
62
function quotedTextClick(oOptions)
63
{
64
		text = '';
0 ignored issues
show
The variable text seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.text.
Loading history...
65
66
		// The process has been started, hide the button.
67
		$('#quoteSelected_' + oOptions.msgID).hide();
68
69
		// Do a call to make sure this is a valid message.
70
		$.ajax({
71
			url: smf_prepareScriptUrl(smf_scripturl) + 'action=quotefast;quote=' + oOptions.msgID + ';xml;pb='+ oEditorID + ';mode=' + (oEditorObject.bRichTextEnabled ? 1 : 0),
72
			type: 'GET',
73
			dataType: 'xml',
74
			beforeSend: function () {
75
				ajax_indicator(true);
76
			},
77
			complete: function(jqXHR, textStatus){
78
				ajax_indicator(false);
79
			},
80
			success: function (data, textStatus, xhr) {
81
				// Search the xml data to get the quote tag.
82
				text = $(data).find('quote').text();
0 ignored issues
show
The variable text seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.text.
Loading history...
83
84
				// Insert the selected text between the quotes BBC tags.
85
				text = text.match(/^\[quote(.*)]/ig) + oOptions.text + '[/quote]' + '\n\n';
86
87
				// Get the editor stuff.
88
				var e = $('#' + oEditorID).get(0);
89
				var oEditor = sceditor.instance(e);
90
91
				// Convert any HTML into BBC tags.
92
				text = oEditor.toBBCode(text);
93
94
				// Push the text to the editor.
95
				oEditor.insert(text);
96
97
				// Move the view to the quick reply box. If available.
98
				if (typeof oJumpAnchor != 'undefined'){
99
					if (navigator.appName == 'Microsoft Internet Explorer')
100
						window.location.hash = oJumpAnchor;
101
					else
102
						window.location.hash = '#' + oJumpAnchor;
103
				}
104
			},
105
			error: function (xhr, textStatus, errorThrown) {
106
				// @todo Show some error.
107
			}
108
		});
109
}
110
111
$(function() {
112
113
	// Event for handling selected quotes.
114
	$(document).on('mouseup', '.inner, .list_posts', function() {
115
116
		// Get everything we need.
117
		var oSelected = {
118
			divID : $(this).attr('id'),
119
			msgID : $(this).data('msgid'),
120
		};
121
122
		// Revome any 'click' event to the button.
123
		$(document).off('click', '#quoteSelected_' + oSelected.msgID + ' a');
124
125
		// If the button is already visible, hide it!
126
		$('#quoteSelected_' + oSelected.msgID).hide();
127
128
		// Get any selected text.
129
		oSelected.text = getSelectedText(oSelected.divID);
130
131
		// Do we have some selected text?
132
		if (typeof oSelected.text == 'undefined' || oSelected.text == false)
133
			return false;
134
135
		// Show the "quote this" button.
136
		$('#quoteSelected_' + oSelected.msgID).show();
137
138
			$(document).one('click', '#quoteSelected_' + oSelected.msgID + ' a', function(e){
139
				e.preventDefault();
140
				quotedTextClick(oSelected);
141
			});
142
143
		return false;
144
	});
145
});
146