diff options
Diffstat (limited to 'askbot/media/js/live_search.js')
-rw-r--r-- | askbot/media/js/live_search.js | 143 |
1 files changed, 142 insertions, 1 deletions
diff --git a/askbot/media/js/live_search.js b/askbot/media/js/live_search.js index dfae8321..15180260 100644 --- a/askbot/media/js/live_search.js +++ b/askbot/media/js/live_search.js @@ -267,6 +267,7 @@ TagWarningBox.prototype.showWarning = function(){ */ var InputToolTip = function() { WrappedElement.call(this); + this._promptText = gettext('search or ask your question'); }; inherits(InputToolTip, WrappedElement); @@ -284,6 +285,10 @@ InputToolTip.prototype.dim = function() { this._element.addClass('dimmed'); }; +InputToolTip.prototype.setPromptText = function(text) { + this._promptText = text; +}; + InputToolTip.prototype.setClickHandler = function(handler) { this._clickHandler = handler; }; @@ -292,7 +297,7 @@ InputToolTip.prototype.createDom = function() { var element = this.makeElement('div'); this._element = element; element.addClass('input-tool-tip'); - element.html(gettext('search or ask your question')); + element.html(this._promptText); this.decorate(element); }; @@ -882,3 +887,139 @@ FullTextSearch.prototype.decorate = function(element) { $("form#searchForm").submit(me.makeFormSubmitHandler()); }; + +/** + * @constructor + */ +var TagSearch = function() { + WrappedElement.call(this); + this._isRunning = false; +}; +inherits(TagSearch, WrappedElement); + +TagSearch.prototype.getQuery = function() { + return $.trim(this._element.val()); +}; + +TagSearch.prototype.setQuery = function(val) { + this._element.val(val); +}; + +TagSearch.prototype.getSort = function() { + //todo: read it off the page + var link = $('.tabBar a.on'); + if (link.length === 1) { + var sort = link.attr('id').replace('sort_', ''); + if (sort === 'name' || sort === 'used') { + return sort; + } + } + return 'name'; +}; + +TagSearch.prototype.getIsRunning = function() { + return this._isRunning; +}; + +TagSearch.prototype.setIsRunning = function(val) { + this._isRunning = val; +}; + +TagSearch.prototype.renderResult = function(html) { + this._contentBox.html(html); +}; + +TagSearch.prototype.runSearch = function() { + var data = { + 'query': this.getQuery(), + 'sort': this.getSort(), + 'page': '1' + }; + var me = this; + $.ajax({ + dataType: 'json', + data: data, + cache: false, + url: askbot['urls']['tags'], + success: function(data) { + if (data['success']) { + me.renderResult(data['html']); + me.setIsRunning(false); + } + }, + error: function() { me.setIsRunning(false); } + }); + me.setIsRunning(true); +}; + +TagSearch.prototype.getToolTip = function() { + return this._toolTip; +}; + +TagSearch.prototype.makeKeyUpHandler = function() { + var me = this; + return function(evt) { + var keyCode = getKeyCode(evt); + if (me.getIsRunning() === false) { + me.runSearch(); + } + }; +}; + +TagSearch.prototype.makeKeyDownHandler = function() { + var me = this; + var xButton = this._xButton; + return function(evt) { + var query = me.getQuery(); + var keyCode = getKeyCode(evt); + var toolTip = me.getToolTip(); + if (keyCode === 27) {//escape + me.setQuery(''); + toolTip.show(); + xButton.hide(); + return; + } + if (keyCode === 8 || keyCode === 48) {//del or backspace + if (query.length === 1) { + toolTip.show(); + xButton.hide(); + } + } else { + toolTip.hide(); + xButton.show(); + } + }; +}; + +TagSearch.prototype.reset = function() { + if (this.getIsRunning() === false) { + this.setQuery(''); + this._toolTip.show(); + this._xButton.hide(); + this.runSearch(); + this._element.focus(); + } +}; + +TagSearch.prototype.decorate = function(element) { + this._element = element; + this._contentBox = $('#ContentLeft'); + this._xButton = $('input[name=reset_query]'); + element.keyup(this.makeKeyUpHandler()); + element.keydown(this.makeKeyDownHandler()); + + var me = this; + this._xButton.click(function(){ me.reset() }); + + var toolTip = new InputToolTip(); + toolTip.setPromptText(askbot['data']['tagSearchPromptText']); + toolTip.setClickHandler(function() { + element.focus(); + }); + element.after(toolTip.getElement()); + //below is called after getElement, b/c element must be defined + if (this.getQuery() !== '') { + toolTip.hide();//hide if search query is not empty + } + this._toolTip = toolTip; +}; |