contactslist.html 3.7 KB
Newer Older
okhin's avatar
okhin committed
1 2 3 4 5 6 7 8
<div class="col s12 m4">
    <ul class="collection contact">
        <nav class="nav-extended teal">
            <div class="nav-wrapper">
                <div class="input-field">
                    <input type="search" id="search" class="autocomplete">
                    <label class="label-icon" for="search"><i class="material-icons">search</i></label>
                </div>
okhin's avatar
okhin committed
9
            </div>
okhin's avatar
okhin committed
10
        </nav>
okhin's avatar
okhin committed
11 12
        <div class="wrapper" data-json='{{ json_encode(@contacts) }}'>
        </div>
okhin's avatar
okhin committed
13 14
    </ul>
</div>
15
<!-- contacts list -->
okhin's avatar
okhin committed
16 17 18 19 20
<script>
$(document).ready(function() {
    $('input.autocomplete').autocomplete({
        data: {
            <repeat group="{{ @contacts }}" value="{{ @contact }}">
okhin's avatar
okhin committed
21
                "{{ @contact.full_name }}": <check if="{{ @contact.photo == '' }}">null<false>"{{ @contact.photo }}"</false></check>,
okhin's avatar
okhin committed
22 23 24 25 26
            </repeat>
        },
        limit: 20,
    });

okhin's avatar
okhin committed
27 28 29 30
    $('#search').change(function() {
        var contacts = JSON.parse(wrapper.attr('data-json'));
        contacts = contacts.filter(function(contact) {
            return contact['full_name'].startsWith($('#search').val());
okhin's avatar
okhin committed
31
        });
okhin's avatar
okhin committed
32 33 34 35 36 37 38 39
        list_meps(contacts, $('.contact .wrapper'));
    });

    var wrapper = $('.contact .wrapper');
    var contacts = JSON.parse(wrapper.attr('data-json'));
    list_meps(contacts, wrapper);

    var random = Math.floor((Math.random() * $('.collection-item').length));
okhin's avatar
okhin committed
40 41
    update_mep($('.collection-item').eq(random));
});
okhin's avatar
okhin committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

function list_meps(contacts, wrapper) {
    contacts = contacts.sort(function(a, b) {
        if (a['last_name'] < b['last_name']) {
            return -1;
        };
        if (a['last_name'] > b['last_name']) {
            return 1;
        };
        return 0;
    });
    // We need to empty the wrapper first
    wrapper.empty();
    contacts.forEach(function(contact) {
        display_mep(contact).appendTo(wrapper);
    });
};

function display_mep(contact) {
    var li = $('<li />')
        .addClass('collection-item avatar')
        .click(function() {
            update_mep($(this));
        })
        .attr('data-json', JSON.stringify(contact));
    if ( contact['photo'] === '') {
        $('<img>').addClass('circle')
            .attr('src', '/static/img/placeholder.jpg')
            .appendTo(li);
    } else {
        $('<img>').addClass('circle')
            .attr('src', contact.photo)
            .appendTo(li);
    };
    $('<span />').addClass('title')
        .text(contact['full_name'])
        .appendTo(li);
    var groups = $('<ul />')
        .appendTo(li);
    contact['groups'].forEach(function(group) {
        $('<li />').addClass('chip')
            .text(group['name'])
            .appendTo(groups);
    });
    return li;
};

function update_mep(element) {
    var contact = JSON.parse($(element).attr('data-json'));
    if (contact['photo'] !== '') {
        $('#contact-image').attr('src', contact['photo']);
    };
    $('#contact-full_name').html(contact['first_name'] + ' ' + contact['last_name']);
okhin's avatar
okhin committed
95
    $('#contact-full_name-2').html(contact['first_name'] + ' ' + contact['last_name']);
okhin's avatar
okhin committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    $('#form-contact').attr('action', '/call/' + contact['id']);
    $('#contact_id').attr('value', contact['id']);
    $('.groups').each(function() {
        var group_type = $(this).attr('data-name');
        var content = '<ul class="groups" data-name="' + group_type + '">' + group_type;
        contact['groups'].forEach(function(group) {
            if ( group['type'] === group_type ) {
                content += '<li class="chip">' + group['name'] + '</li>';
            }
        });
        content += '</ul>';
        $(this).html(content);
    });
    $('#contact-phone').attr('href', 'tel:' + contact['phone']);
    $('#contact-phone').text(contact['phone']);
};
okhin's avatar
okhin committed
112
</script>