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