all files / parser/ attributes.js

100% Statements 38/38
89.29% Branches 25/28
100% Functions 2/2
100% Lines 38/38
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70        337× 337× 337× 390× 390×   390× 180×   210×   390× 47×     390× 49×     390× 390× 113× 277×   277×               277× 18×   277× 49×   277× 277× 277× 277× 47×           290×   290× 18× 18× 18× 18× 18×       272×   27×  
 
import {avalon, directives} from '../seed/core'
export var eventMap = avalon.oneObject('animationend,blur,change,input,'+
        'click,dblclick,focus,keydown,keypress,keyup,mousedown,mouseenter,'+
        'mouseleave,mousemove,mouseout,mouseover,mouseup,scan,scroll,submit','on')
export function parseAttributes(dirs, tuple ) {
    var node = tuple[0], uniq = {}, bindings = []
    var hasIf = false
    for (var name in dirs) {
        var value = dirs[name]
        var arr = name.split('-')
        // ms-click
        if(name in node.props){
           var attrName = name
        }else{
            attrName = ':'+name.slice(3)
        }
        if (eventMap[arr[1]]) {
            arr.splice(1, 0, 'on')
        }
        //ms-on-click
        if (arr[1] === 'on') {
            arr[3] = parseFloat(arr[3]) || 0
        }
 
        var type = arr[1]
        if(type === 'controller' || type === 'important')
            continue
        Eif (directives[type]) {
 
            var binding = {
                type: type,
                param: arr[2],
                attrName: attrName,
                name: arr.join('-'),
                expr: value,
                priority: directives[type].priority || type.charCodeAt(0) * 100
            }
            if(type === 'if'){
                hasIf = true
            }
            if (type === 'on') {
                binding.priority += arr[3]
            }
            Eif (!uniq[binding.name]) {
                uniq[binding.name] = value
                bindings.push(binding)
                if (type === 'for') {
                    return [avalon.mix(binding, tuple[3])]
                }
            }
 
        }
    }
     bindings.sort(byPriority)
      
     if(hasIf){
        var ret = []
        for(var i = 0, el; el = bindings[i++];){
            ret.push(el)
            Eif(el.type === 'if'){
                return ret
            }
        }
     }
     return bindings
}
export function byPriority(a, b) {
    return a.priority - b.priority
}