FRange_wev8.js 3.91 KB
define(['mUtil', "Component", "Form"], function (mUtil, Component, Form) {
	var comp = function (options) {
		var $wevField, $field;

		Component.super(this, options);

		this.type = "FRange";
		this.tpl = this.type + "_html";
		this.css = this.type + "_css";
        this.dataload = true;

		var vm = this.viewModel = {
			form: "",
			field: {
				label: "",	//显示名
				name: "",   //字段名
				id: "",     //字段id
				value: "0",	//字段值
				defaultValue: "",
			},
			min: 0,
			max: 100,
			step: 1,
			detailtable: {
				isA: false	//是否是明细表字段
			},
			required: false,
			readonly: false,
			needWrap: false
		};
		
		this.dbValueHasSet = false; //是否已设置表单数据值,调用this.setValueByDB()方法后修改此状态
		
		this.beforeMount = function () {
			//主表或明细新增,并且值为空 但默认值不为空时,使用默认值代替赋值
            Form.utils.setDefaultValue(this.pageid, vm, '0');
		};
		
		this.mounted = function () {
		    var that = this;
			$wevField = this.$el.find(".wev-field");
			$field = this.$el.find("input[type='range']");

			var $tip = $(".wev-range-tip", this.$el);

			var timer = null;
			var valueChange = function (isShowTip) {
				var v = $field.val();
				var vf = parseFloat(v);

				if (!isShowTip) {
					$tip.removeClass("wev-hide");
				}

				var distince = Math.abs(vm.max - vm.min);
				var percentage = ((vf - vm.min) / distince);

				percentage = percentage > 1 ? 1 : percentage;

				var bgSize = (percentage * 100).toFixed(0);
				$field.css("background-size", bgSize + "% 100%");

				var offsetLeft = $field[0].offsetLeft;
				var offsetWidth = $field[0].offsetWidth - 28;
				var tooltipWidth = $tip[0].offsetWidth;

				var scaleWidth = (offsetWidth / distince) * Math.abs(v - vm.min);
				var l = 14 + offsetLeft + scaleWidth - tooltipWidth / 2;
				$tip.html(v).css("left", l + "px");

				if (isShowTip) return;

				if (timer) {
					clearTimeout(timer);
				}
				timer = setTimeout(function () {
					$tip.addClass("wev-hide");
				}, 1000);
			};

			$field.on("input", function (e, isShowTip) {
				valueChange(isShowTip);
			});

			if(vm.field.needParseSqlValue){
			    //解析默认值,默认值sql
	            return Form.utils.parseDefaultSqlValue(vm.field.defaultValue, this.pageid, function(result){
	                vm.field.value = result;
	                //设置默认值
	                that.setValue(vm.field.value);
	            });
			}else{
			    valueChange(true);
			}
		};

		this.getData = function () {
			return mUtil.parseJSON("fieldname_" + vm.field.name, $field.val());
		};

		this.reset = function () {
			this.setValue(vm.field.value);
		};

		this.checkRequired = function () { };

		this.setRequired = function (required) {
			vm.required = required;
			$wevField.toggleClass("wev-field-required", required);
			!required && $wevField.removeClass("wev-required-remind");
		};
		
		this.bindTrigger = function (triggers, isTrigger) {
			var that = this,
				fieldid = vm.field.id;

			if (fieldid && mUtil.isObject(triggers) && triggers["field" + fieldid]) {
				var trigger = triggers["field" + fieldid];

				$field.bind("change", function () {
					Mobile_NS.readyToTrigger(trigger, $field, that.$container);
				});
				//默认值触发字段联动,主表新建或者是明细表添加数据时触发
				if (vm.field.value && ((vm.form.indexOf("detailtable") == -1 && !$p("billid")) || isTrigger)) {
					Mobile_NS.readyToTrigger(trigger, $field, that.$container);
				}
			}
		};
		
		this.setValueByDB = function(v){
			this.dbValueHasSet = true;
			if(Number(v || '') != 0 || vm.field.value == '0') {
                vm.field.value = v;
                this.setValue(v);
            }
		};
		
		this.setValue = function (v) {
			if(v == ""){
				v = "0";
			}
			$field.val(v).triggerHandler("input", true);
			this.dbValueHasSet && mUtil.trigger('dataload', this.pageid, this.id);
		};
	};

	return Component.init(comp);
});