You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by ah...@apache.org on 2019/11/27 07:12:30 UTC

[royale-asjs] branch develop updated: restrict property for TextInputs

This is an automated email from the ASF dual-hosted git repository.

aharui pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git


The following commit(s) were added to refs/heads/develop by this push:
     new 8dee4cd  restrict property for TextInputs
8dee4cd is described below

commit 8dee4cdd8f9a50cd4d7909ff4217c1ca61b1a3c2
Author: Alex Harui <ah...@apache.org>
AuthorDate: Tue Nov 26 23:12:13 2019 -0800

    restrict property for TextInputs
---
 .../projects/Basic/src/main/royale/BasicClasses.as |   1 +
 .../html/accessories/RestrictTextInputBead.as      | 155 +++++++++++++++++++++
 .../src/main/royale/mx/controls/TextInput.as       |  50 ++-----
 .../src/main/royale/spark/components/TextInput.as  |  21 ++-
 4 files changed, 189 insertions(+), 38 deletions(-)

diff --git a/frameworks/projects/Basic/src/main/royale/BasicClasses.as b/frameworks/projects/Basic/src/main/royale/BasicClasses.as
index 742eef8..1d2dc13 100644
--- a/frameworks/projects/Basic/src/main/royale/BasicClasses.as
+++ b/frameworks/projects/Basic/src/main/royale/BasicClasses.as
@@ -35,6 +35,7 @@ internal class BasicClasses
 	}
     import org.apache.royale.html.ToolTip; ToolTip;
 	import org.apache.royale.html.accessories.NumericOnlyTextInputBead; NumericOnlyTextInputBead;
+    import org.apache.royale.html.accessories.RestrictTextInputBead; RestrictTextInputBead;
     import org.apache.royale.html.beads.DispatchInputFinishedBead; DispatchInputFinishedBead;
 	import org.apache.royale.html.accessories.PasswordInputBead; PasswordInputBead;
 	import org.apache.royale.html.accessories.PasswordInputRemovableBead; PasswordInputRemovableBead;
diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/accessories/RestrictTextInputBead.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/accessories/RestrictTextInputBead.as
new file mode 100644
index 0000000..f37b6c2
--- /dev/null
+++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/accessories/RestrictTextInputBead.as
@@ -0,0 +1,155 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.royale.html.accessories
+{
+	COMPILE::JS
+	{
+		import goog.events.BrowserEvent;
+	}
+	COMPILE::SWF
+	{
+		import flash.events.TextEvent;
+		
+		import org.apache.royale.core.CSSTextField;			
+	}
+	import org.apache.royale.core.IBead;
+	import org.apache.royale.core.IStrand;
+	import org.apache.royale.core.UIBase;
+	import org.apache.royale.events.Event;
+	import org.apache.royale.events.IEventDispatcher;
+	COMPILE::SWF
+	{
+		import org.apache.royale.html.beads.ITextFieldView;			
+	}
+	
+	/**
+	 *  The RestrictTextInputBead class is a specialty bead that can be used with
+	 *  any TextInput control. The bead prevents certain characters from being 
+     *  entered into the text input
+	 *  area.
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @playerversion AIR 2.6
+	 *  @productversion Royale 0.8
+	 */
+	public class RestrictTextInputBead implements IBead
+	{
+		/**
+		 *  constructor.
+		 *
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion Royale 0.8
+		 */
+		public function RestrictTextInputBead()
+		{
+		}
+		
+		private var _strand:IStrand;
+		
+		/**
+		 *  @copy org.apache.royale.core.IBead#strand
+		 *  
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion Royale 0.8
+		 *  @royaleignorecoercion org.apache.royale.core.UIBase
+		 */
+		public function set strand(value:IStrand):void
+		{
+			_strand = value;
+			
+			COMPILE::SWF
+			{
+				IEventDispatcher(value).addEventListener("viewChanged",viewChangeHandler);					
+			}
+			COMPILE::JS
+			{
+                var host:UIBase = _strand as UIBase;
+                host.element.addEventListener("keypress", validateInput, false);
+			}
+		}
+		
+		private var _restrict:String;
+		
+		/**
+		 *  The characters allowed or denied.  Uses flash.text.TextField.restrict syntax
+		 *
+		 *  @langversion 3.0
+		 *  @playerversion Flash 10.2
+		 *  @playerversion AIR 2.6
+		 *  @productversion Royale 0.8
+		 */
+		public function get restrict():String
+		{
+			return _restrict;
+		}
+		public function set restrict(value:String):void
+		{
+			if (_restrict != value) {
+                _restrict = value;
+			}
+		}
+		
+
+        /**
+		 * @private
+		 */
+		COMPILE::SWF
+		private function viewChangeHandler(event:Event):void
+		{			
+			// get the ITextFieldView bead, which is required for this bead to work
+			var textView:ITextFieldView = _strand.getBeadByType(ITextFieldView) as ITextFieldView;
+			if (textView) {
+				var textField:CSSTextField = textView.textField;
+				textField.restrict = restrict;
+			}
+			else {
+				// throw new Error("RestrictTextInputBead requires strand to have an ITextFieldView bead");
+			}
+		}
+				
+		COMPILE::JS
+		private function validateInput(event:BrowserEvent):void
+		{
+			var code:int = event.charCode;
+			
+			// backspace or delete
+			if (event.keyCode == 8 || event.keyCode == 46) return;
+			
+			// tab or return/enter
+			if (event.keyCode == 9 || event.keyCode == 13) return;
+			
+			// left or right cursor arrow
+			if (event.keyCode == 37 || event.keyCode == 39) return;
+			
+			var key:String = String.fromCharCode(code);
+			
+			var regex:RegExp = new RegExp("[" + restrict + "]");
+			if (!regex.test(key)) {
+				event["returnValue"] = false;
+				if (event.preventDefault) event.preventDefault();
+				return;
+			}
+		}
+	}
+}
diff --git a/frameworks/projects/MXRoyale/src/main/royale/mx/controls/TextInput.as b/frameworks/projects/MXRoyale/src/main/royale/mx/controls/TextInput.as
index dc5a75c..d93e42d 100644
--- a/frameworks/projects/MXRoyale/src/main/royale/mx/controls/TextInput.as
+++ b/frameworks/projects/MXRoyale/src/main/royale/mx/controls/TextInput.as
@@ -34,6 +34,7 @@ import mx.events.FlexEvent;
 
 import org.apache.royale.core.ITextModel;
 import org.apache.royale.events.Event;
