summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/bureaucracy/script/user.js
blob: 9d5d806d3bc27d52138ba454b78c45f099442c45 (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
35
36
37
38
39
40
41
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
/**
 * Provides a list of matching user names while user inputs into a userpicker
 *
 * @author Adrian Lang <lang@cosmocode.de>
 * @author Gerrit Uitslag <klapinklapin@gmail.com>
 */
jQuery(function () {
    /**
     * Ajax request for user suggestions
     *
     * @param {Object} request object, with single 'term' property
     * @param {Function} response callback, argument: the data array to suggest to the user.
     * @param {Function} getterm callback, argument: the request Object, returns: search term
     */
    function ajaxsource(request, response, getterm) {
        jQuery.getJSON(
            DOKU_BASE + 'lib/exe/ajax.php', {
                call: 'bureaucracy_user_field',
                search: getterm(request)
            }, function (data) {
                response(jQuery.map(data, function (name, user) {
                    return {
                        label: name + ' (' + user + ')',
                        value: user
                    }
                }))
            }
        );
    }

    function split(val) {
        return val.split(/,\s*/);
    }

    function extractLast(term) {
        return split(term).pop();
    }


    /**
     * pick one user
     */
    jQuery(".userpicker").autocomplete({
        source: function (request, response) {
            ajaxsource(request, response, function (req) {
                return req.term
            })
        }
    });

    /**
     * pick one or more users
     */
    jQuery(".userspicker")
        // don't navigate away from the field on tab when selecting an item
        .bind("keydown", function (event) {
            if (event.keyCode === jQuery.ui.keyCode.TAB &&
                jQuery(this).data("ui-autocomplete").menu.active) {
                event.preventDefault();
            }
        })
        .autocomplete({
            minLength: 0,
            source: function (request, response) {
                ajaxsource(request, response, function (req) {
                    return extractLast(req.term)
                })
            },
            search: function () {
                // custom minLength
                var term = extractLast(this.value);
                return term.length >= 2;
            },
            focus: function () {
                // prevent value inserted on focus
                return false;
            },
            select: function (event, ui) {
                var terms = split(this.value);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push(ui.item.value);
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join(", ");
                return false;
            }
        });
});