Skip to content

Commit

Permalink
replaced americaMode with more generic nationalMode
Browse files Browse the repository at this point in the history
  • Loading branch information
jackocnr committed Mar 3, 2014
1 parent 84e4250 commit 0f80221
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 99 deletions.
25 changes: 11 additions & 14 deletions build/js/intlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ author: Jack O'Connor (http://jackocnr.com)

var pluginName = "intlTelInput", id = 1, // give each instance it's own id for namespaced event handling
defaults = {
// don't display the +1 prefix when America is selected
americaMode: false,
// don't insert international dial codes
nationalMode: false,
// if there is just a dial code in the input: remove it on blur, and re-add it on focus
autoHideDialCode: true,
// default country
Expand Down Expand Up @@ -201,8 +201,8 @@ Plugin.prototype = {
// initialise the main event listeners: input keyup, and click selected flag
_initListeners: function() {
var that = this;
// auto hide dial code option
if (this.options.autoHideDialCode) {
// auto hide dial code option (ignore if in national mode)
if (this.options.autoHideDialCode && !this.options.nationalMode) {
this._initAutoHideDialCode();
}
// update flag on keyup (by extracting the dial code from the input value).
Expand Down Expand Up @@ -414,8 +414,8 @@ Plugin.prototype = {
},
// reset the input value to just a dial code
_resetToDialCode: function(dialCode) {
// if the dialCode is for America, and americaMode is enabled, then don't insert the dial code
var value = dialCode == "1" && this.options.americaMode ? "" : "+" + dialCode + this.options.dialCodeDelimiter;
// if nationalMode is enabled then don't insert the dial code
var value = this.options.nationalMode ? "" : "+" + dialCode + this.options.dialCodeDelimiter;
this.telInput.val(value);
},
// remove highlighting from other list items and highlight the given item
Expand Down Expand Up @@ -452,9 +452,10 @@ Plugin.prototype = {
this._selectFlag(countryCode);
this._closeDropdown();
// update input value
var newNumber = this._updateNumber("+" + listItem.attr("data-dial-code"));
this.telInput.val(newNumber);
this.telInput.trigger("change");
if (!this.options.nationalMode) {
this._updateNumber("+" + listItem.attr("data-dial-code"));
this.telInput.trigger("change");
}
// focus the input
this._focus();
},
Expand Down Expand Up @@ -498,11 +499,7 @@ Plugin.prototype = {
var existingNumber = inputVal && inputVal.substr(0, 1) != "+" ? $.trim(inputVal) : "";
newNumber = newDialCode + this.options.dialCodeDelimiter + existingNumber;
}
// if americaMode is enabled, we dont display the dial code for american numbers
if (this.options.americaMode && newNumber.substring(0, 3) == "+1" + this.options.dialCodeDelimiter) {
newNumber = newNumber.substring(3);
}
return newNumber;
this.telInput.val(newNumber);
},
// try and extract a valid international dial code from a full telephone number
// Note: returns the raw string inc plus character and any whitespace/dots etc
Expand Down
2 changes: 1 addition & 1 deletion build/js/intlTelInput.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@

<script src="src/spec/tests/methods/setNumber.js"></script>

<script src="src/spec/tests/options/americaMode.js"></script>

<script src="src/spec/tests/options/autoHideDialCode.js"></script>

<script src="src/spec/tests/options/defaultCountry.js"></script>

<script src="src/spec/tests/options/defaultStyling.js"></script>

<script src="src/spec/tests/options/nationalMode.js"></script>

<script src="src/spec/tests/options/onlyCountries.js"></script>

<script src="src/spec/tests/options/preferredCountries.js"></script>
Expand Down
27 changes: 12 additions & 15 deletions src/js/intlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
var pluginName = "intlTelInput",
id = 1, // give each instance it's own id for namespaced event handling
defaults = {
// don't display the +1 prefix when America is selected
americaMode: false,
// don't insert international dial codes
nationalMode: false,
// if there is just a dial code in the input: remove it on blur, and re-add it on focus
autoHideDialCode: true,
// default country
Expand Down Expand Up @@ -225,8 +225,8 @@
_initListeners: function() {
var that = this;

// auto hide dial code option
if (this.options.autoHideDialCode) {
// auto hide dial code option (ignore if in national mode)
if (this.options.autoHideDialCode && !this.options.nationalMode) {
this._initAutoHideDialCode();
}

Expand Down Expand Up @@ -478,7 +478,7 @@
alreadySelected = true;
}
});

if (!alreadySelected) {
this._selectFlag(countryCodes[0]);
}
Expand All @@ -488,8 +488,8 @@

// reset the input value to just a dial code
_resetToDialCode: function(dialCode) {
// if the dialCode is for America, and americaMode is enabled, then don't insert the dial code
var value = (dialCode == "1" && this.options.americaMode) ? "" : "+" + dialCode + this.options.dialCodeDelimiter;
// if nationalMode is enabled then don't insert the dial code
var value = (this.options.nationalMode) ? "" : "+" + dialCode + this.options.dialCodeDelimiter;
this.telInput.val(value);
},

Expand Down Expand Up @@ -537,9 +537,10 @@
this._closeDropdown();

// update input value
var newNumber = this._updateNumber("+" + listItem.attr("data-dial-code"));
this.telInput.val(newNumber);
this.telInput.trigger("change");
if (!this.options.nationalMode) {
this._updateNumber("+" + listItem.attr("data-dial-code"));
this.telInput.trigger("change");
}

// focus the input
this._focus();
Expand Down Expand Up @@ -604,11 +605,7 @@
newNumber = newDialCode + this.options.dialCodeDelimiter + existingNumber;
}

// if americaMode is enabled, we dont display the dial code for american numbers
if (this.options.americaMode && newNumber.substring(0, 3) == "+1"+this.options.dialCodeDelimiter) {
newNumber = newNumber.substring(3);
}
return newNumber;
this.telInput.val(newNumber);
},


Expand Down
48 changes: 0 additions & 48 deletions src/spec/tests/options/americaMode.js

This file was deleted.

1 change: 1 addition & 0 deletions src/spec/tests/options/autoHideDialCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("testing autoHideDialCode option", function() {
input.intlTelInput({
autoHideDialCode: true
});
// must be in DOM for focus to work
getParentElement().appendTo($("body"));
});

Expand Down
20 changes: 3 additions & 17 deletions src/spec/tests/options/defaultCountry.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,12 @@ describe("init plugin with a default country", function() {
input = null;
});

it("defaults to that country", function() {
it("sets the selected flag correctly", function() {
expect(getSelectedFlagElement()).toHaveClass(defaultCountry);
});


describe("typing a number with a different dial code", function() {

beforeEach(function() {
input.val("+44 1234567").keyup();
});

it("updates the selected flag", function() {
expect(getSelectedFlagElement()).toHaveClass("gb");
});

it("clearing the input again defaults to the right flag", function() {
input.val("").keyup();
expect(getSelectedFlagElement()).toHaveClass(defaultCountry);
});

it("sets the active list item correctly", function() {
expect(getActiveListItem().attr("data-country-code")).toEqual(defaultCountry);
});

});
38 changes: 38 additions & 0 deletions src/spec/tests/options/nationalMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";

describe("init plugin with nationalMode set to true", function() {

beforeEach(function() {
input = $("<input>");
input.intlTelInput({
nationalMode: true
});
// must be in DOM for focus to work
getParentElement().appendTo($("body"));
});

afterEach(function() {
getParentElement().remove();
input = null;
});

it("defaults to no dial code", function() {
expect(getInputVal()).toEqual("");
});

it("focusing the input does not insert the dial code", function() {
input.focus();
expect(getInputVal()).toEqual("");
});

it("selecting another country does not insert the dial code", function() {
selectFlag("gb");
expect(getInputVal()).toEqual("");
});

it("but typing a dial code does still update the selected country", function() {
input.val("+44 1234567").keyup();
expect(getSelectedFlagElement()).toHaveClass("gb");
});

});
4 changes: 2 additions & 2 deletions src/spec/tests/vanilla/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ describe("init vanilla plugin", function() {
expect(getSelectedFlagElement()).toHaveClass("gb");
});

it("clearing the input again defaults to the right flag", function() {
it("clearing the input again does not change the selected flag", function() {
input.val("").keyup();
expect(getSelectedFlagElement()).toHaveClass("us");
expect(getSelectedFlagElement()).toHaveClass("gb");
});

});
Expand Down

0 comments on commit 0f80221

Please sign in to comment.