1jQuery(function () {
2    /**
3     * Attach the click handler
4     */
5    jQuery('.structstatus-full.editable').find('button.struct_status')
6        .click(function () {
7            const $self = jQuery(this);
8            $self.parent().css('visibility', 'hidden');
9
10            const set = makeDataSet($self.parent(), $self.data('rid'));
11
12            const data = {
13                sectok: $self.parent().data('st'),
14                field: $self.parent().data('field'),
15                pid: $self.parent().data('page'),
16                rid: $self.parent().data('rid'),
17                rev: $self.parent().data('rev'),
18                entry: set,
19                call: 'plugin_struct_inline_save'
20            };
21
22            jQuery.post(
23                DOKU_BASE + 'lib/exe/ajax.php',
24                data
25            )
26                .fail(function (jqXHR) {
27                    console.log('error');
28                    alert(jqXHR.responseText);
29                })
30                .done(function (response) {
31                    const value = JSON.parse(response).value;
32                    applyDataSet($self.parent(), set);
33                    jQuery('#plugin__struct_output').find('td[data-struct="' + $self.parent().data('field') + '"]').html(value);
34                })
35                .always(function () {
36                    $self.parent().css('visibility', 'visible');
37                })
38            ;
39
40        })
41    ;
42
43    /**
44     * Set the statuses according to the given set
45     *
46     * @param {jQuery} $full the wrapper around the statuses
47     * @param {Array} set the status or the list of statuses to enable
48     */
49    function applyDataSet($full, set) {
50        $full.find('button.struct_status').each(function () {
51            if (typeof set == 'undefined') {
52                set = [];
53            }
54            const $self = jQuery(this);
55            if (set.indexOf(JSON.stringify($self.data('rid'))) === -1) {
56                $self.addClass('disabled');
57            } else {
58                $self.removeClass('disabled');
59            }
60        });
61    }
62
63    /**
64     * Create a set based on the current set and the status to toggle
65     *
66     * @param {jQuery} $full the wrapper around the statuses
67     * @param {[]} toggle the rid of the status to toggle
68     * @return {Array} the resulting new set
69     */
70    function makeDataSet($full, toggle) {
71        const set = [];
72        let wason = false;
73
74        $full.find('button.struct_status').not('.disabled').each(function () {
75            const rid = jQuery(this).data('rid');
76            if (rid === toggle) {
77                wason = true; // this is the value we're toggling
78            } else {
79                set.push(rid); // this is an enabled value we keep
80            }
81        });
82        if (!wason) {
83            set.push(toggle); // value was not enabled previously, we toggle it
84        }
85
86        // for non-multi field only one value is allowed
87        if (!$full.data('multi')) {
88            return [JSON.stringify(set.pop())];
89        }
90
91        return set.map(function (entry) {
92            return JSON.stringify(entry);
93        });
94    }
95});
96