Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

Commit

Permalink
Fixes editing issue with politespace-us-telephone plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Sep 26, 2016
1 parent 34ceb6f commit 78c93db
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ <h2>input type="tel"</h2>
<input type="tel" data-politespace data-politespace-strip="[^\d]*" data-grouplength="4,3,3" data-reverse value="2223334444">
</label>
<label>
<code>input[type=tel]</code> (US Phone Number, strips country code transparently) with <code>data-politespace-us-telephone data-grouplength="4,3,3" data-reverse data-delimiter="-"</code>
<code>input[type=tel]</code> (US Phone Number, strips country code transparently) with <code>data-politespace-us-telephone data-grouplength="3,3,4" data-reverse data-delimiter="-"</code>
<input type="tel" data-politespace data-politespace-us-telephone data-grouplength="4,3,3" data-reverse data-delimiter="-" value="12223334444" maxlength="10">
</label>

Expand Down
2 changes: 2 additions & 0 deletions src/politespace-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
polite.updateProxy();
})
.bind( "blur", function() {
$( this ).trigger( "politespace-beforeblur" );

polite.update();

if( polite.useProxy() ){
Expand Down
34 changes: 22 additions & 12 deletions src/politespace-us-telephone.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,46 @@
(function( w, $ ) {
"use strict";

var maxlengthCacheKey = "politespace-us-telephone-maxlength";
var eventName = "politespace-beforeblur.politespace-us-telephone";

function cleanup( el ) {
var $t = $( el );
$t.val( $t.val().replace( /^1/, "" ) );
var val = $t.val();

$t.val( val.replace( /^1/, "" ) );
}

$( document ).bind( "politespace-init politespace-input", function( event ) {
// On init
$( document ).bind( "politespace-init", function( event ) {
var $t = $( event.target );
if( !$t.is( "[data-politespace-us-telephone]" ) ) {
return;
}
var val = $t.val();

// Adjust maxlength
var maxlength= $t.attr( "maxlength" );
var maxlengthCacheKey = "politespace-us-telephone-maxlength";
var maxlengthCache = $t.data( maxlengthCacheKey );

if( maxlength && !maxlengthCache ) {
maxlengthCache = maxlength;
$t.data( maxlengthCacheKey, maxlength );
if( maxlength ) {
$t.data( maxlengthCacheKey, parseInt( maxlength, 10 ) );

cleanup( $t[ 0 ] );
$t.one( "blur", function() {
$( this ).attr( "maxlength", maxlength );
$t.off( eventName ).on( eventName, function() {
$( this ).attr( "maxlength", $t.data( maxlengthCacheKey ) );
cleanup( this );
});
}
});

// On input
$( document ).bind( "politespace-input", function( event ) {
var $t = $( event.target );
if( !$t.is( "[data-politespace-us-telephone]" ) ) {
return;
}

if( val.indexOf( '1' ) === 0 ) {
$t.attr( "maxlength", parseInt( maxlengthCache, 10 ) + 1 );
if( $t.val().indexOf( '1' ) === 0 ) {
$t.attr( "maxlength", $t.data( maxlengthCacheKey ) + 1 );
}
});

Expand Down

0 comments on commit 78c93db

Please sign in to comment.