FloatButton_wev8.js 2.42 KB
define(['mUtil', "Component"],function(mUtil, Component) {
	var comp = function(options) {
		
		Component.super(this, options);	
		
		this.type = "FloatButton";
		this.tpl = this.type + "_html";
		this.css = this.type + "_css";

		var goTopFn = function(){
			$(".page-content").scrollTop(0);
		};
		var isGoTopFn = true;
		
		var vm = this.viewModel = {
			position:{
				top: "auto",
				right: "auto",
				bottom: "auto",
				left: "auto"
			},
			img: "",
			text: "",
			remind: false,
			badge: null,
			click: goTopFn
		};
		
		this.beforeMount = function(){
			var compStyle = "";
			for(var key in vm.position){
				var value = vm.position[key];
				if(value && value != "auto"){
					compStyle += key + ":" + value + ";";
				}
			}
			
			isGoTopFn = vm.click && (vm.click.toString() == goTopFn.toString());
			if(isGoTopFn){
				compStyle += "display:none;";
			}
			vm.compStyle = compStyle;
			
			if(vm.badge === true){
        		vm.badge = mUtil.getActionUrl(this.type, {action:"getBadge", "mec_id": this.id}, this.pageid);
    		}
			if(vm.text){
				if(vm.text.length == 4){
					vm.wordsClass = "fourletters";
				}else if(vm.text.length > 4){
					vm.wordsClass = "wordsLoop";
				}
			}  
		};
		
		var $comp;
		this.mounted = function(){
			var that = this;
    		$comp = that.$el.children(".wev-comp-"+that.type);
			
			that.$container.append($comp);
			$comp.css("visibility", "visible");
			
			if(isGoTopFn){
				var $pageContent = that.$container.children(".page-content");
				$pageContent.scroll(function(){
					var h = $(window).height();
					var t = $pageContent.scrollTop();
					if(t > h){
						$comp.show();
					}else{
						$comp.hide();
					}
				});
			}
			
			$comp.on("click", function(){
				mUtil.eval(vm.click, that.pageid);
			});

            //绑定插件内部的pageChange方法
            this.$on('pageChange', function(isPageIn){
            	if(!isGoTopFn){
                    $comp.toggle(isPageIn);
				}
            });
			
			_floatButton.loadBadge();
		};
		
		this.refreshBadge = function(){
			_floatButton.loadBadge();
		};
		
		var _floatButton = {
			loadBadge: function(){
				if(!vm.remind){
					return;
				}
				var $badges = $(".wev-badge", $comp);
        	
	        	require(["remindHelper"], function(helper){
					helper.renderBadge(vm.badge, $badges, vm.remindapi);
	        	});
			}	
		};
		
		_floatButton.loadBadge = _floatButton.loadBadge.bind(this);
    };

    return Component.init(comp);
});