view farol/templates/macros.j2 @ 128:79abdecb2d0b

Add a regex for dates
author Benoît Allard <benoit.allard@greenbone.net>
date Thu, 23 Oct 2014 16:57:36 +0200
parents e0830bcab004
children a7ce1660aaaf
line wrap: on
line source
{#
# Description:
# Web Template used in Farol Design
#
# Authors:
# BenoƮt Allard <benoit.allard@greenbone.net>
#
# Copyright:
# Copyright (C) 2014 Greenbone Networks GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-#}

{% macro textinput(name, label, placeholder="", value=None, required=False, type="text", extras={}, help='', regex=None) %}
<div class="form-group">
  {% if caller %}
    {% set content=caller () %}
    {% call modal(name + "_modal", label) %}
      {{ content }}
    {% endcall %}
  {% endif %}
  <label for="{{ name }}">{{ label }}:
    {% if required %}<span class="text-danger"> *</span>{% endif %}
    {% if caller %}<a href="#{{ name }}_modal" data-toggle="modal">?</a>{% endif %}
  </label>
  {% if type == "datetime" %}<div class="input-group">{% endif %}
  <input
     type="{{ type }}"
     {%- if type != 'file' %} class="form-control"{% endif %}
     id="{{ name }}" name="{{ name }}"
     {%- if placeholder %} placeholder="{{ placeholder }}"{% endif %}
     {%- if value %} value="{{ value }}"{% endif %}
     {%- if regex %} pattern="{{ regex }}"
     {%- elif type == "datetime" %} pattern="\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?([+-]\d+(:\d+)?|Z)?)?"
     {%- endif %}
     {%- if required %} required{% endif %}
     {{- extras | xmlattr }}>
  {% if type == "datetime" %}
    <span class="input-group-btn">
        <button class="btn btn-default" type="button" onclick="{{ name }}.value = new Date().toISOString()">now</button>
    </span>
  </div>
  {% endif %}
  {% if help %}
  <span class="help-block">{{ help }}</span>
  {% endif %}
</div>
{% endmacro %}

{% macro textarea(name, label, placeholder="", value=None, rows=10, required=False) %}
<div class="form-group">
  {% if caller %}
    {% set content=caller () %}
    {% call modal(name + "_modal", label) %}
      {{ content }}
    {% endcall %}
  {% endif %}
  <label for="{{ name }}">
    {{ label }}:
    {% if required %}<span class="text-danger"> *</span>{% endif %}
    {% if caller %}<a href="#{{ name }}_modal" data-toggle="modal">?</a>{% endif %}
  </label>
  <textarea class="form-control" rows={{ rows }} id="{{ name }}" name="{{ name }}" placeholder="{{ placeholder }}"{% if required %} required{% endif %}>{% if value %}{{ value }}{% endif %}</textarea>
</div>
{% endmacro %}

{% macro selectinput(name, label, choices, value) %}
<div class="form-group">
  {% if caller %}
    {% set content=caller () %}
    {% call modal(name + "_modal", label) %}
      {{ content }}
    {% endcall %}
  {% endif %}
  <label for="{{ name }}">{{ label }}:
    {% if caller %}<a href="#{{ name }}_modal" data-toggle="modal">?</a>{% endif %}
  </label>
  <select class="form-control" name="{{name}}", id="{{ name }}">
    {% for choice in choices %}<option{% if value==choice %} selected{% endif %}>{{ choice }}</option>{% endfor %}
  </select>
</div>
{% endmacro %}

{# Big difference with the one above is that the choices are tuple (name, value) #}
{% macro selectinput2(name, label, choices, value, multiple=False) %}
{% if not multiple %}
  {% set value= [value] %}
{% endif %}
<div class="form-group">
  <label for="{{ name }}">{{ label }}:</label>
  <select class="form-control" name="{{name}}" id="{{ name }}"{% if multiple %} multiple size="{{ choices | length }}"{% endif %}>
    {% for name, val in choices %}<option value="{{ val }}"{% if val in value %} selected{% endif %}>{{ name }}</option>{% endfor %}
  </select>
</div>
{% endmacro %}

{% macro panel(type="default", heading=None, badge=None, title=0, collapsible=True, extended=False) %}
{% if not heading %}
  {% set collapsible = False %}
{% endif %}
<div class="{{ ["panel", "panel-" + type] | join(" ") }}">
  {% if heading %}
  <div class="panel-heading">
    {%- if title %}<h{{ title }} class="panel-title">{% endif -%}
    {%- if collapsible %}<a data-toggle="collapse" href="#{{ heading | secure_filename }}">{% endif -%}
    {{ heading }}
    {%- if badge is not none %}<span class="pull-right badge">{{ badge }}</span>{% endif %}
    {%- if collapsible %}</a>{% endif -%}
    {%- if title %}</h{{ title }}>{% endif -%}
  </div>
  {% endif %}
  {% if collapsible %}<div id="{{ heading | secure_filename }}" class="panel-collapse collapse{{ ' in' if extended}}">{% endif %}
  <div class="panel-body">
    {{ caller() }}
  </div>
  {% if collapsible %}</div>{% endif %}
</div>
{% endmacro %}

{% macro modal(id, title, size=None) %}
<div class="modal fade" id="{{ id }}" tabindex="-1" role="dialog" aria-labelledby="{{ id }}_title" aria-hidden="true">
  <div class="modal-dialog{{ ' modal-%s' % size if size}}">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="{{ id }}_title">{{ title }}</h4>
      </div>
      <div class="modal-body">
        {{ caller() }}
      </div>{# We don't need a footer yet ...
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>#}
    </div>{# /.modal-content #}
  </div>{# /.modal-dialog #}
</div>{# /.modal #}
{% endmacro %}

{# This should be placed inside a .form-horizontal #}
{% macro label_value(label, value, left=2, right=10) %}
<div class="form-group">
  <label class="col-sm-{{ left }} control-label">{{ label }}:</label>
  <div class="col-sm-{{ right }}">
    <p class="form-control-static">{{ value }}</p>
  </div>
</div>
{% endmacro %}

{% macro examples(list, title='') %}
<p><strong>{{ title + ' ' if title }}Example{{ 's' if list | length > 1 }}:</strong></p>
{% if list | length == 1 %}
<samp>{{ list[0] }}</samp>
{% else %}
<ul>
  {%- for elem in list %}
    <li><samp>{{ elem }}</samp></li>
  {%- endfor %}
</ul>
{% endif %}
{% endmacro %}

{% macro POST_button(url, text, fields, out=False, style='btn-default') %}
{% if caller and out %}
  {{ caller('$(this).next()') }}
{% endif %}
<form class="inlined-form" action="{{ url }}" method="POST"{{ ' style="display:none"' if out}}>
  {% for elem in fields %}
  <input type=hidden name="{{ elem }}", value="{{ fields[elem] }}">
  {% endfor %}
  {% if caller %}
    {{ caller('parentNode') if not out }}
  {% else %}
  <button type="submit" class="btn {{ style }}">{{ text }}</button>
  {% endif %}
</form>
{% endmacro %}

{% macro edit_button(url, text="edit") -%}
  <a href="{{ url }}" class="btn btn-default btn-xs" role="btn">{{ text }}</a>
{%- endmacro %}

{% macro add_button(url, text="add") -%}
  <a href="{{ url }}" class="btn btn-success btn-xs" role="btn">{{ text }}</a>
{%- endmacro %}

{% macro delete_button(url, hiddens={}, text="delete") -%}
  {{ POST_button(url, text, hiddens, style="btn-danger btn-xs") }}
{%- endmacro %}

http://farol.wald.intevation.org