1window.issuelinksUtil = window.issuelinksUtil || {};
2jQuery(function initializeRepoAdminInterface() {
3    'use strict';
4
5    const $repoadmin = jQuery('#plugin__issuelinks_repoadmin');
6    const $tabs = $repoadmin.find('.tabs a');
7
8    function toggleHookIndicator(data, $this) {
9        $this.toggleClass('active inactive');
10        if ('id' in $this.data()) {
11            $this.removeData('id');
12            $this.removeAttr('data-id');
13            return;
14        }
15
16        if (typeof data.id !== 'undefined') {
17            $this.data('id', data.id);
18        }
19    }
20
21    function showError(jqXHR, $this) {
22        let data;
23        const HTTP_STATUS_FORBIDDEN = 403;
24        console.error(jqXHR);
25        try {
26            const response = JSON.parse(jqXHR.responseText);
27            window.issuelinksUtil.showAjaxMessages(response);
28            if (typeof response.data !== 'undefined') {
29                data = response.data;
30            } else {
31                data = jqXHR.responseText;
32            }
33        } catch (e) {
34            data = jqXHR.responseText;
35        }
36        switch (jqXHR.status) {
37        case HTTP_STATUS_FORBIDDEN:
38            $this.text(jqXHR.status + ' ' + jqXHR.statusText + '!');
39            break;
40        default:
41            $this.text(data);
42        }
43        $this.removeClass('active inactive').addClass('error');
44        $this.off('click');
45    }
46
47    function requestHookToogle() {
48        const $this = jQuery(this);
49        const servicename = $this.parents('.repo_area').data('service');
50        if ($this.hasClass('pulse')) {
51            return;
52        }
53
54        const settings = {
55            url: DOKU_BASE + 'lib/exe/ajax.php',
56            data: {
57                call: 'issuelinks_repo_admin_toggle',
58                sectok: jQuery('input[name="sectok"]').val(),
59                project: $this.data('project'),
60                hookid: $this.data('id'),
61                hooktype: 'issue',
62                servicename: servicename,
63            },
64        };
65        jQuery.post(settings)
66            .done(function adjustHookDisplay(response) {
67                const data = response.data;
68                window.issuelinksUtil.showAjaxMessages(response);
69                toggleHookIndicator(data, $this);
70            })
71            .fail(function showErrorOnHook(jqXHR) {
72                showError(jqXHR, $this);
73            })
74            .always(function disablePulse() {
75                $this.removeClass('pulse');
76            })
77        ;
78        $this.addClass('pulse');
79    }
80
81    $tabs.first().closest('li').addClass('active');
82    $repoadmin.find('.service_wrapper').not('[data-service="' + $tabs.data('service') + '"]').hide();
83    $tabs.click(function switchTab() {
84        const $this = jQuery(this);
85        const servicename = $this.data('service');
86        $tabs.closest('li').removeClass('active');
87        $this.closest('li').addClass('active');
88        $repoadmin.find('.service_wrapper[data-service="' + servicename + '"]').show();
89        $repoadmin.find('.service_wrapper').not('[data-service="' + servicename + '"]').hide();
90    });
91
92    jQuery('select[name="mm_organisation"]').change(function organisationChanged() {
93        const $this = jQuery(this);
94        const servicename = $this.closest('form').data('service');
95        const $reposDiv = jQuery('.repo_area[data-service="' + servicename + '"]');
96        if (!$this.val()) {
97            $reposDiv.html('');
98            return;
99        }
100        const settings = {
101            url: DOKU_BASE + 'lib/exe/ajax.php',
102            data: {
103                call: 'issuelinks_repo_admin_getorg',
104                sectok: $this.parents('form').find('input[name="sectok"]').val(),
105                org: $this.val(),
106                servicename: servicename,
107            },
108        };
109        jQuery.post(settings)
110            .done(function updateReposForOrganisation(response) {
111                const data = response.data;
112                window.issuelinksUtil.showAjaxMessages(response);
113                $reposDiv.html(data);
114                $reposDiv.find('span.repohookstatus:not(.forbidden)').click(requestHookToogle);
115            })
116            .fail(function showErrorOnRepoArea(jqXHR) {
117                showError(jqXHR, $reposDiv);
118            })
119            .always(function enableThisSelectAgain() {
120                $this.prop('disabled', false);
121            })
122        ;
123        $reposDiv.html(jQuery('<span>').addClass('pulse').css('padding', '5px'));
124        $this.prop('disabled', 'disabled');
125    });
126    const CHECK_IMPORT_STATUS_TIMEOUT = 1000;
127
128    function checkImportStatus(servicename, project, $importStatusElement) {
129        const checkImportSettings = {
130            url: DOKU_BASE + 'lib/exe/ajax.php',
131            data: {
132                call: 'plugin_issuelinks',
133                'issuelinks-action': 'checkImportStatus',
134                'issuelinks-service': servicename,
135                'issuelinks-project': project,
136            },
137        };
138        jQuery.post(checkImportSettings)
139            .done(function (response) {
140                const data = response.data;
141                window.issuelinksUtil.showAjaxMessages(response);
142
143                let total = '?';
144                let percent = '?';
145                const count = jQuery.isNumeric(data.count) ? data.count : 0;
146                if (jQuery.isNumeric(data.total) && data.total > 0) {
147                    total = data.total;
148                    percent = Math.round(count / total * 100);
149                }
150                const statusText = LANG.plugins.issuelinks['status:' + data.status];
151                const progressText = '' + count + '/' + total + ' (' + percent + ' %) ' + statusText;
152                $importStatusElement
153                    .text(progressText)
154                    .css('background-color', '#ff9')
155                    .animate({ backgroundColor: 'transparent' }, CHECK_IMPORT_STATUS_TIMEOUT / 2)
156                ;
157                if (data.status && data.status === 'done') {
158                    return;
159                }
160                window.setTimeout(
161                    checkImportStatus,
162                    CHECK_IMPORT_STATUS_TIMEOUT,
163                    servicename,
164                    project,
165                    $importStatusElement
166                );
167            })
168            .fail(function (jqXHR) {
169                $importStatusElement.text('Check failed!');
170                showError(jqXHR, $importStatusElement.parents('.repo_area'));
171            });
172    }
173
174    $repoadmin.on('click', '.js-importIssues', function (event) {
175        console.log('I ran!');
176        const $this = jQuery(this);
177        const servicename = $this.closest('[data-service]').data('service');
178        const project = $this.data('project');
179
180        const settings = {
181            url: DOKU_BASE + 'lib/exe/ajax.php',
182            data: {
183                call: 'issuelinks_import_all_issues_async',
184                project: project,
185                servicename: servicename,
186            },
187        };
188
189        jQuery.post(settings)
190            .done(function (response) {
191                window.issuelinksUtil.showAjaxMessages(response);
192                const $importStatusElement = jQuery('<span class="js-importRunning importRunning">Import started</span>');
193                $this.replaceWith($importStatusElement);
194                window.setTimeout(
195                    checkImportStatus,
196                    CHECK_IMPORT_STATUS_TIMEOUT,
197                    servicename,
198                    project,
199                    $importStatusElement
200                );
201            });
202    });
203});
204