Created
October 5, 2016 21:19
-
-
Save ddpunk/b9f3364ad708df05ee46f2d030ab250b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define("app/view/question/lookup_bar", ["w2.quora", "interface/jquery", "view/selector", "shared/window_manager", "shared/events/index", "shared/siteuri", "shared/cookies", "app/view/site_header/logged_in", "unified_view/toggle"], function nn(require, exports, module) { | |
var e = require("w2.quora").Component, | |
$ = require("interface/jquery")._jQuery, | |
t = require("view/selector").WithServerCallMessageMixin, | |
i = require("shared/window_manager"), | |
n = require("shared/events/index"), | |
s = require("shared/siteuri").URI, | |
o = require("shared/cookies"), | |
r = require("app/view/site_header/logged_in").LoggedInSiteHeaderContext, | |
a = require("unified_view/toggle"); | |
var d = null; | |
exports.LookupBarSelector = t.extend({ | |
onLoad: function() { | |
var e = this; | |
d = e; | |
this._super.apply(this, arguments); | |
e.context = e.getContext(r); | |
if (e.logged_in) { | |
e.context.set("question_query_empty", this.isQueryEmpty()) | |
} | |
this.$results_wrapper = this.$id("@results_wrapper"); | |
this.$results_container.removeClass("hidden"); | |
e.displayedRevision = 0; | |
e.currentRevision = 0; | |
e.currentOutstandingRequests = 0; | |
e.lastRevisionNeedsUpdate = false; | |
e.cache = {}; | |
e.cacheSize = 0; | |
e.focused = true; | |
e.shouldSimScroll = true; | |
e.$wrapper = e.$id("@wrapper"); | |
e.shouldHighlightOnFocus = !e.should_autofocus; | |
e.editingQuestionDetails = false; | |
e.$(document).mousedown(function(t) { | |
if (e.logged_in) { | |
e.context.set("lookup_bar_focused", true) | |
} | |
if (!e.$wrapper.is(t.target) && e.$wrapper.has(t.target).length === 0 && !$(t.target).closest(".SiteHeader.search_focused")[0] && !$(t.target).closest(".ClearQuestionDetailsConfirmationModal")[0]) { | |
e.focused = false; | |
e.setHighlight(false); | |
e.showResults(false); | |
if (e.askMode === "forced") { | |
if (e.isQueryEmpty()) { | |
e.setAskMode(false); | |
e.setQuery(""); | |
} else { | |
e.askMode = true | |
} | |
} | |
} else if (e.$id("@details_wrapper").has(t.target).length > 0) { | |
e.showResults(false) | |
} else { | |
e.shouldSimScroll = false; | |
e.focused = true; | |
if (e.query.trim().length !== 0) { | |
if (!e.hasResults) { | |
e._setQuery(e.$input.val()) | |
} else { | |
e.showResults(true) | |
} | |
} else { | |
e.showResults(false) | |
} | |
} | |
}); | |
e.$input.click(function() { | |
if (!e.shouldHighlightOnFocus) { | |
e.setHighlight(true) | |
} | |
if (e._queryIsPunctation(e.query) && e.askMode === "forced") { | |
var t = e.opening_question_punctuation.length; | |
e.$input[0].setSelectionRange(t, t) | |
} | |
}); | |
e.askMode = false; | |
e.showingInterstitial = false; | |
e.source = null; | |
e.currentSession = 0; | |
this.handleEvent(n.Types.LOOKUP_BAR_ASK_QUESTION, function(t) { | |
e.ask(t.payload.isPrivate) | |
}); | |
this.handleEvent(n.Types.LOOKUP_BAR_QUESTION_DETAILS_STATE, function(t) { | |
if (t.payload.state == "focused") { | |
e.showResults(false) | |
} else if (t.payload.state == "shown") { | |
e.$id("@details_wrapper").removeClass("hidden"); | |
e.editingQuestionDetails = true | |
} else if (t.payload.state == "hidden") { | |
e.$id("@details_wrapper").addClass("hidden"); | |
e.editingQuestionDetails = false; | |
e.$input.focus() | |
} | |
}) | |
}, | |
inputFocusHandler: function() { | |
this._super.apply(this, arguments); | |
if (this.shouldHighlightOnFocus) { | |
this.setHighlight(true); | |
if (!this.isQueryEmpty() && this.hasResults && !this.editingQuestionDetails) { | |
this.showResults(true) | |
} | |
} | |
}, | |
inputBlurHandler: function() { | |
this._super.apply(this, arguments); | |
if (this.logged_in) { | |
var e = this; | |
if (document.activeElement.id != e.$input.attr("id") && !e.focused) { | |
e.setHighlight(false); | |
e.shouldHighlightOnFocus = true | |
} | |
} | |
}, | |
handleEscKey: function() { | |
this._super.apply(this, arguments); | |
if (this.logged_in) { | |
this.setHighlight(false) | |
} | |
}, | |
capitalize: function(e) { | |
if (!e) { | |
return e | |
} | |
e = e.charAt(0).toUpperCase() + e.slice(1); | |
return e | |
}, | |
setInputVal: function(e, t) { | |
var i = this.$input[0].selectionStart, | |
n = this.$input[0].selectionEnd; | |
if (t) { | |
i += t; | |
n += t | |
} | |
this.$input.val(e); | |
if (e.length > 0) { | |
this.$input[0].setSelectionRange(i, n) | |
} | |
}, | |
_queryIsPunctation: function(e) { | |
return e === this.opening_question_punctuation + this.closing_question_punctuation || e === this.opening_question_punctuation && e.length > 0 || e === this.closing_question_punctuation && e.length > 0; | |
}, | |
_setQuery: function(e, t) { | |
var i = this.capitalize(e); | |
if (t || this.askMode && i !== e) { | |
var n = ""; | |
if ((e || t) && e.indexOf(this.opening_question_punctuation) == -1) { | |
n = this.opening_question_punctuation | |
} | |
var s = ""; | |
if ((e || t) && e.indexOf(this.closing_question_punctuation) == -1) { | |
s = this.closing_question_punctuation | |
} | |
e = n + i + s; | |
this.setInputVal(e, n.length) | |
} else if (this.askMode && this.askMode !== "forced" && this._queryIsPunctation(e)) { | |
e = ""; | |
this.setInputVal(e) | |
} | |
if (this.length_limit && this.logged_in) { | |
var o = e.length > this.length_limit; | |
this.context.set("question_title_over_limit", o) | |
} | |
this._super.apply(this, [e]) | |
}, | |
addToCache: function(e, t) { | |
if (!(e in this.cache)) { | |
this.cacheSize++ | |
} | |
this.cache[e] = t; | |
if (this.cacheSize > this.max_cache_size) { | |
var i = this.currentRevision + 1; | |
var n = null; | |
for (var s in this.cache) { | |
if (this.cache[s][1] < i) { | |
i = this.cache[s][1]; | |
n = s | |
} | |
} | |
this.cacheSize--; | |
delete this.cache[n] | |
} | |
}, | |
updateResults: function() { | |
var e = this; | |
var t = e.query.trim(); | |
if (e.askMode === "forced" && (e._queryIsPunctation(t) || t === "")) { | |
e.showInterstitial("ask_question"); | |
return; | |
} | |
var i = { | |
query: e.query | |
}; | |
var n = e.getExtraKwargs(); | |
for (var s in n) { | |
i[s] = n[s] | |
} | |
var o = JSON.stringify(i); | |
var r = e.currentRevision; | |
e.lastRevisionNeedsUpdate = false; | |
if (t.length === 0) { | |
e.displayedRevision = r; | |
e.setAskMode(false); | |
e.showResults(false); | |
e.currentSession++ | |
} else if (o in e.cache) { | |
e.currentOutstandingRequests++; | |
e.resultsServerCallHandler(r, e.cache[o][0], this.showingInterstitial) | |
} else { | |
var a = function() { | |
if (e.query !== i.query) { | |
return | |
} | |
if (e.currentOutstandingRequests < e.max_concurrent_requests) { | |
i.parent_cid = e.cid; | |
i.selector_id = e.selector_id; | |
e.currentOutstandingRequests++; | |
var t = e.showingInterstitial; | |
e.serverCall("results_content_for").kwargs(i).sendJsData(e.shouldSendJSData).success(function(i) { | |
e.resultsServerCallHandler(r, i, o, t) | |
}).except(function() { | |
e.resultsServerCallHandler(r, null, o, t) | |
}).error(function() { | |
e.resultsServerCallHandler(r, null, o, t) | |
}).send() | |
} else { | |
e.lastRevisionNeedsUpdate = true | |
} | |
}; | |
if (!e.queryTypingDelay) { | |
a() | |
} else { | |
setTimeout(a, e.queryTypingDelay) | |
} | |
} | |
}, | |
getExtraKwargs: function() { | |
return { | |
ask_mode: this.askMode, | |
current_session: this.currentSession | |
} | |
}, | |
resultsServerCallHandler: function(e, t, i, n) { | |
this.currentOutstandingRequests--; | |
this.hasResults = true; | |
if (e < this.displayedRevision) { | |
return | |
} | |
if (n) { | |
this.showingInterstitial = false; | |
this.$id("@ask_mode_interstitial").addClass("hidden"); | |
this.$id("@results").removeClass("hidden") | |
} | |
if (t !== null) { | |
this.$results.forgetContents(); | |
this.$results.setContent(t); | |
this.displayedRevision = e; | |
this.addToCache(i, [t, e]) | |
} | |
this.showResults(true); | |
if (this.lastRevisionNeedsUpdate) { | |
this.updateResults() | |
} | |
}, | |
queryDidChange: function() { | |
this.currentRevision++; | |
if (this.logged_in) { | |
this.context.set("question_query_empty", this.isQueryEmpty()); | |
this.context.set("force_enable_button", true) | |
} | |
this._super.apply(this, arguments) | |
}, | |
isQueryEmpty: function() { | |
return this.query.trim() === "" || this._queryIsPunctation(this.query.trim()) | |
}, | |
showResults: function(e) { | |
var t = this; | |
if (e && t.focused && !t.isQueryEmpty()) { | |
t.$results_wrapper.removeClass("hidden"); | |
t.$results_wrapper.removeClass("fade_out"); | |
t.setHighlight(true) | |
} else { | |
t.$results_wrapper.addAnimationClass("fade_out", function() { | |
if (!t.$results_wrapper.hasClass("hidden")) { | |
t.$results_wrapper.addClass("hidden") | |
} | |
}) | |
} | |
}, | |
resultWasSelected: function(e, t) { | |
var i = this; | |
var n = this.child("results").child(e.data.cname); | |
n.processAction(e.data.action_mousedown); | |
if (e.ty === "search") { | |
i.goToSearch(t) | |
} else { | |
if (e.data.qid !== -1) { | |
o.setMessage("look", [e.data.qid, true]) | |
} | |
if (i.query.length < 140) { | |
o.setCookie("qsu-q", JSON.stringify(i.query), { | |
expires: 1 | |
}) | |
} | |
if (t) { | |
window.open(e.data.url) | |
} else { | |
window.location.href = e.data.url | |
} | |
} | |
}, | |
hideInterstitials: function() { | |
this.showingInterstitial = false; | |
this.$id("@ask_mode_interstitial").addClass("hidden"); | |
this.$id("@results").removeClass("hidden") | |
}, | |
showInterstitial: function(e) { | |
this.hideInterstitials(); | |
this.showingInterstitial = true; | |
this.$id("@ask_mode_interstitial").removeClass("hidden"); | |
this.$id("@results").addClass("hidden"); | |
var t = this.interstitials[e][0]; | |
var i = this.interstitials[e][1]; | |
this.$id("@interstitial_title").text(t); | |
this.$id("@interstitial_text").text(i) | |
}, | |
setHighlight: function(e, t) { | |
var i = this.parent().parent().$("#@body_blur"); | |
if (e) { | |
i.addClass("lookup_bar_modal_overlay"); | |
i.removeClass("closing"); | |
if (this.logged_in) { | |
this.context.set("force_enable_button", true) | |
} | |
this.emitEvent({ | |
type: n.Types.SITE_HEADER_SEARCH_FOCUSED, | |
payload: { | |
focused: true | |
} | |
}); | |
var s = this.$input.outerWidth(); | |
this.$results_wrapper.width(s - 2); | |
this.focused = true | |
} else { | |
i.addAnimationClass("closing", function() { | |
if (i.hasClass("lookup_bar_modal_overlay")) { | |
i.removeClass("lookup_bar_modal_overlay") | |
} | |
}); | |
this.emitEvent({ | |
type: n.Types.SITE_HEADER_SEARCH_FOCUSED, | |
payload: { | |
focused: false | |
} | |
}); | |
this.focused = false | |
} | |
if (this.nextHighlightCallback) { | |
this.nextHighlightCallback() | |
} | |
this.nextHighlightCallback = t | |
}, | |
setAskMode: function(e, t) { | |
this.askMode = e; | |
if (this.query.trim() || e) { | |
this._setQuery(this.query.trim(), true) | |
} | |
if (e) { | |
this.$input.focus(); | |
if (t) { | |
this.showInterstitial(t) | |
} else if (this.query.trim().length >= 10 && this.query.split(" ").length >= 3) { | |
this.showInterstitial("good") | |
} else { | |
this.showInterstitial("bad") | |
} | |
this.logMiscAction("LookupBarSetAskModeClickthrough"); | |
this.lastRevisionNeedsUpdate = false; | |
this.focused = true; | |
this.showResults(true) | |
} | |
}, | |
logMiscAction: function(e) { | |
var t = this.child("results"); | |
if (!t) { | |
return | |
} | |
var i = t.child("search"); | |
if (!i) { | |
return | |
} | |
i.processAction(e) | |
}, | |
submit: function(e) { | |
if (!this.logged_in) { | |
this.logMiscAction("LookupBarPressEnterSearch"); | |
this.goToSearch(e) | |
} else { | |
if (this.should_go_to_search_on_enter) { | |
this.logMiscAction("LookupBarPressEnterSearch"); | |
this.goToSearch(e) | |
} else { | |
this.logMiscAction("LookupBarPressEnterNothing") | |
} | |
} | |
}, | |
goToSearch: function(e) { | |
if (this.query.trim()) { | |
var t = s("/search?q=" + encodeURIComponent(this.query).replace(/%20/g, "+") + this.keepFilters); | |
if (e) { | |
window.open(t.unicode()) | |
} else { | |
window.location.href = t.unicode() | |
} | |
} | |
}, | |
ask: function(e) { | |
var t = this; | |
if (t.hasPendingServerCall()) { | |
return | |
} | |
this.context.set("force_enable_button", false); | |
var i = t.getQuestionDetails(); | |
t.makeServerCall(t.serverCall("add_question").kwargs({ | |
input_text: t.query, | |
is_anon: e, | |
question_details: i, | |
source: t.source | |
})) | |
}, | |
getQuestionDetails: function() { | |
return this.context.get("question_details_text_serialized") | |
}, | |
setSource: function(e) { | |
var t = this; | |
t.source = e | |
}, | |
updateServerMessage: function(e, t) { | |
var n = this; | |
if (!e) { | |
if (t.type === "confirm_email") { | |
n.createModal("email_confirmation_dialog_contents").onSubmit(function() { | |
n.serverCall("resend_email_confirmation").send() | |
}).show(); | |
return false | |
} | |
n.logMiscAction("LookupBarAddQuestionFail"); | |
n.context.set("force_enable_button", true); | |
n.$input.focus(); | |
if (t && t["message"]) { | |
this.$id("@server_message_title").text(t["message"]["title"]); | |
if (t["message"]["note"]) { | |
this.$id("@server_message_note").text(t["message"]["note"]) | |
} | |
return true | |
} | |
return false | |
} else { | |
if (t["url"]) { | |
n.logMiscAction("LookupBarAddQuestionSuccess"); | |
i.navigateTo(t["url"]) | |
} | |
return false | |
} | |
} | |
}); | |
exports.LookupBarSiteHeaderAddQuestion = e.extend({ | |
onLoad: function() { | |
var e = this; | |
e.$askButton = e.$id("@submit_question"); | |
e.context = e.getContext(r); | |
e.context.on("question_details_over_limit", function(t, i) { | |
e.enableButton(!i) | |
}); | |
e.context.on("question_title_over_limit", function(t, i) { | |
e.enableButton(!i) | |
}); | |
e.context.on("force_enable_button", function(t, i) { | |
e.enableButton(i); | |
}); | |
e.$askButton.click(function(t) { | |
t.preventDefault(); | |
e.emitEvent({ | |
type: n.Types.LOOKUP_BAR_ASK_QUESTION, | |
payload: { | |
isPrivate: e.getIsPrivate() | |
} | |
}) | |
}); | |
e.$id("@add_details").click(function(t) { | |
t.preventDefault(); | |
e.showQuestionDetailsEditor(true) | |
}); | |
e.$id("@remove_details").click(function(t) { | |
t.preventDefault(); | |
if (e.context.get("question_details_is_empty")) { | |
e.showQuestionDetailsEditor(false) | |
} else { | |
e.createModal("load_clear_confirmation").onSubmit(function() { | |
e.showQuestionDetailsEditor(false); | |
e.context.set("question_details_cleared", true) | |
}).show() | |
} | |
}); | |
e.context.on("question_details_in_focus", function(t, i) { | |
if (i) { | |
e.emitEvent({ | |
type: n.Types.LOOKUP_BAR_QUESTION_DETAILS_STATE, | |
payload: { | |
state: "focused" | |
} | |
}) | |
} | |
}) | |
}, | |
showQuestionDetailsEditor: function(e) { | |
if (e) { | |
this.$id("@add_details").addClass("hidden"); | |
this.$id("@remove_details").removeClass("hidden"); | |
this.context.set("question_details_in_focus", true) | |
} else { | |
this.$id("@remove_details").addClass("hidden"); | |
this.$id("@add_details").removeClass("hidden") | |
} | |
this.emitEvent({ | |
type: n.Types.LOOKUP_BAR_QUESTION_DETAILS_STATE, | |
payload: { | |
state: e ? "shown" : "hidden" | |
} | |
}) | |
}, | |
getIsPrivate: function() { | |
return this.$id("@anon_checkbox").prop("checked") || false | |
}, | |
enableButton: function(e) { | |
if (this.context.get("question_title_over_limit") || this.context.get("question_details_over_limit")) { | |
this.$askButton.addClass("fake_disabled") | |
} else if (this.context.get("question_query_empty")) { | |
this.$askButton.addClass("disabled") | |
} else { | |
if (e) { | |
this.$askButton.removeClass("disabled"); | |
this.$askButton.removeClass("fake_disabled") | |
} else { | |
this.$askButton.addClass("disabled") | |
} | |
} | |
} | |
}); | |
var l = []; | |
exports.AddQuestionLookupBarButton = e.extend({ | |
setAllEnabled: function(e) { | |
l.forEach(function(t) { | |
t.setEnabled(e) | |
}) | |
}, | |
setEnabled: function(e) { | |
if (e) { | |
this.$id("@dialog_link").removeClass("disabled") | |
} else { | |
this.$id("@dialog_link").addClass("disabled") | |
} | |
}, | |
onLoad: function() { | |
var e = this; | |
l.push(e); | |
this.$id("@dialog_link").click(function() { | |
var t = d; | |
var i = "button"; | |
if (e.source) { | |
i = "answer" | |
} else { | |
i = "ask_question" | |
} | |
t.setAskMode("forced", i); | |
e.setAllEnabled(false); | |
t.setHighlight(true, function() { | |
e.setAllEnabled(true); | |
}); | |
t.setSource(e.source); | |
e.serverCall("log_click").kwargs({ | |
source: e.source | |
}).send(); | |
return false | |
}) | |
} | |
}); | |
exports.LazyAskBarDetails = a.ToggleBase.extend({ | |
onLoad: function() { | |
this._super.apply(this, arguments); | |
var e = this; | |
e.context = e.getContext(r); | |
e.context.on("lookup_bar_focused", function(t, i) { | |
if (i && !t) { | |
e.loadAskBarDetails() | |
} | |
}) | |
}, | |
loadAskBarDetails: function() { | |
this.fetch(); | |
this.triggerSwitch() | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment