Skip to content

Commit

Permalink
Add import-map-overrides:init event. (#49)
Browse files Browse the repository at this point in the history
* Add import-map-overrides:init event.

* Docs
  • Loading branch information
joeldenning committed Jan 22, 2021
1 parent 9517630 commit 6e0f50e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
12 changes: 12 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ window.importMapOverrides.isExternalMapValid(

### Events

#### Init

The import-map-overrides library fires an event called `import-map-overrides:init` on the window when the library has successfully initialized. Note that this event will not fire if import-map-overrides is disabled.

```sh
window.addEventListener("import-map-overrides:init", () => {
console.log('init');
})
```

#### Change

The import-map-overrides library fires an event called `import-map-overrides:change` on the window whenever the
override import map changes. The event is a [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent)
that has no [detail property](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/detail).
Expand Down
14 changes: 11 additions & 3 deletions src/api/js-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,17 @@ function init() {
}

function fireChangedEvent() {
fireEvent("change");
}

function fireEvent(type) {
// Set timeout so that event fires after the change has totally finished
setTimeout(() => {
if (canFireCustomEvents) {
window.dispatchEvent(new CustomEvent("import-map-overrides:change"));
}
const eventType = `import-map-overrides:${type}`;

This comment has been minimized.

Copy link
@nilzona

nilzona Jun 29, 2021

Contributor

Has this change been tested on internet explorer 11 ? .. I'm getting a "NotSupportedError" when running import-map-overrides in IE 11. And it's complaining on this line. Templates are not supported in IE11

This comment has been minimized.

Copy link
@joeldenning

joeldenning Jul 4, 2021

Author Member

Discussed in #58

const event = canFireCustomEvents
? new CustomEvent(eventType)
: document.createEvent(eventType);
window.dispatchEvent(event);
});
}

Expand Down Expand Up @@ -425,6 +431,8 @@ function init() {
}
}

fireEvent("init");

function insertOverrideMap(map, id, referenceNode) {
const overrideMapElement = document.createElement("script");
overrideMapElement.type = importMapType;
Expand Down
5 changes: 5 additions & 0 deletions test/embedded-map.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
}
}
</script>
<script>
window.addEventListener("import-map-overrides:init", () => {
console.log("IMO init fired");
});
</script>
<script src="/import-map-overrides.js"></script>
<script src="https://cdn.jsdelivr.net/npm/systemjs/dist/system.js"></script>
</head>
Expand Down

0 comments on commit 6e0f50e

Please sign in to comment.