Button_wev8.js 2.52 KB
define(['mUtil', "Component"],function(mUtil, Component) {
	var Button = function(options) {
		var _button = {};
		
		Component.super(this, options);
		
		this.type = "Button";
		this.tpl = this.type + "_html";
		this.css = this.type + "_css";

		var vm = this.viewModel = {
			layout: "relative",
			items: [],
			fixed: false,
			badge: null
		};
		
		this.beforeMount = function(){
        	if(vm.badge === true){
        		vm.badge = mUtil.getActionUrl(this.type, {action:"getBadge", "mec_id": this.id}, this.pageid);
    		}
        };
        
		this.mounted = function(){
			var $comp = this.$el.children(".wev-comp-"+this.type);
			var that = this;

			// 按钮单击事件
			$comp.on("click", ".wev-btn", function(event){
				var index = $(this).index(),
					btn = vm.items[index];
				
				btn && mUtil.eval(btn.click, that.pageid);
			});
            
            _button.loadBadge();
            _button.calculatePosition();
		};
		
		this.refreshBadge = function(){
			_button.loadBadge();
		};
		
		_button.loadBadge = function() {
			if(vm.badge == null){
        		return;
        	}
        	var $badges = $(".wev-badge", this.$el);
        	
        	require(["remindHelper"], function(helper){
				helper.renderBadge(vm.badge, $badges);
        	});
		};
		
		_button.calculatePosition = function(){
			var $page = this.$container;
			if (vm.fixed) {
				this.$once("moveComponent", function(){
                    var $footer = $page.children(".page-footer");
                    var $content = $page.children(".page-content");
                    if (!$footer.length) {
                        $footer = $("<div class=\"page-footer\"></div>");
                        $content.after($footer);
                    }
                    if (!this.$el.parent().hasClass("page-footer")) {
                        $footer.append(this.$el).show();
                    }

                    if($footer.height()){
                        $footer.attr("footer-height", $footer.height());
                        $content.css("bottom", $footer.height() + "px");
                    }else{
                        if($footer.attr("footer-height")){
                            $content.css("bottom", $footer.attr("footer-height") + "px");
                        }else{
                            $content.css("bottom", "0px");
                        }
                    }
				});
			}
		};
		
		_button.loadBadge = _button.loadBadge.bind(this);
		_button.calculatePosition = _button.calculatePosition.bind(this);
    };

    return Component.init(Button);
});