Skip to content

Commit

Permalink
Add deep linking of dbt docs (#1038)
Browse files Browse the repository at this point in the history
Ties the URL in the browser to the URL of the dbt docs

Resolves #1029
  • Loading branch information
dwreeves authored Jun 10, 2024
1 parent 74c28e2 commit 3c9cf6f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
20 changes: 20 additions & 0 deletions cosmos/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,26 @@ def open_file(path: str, conn_id: Optional[str] = None) -> str:
)
}
}
// Prevent parent hash changes from sending a message back to the parent.
// This is necessary for making sure the browser back button works properly.
let hashChangeLock = true;
window.addEventListener('hashchange', function () {
if (!hashChangeLock) {
window.parent.postMessage(window.location.hash);
}
hashChangeLock = false;
});
window.addEventListener('message', function (event) {
let msgData = event.data;
if (typeof msgData === 'string' && msgData.startsWith('#!')) {
let updateUrl = new URL(window.location);
updateUrl.hash = msgData;
hashChangeLock = true;
history.replaceState(null, null, updateUrl);
}
});
</script>
"""

Expand Down
22 changes: 21 additions & 1 deletion cosmos/plugin/templates/dbt_docs.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends base_template %}
{% block page_title %}dbt docs - {{ appbuilder.app_name }}{% endblock %}
{% block content %}
<script src="{{ url_for('.static', filename='iframeResizer.min.js') }}"></script>
<iframe id="dbtIframe" src="{{ url_for('.dbt_docs_index') }}" style="min-width: 100%; border: 0;"></iframe>
Expand All @@ -10,6 +11,25 @@
minHeight: 500
},
'#dbtIframe'
)
);

window.addEventListener('message', function (event) {
let msgData = event.data;
if (msgData.startsWith('#!')) {
let updateUrl = new URL(window.location);
updateUrl.hash = msgData;
history.replaceState(null, null, updateUrl);
}
});

window.addEventListener('popstate', function () {
dbtIframe.contentWindow.postMessage(window.location.hash);
});

let dbtIframe = document.getElementById('dbtIframe');
let iframeUrl = new URL(dbtIframe.src);
iframeUrl.hash = window.location.hash;
dbtIframe.src = iframeUrl.href;

</script>
{% endblock %}

0 comments on commit 3c9cf6f

Please sign in to comment.