-
Notifications
You must be signed in to change notification settings - Fork 4
/
materialtextfield.js
1 lines (1 loc) · 4.36 KB
/
materialtextfield.js
1
var isAndroid="android"===Ti.Platform.osname?!0:!1,Widget=function(_args){var self=Ti.UI.createView({width:"90%",height:72,windowSoftInputMode:isAndroid?Ti.UI.Android.SOFT_INPUT_STATE_HIDDEN:void 0});self.add(Ti.UI.createLabel({height:28,top:23,font:{fontSize:16},opacity:.7,left:0})),self.add(Ti.UI.createTextField({width:"99%",height:isAndroid?37:28,top:23,backgroundColor:"transparent",font:{fontSize:16},color:"#444"})),self.add(Ti.UI.createView({width:"100%",height:2,bottom:15}));var args=arguments[0]||{},_config={color:{pattern:"#aaa",post:"#208FE5",exceedingColor:"FF0000"},duration:200,editable:!0,exceeding:!1},_events={CLICK:"click",FOCUS:"focus",BLUR:"blur",CHANGE:"change"},_animation={ANIMATION_UP:function(){if(_config.editable){var t=self.children[0].getText().length,e=_config.exceeding?_config.color.exceeding:_config.color.post;t+=t*(Number(t)>25?.2:.1),self.children[0].animate({top:0,color:e,transform:Ti.UI.create2DMatrix().scale(.7),left:"android"==Ti.Platform.osname?-t+2:-t,duration:_config.duration}),self.children[2].animate({backgroundColor:e})}},ANIMATION_DOWN:function(){if(_config.editable){var t=self.children[0].getText().length,e=_config.exceeding?_config.color.exceeding:_config.color.pattern;t+=t*(Number(t)>25?.2:.1),self.children[2].animate({backgroundColor:e});var o={top:self.children[1].top,color:e,transform:Ti.UI.create2DMatrix().scale(1),left:0,duration:_config.duration};self.children[1].getValue()&&(o.top=0,o.transform=Ti.UI.create2DMatrix().scale(.7),o.left=-t),self.children[0].animate(o)}}};return self.children[1].addEventListener(_events.FOCUS,_animation.ANIMATION_UP),self.children[1].addEventListener(_events.BLUR,_animation.ANIMATION_DOWN),function(){_config.color.post=args.colorFocus||_config.color.post,_config.color.pattern=args.colorPattern||_config.color.pattern,_config.color.exceeding=args.exceedingColor||"#FF0000",_config.duration=args.animationDuration||_config.duration;var _init={titleHint:args.titleHint,width:args.width,top:args.top,left:args.left,right:args.right,bottom:args.bottom,colorFont:args.colorFont,keyboardType:args.keyboardType,returnKey:args.returnKey,password:args.password,editable:args.editable,maxLength:args.maxLength,minLength:args.minLength};if("string"==typeof _init.editable&&(_init.editable=eval(_init.editable)),_init.titleHint||self.children[0].setVisible(!1),_init.width&&self.setWidth(_init.width),_init.top&&self.setTop(_init.top),_init.bottom&&self.setBottom(_init.bottom),_init.left&&self.setLeft(_init.left),_init.right&&self.setRight(_init.right),_init.colorFont&&self.children[1].setColor(_init.colorFont),_init.keyboardType&&self.children[1].setKeyboardType(_init.keyboardType),_init.returnKey&&self.children[1].setReturnKeyType(_init.returnKey),_init.password&&self.children[1].setPasswordMask(_init.password),self.children[0].setText(_init.titleHint),self.children[0].setColor(_config.color.pattern),self.children[2].setBackgroundColor(_config.color.pattern),0==_init.editable&&(self.setOpacity(.3),self.children[1].setEditable(!1),_config.editable=!1),_init.maxLength>0){var counter=Ti.UI.createLabel({height:15,width:64,font:{fontSize:11},opacity:.7,right:-64,textAlign:"right",bottom:0});self.add(counter),self.children[1].addEventListener(_events.CHANGE,function(){return 0==length?void counter.animate({right:-64,duration:350}):(1==length&&counter.animate({right:0,duration:350}),length<_init.minLength||length>_init.maxLength?(_config.exceeding=!0,self.children[2].backgroundColor=_config.color.exceeding,counter.color=_config.color.exceeding,self.children[0].color=_config.color.exceeding):self.children[2].backgroundColor!=_config.color.post&&(_config.exceeding=!1,self.children[2].backgroundColor=_config.color.post,counter.color="#000",self.children[0].color=_config.color.post),void counter.setText(length+" / "+_init.maxLength))})}}(),self.getValue=function(){return self.children[1].getValue()},self.ANIMATION_UP=function(){_animation.ANIMATION_UP()},self.ANIMATION_DOWN=function(){_animation.ANIMATION_DOWN()},self.setValue=function(t,e){e&&_animation.ANIMATION_UP(),self.children[1].setValue(t)},self.addEventListener=function(t,e){self.children[1].addEventListener(t,function(t){e(t)})},self.removeEventListener=function(t,e){self.children[1].removeEventListener(t,function(t){e(t)})},self.blur=function(){self.children[1].blur()},self.focus=function(){self.children[1].focus()},self};exports.createView=function(t){return new Widget(t)};