Commit d6126ef1 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Move delayed search functionality to GuideSearch class.

parent 69176cf1
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -13,6 +13,29 @@ Ext.define("Docs.GuideSearch", {
        return !!Docs.data.guideSearch.url;
    },

    /**
     * Invokes #search after a specified delay.
     *
     * @param {String} term  The query string to search for
     * @param {Function} callback Function to call with an array of search results.
     * @param {Object} scope Scope for the function.
     * @param {Number} delay Milliseconds to wait before starting.
     */
    deferredSearch: function(term, callback, scope, delay) {
        // When new search term comes in, cancel the previous delayed search.
        clearTimeout(this.timeout);

        var timeout = this.timeout = Ext.Function.defer(function() {
            this.search(term, function(results) {
                // When new search is already started, don't show the
                // results of old search.
                if (timeout === this.timeout) {
                    callback.call(scope, results);
                }
            }, this);
        }, delay, this);
    },

    /**
     * Peforms the search remotely, then calls the given function.
     *
+3 −12
Original line number Diff line number Diff line
@@ -151,19 +151,10 @@ Ext.define('Docs.controller.Search', {
        }
    },

    // Wait a bit before launching guides search.
    // When new search term comes in, cancel the previous delayed search.
    // When the search had already started, don't show the results when it finishes.
    guideSearch: function(term) {
        clearTimeout(this.guideSearchTimeout);

        var timeout = this.guideSearchTimeout = Ext.Function.defer(function() {
            Docs.GuideSearch.search(term, function(guideResults) {
                if (timeout === this.guideSearchTimeout) {
        Docs.GuideSearch.deferredSearch(term, function(guideResults) {
            this.basicSearch(term, guideResults);
                }
            }, this);
        }, this.guideSearchDelay, this);
        }, this, this.guideSearchDelay);
    },

    basicSearch: function(term, guideResults) {