Files: 15db93aa5f5d58eb433e80d31bd6e6615d70a109 / modules_core / suggest-box.js
1053 bytesRaw
1 | const fs = require('fs') |
2 | const h = require('../h') |
3 | const onload = require('on-load') |
4 | const { para } = require('cont') |
5 | const Suggest = require('suggest-box') |
6 | |
7 | exports.needs = {} |
8 | |
9 | exports.gives = { |
10 | build_suggest_box: true, |
11 | css: true |
12 | } |
13 | |
14 | exports.create = function (api) { |
15 | return { |
16 | build_suggest_box, |
17 | css: () => fs.readFileSync(__filename.replace(/js$/, 'css'), 'utf8') // NOTE css |
18 | } |
19 | |
20 | function build_suggest_box (inputNode, asyncSuggesters, opts = {}) { |
21 | function suggester (inputText, cb) { |
22 | para(asyncSuggesters(inputText)) |
23 | ((err, ary) => { |
24 | if(err) return cb(err) |
25 | |
26 | var suggestions = ary.filter(Boolean).reduce((a, b) => a.concat(b), []) |
27 | cb(null, suggestions) |
28 | }) |
29 | } |
30 | |
31 | var suggestBox |
32 | onload(inputNode, (el) => { |
33 | suggestBox = Suggest(el, suggester, opts) |
34 | }) |
35 | |
36 | // HACK (mix) : onload is needed because Suggest demands a parent node. |
37 | // I've chosen this over forcing users to pass a callback |
38 | return { |
39 | complete: () => suggestBox.complete() |
40 | } |
41 | } |
42 | } |
43 | |
44 |
Built with git-ssb-web