Skip to content

Commit e5131bd

Browse files
committed
Issues-83: set focus on a new comment(RichEditor)
1 parent 52dd7e1 commit e5131bd

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// Quotes object constructor
2+
function Gdn_Quotes() {
3+
var currentEditor = null;
4+
}
5+
6+
// Attach event handler for quotes on the page.
7+
Gdn_Quotes.prototype.Prepare = function () {
8+
// Capture "this" for use in callbacks.
9+
var Quotes = this,
10+
$document = $(document);
11+
12+
// Attach quote event to each Quote button, and return false to prevent link follow.
13+
$document.on('click', 'a.ReactButton.Quote', function(event) {
14+
var QuoteLink = $(event.target).closest('a'),
15+
ObjectID = QuoteLink.attr('href').split('/').pop();
16+
17+
Quotes.Quote(ObjectID, QuoteLink);
18+
return false;
19+
});
20+
21+
// Track active editor.
22+
$document.on('focus', 'textarea.TextBox', function () {
23+
Quotes.currentEditor = this;
24+
});
25+
26+
// Handle quote folding clicks.
27+
$document.on('click', 'a.QuoteFolding', function () {
28+
var Anchor = $(this),
29+
QuoteTarget = Anchor
30+
.parent()
31+
.next()
32+
.toggle();
33+
34+
if (QuoteTarget.css('display') != 'none') {
35+
Anchor.html(gdn.definition('hide previous quotes'));
36+
} else {
37+
Anchor.html(gdn.definition('show previous quotes'));
38+
}
39+
40+
return false;
41+
});
42+
};
43+
44+
/**
45+
* Format the quotes within a given parent element.
46+
*
47+
* @param elem The parent element.
48+
*/
49+
Gdn_Quotes.prototype.format = function (elem) {
50+
// Handle quote folding.
51+
var QuoteFoldingLevel = parseInt(gdn.getMeta('QuotesFolding', 1)) + 1,
52+
Quotes = this,
53+
MaxFoldingLevel = 6;
54+
55+
$('.Discussion .Message, .Comment .Message', elem).each(function () {
56+
// Find the closest child quote
57+
var Message = $(this),
58+
PetQuote = Message.children('.Quote, .UserQuote');
59+
60+
if (Message.data('QuoteFolding') || !PetQuote.length) {
61+
return;
62+
}
63+
Message.data('QuoteFolding', '1');
64+
65+
Quotes.ExploreFold(PetQuote, 1, MaxFoldingLevel, QuoteFoldingLevel);
66+
});
67+
};
68+
69+
70+
// Recursively transform folded quotes.
71+
Gdn_Quotes.prototype.ExploreFold = function(QuoteTree, FoldingLevel, MaxLevel, TargetLevel) {
72+
if (FoldingLevel > MaxLevel || FoldingLevel > TargetLevel) {
73+
return;
74+
}
75+
76+
var Quotes = this;
77+
$(QuoteTree).each(function(i, el) {
78+
var ExamineQuote = $(el),
79+
FoldQuote;
80+
81+
if (FoldingLevel == TargetLevel) {
82+
ExamineQuote
83+
.addClass('QuoteFolded')
84+
.hide()
85+
.before(
86+
'<div class="QuoteFoldingWrapper"><a href="" class="QuoteFolding">' +
87+
gdn.definition('show previous quotes') +
88+
'</a></div>'
89+
);
90+
return;
91+
}
92+
93+
FoldQuote = ExamineQuote.children('.QuoteText').children('.Quote, .UserQuote');
94+
if (!FoldQuote.length) {
95+
return;
96+
}
97+
98+
Quotes.ExploreFold(FoldQuote, FoldingLevel + 1, MaxLevel, TargetLevel);
99+
});
100+
};
101+
102+
103+
// Get the currently active editor (last in focus).
104+
Gdn_Quotes.prototype.GetEditor = function () {
105+
var editor = $(this.currentEditor);
106+
if (!document.body.contains(this.currentEditor) || !editor.length) {
107+
// FIX: https://github.com/topcoder-platform/forums/issues/83
108+
editor = $('.richEditor-textWrap').first();
109+
}
110+
111+
return editor;
112+
};
113+
114+
115+
// Handle quote insertion clicks.
116+
Gdn_Quotes.prototype.Quote = function(ObjectID, QuoteLink) {
117+
if (!this.GetQuoteData(ObjectID)) {
118+
return;
119+
}
120+
121+
// FIX: https://github.com/topcoder-platform/forums/issues/83
122+
var ScrollY = $(this.GetEditor().get(0)).offset().top - 100;
123+
$('html,body').animate({scrollTop: ScrollY}, 800);
124+
};
125+
126+
127+
// Request the quote data.
128+
Gdn_Quotes.prototype.GetQuoteData = function(ObjectID) {
129+
this.AddSpinner();
130+
131+
$.getJSON(
132+
gdn.url('/discussion/getquote/' + ObjectID),
133+
{format: $('#Form_Format').val()},
134+
$.proxy(this.QuoteResponse, this)
135+
);
136+
return true;
137+
};
138+
139+
140+
// Overridable function.
141+
Gdn_Quotes.prototype.AddSpinner = function () {
142+
143+
};
144+
145+
146+
// Overridable function.
147+
Gdn_Quotes.prototype.RemoveSpinner = function () {
148+
149+
};
150+
151+
152+
// Handle a successful request for quote data.
153+
Gdn_Quotes.prototype.QuoteResponse = function(Data, Status, XHR) {
154+
gdn.inform(Data);
155+
156+
if (Data && Data.Quote.selector) {
157+
this.RemoveSpinner();
158+
} else {
159+
return;
160+
}
161+
162+
this.ApplyQuoteText(Data.Quote.body);
163+
};
164+
165+
166+
// Insert the quote text into the editor.
167+
Gdn_Quotes.prototype.ApplyQuoteText = function(QuoteText) {
168+
var Editor = this.GetEditor();
169+
170+
// First try and throw an event.
171+
Editor.trigger('appendHtml', QuoteText + '<br />');
172+
173+
QuoteText = QuoteText + '\n';
174+
Editor.val(Editor.val() + QuoteText);
175+
176+
// DEPRECATED: cleditor support
177+
if ($('div.cleditorMain').length) {
178+
Editor.val(Editor.val() + '<br/>');
179+
Editor.get(0).editor.updateFrame();
180+
}
181+
182+
Editor
183+
.focus()
184+
.trigger('autosize.resize');
185+
};
186+
187+
(function(window) {
188+
window.GdnQuotes = new Gdn_Quotes();
189+
window.GdnQuotes.Prepare();
190+
})(window);
191+
192+
$(document).on('contentLoad', function (e) {
193+
GdnQuotes.format(e.target);
194+
});

0 commit comments

Comments
 (0)