Workflowy computable values

Compute things with sublists and display values on an item name.

Tính đến 18-08-2016. Xem phiên bản mới nhất.

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greasyfork.org/scripts/22313/142414/Workflowy%20computable%20values.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

    var parseSelector = window.Parser.parse

    window.funcs = []
    window.nodeCache = {}
    var observer

    // observe dom mutations to make it run every time an item is opened / closed
    function start () {
        GM_addStyle(`
.project .content {
  display: inline-block !important;
}

.project .result {
  display: inline;
  color: blue;
  margin-left: 10px;
}
        `)


        document.body.addEventListener('click', function (e) {
            if (e.target.id === 'expandButton') {
                let restrictTo = e.target.parentNode.parentNode.getAttribute('projectid')
                setTimeout(function () {
                    itemOpenedOrClosed(restrictTo)
                }, 1000)
            }
        })

        itemOpenedOrClosed()
    }

    function itemOpenedOrClosed (restrictTo) {
        for (let i = 0; i < funcs.length; i++) {
            let script = funcs[i]
            execScript(script, restrictTo)
        }
    }

    function execScript ({script, selector, id, color}, restrictTo) {
        let items = findItems(selector)
        for (let i = 0; i < items.length; i++) {
            let item = items[i]
            if (restrictTo && item.getAttribute('projectid') !== restrictTo) {
                continue
            }
            execForItem(item, script, id, color)
        }
    }

    function execForItem (item, fun, id, color) {
        var children = []
        let par = item.querySelector('.children')
        for (let p = 0; p < par.children.length; p++) {
            if (par.children[p].classList.contains('project')) {
                children.push(par.children[p])
            }
        }

        var args = []
        for (let i = 0; i < children.length; i++) {
            let child = children[i]
            arg = nodeToArg(child)
            args.push(arg)
        }

        var result = item.querySelector('.result.script-' + id)
        if (!result) {
            let content = item.querySelector('.name .content')
            result = document.createElement('span')
            result.className = 'result script-' + id
            result.title = id
            result.style.color = color
            content.parentNode.insertBefore(result, content.nextSibling)
        }
        let resultvalue = fun(args, nodeToArg(item))
        result.innerHTML = (resultvalue || '').toString()
    }

    function nodeToArg (node) {
        let name = node.querySelector('.name .content').innerText.trim()
        let note = node.querySelector('.notes').innerText.trim()

        var arg = {name, note}

        let results = node.querySelector('.name').querySelectorAll('.result')
        for (let r = 0; r < results.length; r++) {
            let result = results[r]
            arg[result.classList.item(1)] = result.innerText.trim()
        }

        return arg
    }

    function findItems (selector) {
        let parsed = parseSelector(selector)
        var items = null
        for (let l = 0; l < parsed.length; l++) {
            let layer = parsed[l]
            items = select(layer, items)
        }
        return items || []
    }

    function select (layer, base) {
        if (!base) {
            base = document.querySelectorAll('.mainTreeRoot')
        }

        let [connector, selector] = layer
        return getChildren(base, selector, connector === 'directchild')
    }

    function getChildren (base, selector, onlydirect=false) {
        var filter
        switch (selector.type) {
            case 'id':
                filter = node =>
                node.getAttribute('projectid') === selector.val ||
                    node.querySelector('.bullet').href.split('#/')[1] === selector.val
                break
            case 'regex':
                filter = node =>
                node.querySelector('.name .content').innerText.search(selector.val) !== -1
                break
            case 'name':
                filter = node =>
                node.querySelector('.name .content').innerText.trim() === selector.val
                break
            case 'any':
                filter = () => true
                break
            default:
                throw new Error('INVALID SELECTOR: ', selector)
        }

        var children = []
        for (let i = 0; i < base.length; i++) {
            if (onlydirect) {
                let par = base[i].querySelector('.children')
                for (let p = 0; p < par.children.length; p++) {
                    if (par.children[p].classList.contains('project') && par.children[p].classList.contains('open')) {
                        children.push(par.children[p])
                    }
                }
            } else {
                let all = base[i].querySelectorAll('.children > .project.open')
                for (let i = 0; i < all.length; i++) {
                    children.push(all[i])
                }
            }
        }

        return children.filter(filter)
    }

    function registerScript (s) {
        funcs.push(s)
    }

    function waitFor (selector, callback) {
        let res = document.querySelector(selector)
        if (res) return callback()

        setTimeout(() => {
            waitFor(selector, callback)
        }, 1000)
    }