PageSourceSearch

https://member.tongtool.com/static/t/js/T.TextField.js?%7B12%7D

js tongtool.com collected 2026-10-03 19:20:44 UTC 6,980 bytes, 245 lines download raw bytes

1(function(){
2	var _c=T.TextField=function(node,config,container){
3		this.node=node;
4		if(typeof(config.allowBlank)=="undefined"){
5			config.allowBlank=true;
6		}
7		this.config=config;
8		if(!this.config.invalidChars){
9			//this.config.invalidChars = ["'",'"',";"];
10			this.config.invalidChars = [];
11		}
12		this.id=T.uuid();
13		this.init();
14	};
15	T.extend(_c,{
16		hoverClass:"bg7",
17		validate:function(){
18			if(this.isDisabled()||!T.isVisible(this.text)){
19				return true;
20			}
21			return T.Validate.check(this.getValue(),this.config,[this.text]);
22		},
23		getId:function(){
24			return this.id;
25		},
26		init:function(){
27			var text=this.text=T.dc("input");
28			text.type="text";
29			var self=this;
30			this.node.appendChild(text);
31			text.setAttribute("generatedBy",this.id);
32			var c=this.config;
33			text.name=c.name;
34			text.id=c.id;
35			text.value=c.value||"";
36			text.title=c.value||c.displayText||"";
37			if(c.displayText){
38				if(text.value == ""){
39					text.value = c.displayText;
40					text.style.color = "#999999";
41					if(c.displayColor){
42						text.style.color =c.displayColor;
43					}
44				}
45				text.onfocus = function(){
46					if(T.trim(text.value) == c.displayText){
47						text.style.color = "#000";
48						text.value = "";
49					}
50				}
51				text.onblur = function(){
52					if(T.trim(text.value) == ""){
53						text.style.color = "#999999";
54						text.value = c.displayText;
55						if(c.displayColor){
56							text.style.color =c.displayColor;
57						}
58					}
59				}
60			}
61			var width=c.width;
62			var height=c.height;
63			var readOnly=(""+c.readOnly)=="true";
64			c.readOnly=readOnly;
65			var disabled=c.disabled;
66			var className=c.className;
67			text.className="np ";
68			if(className){text.className+=className}
69			
70			if(width){ text.style.width=width+"px";}
71			if(height){ text.style.height=height+"px"; }
72		    if(readOnly){
73		    	this.setReadonly(true);
74		    }
75		    if(disabled){
76		    	text.disabled=disabled;
77		    }
78		    
79		    self.createWarning();
80		    
81		    //注册事件
82			var self=this;
83			T.Event.browserBind(text,"change",function(event){
84				text.title=text.value;
85				T.Event.fire(self,"change");
86				if ( event.preventDefault ) {
87					event.preventDefault();
88				} else {
89					event.returnValue = false;
90				}
91			});
92			T.Event.browserBind(text,"focus",function(){
93				T.Css.addClass(text,self.hoverClass);
94				T.Event.fire(self,"focus");
95			});
96			T.Event.browserBind(text,"blur",function(event){
97				text.value=T.trim(text.value);
98				T.Css.removeClass(text,self.hoverClass);
99				T.Validate.validateWidget(self);
100				T.Event.fire(self,"blur");
101			});
102			T.Event.browserBind(text,"keypress",function(event){
103				T.Event.fire(self,"keypress",[event]);
104				
105				for ( var i = 0; i < self.config.invalidChars.length; i++) {
106					 var charCode = self.config.invalidChars[i].charCodeAt(0);
107					 if(event.charCode==charCode || event.keyCode==charCode){
108						  self.preventEvent(event);
109						  self.showErrorMsg("输入无效字符: "+self.config.invalidChars[i]);
110						  break;
111					}
112				}
113			});
114			
115			T.Event.browserBind(text,"paste",function(event){
116				T.Event.fire(self,"paste",[event]);
117				if(self.config.invalidChars.length == 0){
118					return;
119				}
120				var textObj = jQuery(self.text);
121				var oldValue = textObj.val();
122				
123				//发生粘贴事件时不能马上获得粘贴内容,只能不断取值,直至新值与旧值不同或者重复次数达到最大
124				var delayTimes = 10;
125				var waitForPasteData  = function(){
126					var newValue = textObj.val();
127					if(oldValue==newValue && delayTimes-- > 0){
128						setTimeout(waitForPasteData,20);
129						return;
130					}
131					for ( var i = 0; i < self.config.invalidChars.length; i++) {
132						for ( var j = 0; j < newValue.length; j++) {
133							 if(self.config.invalidChars[i]==newValue.charAt(j)){
134								 textObj.val(oldValue);
135								 self.showErrorMsg("粘贴无效字符: "+self.config.invalidChars[i]);
136								 break;
137							}
138						}
139					}
140				}
141				waitForPasteData();
142			});
143			
144			T.Event.browserBind(text,"keydown",function(event){
145				T.Event.fire(self,"keydown",[event]);
146			});
147
148		},
149		preventEvent:function(event){
150			if ( event.preventDefault ) {
151				event.preventDefault();
152			} else {
153				event.returnValue = false;
154			}
155		},
156		showErrorMsg:function(message){
157			var warningObj = jQuery(this.warning);
158			warningObj.text(message);
159			warningObj.css("display","block"); 
160			var position = jQuery(this.text).offset();
161			warningObj.css("top",position.top-30);
162			warningObj.css("left",position.left);
163			setTimeout(function(){
164				warningObj.css("display","none");
165			},1000);
166		},
167		createWarning:function(){
168			var warningObj = jQuery("#invalidErrorObj");
169			if(warningObj.get(0)){
170				this.warning=warningObj.get(0);
171			}else {
172				this.warning=T.dc("div");
173				this.warning.setAttribute("id","invalidErrorObj");
174				T.body().appendChild(this.warning);
175				this.warning.className = "invalidError";
176			}
177		},
178		setReadonly:function(ro){
179			var text=this.text;
180	    	text.readOnly=ro;
181	    	this.config.readOnly=ro;
182	    	if(ro){
183		    	T.Css.addClass(text,"readonly");
184	    	}else{
185		    	T.Css.removeClass(text,"readonly");
186	    	}
187		},
188		getReadonly:function(){
189			return this.config.readOnly;
190		},
191		disable:function(){
192			this.text.style.backgroundColor="";		
193			this.text.disabled=true;
194		},
195		enable:function(){
196			this.text.disabled=false;
197		},
198		isDisabled:function(){
199			return this.text.disabled;
200		},
201		getValue:function(){
202			if(this.text.value == this.config.displayText){
203				return "";
204			}else{
205				return this.text.value;
206			}	
207		},
208		setValue:function(value){
209			this.text.value=value;
210			this.text.title=value;
211		},
212		insertStrAtCursor:function(str){
213			var txa = this.text;
214			txa.focus();
215			var tclen = txa.value.length;
216		    var explorerMap = T.detect()["browser"];	
217			if(explorerMap["ie"]){//IE
218		    	if(txa.createTextRange&&txa.curRage){//判断光标是否在区域内
219					var caretPos=txa.curRage;
220					caretPos.text = str ;
221					caretPos.select() ;
222		        }else{
223		        	txa.value = str; 
224		        } 
225		    }else{//mozzila
226		    	var cursorPos =txa.selectionStart;
227		        txa.value = txa.value.substr(0,txa.selectionStart)+str+txa.value.substring(txa.selectionStart,tclen);
228		        cursorPos+=str.length;
229		        txa.selectionStart = txa.selectionEnd = cursorPos;	//光标位置置于添加内容之后
230		    }
231		}
232	});
233	_c.create=function(el,container){
234		var jsonStr=el.innerHTML;
235		el.innerHTML="";
236		var escaped=(el.getAttribute("escaped")||false)=="true";
237		if(escaped){
238			jsonStr=unescape(jsonStr);
239		}
240		var config=T.evalJson(jsonStr);
241		T.Css.removeClass(el,"hide"); //把<span> 的隐藏样式去掉
242		return new _c(el,config,container);
243	};
244	T.Widget.registerType("textfield",_c);
245})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.