import Vue from 'vue';

Vue.directive('autoTrim', {
    bind(el, binding, vnode, oldVnode){
        if($(el).has('input').length>0){
            $(el).on('blur','input',function (e){
                if(!!e.target.value){
                    if(!!binding.value.obj){
                        var expression = null;
                        var expressionCatchArr = [];
                        for(var i=0;i<vnode.data.directives.length;i++){
                            if(vnode.data.directives[i].name == "autoTrim"){
                                expressionCatchArr = vnode.data.directives[i].expression.split(',');
                                for(var j=0;j<expressionCatchArr.length;j++){
                                    if(expressionCatchArr[j].includes('obj')){
                                        expression = expressionCatchArr[j].split(':')[1];
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                        if(expression.includes('scope')){
                            binding.value.obj[binding.value.key] = e.target.value.trim();
                        }else{
                            var expressionArr = expression.split('.');
                            var expressionObj = vnode.context;
                            expressionArr.forEach((item)=>{
                                expressionObj = expressionObj[item.trim()]
                            });
                            expressionObj[binding.value.key] = e.target.value.trim()
                        }
                    }else{
                        vnode.context[binding.value.key] = e.target.value.trim()
                    }
                }
            })
        }else if($(el).has('textarea').length>0){
            $(el).on('blur','textarea',function (e){
                if(!!e.target.value){
                    if(!!binding.value.obj){
                        var expression = null;
                        var expressionCatchArr = [];
                        for(var i=0;i<vnode.data.directives.length;i++){
                            if(vnode.data.directives[i].name == "autoTrim"){
                                expressionCatchArr = vnode.data.directives[i].expression.split(',');
                                for(var j=0;j<expressionCatchArr.length;j++){
                                    if(expressionCatchArr[j].includes('obj')){
                                        expression = expressionCatchArr[j].split(':')[1];
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                        if(expression.includes('scope')){
                            binding.value.obj[binding.value.key] = e.target.value.trim();
                        }else{
                            var expressionArr = expression.split('.');
                            var expressionObj = vnode.context
                            expressionArr.forEach(item => {
                              expressionObj = expressionObj[item.trim()]
                            })
                            expressionObj[binding.value.key] = e.target.value.trim()
                        }
                    }else{
                        vnode.context[binding.value.key] = e.target.value.trim()
                    }
                }
            })
        }

    }
})
