Steps_wev8.js 2.18 KB
define(['mUtil', "Component"],function(mUtil, Component) {
    var Steps = function(options) {
        
        Component.super(this, options);
        
        this.type = "Steps";
        this.tpl = this.type + "_html";
        this.css = this.type + "_css";

        var vm = this.viewModel = {
            direction: 'horizontal',
            steps: []
        };
        
        this.current = -1;
        
        this.beforeMount = function(){
            var that = this, getStatus = function(condition){
                try{
                    var regex = new RegExp("(\\w+\\s*)=(\\s*\\w+)"), m;
                    while((m = regex.exec(condition)) != null) {
                        condition = condition.replace(m[0], m[1]+"=="+m[2]);
                    }
                    return eval(condition);
                }catch(e){
                    return false;
                }
            };
            vm.steps.forEach(function(step, i){
                if(getStatus(step.condition) === true || step.status == 'current') that.current = i;
            });
            vm.steps.forEach(function(step, i) {
                var status = step.status || "wait";
                if(that.current != -1) status = (that.current == i ? "current" : (that.current > i ? "finish" : "wait"));
                step.status = status;
            });
            that.current++;
        };
        
        this.mounted = function(){
            
        };
        
        this.setCurrent = function(current){
            if(isNaN(current)) return;
            this.current = current;
            current--;
            var $el = this.$el, prefix = "wev-steps-";
            vm.steps.forEach(function(step, i) {
                var status = (current == i ? "current" : (current > i ? "finish" : "wait"));
                $el.find(".wev-steps-item").eq(i).removeClass(prefix + step.status).addClass(prefix + status);
                step.status = status;
            });
        };
        
        this.setNext = function(){
            this.setCurrent(this.current + 1);
        }
        
        this.setPrev = function(){
            this.setCurrent(this.current - 1);
        }
    };

    return Component.init(Steps);
});