From 064b82eab734d3ad97c719d3758b0e1f17cc1fad Mon Sep 17 00:00:00 2001 From: Alexey Kryazhev Date: Tue, 30 Jan 2018 20:45:54 +0300 Subject: [PATCH] fix #32 --- guide.html | 9 ++++++--- src/core/change-details.js | 2 +- src/masked/dynamic.js | 31 ++++++++++++++++++++----------- src/masked/pattern.js | 2 +- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/guide.html b/guide.html index 648966bf..95316439 100644 --- a/guide.html +++ b/guide.html @@ -619,18 +619,21 @@

Dynamic

var dispatchMask = new IMask(element, {
     mask: [
       {
-        mask: '+{3\\0} {21} 0 000 0000',
+        mask: '+00 {21} 0 000 0000',
         startsWith: '30',
+        lazy: false,
         country: 'Greece'
       },
       {
-        mask: '+{7} 000 000-00-00',
+        mask: '+0 000 000-00-00',
         startsWith: '7',
+        lazy: false,
         country: 'Russia'
       },
       {
-        mask: '+{91}-0000-000000',
+        mask: '+00-0000-000000',
         startsWith: '91',
+        lazy: false,
         country: 'India'
       },
       {
diff --git a/src/core/change-details.js b/src/core/change-details.js
index ef571510..e99238f4 100644
--- a/src/core/change-details.js
+++ b/src/core/change-details.js
@@ -36,10 +36,10 @@ class ChangeDetails {
     @returns {ChangeDetails} `this`
   */
   aggregate (details: ChangeDetails): ChangeDetails {
+    if (details.rawInserted) this.rawInserted += details.rawInserted;
     this.inserted += details.inserted;
     this.shift += details.shift;
     this.overflow = this.overflow || details.overflow;
-    if (details.rawInserted) this.rawInserted += details.rawInserted;
     return this;
   }
 
diff --git a/src/masked/dynamic.js b/src/masked/dynamic.js
index bdf39500..6e755ad5 100644
--- a/src/masked/dynamic.js
+++ b/src/masked/dynamic.js
@@ -54,14 +54,18 @@ class MaskedDynamic extends Masked {
     return details;
   }
 
-  _applyDispatch (appended: string, ...args: *) {
+  _applyDispatch (appended: string='', ...args: *) {
     const oldValueLength = this.value.length;
     const inputValue = this.rawInputValue;
     const oldMask = this.currentMask;
     const details = new ChangeDetails();
 
+    // dispatch SHOULD NOT modify mask
     this.currentMask = this.doDispatch(appended, ...args);
+
+    // restore state after dispatch
     if (this.currentMask && this.currentMask !== oldMask) {
+      // if mask changed reapply input
       this.currentMask.reset();
       // $FlowFixMe - it's ok, we don't change current mask
       this.currentMask._append(inputValue, {raw: true});
@@ -141,7 +145,7 @@ class MaskedDynamic extends Masked {
     if (this.currentMask) {
       details.aggregate(this.currentMask.remove(...args))
         // update with dispatch
-        .aggregate(this._applyDispatch(''));
+        .aggregate(this._applyDispatch());
     }
 
     return details;
@@ -168,10 +172,13 @@ class MaskedDynamic extends Masked {
   /**
     @override
   */
-  _appendTail (...args: *): ChangeDetails {
-    return this.currentMask ?
-      this.currentMask._appendTail(...args) :
-      super._appendTail(...args);
+  _appendTail (tail?: TailDetails): ChangeDetails {
+    const details = new ChangeDetails();
+    if (tail) details.aggregate(this._applyDispatch(tail.value));
+
+    return details.aggregate(this.currentMask ?
+      this.currentMask._appendTail(tail) :
+      super._appendTail(tail));
   }
 
   /**
@@ -198,14 +205,16 @@ MaskedDynamic.DEFAULTS = {
 
     const inputValue = masked.rawInputValue;
 
-    // update all
-    masked.compiledMasks.forEach(cm => {
-      cm.rawInputValue = inputValue;
-      cm._append(appended, flags);
+    // simulate input
+    const inputs = masked.compiledMasks.map((cm, index) => {
+      const m = cm.clone();
+      m.rawInputValue = inputValue;
+      m._append(appended, flags);
+
+      return {value: m.rawInputValue.length, index};
     });
 
     // pop masks with longer values first
-    const inputs = masked.compiledMasks.map((cm, index) => ({value: cm.rawInputValue.length, index}));
     inputs.sort((i1, i2) => i2.value - i1.value);
 
     return masked.compiledMasks[inputs[0].index];
diff --git a/src/masked/pattern.js b/src/masked/pattern.js
index 1eb8cf0e..614fd477 100644
--- a/src/masked/pattern.js
+++ b/src/masked/pattern.js
@@ -457,7 +457,7 @@ class MaskedPattern extends Masked {
     }
 
     // for lazy if has aligned left inside fixed and has came to the start - use start position
-    if (direction === DIRECTION.LEFT && di === 0 && this.lazy &&
+    if (direction === DIRECTION.LEFT && di === 0 && this.lazy && !this.extractInput() &&
       (!initialDef || !initialDef.isInput)) firstInputIndex = 0;
 
     if (direction === DIRECTION.LEFT || firstInputIndex == null) {