{% extends "base_status.html" %}
{% block title %}Add new Object{% endblock %}
{% block smallheader %}Add{% endblock %}
{% block largeheader %}{{ object_type }}{% endblock %}
{% block nav1 %}<a href="{% url objectbrowser %}">Object Browser</a>{% endblock %}

{% block toolbar %}
{% endblock %}

{% block content %}
    <table class="table table-condensed well datatable">
    <thead>
    <tr>
    <th>Short name</th>
    <th>{{ attribute_name }}</th>
    </tr>
    </thead>
    <tbody>
    {% for i in objects %}
        <tr>
        <td>{{ i.0 }}</td>
        <td>
            <input style="width: 500px;" class="pull-left editable_field" name="_{{ i.3 }}" type=text data-orig-value="{{ i.1 }}" data-object_type="{{ object_type }}" data-attribute_name="{{ attribute_name }}" data-short_name="{{ i.0 }}" value="{{ i.1 }}"/>
            <div id="_{{ i.3 }}_status"></div>
        </td>
        </tr>
    {% endfor %}

    </tbody>
    </table>
{% endblock %}

{% block footer %}

    {{ block.super }}
    <script>

            // When an editable field is change, lets update configuration
        $('.editable_field').focus(function(e) {
            $(e.target).data('orig-value', "" + $(e.target).val());
        })
        $('.editable_field').blur(function(e) {
            var old_value = "" + $(e.target).data('orig-value');
            var new_value = "" + $(e.target).val();
            var attribute_name = "" + $(e.target).data('attribute_name');
            var object_type = $(e.target).data('object_type');
            var short_name = $(e.target).data('short_name');
            if (old_value == new_value)
                return false;

            // Get the div right next to the input box for editing
            var message_div = $('#' + $(e.target).attr('name') + "_status");
            message_div.html('<div class="pull-right"><div class="circle state_3"></div> Saving ...</div>');

            tmp = change_attribute(
                    object_type,
                    short_name,
                    attribute_name,
                    new_value,
                    function(e) {
                        message_div.html(
                        '<a class="pull-left" href="" onclick=undo_attribute(\''+attribute_name+'\')>undo </a>' +
                        '<div class="pull-right">' +
                        '<div class="circle state_0"></div>  Saved. ' +
                        '</div> '
                        );
                    },
                    function(e) {
                        message_div.html(
                        '<a class="pull-left" href="" onclick=undo_attribute(\''+attribute_name+'\')>undo </a>' +
                        '<div class="pull-right">' +
                        '<div class="circle state_2"></div>  Failed. ' +
                        '</div> '
                        );
                    }

            );

        });

    </script>
{% endblock %}