Countdown_wev8.js 1.72 KB
define(['mUtil', "Component"],function(mUtil, Component) {
	var Countdown = function(options) {
        
		Component.super(this, options);
		
		this.type = "Countdown";
		this.tpl = this.type + "_html";
		this.css = this.type + "_css";
		
		var vm = this.viewModel = {
			countdowndate: new Date().getFullYear() + 1 + "-01-01 00:00:01"   //默认值
		};

		this.beforeMount = function () {
			vm.validate = true;//合法日期
			if (!vm.countdowndate.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{1,2}:[0-9]{2}:[0-9]{2}$/)) {
				vm.countdowndate = "0";
				vm.validate = false;
			}
		};
		
		this.mounted = function(){
			var $comp = $('.wev-comp-Countdown > div',this.$el);
			var oriDate = vm.validate ? new Date(vm.countdowndate) : new Date();

			require(["timeCircles"], function () {
				$comp.TimeCircles({
					time: {
						Days: {
							show: false,
							text: "天",
							color: "#FC6"
						},
						Hours: {
							show: false,
							text: "时",
							color: "#9CF"
						},
						Minutes: {
							show: false,
							text: "分",
							color: "#BFB"
						},
						Seconds: {
							show: false,
							text: "秒",
							color: "#F99"
						}
					},
					refresh_interval: 0.1,
					count_past_zero: vm.validate && ((new Date()).getTime() - oriDate.getTime())/1000 >= 0 ? false : vm.validate,
					circle_bg_color: "#ddd",
					fg_width: 0.03,
					bg_width: 0.2
				}).addListener(function (unit, value, total) {
					var diff = ((new Date()).getTime() - oriDate.getTime())/1000;
					if ((total <= 1 && Math.abs(diff) <= 1) || diff > 1) {
						if (diff <= 1) {
							setTimeout(function () {
								$comp.TimeCircles().stop();
							}, 1000);
						}
					}
				});
			});
		};
    };
    return Component.init(Countdown);
});