Пример #1
0
def render_component(name, attrs=None, json_encode=True):
    """
    :param str name: Name of the component (excluding extension)
    :param dict attrs: attributes to add to the component element
    """
    from openlibrary.plugins.upstream.code import static_url

    attrs = attrs or {}
    attrs_str = ''
    for (key, val) in attrs.items():
        if json_encode and isinstance(val, dict) or isinstance(val, list):
            val = json.dumps(val)
            # On the Vue side use decodeURIComponent to decode
            val = urllib.parse.quote(val)
        attrs_str += f' {key}="{val}"'
    html = ''
    included = web.ctx.setdefault("included-components", [])

    if len(included) == 0:
        # Need to include Vue
        html += '<script src="%s"></script>' % static_url('build/vue.js')

    if name not in included:
        url = static_url('build/components/production/ol-%s.min.js' % name)
        html += '<script src="%s"></script>' % url
        included.append(name)

    html += '<ol-{name} {attrs}></ol-{name}>'.format(
        name=kebab_case(name),
        attrs=attrs_str,
    )
    return html
Пример #2
0
def render_component(name, attrs=None, json_encode=True):
    """
    :param str name: Name of the component (excluding extension)
    :param dict attrs: attributes to add to the component element
    """
    from openlibrary.plugins.upstream.code import static_url

    attrs = attrs or {}
    attrs_str = ''
    for (key, val) in attrs.items():
        if json_encode and isinstance(val, dict) or isinstance(val, list):
            val = json.dumps(val)
        attrs_str += ' %s="%s"' % (key, val.replace('"', "'"))

    html = ''
    included = web.ctx.setdefault("included-components", [])

    if len(included) == 0:
        # Need to include Vue
        html += '<script src="%s"></script>' % static_url('build/vue.js')

    if name not in included:
        url = static_url('build/components/production/ol-%s.min.js' % name)
        if query_param('debug'):
            url = static_url('build/components/development/ol-%s.js' % name)
        html += '<script src="%s"></script>' % url
        included.append(name)

    html += '<ol-%(name)s %(attrs)s></ol-%(name)s>' % {
        'name': kebab_case(name),
        'attrs': attrs_str,
    }
    return html