aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/selection.js
blob: 07369cdd08cc4921b12bb72ad83eef1815707571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function findList(selector, context) {
    var list = $(context)
        .closest('.studip-selection')
        .find(selector);
    if (list.is('ul')) {
        return list;
    }
    return list.find('ul:first');
}

$(document).on('click', '.studip-selection:not(.disabled) li:not(.empty-placeholder)', function() {
    var remove = $(this).is('.studip-selection-selected li'),
        item_id = $(this).data().selectionId,
        attr_name =
            $(this)
                .closest('.studip-selection')
                .data().attributeName || 'selected',
        list;
    if (remove) {
        list = findList('.studip-selection-selectable', this);
        $('input[type=hidden]', this).remove();
    } else {
        list = findList('.studip-selection-selected', this);
        $('<input type="hidden" name="' + attr_name + '[]">')
            .val(item_id)
            .prependTo(this);
    }

    $(this)
        .remove()
        .appendTo(list);

    return false;
});