forked from esbanarango/ember-place-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISSUE-105: fix: Clean DOM only when possible (esbanarango#108)
* ISSUE-105: fix: Clean DOM only when possible * Only remove Google elements when there are not more active components. I have to review if there might be race conditions when adding or removing components. * Add service to manage Google autocomplete instances global state. * Update Mocha and update tests. * Force build * Force build in Travis beta
- Loading branch information
Showing
11 changed files
with
1,207 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,8 @@ | |
/package.json.ember-try | ||
|
||
#nvm | ||
.nvmrc | ||
.nvmrc | ||
|
||
# VSCODE | ||
|
||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Service from '@ember/service'; | ||
|
||
export default Service.extend({ | ||
init() { | ||
this._super(...arguments); | ||
|
||
this.set('numberOfActiveAutoCompleteFields', 0); | ||
}, | ||
|
||
/** | ||
* @description Increments the counter of active components. | ||
* Intended to be used everytime a new place-autocomplete-filed is | ||
* instanciated. | ||
*/ | ||
register() { | ||
this.incrementProperty('numberOfActiveAutoCompleteFields'); | ||
}, | ||
|
||
/** | ||
* @description Decrements the counter of active components. | ||
* Intended to be used everytime a new place-autocomplete-filed is | ||
* going to be destroyed. | ||
*/ | ||
unregister() { | ||
this.decrementProperty('numberOfActiveAutoCompleteFields'); | ||
}, | ||
|
||
/** | ||
* @description Cleanup DOM when ALL component instances of place-autocomplete-field | ||
* are removed from the DOM. If there are still components active, it does nothing. | ||
* | ||
* @returns { Boolean } - Indicates whether the DOM was cleaned or not. | ||
*/ | ||
removePlacesAutoCompleteContainersIfRequired() { | ||
if (!document || this.numberOfActiveAutoCompleteFields > 0) { | ||
return false; | ||
} | ||
|
||
const pacContainers = document.querySelectorAll('.pac-container'); | ||
|
||
for (let index = 0; pacContainers.length > index; index++) { | ||
pacContainers[index].parentNode.removeChild(pacContainers[index]); | ||
} | ||
|
||
return true; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-place-autocomplete/services/google-place-autocomplete/manager'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import Application from '../app'; | ||
import config from '../config/environment'; | ||
import { setApplication } from '@ember/test-helpers'; | ||
import { start } from 'ember-mocha'; | ||
|
||
setApplication(Application.create(config.APP)); | ||
start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/unit/services/google-place-autocomplete/manager-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
import { setupTest } from 'ember-mocha'; | ||
|
||
describe('Unit | Service | google-place-autocomplete/manager', function() { | ||
setupTest(); | ||
|
||
// Replace this with your real tests. | ||
it('exists', function() { | ||
let service = this.owner.lookup('service:google-place-autocomplete/manager'); | ||
expect(service).to.be.ok; | ||
}); | ||
}); |
Oops, something went wrong.