+import org.apache.royale.html.accessories.RestrictTextInputBead;
 import org.apache.royale.html.accessories.PasswordInputBead;
 import org.apache.royale.html.beads.DisableBead;
 import org.apache.royale.core.TextLineMetrics;
@@ -1091,47 +1092,22 @@ public class TextInput extends UIComponent implements ITextInput
     //  restrict
     //----------------------------------
 
-    /**
-     *  @private
-     *  Storage for the restrict property.
-     */
-    private var _restrict:String;
-
-    /**
-     *  @private
-     */
-    private var restrictChanged:Boolean = false;
-
-    [Bindable("restrictChanged")]
-    [Inspectable(category="General")]
-
-    /**
-     *  @inheritDoc
-     *
-     *  @langversion 3.0
-     *  @playerversion Flash 9
-     *  @playerversion AIR 1.1
-     *  @productversion Flex 3
-     */
-    public function get restrict():String
+    private var restrictBead:RestrictTextInputBead;
+    
+    public function get restrict():String 
     {
-        return _restrict;
+        if (!restrictBead) return null;
+        return restrictBead.restrict;
     }
-
-    /**
-     *  @private
-     */
+    
     public function set restrict(value:String):void
     {
-        if (value == _restrict)
-            return;
-
-        _restrict = value;
-        restrictChanged = true;
-
-        invalidateProperties();
-
-        dispatchEvent(new Event("restrictChanged"));
+        if (!restrictBead)
+        {
+            restrictBead = new RestrictTextInputBead();
+            addBead(restrictBead);
+        }
+        restrictBead.restrict = value;
     }
 
     //----------------------------------
diff --git a/frameworks/projects/SparkRoyale/src/main/royale/spark/components/TextInput.as b/frameworks/projects/SparkRoyale/src/main/royale/spark/components/TextInput.as
index f67eb83..0148f07 100644
--- a/frameworks/projects/SparkRoyale/src/main/royale/spark/components/TextInput.as
+++ b/frameworks/projects/SparkRoyale/src/main/royale/spark/components/TextInput.as
@@ -33,6 +33,7 @@ COMPILE::JS
 
 import org.apache.royale.core.ITextModel;
 import org.apache.royale.events.Event;
+import org.apache.royale.html.accessories.RestrictTextInputBead;
 import mx.core.mx_internal;
 import mx.events.FlexEvent;
     
@@ -339,13 +340,31 @@ public class TextInput extends SkinnableTextBase
             //dispatchEvent(new Event('htmlTextChanged'));
         }  
     }
+    
+    private var restrictBead:RestrictTextInputBead;
+    
+    override public function get restrict():String 
+    {
+        if (!restrictBead) return null;
+        return restrictBead.restrict;
+    }
 
+    override public function set restrict(value:String):void
+    {
+        if (!restrictBead)
+        {
+            restrictBead = new RestrictTextInputBead();
+            addBead(restrictBead);
+        }
+        restrictBead.restrict = value;
+    }
+    
     COMPILE::JS
 	override protected function createElement():WrappedHTMLElement
 	{
 		addElementToWrapper(this,'input');
 		element.setAttribute('type', 'text');
-		
+        
 		//attach input handler to dispatch royale change event when user write in textinput
 		//goog.events.listen(element, 'change', killChangeHandler);
 		goog.events.listen(element, 'input', textChangeHandler);