SegControl_wev8.js 2.13 KB
define(['mUtil', "Component"], function (mUtil, Component) {
	var SegControl = function (options) {
		var _segControl = {};

		Component.super(this, options);

		this.type = "SegControl";
		this.tpl = this.type + "_html";
		this.css = this.type + "_css";

		var vm = this.viewModel = {
			segments: [{
				text: " ",
				active: false,
				click: function () { }
			}],
			badge: null
		};

		this.beforeMount = function(){
		    vm.remindApis = [];
            vm.segments.map(function(segment){
                if(segment.remind && segment.remindapi && segment.remindapi.api){
                    segment.uuid = segment.remindapi.uuid = mUtil.UUID();
                    segment.apiid = segment.remindapi.api.id;
                    vm.remindApis.push(segment.remindapi);
                    delete segment.remindapi;
                }
            });
            
        	if(vm.badge === true){
        		vm.badge = mUtil.getActionUrl(this.type, {action:"getBadge", "mec_id": this.id}, this.pageid);
    		}
        };
        
		this.mounted = function () {
			var that = this,
				segments = vm.segments,
				$comp = that.$el.children(".wev-comp-" + that.type);

			$comp.on("click", ".wev-control-item", function (e) {
				var index = $(this).index(),
					segment = segments[index];

				$(this).addClass("active").siblings().removeClass("active");

				segment && mUtil.eval(segment.click, that.pageid);

				e.stopPropagation();
			});
			var notActive = true;
			segments.map(function (segment) {
				if(segment && segment.active){
					mUtil.eval(segment.click, that.pageid);
					notActive = false;
				}
			});
			if(notActive){
				$comp.find(".wev-control-item").eq(0).addClass("active")
			}
			_segControl.loadBadge();
		};

		this.refreshBadge = function () {
			_segControl.loadBadge();
		};

		_segControl.loadBadge = function () {
			if(vm.badge == null){
        		return;
        	}
			var $badges = $(".wev-numremind", this.$el);

			require(["remindHelper"], function (helper) {
				helper.renderBadge(vm.badge, $badges, vm.remindApis);
			});
		};
		_segControl.loadBadge = _segControl.loadBadge.bind(this);
	};

	return Component.init(SegControl);
});