In my application I am displaying photos and videos with a repeating panel. The item template for the repeating panel checks if it is a photo or video and then either displays the standard image component or a custom html video component I built.
if 'video' in self.item['content_file'].content_type:
self.content_panel.add_component(video_player(url=self.item['content_file'].url, video_type = self.item['content_file'].content_type))
self.content.visible=False
The video player form raises the following event
def form_show(self, **event_args):
self.custom_1.raise_event('x-click', video_url = self.video_url, video_type=self.video_type
This passes the video url and type to the event handler in the custom html form, via.
self.set_event_handler('x-click', lambda video_url, video_type, **e: self.call_js('videoPlayer',video_url, video_type ))
My html and javascript function are as follows:
<center>
<div id="main">
</div>
</center>
<script>
function videoPlayer(video_url, video_type) {
var video_element = document.createElement("VIDEO");
video_element.id = "vid"
video_element.width = 300;
video_element.height = 240;
video_element.controls = 'True';
var video_source = document.createElement("SOURCE");
video_source.src = video_url;
video_element.appendChild(video_source);
var main_element = document.getElementById('main')
main_element.appendChild(video_element)
return String(video_source.src)
}
</script>
the strange thing is that when the âx-clickâ event is raised on form load with the show event, it puts all the videos that are included in the repeating panel in the first content card.
However, when I raise the same âx-clickâ event on a button press on this form, the behavior is correct.
Could someone explain to me the nuts-and-bolts of the show event so I can hopefully solve this problem? thank you!
Update
The problem was because I was appending the video element to the div with id âmainâ. HTML must append it to the first element with that ID because when I re-named the div with main_element.id = 'something_else
after appending the video, the next video element would get displayed on the next panel in line. This caused problems if there is a photo in the feed, because the next time a video comes up, it gets displayed in the photoâs item template because the div in there had the same id that the javascript function was looking for.
My dirty solution which now works is to raise an event called ârename_element_idâ on show of photo which renamed the HTML element id so video elements donât get appended to the photo template adjacent to it in the feed. Itâs almost like I am incrementing a panel counter by renaming the id of the element that gets created on each panel and looking for the first occurance of the default name. This is obviously not a pythonic way to do html, so any advice on dynamically adding html elements and manipulating attributes would be greatly appreciated.