Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Allow a Trigger Function for the dropdown API #19

Open
sourabhdesai opened this issue Dec 28, 2015 · 2 comments
Open

Allow a Trigger Function for the dropdown API #19

sourabhdesai opened this issue Dec 28, 2015 · 2 comments

Comments

@sourabhdesai
Copy link

Right now, the API only allows for the trigger to be a string or a RegEx. For my application, I want the trigger to fire every time the input is a prefix for a word within a dynamic list of words. As a workaround I, I created an object that matches the RegEx API that the current smart-area code uses.

To show what I mean, I have this:

var WordTrigger = function() {
    this.words = [];
    WordTrigger.prototype.exec = (text) => {

        var lastSpaceIdx = lastWhitespaceIndex(text);
        var currToken = text.substring(lastSpaceIdx >= 0 ? lastSpaceIdx+1 : 0).toLowerCase();

        if (currToken === '') {
          return null;
        }

        var results = setFilter(this.bigramDist.words, word => _.startsWith(word, currToken) && word !== currToken);
        var completions = results.map(result => result.substring(currToken.length));

        if (_.isEmpty(results)) {
          return null;
        }

        var retVal = [''];
        retVal.index = text.length;
        retVal.input = text;
        // The above 3 lines are just to appease the smart-area code
        // real results are stored in the 'results' property
        retVal.results = _.zipWith(results, completions, function (result, completion) {
            return {
                'display': result,
                'item': completion
            };
        });

        return retVal;

    };

    WordTrigger.prototype.addWord = (word) => {
        this.words.push(word);
    };
};

And in my areaConfig I just have this:

'trigger': new WordTrigger(), ...

This isn't a very clean workaround and I feel like it would be simple enough to just add support for trigger functions.

@sourabhdesai sourabhdesai changed the title Allow a Trigger Function to the dropdown API Allow a Trigger Function for the dropdown API Dec 28, 2015
@aurbano
Copy link
Owner

aurbano commented Jan 21, 2016

Very interesting, I hadn't really thought of this use case but it definitely makes sense, I'll try to look into it this weekend.

Also, sorry for not getting back to you before, somehow missed this.

@sourabhdesai
Copy link
Author

That'd be great! I found a hacky way to get what I wanted but I'm sure someone else would appreciate this feature!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants