summaryrefslogtreecommitdiffstats
path: root/public/js/behavior/sortable.js
blob: 8f32ab7e8f9f89d825216633a831406184399f34 (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
/*! Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */

(function(Icinga, $) {

    'use strict';

    Icinga.Behaviors = Icinga.Behaviors || {};

    var Sortable = function (icinga) {
        Icinga.EventListener.call(this, icinga);
        this.on('rendered', this.onRendered, this);
    };

    Sortable.prototype = new Icinga.EventListener();

    Sortable.prototype.onRendered = function(e) {
        $(e.target).find('.sortable').each(function() {
            var $el = $(this);
            var options = {
                scroll: $el.closest('.container')[0],
                onMove: function (/**Event*/ event, /**Event*/ originalEvent) {
                    if (typeof this.options['filter'] !== 'undefined' && $(event.related).is(this.options['filter'])) {
                        // Assumes the filtered item is either at the very start or end of the list and prevents the
                        // user from dropping other items before (if at the very start) or after it.
                        return false;
                    }
                }
            };

            $.each($el.data(), function (i, v) {
                if (i.length > 8 && i.substring(0, 8) === 'sortable') {
                    options[i.charAt(8).toLowerCase() + i.substr(9)] = v;
                }
            });

            if (typeof options.group !== 'undefined' && typeof options.group.put === 'string' && options.group.put.substring(0, 9) === 'function:') {
                var module = icinga.module($el.closest('.icinga-module').data('icingaModule'));
                options.group.put = module.object[options.group.put.substr(9)];
            }

            $(this).sortable(options);
        });
    };

    Icinga.Behaviors.Sortable = Sortable;

})(Icinga, jQuery);