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

piotrz 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 47c1714a85 Jewel: Add RestrictRegExp which is essentially a copy of 
Restrict, but with RegExp property
47c1714a85 is described below

commit 47c1714a853046f733b3167dfcbf89ce48230f27
Author: Piotr Zarzycki <[email protected]>
AuthorDate: Mon Jul 1 18:11:28 2024 +0200

    Jewel: Add RestrictRegExp which is essentially a copy of Restrict, but with 
RegExp property
---
 .../beads/controls/textinput/RestrictRegExp.as     | 139 +++++++++++++++++++++
 1 file changed, 139 insertions(+)

diff --git 
a/frameworks/projects/Jewel/src/main/royale/org/apache/royale/jewel/beads/controls/textinput/RestrictRegExp.as
 
b/frameworks/projects/Jewel/src/main/royale/org/apache/royale/jewel/beads/controls/textinput/RestrictRegExp.as
new file mode 100644
index 0000000000..b4ecb91532
--- /dev/null
+++ 
b/frameworks/projects/Jewel/src/main/royale/org/apache/royale/jewel/beads/controls/textinput/RestrictRegExp.as
@@ -0,0 +1,139 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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.jewel.beads.controls.textinput
+{
+       import org.apache.royale.core.IBead;
+       import org.apache.royale.core.IStrand;
+       import org.apache.royale.events.Event;
+       import org.apache.royale.jewel.supportClasses.textinput.TextInputBase;
+       
+       /**
+        *  The Restrict bead class is a specialty bead that can be used with
+        *  any Jewel TextInputBase control. The bead uses a reg exp pattern to 
validate
+        *  input from user. A text property allows to configure error text.
+        *  
+        *  pattern examples:
+        *  Numeric ony pattern = [^0-9]
+        *  Letters only pattern = [^a-zA-Z]
+        *  Numeric and letters only pattern = [^0-9a-zA-Z]
+        *  
+        *  @langversion 3.0
+        *  @playerversion Flash 10.2
+        *  @playerversion AIR 2.6
+        *  @productversion Royale 0.9.4
+        */
+       public class RestrictRegExp implements IBead
+       {
+               /**
+                *  constructor.
+                *
+                *  @langversion 3.0
+                *  @playerversion Flash 10.2
+                *  @playerversion AIR 2.6
+                *  @productversion Royale 0.9.11
+                */
+               public function RestrictRegExp()
+               {
+               }
+               
+               private var _pattern:RegExp;
+
+               /**
+                *  The RegExp to restrict TextInput.
+                *
+                *  @langversion 3.0
+                *  @playerversion Flash 10.2
+                *  @playerversion AIR 2.6
+                *  @productversion Royale 0.9.11
+                */
+               public function get pattern():RegExp
+               {
+                       return _pattern;
+               }
+
+               public function set pattern(value:RegExp):void
+               {
+                   if (_pattern != value)
+                   {
+                           _pattern = value;
+                           updateTextToRestriction();
+                       }
+               }
+
+               private var host:TextInputBase;
+
+               /**
+                *  @copy org.apache.royale.core.IBead#strand
+                *  
+                *  @langversion 3.0
+                *  @playerversion Flash 10.2
+                *  @playerversion AIR 2.6
+                *  @productversion Royale 0.9.11
+                *  @royaleignorecoercion org.apache.royale.core.UIBase
+                */
+               public function set strand(value:IStrand):void
+               {
+                       host = value as TextInputBase;
+                       host.addEventListener(Event.CHANGE, 
restrictTextToPattern);
+                       updateTextToRestriction();
+               }
+
+               /**
+                * Restrict the text to the reg exp pattern as user types
+                * @private
+                */
+               protected function restrictTextToPattern(event:Event):void
+               {
+                       COMPILE::JS
+                       {
+                       var start:Number = host.input.selectionStart;
+                       var end:Number = host.input.selectionEnd;
+                       var textChanged:Boolean = updateTextToRestriction();
+                       if(textChanged)
+                               host.input.setSelectionRange(start, end);
+                       else
+                               host.input.setSelectionRange(start - 1, end - 
1);
+                       }
+               }
+
+               /**
+                * update the text in the input to the restriction pattern
+                * 
+                * @return true if text changed, false otherwise
+                */
+               protected function updateTextToRestriction():Boolean
+               {
+                       var textChanged:Boolean = false;
+                       if(!host) 
+                               return textChanged;
+                       const re:RegExp = pattern;
+                       COMPILE::JS
+                       {
+                               const oldText:String = host.input.value;
+                               const restrictedText:String = 
oldText.replace(re, '');
+                               if (oldText != restrictedText) {
+                                       host.input.value = restrictedText;
+                               } else {
+                                       textChanged = true;
+                               }
+                       }
+                       return textChanged;
+               }
+       }
+}

Reply via email to