Skip to content

Commit

Permalink
Merge pull request #1038 from zauberzeug/sources-behind-proxy
Browse files Browse the repository at this point in the history
Sources behind proxy #934
  • Loading branch information
rodja committed Jun 18, 2023
2 parents dc91187 + a72872b commit 66f1b11
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
18 changes: 17 additions & 1 deletion nicegui/elements/audio.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
export default {
template: `<audio :controls="controls" :autoplay="autoplay" :muted="muted" :src="src" />`,
template: `<audio :controls="controls" :autoplay="autoplay" :muted="muted" :src="computed_src" />`,
props: {
controls: Boolean,
autoplay: Boolean,
muted: Boolean,
src: String,
},
data: function () {
return {
computed_src: undefined,
};
},
mounted() {
setTimeout(() => this.compute_src(), 0); // NOTE: wait for window.path_prefix to be set in app.mounted()
},
updated() {
this.compute_src();
},
methods: {
compute_src() {
this.computed_src = (this.src.startsWith("/") ? window.path_prefix : "") + this.src;
},
},
};
28 changes: 28 additions & 0 deletions nicegui/elements/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default {
template: `
<q-img v-bind="$attrs" :src="computed_src">
<template v-for="(_, slot) in $slots" v-slot:[slot]="slotProps">
<slot :name="slot" v-bind="slotProps || {}" />
</template>
</q-img>
`,
props: {
src: String,
},
data: function () {
return {
computed_src: undefined,
};
},
mounted() {
setTimeout(() => this.compute_src(), 0); // NOTE: wait for window.path_prefix to be set in app.mounted()
},
updated() {
this.compute_src();
},
methods: {
compute_src() {
this.computed_src = (this.src.startsWith("/") ? window.path_prefix : "") + this.src;
},
},
};
6 changes: 5 additions & 1 deletion nicegui/elements/image.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from pathlib import Path
from typing import Union

from nicegui.dependencies import register_component

from .mixins.source_element import SourceElement

register_component('image', __file__, 'image.js')


class Image(SourceElement):

Expand All @@ -13,4 +17,4 @@ def __init__(self, source: Union[str, Path] = '') -> None:
:param source: the source of the image; can be a URL, local file path or a base64 string
"""
super().__init__(tag='q-img', source=source)
super().__init__(tag='image', source=source)
12 changes: 11 additions & 1 deletion nicegui/elements/interactive_image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
template: `
<div style="position:relative">
<img :src="src" style="width:100%; height:100%;" v-on="onEvents" draggable="false" />
<img :src="computed_src" style="width:100%; height:100%;" v-on="onEvents" draggable="false" />
<svg style="position:absolute;top:0;left:0;pointer-events:none" :viewBox="viewBox">
<g v-if="cross" :style="{ display: cssDisplay }">
<line :x1="x" y1="0" :x2="x" y2="100%" stroke="black" />
Expand All @@ -18,9 +18,19 @@ export default {
x: 100,
y: 100,
cssDisplay: "none",
computed_src: undefined,
};
},
mounted() {
setTimeout(() => this.compute_src(), 0); // NOTE: wait for window.path_prefix to be set in app.mounted()
},
updated() {
this.compute_src();
},
methods: {
compute_src() {
this.computed_src = (this.src.startsWith("/") ? window.path_prefix : "") + this.src;
},
updateCrossHair(e) {
this.x = (e.offsetX * e.target.naturalWidth) / e.target.clientWidth;
this.y = (e.offsetY * e.target.naturalHeight) / e.target.clientHeight;
Expand Down
10 changes: 7 additions & 3 deletions nicegui/elements/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ export default {
</q-uploader>
`,
mounted() {
setTimeout(() => {
this.computed_url = (window.path_prefix || "") + this.url;
}, 0); // NOTE: wait for window.path_prefix to be set in app.mounted()
setTimeout(() => compute_url, 0); // NOTE: wait for window.path_prefix to be set in app.mounted()
},
updated() {
this.compute_url();
},
methods: {
compute_url() {
this.computed_url = (this.url.startsWith("/") ? window.path_prefix : "") + this.url;
},
reset() {
this.$refs.uploader.reset();
},
Expand Down
26 changes: 20 additions & 6 deletions nicegui/elements/video.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
export default {
template: `<video :controls="controls" :autoplay="autoplay" :muted="muted" :src="src" />`,
methods: {
seek(seconds) {
this.$el.currentTime = seconds;
},
},
template: `<video :controls="controls" :autoplay="autoplay" :muted="muted" :src="computed_src" />`,
props: {
controls: Boolean,
autoplay: Boolean,
muted: Boolean,
src: String,
},
data: function () {
return {
computed_src: undefined,
};
},
mounted() {
setTimeout(() => this.compute_src(), 0); // NOTE: wait for window.path_prefix to be set in app.mounted()
},
updated() {
this.compute_src();
},
methods: {
compute_src() {
this.computed_src = (this.src.startsWith("/") ? window.path_prefix : "") + this.src;
},
seek(seconds) {
this.$el.currentTime = seconds;
},
},
};

0 comments on commit 66f1b11

Please sign in to comment.