Source code for org.elements

""" Contains small helper classes used as abstraction for various templating
macros.

"""
from random import choice

from lxml.html import builder, tostring
from markupsafe import Markup

from onegov.core.elements import AccessMixin, LinkGroup
from onegov.core.elements import Link as BaseLink
from onegov.org import _
from purl import URL


from typing import Any, TYPE_CHECKING
if TYPE_CHECKING:
    from collections.abc import Collection, Iterable
    from onegov.core.elements import Trait
    from onegov.core.elements import ChameleonLayout
    from onegov.core.request import CoreRequest

    # NOTE: We pretend to inherit from BaseLink at type checking time
    #       so we're not stuck in dependency hell everywhere else
    #       In reality we probably should actually inherit from this
    #       class and clean up redundancies...
    _Base = BaseLink
else:
    _Base = AccessMixin


















__all__ = (
    'AccessMixin',
    'ConfirmLink',
    'DeleteLink',
    'Link',
    'LinkGroup',
    'QrCodeLink'
)


class IFrameLink(BaseLink):
    """ Implements an iframe link that shows a modal with the iframe.
    The url is sent to the iframe endpoint url which generates the iframe
    and sends it back.
    """

    id = 'iframe_link'

    __slots__ = (
        'active',
        'attributes',
        'classes',
        'text',
        'url',
        'title'
    )

    def __init__(
        self,
        text: str,
        url: str,
        title: str | None = None,
        attrs: dict[str, Any] | None = None,
        traits: 'Iterable[Trait] | Trait' = (),
        **props: Any
    ) -> None:

        attrs = attrs or {}
        attrs['new-iframe-link'] = (
            '<iframe src="'
            + url
            + '" width="100%" height="800" frameborder="0"></iframe>'
        )
        attrs['data-reveal-id'] = ''.join(
            choice('abcdefghi') for i in range(8)  # nosec B311
        )
        # Foundation 6 Compatibility
        attrs['data-open'] = attrs['data-reveal-id']
        attrs['data-image-parent'] = f"iframe-{attrs['data-reveal-id']}"

        super().__init__(text, '#', attrs, traits, **props)
        self.title = title

    def __repr__(self) -> str:
        return f'<IFrameLink {self.text}>'