all files / directives/duplex/ option.js

100% Statements 32/32
87.5% Branches 21/24
100% Functions 7/7
100% Lines 32/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 135× 198× 85×   113×         85× 85× 85× 85× 85×   85× 83× 83×           91× 11×   80× 80× 80× 79×     80×     35× 103× 43× 60× 24×     35×          
export function lookupOption(vdom, values) {
   vdom.children && vdom.children.forEach(function (el) {
        if (el.nodeName === 'option') {
            setOption(el, values)
        } else {
            lookupOption(el, values)
        } 
    })
}
 
function setOption(vdom, values) {
    var props = vdom.props
    Eif (!('disabled' in props)) {
        var value = getOptionValue(vdom, props)
            value = String(value || '').trim()
       if(typeof values === 'string'){
            props.selected = value === values
        }else{
            props.selected = values.indexOf(value) !== -1;
        }
       
        if (vdom.dom) {
            vdom.dom.selected = props.selected
            var v = vdom.dom.selected //必须加上这个,防止移出节点selected失效
        }
        
    }
}
 
function getOptionValue(vdom, props) {
    if (props && 'value' in props) {
        return props.value+ ''
    }
    var arr = [E]
    vdom.children.forEach(function (el) {
        if (el.nodeName === '#text') {
            arr.push(el.nodeValue)
        } else if (el.nodeName === '#document-fragment') {
            arr.push(getOptionValue(el))
        }
    })
    return arr.join('')
}
 
export function getSelectedValue(vdom, arr) {
    vdom.children.forEach(function (el) {
        if (el.nodeName === 'option') {
            if(el.props.selected === true)
               arr.push(getOptionValue(el, el.props))
        } else if (el.children) {
            getSelectedValue(el,arr)
        }
    })
    return arr
}