Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix option lazyLoad: false #154

Merged
merged 5 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 38 additions & 19 deletions addon/components/phone-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,37 @@ export default Component.extend({
didInsertElement() {
this._super(...arguments);

this._loadAndSetup();
},

// this is a trick to format the number on user input
didRender() {
this._super(...arguments);

this._formatNumber();
},

willDestroyElement() {
this._iti.destroy();
this.element.removeEventListener('countrychange', this._onCountryChange);

this._super(...arguments);
},

async _loadAndSetup() {
await this.phoneInput.load();

this._setupLibrary();

this._formatNumber();

this.element.addEventListener(
'countrychange',
this._onCountryChange.bind(this)
);
},

_setupLibrary() {
const {
allowDropdown,
autoPlaceholder,
Expand All @@ -173,29 +204,19 @@ export default Component.extend({
separateDialCode
});

const number = this.number;
if (number) {
_iti.setNumber(number);
if (this.number) {
_iti.setNumber(this.number);
}
this._iti = _iti;

if (this.initialCountry) {
this._iti.setCountry(this.initialCountry);
}

this.update(number, this._metaData(_iti));

this.onCountryChange = () => {
this._iti.setCountry(this._iti.getSelectedCountryData().iso2);
this.input();
};
this.element.addEventListener('countrychange', this.onCountryChange);
this.update(this.number, this._metaData(_iti));
},

// this is a trick to format the number on user input
didRender() {
this._super(...arguments);

_formatNumber() {
if (!this._iti) {
return;
}
Expand All @@ -209,11 +230,9 @@ export default Component.extend({
}
},

willDestroyElement() {
this._iti.destroy();
this.element.removeEventListener('countrychange', this.onCountryChange);

this._super(...arguments);
_onCountryChange() {
this._iti.setCountry(this._iti.getSelectedCountryData().iso2);
this.input();
},

_metaData(iti) {
Expand Down
36 changes: 0 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
'use strict';

const Funnel = require('broccoli-funnel');
const MergeTrees = require('broccoli-merge-trees');
const path = require('path');

const scriptsDestDir = 'assets/ember-phone-input/scripts/';
const intlTelInputScriptName = 'intlTelInput.min.js';
const utilsScriptName = 'utils.js';

module.exports = {
name: 'ember-phone-input',

Expand All @@ -33,33 +25,5 @@ module.exports = {
// it get merged into vendor.css
app.import('node_modules/intl-tel-input/build/css/intlTelInput.css');
app.import('vendor/ember-phone-input.css');
},

treeForPublic() {
// copy these files to destDir
// to be able to lazyLoad them || not to bundle them into vendor.js
const intlTelInputPath = path.resolve(
require.resolve('intl-tel-input'),
'..'
);
const intlTelInputFiles = new Funnel(intlTelInputPath, {
srcDir: '/build/js',
include: [intlTelInputScriptName, utilsScriptName],
destDir: `/${scriptsDestDir}`
});

return new MergeTrees([intlTelInputFiles]);
},

contentFor(type, config) {
const { phoneInput, rootURL } = config;
const shouldLazyLoad = phoneInput ? phoneInput.lazyLoad : false;

if (type === 'body-footer' && !shouldLazyLoad) {
return `
<script type="text/javascript" src="${rootURL}${scriptsDestDir}${intlTelInputScriptName}"></script>
<script type="text/javascript" src="${rootURL}${scriptsDestDir}${utilsScriptName}"></script>
`;
}
}
};
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
"@ember/optional-features": "^1.0.0",
"babel-eslint": "^10.0.1",
"broccoli-asset-rev": "^3.0.0",
"broccoli-funnel": "^2.0.1",
"broccoli-merge-trees": "^3.0.1",
"ember-cli": "~3.13.1",
"ember-cli-addon-docs": "^0.6.1",
"ember-cli-addon-docs-yuidoc": "^0.2.1",
Expand Down
12 changes: 1 addition & 11 deletions tests/integration/components/phone-input-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ module('Integration | Component | phone-input', function(hooks) {
test('renders an input of type tel', async function(assert) {
assert.expect(1);

await this.owner.lookup('service:phone-input').load();

await render(hbs`{{phone-input number='1111'}}`);

assert.dom('input').hasAttribute('type', 'tel');
Expand All @@ -23,8 +21,6 @@ module('Integration | Component | phone-input', function(hooks) {
test('renders the value', async function(assert) {
assert.expect(3);

await this.owner.lookup('service:phone-input').load();

const newValue = '2';
this.set('number', null);
this.set('update', () => {});
Expand All @@ -46,8 +42,6 @@ module('Integration | Component | phone-input', function(hooks) {
test('renders the value with separate dial code option', async function(assert) {
assert.expect(3);

await this.owner.lookup('service:phone-input').load();

const newValue = '2';
this.set('separateDialNumber', null);
this.set('update', value => {
Expand All @@ -69,8 +63,6 @@ module('Integration | Component | phone-input', function(hooks) {
test('can update the country', async function(assert) {
assert.expect(2);

await this.owner.lookup('service:phone-input').load();

const country = 'us';
this.set('number', null);
this.set('update', () => {});
Expand All @@ -87,11 +79,9 @@ module('Integration | Component | phone-input', function(hooks) {
assert.dom('.iti-flag').hasClass('nz');
});

test('can update the country', async function(assert) {
test('phoneNumber is correctly invalid when country is changed', async function(assert) {
assert.expect(2);

await this.owner.lookup('service:phone-input').load();

const country = 'fr';
const validFrenchNumber = '0622334455';
this.set('number', null);
Expand Down