Add loading callback to widget

This commit is contained in:
Raphael Michel
2019-10-23 15:09:20 +02:00
parent 277b7ad71c
commit 08460f9918
2 changed files with 29 additions and 0 deletions

View File

@@ -206,6 +206,21 @@ If you want, you can suppress us loading the widget and/or modify the user data
If you then later want to trigger loading the widgets, just call ``window.PretixWidget.buildWidgets()``. If you then later want to trigger loading the widgets, just call ``window.PretixWidget.buildWidgets()``.
Waiting for the widget to load
------------------------------
If you want to run custom JavaScript once the widget is fully loaded, you can register a callback function. Note that
this function might be run multiple times, for example if you have multiple widgets on a page or if the user switches
e.g. from an event list to an event detail view::
<script type="text/javascript">
window.pretixWidgetCallback = function () {
window.PretixWidget.addLoadListener(function () {
console.log("Widget has loaded!");
});
}
</script>
Passing user data to the widget Passing user data to the widget
------------------------------- -------------------------------

View File

@@ -784,6 +784,7 @@ Vue.component('pretix-widget-event-form', {
this.$root.target_url = this.$root.parent_stack.pop(); this.$root.target_url = this.$root.parent_stack.pop();
this.$root.error = null; this.$root.error = null;
this.$root.subevent = null; this.$root.subevent = null;
this.$root.trigger_load_callback();
if (this.$root.events !== undefined) { if (this.$root.events !== undefined) {
this.$root.view = "events"; this.$root.view = "events";
} else { } else {
@@ -1086,6 +1087,13 @@ var shared_root_methods = {
return; return;
} }
}, },
trigger_load_callback: function () {
this.$nextTick(function () {
for (var i = 0; i < window.PretixWidget._loaded.length; i++) {
window.PretixWidget._loaded[i]()
}
});
},
reload: function () { reload: function () {
var url; var url;
if (this.$root.subevent) { if (this.$root.subevent) {
@@ -1148,6 +1156,7 @@ var shared_root_methods = {
} }
if (root.loading > 0) { if (root.loading > 0) {
root.loading--; root.loading--;
root.trigger_load_callback();
} }
}, function (error) { }, function (error) {
root.categories = []; root.categories = [];
@@ -1155,6 +1164,7 @@ var shared_root_methods = {
root.error = strings['loading_error']; root.error = strings['loading_error'];
if (root.loading > 0) { if (root.loading > 0) {
root.loading--; root.loading--;
root.trigger_load_callback();
} }
}); });
}, },
@@ -1385,6 +1395,10 @@ var create_button = function (element) {
/* Find all widgets on the page and render them */ /* Find all widgets on the page and render them */
widgetlist = []; widgetlist = [];
buttonlist = []; buttonlist = [];
window.PretixWidget._loaded = [];
window.PretixWidget.addLoadListener = function (f) {
window.PretixWidget._loaded.push(f);
}
window.PretixWidget.buildWidgets = function () { window.PretixWidget.buildWidgets = function () {
document.createElement("pretix-widget"); document.createElement("pretix-widget");
document.createElement("pretix-button"); document.createElement("pretix-button");