research & implement filter methods (first draft)

pull/1/head
serapath 7 years ago
parent 741df25034
commit e40d5e5991
  1. 118
      src/app/panels/terminal.js

@ -1,4 +1,4 @@
/* global Node */ /* global Node, requestAnimationFrame */
var yo = require('yo-yo') var yo = require('yo-yo')
var csjs = require('csjs-inject') var csjs = require('csjs-inject')
var javascriptserialize = require('javascript-serialize') var javascriptserialize = require('javascript-serialize')
@ -149,6 +149,8 @@ class Terminal {
}) })
self._commands = {} self._commands = {}
self.commands = {} self.commands = {}
self._JOURNAL = []
self._jobs = []
self._INDEX = {} self._INDEX = {}
self._INDEX.all = [] self._INDEX.all = []
self._INDEX.allMain = [] self._INDEX.allMain = []
@ -427,44 +429,71 @@ class Terminal {
} }
updateJournal (filterEvent) { updateJournal (filterEvent) {
var self = this var self = this
var commands = self.data.activeFilters.commands
var value = filterEvent.value var value = filterEvent.value
if (filterEvent.type === 'select') { if (filterEvent.type === 'select') {
self.data.activeFilters.commands[value] = true commands[value] = true
// @TODO: add all items with `value === item.cmd` if (!self._INDEX.commandsMain[value]) return
self._INDEX.commandsMain[value].forEach(item => {
item.root.steps.forEach(item => {
item.hide = false
self._JOURNAL[item.gidx] = item
})
item.hide = false
self._JOURNAL[item.gidx] = item
})
} else if (filterEvent.type === 'deselect') { } else if (filterEvent.type === 'deselect') {
self.data.activeFilters.commands[value] = false commands[value] = false
// @TODO: remove all items with `value === item.cmd` if (!self._INDEX.commandsMain[value]) return
self._INDEX.commandsMain[value].forEach(item => {
item.root.steps.forEach(item => {
item.hide = true
self._JOURNAL[item.gidx] = item
})
item.hide = true
self._JOURNAL[item.gidx] = item
})
} else if (filterEvent.type === 'search') { } else if (filterEvent.type === 'search') {
self.data.activeFilters.input = value var query = self.data.activeFilters.input = value
// @TODO: filter all active items with not `match(value, item.args)` var items = self._JOURNAL
for (var gidx = 0, len = items.length; gidx < len; gidx++) {
// @TODO: implement a `match(query, args)` function var item = items[gidx]
if (item) {
// make use of: var show = match(item.args, query)
// self._INDEX.all[item.gidx] item.hide = !show
// self._INDEX.allMain
// self._INDEX.commands
// self._INDEX.commandsMain
} }
} }
_keep (item) { }
var df = document.createDocumentFragment()
// var children = self._JOURNAL.map(item => !item.hide && item.el)
self._JOURNAL.forEach(item => {
if (!item.hide && item.el) df.appendChild(item.el)
})
requestAnimationFrame(function updateDOM () {
self._view.journal.innerHTML = ''
self._view.journal.appendChild(df)
})
}
_shouldAdd (item) {
var self = this var self = this
if (self.data.activeFilters.commands[item.root.cmd]) { if (self.data.activeFilters.commands[item.root.cmd]) {
var query = self.data.activeFilters.input var query = self.data.activeFilters.input
// @TODO: filter item if not `match(value, item.args)` var args = item.args
// @TODO: implement a `match(query, args)` function return match(args, query)
if (item.args === query) return true
} }
} }
_appendItem (item) { _appendItem (item) {
var self = this var self = this
var { el, gidx } = item var { el, gidx } = item
if (self._keep(item)) { self._JOURNAL[gidx] = item
self._view.journal.appendChild(yo` if (!self._jobs.length) {
<div data-gidx=${gidx} class=${css.block}>${el}</div> requestAnimationFrame(function updateTerminal () {
`) self._jobs.forEach(el => self._view.journal.appendChild(el))
self.scroll2bottom() self.scroll2bottom()
self._jobs = []
})
} }
self._jobs.push(el)
} }
scroll2bottom () { scroll2bottom () {
var self = this var self = this
@ -498,7 +527,7 @@ class Terminal {
var command = self._commands[cmd] var command = self._commands[cmd]
scopedCommands[cmd] = function _command () { scopedCommands[cmd] = function _command () {
var args = [...arguments] var args = [...arguments]
command(args, scopedCommands, el => append(cmd, args, el)) command(args, scopedCommands, el => append(cmd, args, blockify(el)))
} }
}) })
return scopedCommands return scopedCommands
@ -531,10 +560,10 @@ class Terminal {
item.idx = self._INDEX.commands[cmd].push(item) - 1 item.idx = self._INDEX.commands[cmd].push(item) - 1
item.step = steps.push(item) - 1 item.step = steps.push(item) - 1
item.args = params item.args = params
self._appendItem(item) if (self._shouldAdd(item)) self._appendItem(item)
} }
var scopedCommands = self._scopeCommands(append) var scopedCommands = self._scopeCommands(append)
command(args, scopedCommands, el => append(null, args, el)) command(args, scopedCommands, el => append(null, args, blockify(el)))
} }
var help = typeof command.help === 'string' ? command.help : [ var help = typeof command.help === 'string' ? command.help : [
`// no help available for:`, `// no help available for:`,
@ -568,4 +597,41 @@ function domTerminalFeatures (self) {
} }
} }
function fuzzysearch (needle, haystack) {
var hlen = haystack.length
var nlen = needle.length
if (nlen > hlen) return
if (nlen === hlen) return needle === haystack
outer: for (var i = 0, j = 0; i < nlen; i++) { // eslint-disable-line
var nch = needle.charCodeAt(i)
while (j < hlen) if (haystack.charCodeAt(j++) === nch) continue outer // eslint-disable-line
return
}
return true
}
function findDeep (object, fn, found = { break: false, value: undefined }) {
if (typeof object !== 'object' || object === null) return
for (var i in object) {
if (found.break) break
var el = object[i]
if (!fn(el, i, object)) findDeep(el, fn, found)
else if (found.break = true) return found.value = el // eslint-disable-line
}
return found.value
}
function match (args, query) {
return findDeep(args, function check (value, key) {
if (value === undefined || value === null) return false
if (typeof value === 'function') return false
if (typeof value === 'object') return false
return fuzzysearch(query, String(value))
})
}
function blockify (el) {
return yo`<div class=${css.block}>${el}</div>`
}
module.exports = Terminal module.exports = Terminal

Loading…
Cancel
Save