Passed
Push — master ( b1065f...542723 )
by M. Mikkel
06:27
created

cacheflag.js ➔ clearCaches   B

Complexity

Conditions 6

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 25
c 0
b 0
f 0
dl 0
loc 41
rs 8.3466
1
/** global: Craft */
2
3
$(function () {
4
5
    var $form = $('.cacheFlag-form');
6
    var $submitRequest = null;
7
8
    $form
9
        .on('submit', onFormSubmit)
10
        .on('click', '[data-clearflags]', clearCaches);
11
12
    function onFormSubmit(e) {
13
        e.preventDefault();
14
        submitForm();
15
    }
16
17
    function submitForm() {
18
19
        if ($submitRequest) {
20
            $submitRequest.abort();
21
        }
22
23
        $form.addClass('js-submitting');
24
        $form.find('.spinner').removeClass('hidden');
25
        $form.find('input[type="submit"]').prop('disabled', true).addClass('disabled');
26
27
        $submitRequest = $.ajax($form.attr('action'), {
28
            data: $form.serialize(),
29
            type: 'POST',
30
            dataType: 'json',
31
            success: function (response) {
32
                if (response.success) {
33
                    Craft.cp.displayNotice(response.message);
34
                    $form.find('input[type="text"][name^="cacheflags"]').each(function () {
35
                        var $input = $(this);
36
                        var source = $input.attr('name').replace('cacheflags[', '').replace(']', '').split(':');
37
                        var sourceColumn = source[0] || null;
38
                        var sourceValue = (source[1] || '').toString();
39
                        if (!sourceColumn || !sourceValue) {
40
                            return;
41
                        }
42
                        var flags = '';
43
                        for (var i = 0; i < response.flags.length; ++i) {
44
                            if ((response.flags[i][sourceColumn] || '').toString() === sourceValue) {
45
                                flags = response.flags[i].flags || '';
46
                                break;
47
                            }
48
                        }
49
                        $input.val(flags);
50
                    });
51
                } else {
52
                    Craft.cp.displayError(response.message);
53
                }
54
            },
55
            error: function (response) {
56
                if (response.statusText !== 'abort') {
57
                    Craft.cp.displayError(response.statusText);
58
                }
59
            },
60
            complete: function () {
61
                $submitRequest = null;
62
                $form.removeClass('js-submitting');
63
                $form.find('.spinner').addClass('hidden');
64
                $form.find('input[type="submit"]').prop('disabled', false).removeClass('disabled');
65
            }
66
        });
67
68
    }
69
70
    function clearCaches(e) {
71
72
        e.preventDefault();
73
74
        var actionUrl = Craft.getActionUrl('cache-flag/default/delete-flagged-caches-by-flags'),
75
            $target = $(e.currentTarget),
76
            flags = $target.data('clearflags');
77
78
        if ($target.hasClass('disabled') || !flags || flags == '') {
79
            return;
80
        }
81
82
        var data = {
83
            flags: flags
84
        };
85
86
        data[$form.data('csrf-name')] = $form.data('csrf-token');
87
88
        $target.addClass('disabled');
89
90
        $.ajax(actionUrl, {
91
            type: 'POST',
92
            data: data,
93
            dataType: 'json',
94
            success: function (response) {
95
                if (response.success) {
96
                    Craft.cp.displayNotice(response.message);
97
                } else {
98
                    Craft.cp.displayError(response.message);
99
                    $target.removeClass('disabled');
100
                }
101
            },
102
            error: function (response) {
103
                if (response.statusText !== 'abort') {
104
                    Craft.cp.displayError(response.statusText);
105
                }
106
                $target.removeClass('disabled');
107
            }
108
        });
109
110
    }
111
112
});
113