The Framework Code

twigs/framework/admin/beans.twig

File List

{% set ajax = TRUE %}
{% set fwutils = TRUE %}
{% set parsley = TRUE %}

{% extends '@content/page.twig' %}

{% import '@content/iconmacro.twig' as i %}
{% import '@util/formmacro.twig' as f %}
{% import '@util/modalmacro.twig' as m %}

{% block setup %}
    function deltable(e)
    {
        e.preventDefault();
        e.stopPropagation();
        let tr = $(this).parent().parent();
        let table = $('.bname', tr).text();
        bootbox.confirm('Are you sure you you want to delete the '+table+' bean type?', function(r){
            if (r)
            { // user picked OK
                $.ajax(base+'/ajax/table/'+table, {
                    method: 'DELETE',
                }).done(function(){
                    framework.fadetodel(tr);
                }).fail(function(jx){
                    bootbox.alert('<h3>Delete failed</h3>'+jx.responseText);
                });
            }
        });
    }
{% endblock setup %}

{% block onload %}
    $('#nbform').on('submit', false).parsley();
    $('#addb').on('click', function(e){
        const btn = $(this);
        const form = $('#nbform');
        btn.attr('disabled', true);
        e.preventDefault()
        let name = $('#xbn').val();
        let sp = null;
        let error = false;
        $('.bname').each(function(e){
            const t = $(this).text();
            if (t == name)
            {
                error = true;
                return false;
            }
            if (t > name)
            {
                sp = $(this);
                return false;
            }
        })
        if (error)
        {
            bootbox.alert('That page already exists');
            $(this).attr('disabled', false);
            return;
        }
        const dt = {};
        $('input[name="field[]"]', form).each(function(index) {
            dt['field[' + index + ']'] = $(this).val();
        });
        $('input[name="sample[]"]', form).each(function(index) {
            dt['sample[' + index + ']'] = $(this).val();
        });
        const fn = function(data){
            var htm = '<tr><td class="bname">'+name+'</td>'+
            '<td><a href="'+base+'/admin/view/table/'+name+'/">{{i.view()}}</a></td>'+
            '<td><a href="'+base+'/admin/edit/table/'+name+'/">{{i.edit()}}</a></td>'+
            '<td>{{i.delete('delb')}}</td></tr>';
            var nx = sp == null ? $(htm).appendTo($('#btab tbody')) : $(htm).insertBefore(sp.parent());
            $('.delb', nx).on('click', deltable);
            nx.data('id', data);
            $('#nbean').modal('hide');
        };
        $.post('{{base}}/ajax/table/' + name, dt).done(fn).fail(function(jx){
            bootbox.alert('<h3>Failed to create new bean<h3>'+jx.responseText);
        }).always(function() { btn.attr('disabled', false); });
    });
    $('#nbean').on('show.bs.modal', function(e){
        $('#nbform input,select').val('');
    });
    $('.delb').on('click', deltable);
    $('#mrow button').on('click', function(e) { framework.addMore(e); });
{% endblock onload %}

{% if not page is defined %}
    {% set page = 1 %}
    {% set pagesize = 10 %}
{% endif %}

{% set extra = '' %}

{% if all is defined %}
    {% if all %}
        {% set extra = 'all=1' %}
    {% endif %}
{% else %}
    {% set all = FALSE %}
{% endif %}

{% set beans = siteinfo.tables(all, page,  pagesize) %}

{% set pages = ((siteinfo.tablecount(all) + pagesize) / pagesize)|round(0, 'floor') %}

{% block header %}
    <section class="col-md-12 mt-5">
        <h1>Beans</h1>
    </section>
{% endblock header %}

{% block main %}
    <section class="row">
        <article class="offset-md-1 col-md-9">
            {% include '@util/message.twig' %}
            {% include '@util/paginate.twig' with { page : page, pagesize: pagesize, pages: pages, extra: extra } %}
            <table class="table table-responsive table-striped table-hover" id="btab">
                <thead class="thead-inverse">
                    <tr>
                        <th>Name</th>
                        <th colspan="3">&nbsp;</th>
                    </tr>
                </thead>
                <tbody>
                    {% for nb in beans %}
                        <tr data-bean="{{nb.name}}">
                            <td class="bname">{{nb.name}}</td>
                            <td><a href="{{base}}/admin/view/table/{{nb.name}}">{{i.view('editb')}}</a></td>
                            <td><a href="{{base}}/admin/edit/table/{{nb.name}}">{{i.edit('editb')}}</a></td>
                            <td>{{i.delete('delb')}}</td>
                        </td>
                    {% else %}
                        <tr><td colspan="4">No beans defined</td></tr>
                    {% endfor %}
                </tbody>
            </table>
            {% include '@util/paginate.twig' with { page : page, pagesize: pagesize, pages: pages, extra: extra } %}
	    <p><button class="btn btn-primary" data-toggle="modal" data-target="#nbean" type="button">Add Bean</button></p>
        </article>
    </section>
    {{m.open({id: 'nbean', title: 'New Bean'})}}
    <div class="modal-body">
        <form action="#" method="POST" id="nbform">
            {{f.text({label: 'Bean Name', id: 'xbn', name: 'name', ph: 'Bean name - alphanumeric characters only', required: TRUE,
                parsley: { trigger: 'blur', pattern: '[A-Za-z][a-zA-Z0-9]*', 'remote-reverse': 'false', remote: base ~ '/ajax/tablecheck/{value}',
                'remote-message': 'That bean already exists'} })}}
            <fieldset id="example">
                {{f.text({label: 'Field Name', name: 'field[]', ph: 'Field name - alphanumeric characters only', parsley: { trigger: 'blur', pattern: '[A-Za-z][a-zA-Z0-9]*'} })}}
                {{f.text({label: 'Sample Value', name: 'sample[]', ph: 'Field Sample Value'})}}
            </fieldset>
            <p id="mrow">
                <button class="btn btn-sm btn-info" type="button">More</button>
            </p>
        </form>
    </div>
    {{m.close({action: 'Add', id: 'addb'})}}
{% endblock main %}

{% block pagefooter %}
{# I don't want a footer #}
{% endblock pagefooter %}