FTextarea_wev8.js 3.75 KB
define(['mUtil', "Component", "Form"], function (mUtil, Component, Form) {
	var FTextarea = function (options) {
		var _text = {}, $wevField, $field, $fieldspan;

		Component.super(this, options);

		this.type = "FTextarea";
		this.tpl = this.type + "_html";
        this.dataload = true;

		var vm = this.viewModel = {
			form: "",
			field: {
				label: "", //显示名
				type: 1,	//1 普通多行 2 html多行
				name: "",  //字段名
				id: "",    //字段id
				value: "",  //字段值
				defaultValue: ""
			},
			detailtable: {
				isA: false	//是否是明细表字段
			},
			placeholder: "请输入...",
			height: '80',
			required: false,
			readonly: false
		};
		
		this.dbValueHasSet = false; //是否已设置表单数据值,调用this.setValueByDB()方法后修改此状态
		
		this.beforeMount = function () {
			//主表或明细新增,并且值为空 但默认值不为空时,使用默认值代替赋值
            Form.utils.setDefaultValue(this.pageid, vm);
			vm.field.valueHtml = _text.formatValue(vm.field.value);
		};

		this.mounted = function () {
		    var that = this;
            $wevField = this.$el.find(".wev-field");
			$field = this.$el.find("textarea");
			$fieldspan = this.$el.find(".wev-field-view-span");
			if(vm.field.needParseSqlValue){
			    //解析默认值,默认值sql
	            return Form.utils.parseDefaultSqlValue(vm.field.defaultValue, this.pageid, function(result){
	                vm.field.value = result;
	                vm.field.value && that.setValue(vm.field.value);
	            });
			}
		};

		this.getData = function () {
			return mUtil.parseJSON("fieldname_" + vm.field.name, $field.val());
		};
		
		this.getShowData = function(){
			var showData = mUtil.parseJSON(vm.field.name + "_showvalue", _text.formatValue($field.val()));
			showData[vm.field.name] = $field.val();
			return showData;
		};

		this.reset = function () {
			this.setValue(vm.field.value);
			$wevField.removeClass("wev-required-remind");
		};

		this.checkRequired = function () {
			var required = vm.required && !$field.val();
			var fieldLabel = vm.field.label || mUtil.getLabel(386757, "多行文本");

			$wevField.toggleClass("wev-required-remind", required);

			return required && fieldLabel;
		};
		
		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.on("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(value){
			this.dbValueHasSet = true;
			if(value || !vm.field.value) {
                vm.field.value = value;
                this.setValue(value);
            }
		};
		
		this.setValue = function (v) {
			//对emoji的html字符实体进行转义
			$field.val(v.replace(/&#\d{5,7};/g, function(char){
				return $('<div></div>').html(char).html();
			})).triggerHandler("change");

			$fieldspan.html(_text.formatValue(v));
			this.dbValueHasSet && mUtil.trigger('dataload', this.pageid, this.id);
		};
		
		_text.formatValue = function(v){
			if(vm.field.type === 2) return v;//多行文本html原样输出
			return new String(v).replace(/\n/gi, "<br>").replace(/\s/gi, "&nbsp;");
		}
	};

	return Component.init(FTextarea);
});