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

Add deep linking of dbt docs #1038

Merged
merged 2 commits into from
Jun 10, 2024
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
20 changes: 20 additions & 0 deletions cosmos/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ def open_file(path: str) -> 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 %}
Loading