summaryrefslogtreecommitdiff
path: root/bug/bug/bug.js
blob: 5bcad72a96ca046a3bcabf3560a9d48f177bb7ca (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
//     Copyright (C) 2011 Loic Dachary <loic@dachary.org>
//
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
//
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
//
//     You should have received a copy of the GNU General Public License
//     along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
(function($) {

    $.bug = {

        window: window,

        ajax: function(type, url, args) {
            return $.ajax({
                type: type,
                url: url,
                data: args
            }).pipe(null, function(error) {
                var message;
		if (args == undefined) {
		  message = url + '() XHR error. ';
		} else {
		  message = url + '(' + $.param(args) + ') XHR error. ';
		}

		if (url == "/enter_bug.cgi" && 'status' in error && (error.status == 404 || error.status == 0)) {
		  message = "It seems there is a problem to connect with Bugzilla. Please try again later.";
		}
		else
		{
                    if('status' in error) {
		        if(error.status == 404 || error.status == 0) {
			    message += "Cannot find the URL specified.\n\n";
		        }
                        message += 'status = ' + error.status + ' ';
                    }
                    if('responseText' in error) {
                        message += 'responseText = ' + error.responseText + ' ';
                    }
                    if('statusText' in error) {
                        message += 'statusText = ' + error.statusText + ' ';
                    }
		}
                $.bug.error_set(message);
                window.scrollTo(0,0);
                throw error;
            });
        },

        lookup_result: function(data, error_regexps, success_regexp) {
            var error = null;
            for(var i = 0; i < error_regexps.length; i++) {
                error = data.match(error_regexps[i]);
                if(error !== null) {
                    break;
                }
            }
            if(error !== null) {
                $.bug.error_set(error[1]);
                throw error;
            } else {
                var success = data.match(success_regexp);
                if(success !== null) {
                    return success[1];
                } else {
                    $.bug.error_set("could not match " + success_regexp + " on the string returned by the server " + data);
                    throw data;
                }
            }
        },

        current_step: function(name) {
            $('.step').removeClass('current');
            $('.step_' + name).addClass('current');
        },

        error_clear: function() {
            $('.error-container').hide();
        },

        error_set: function(message) {
            $('.error').text(message);
            $('.error-container').show();
        },

        url: '',
	token: '',

        state_signin_error_regexps: [/CLASS="THROW_ERROR">([^<]*)/i],
        state_signin_success_regexp: /LOG&NBSP;OUT<\/A>([^<]*)/i,

        state_signin: function() {
            var element = $('.signin');
            $('.go', element).click(function() {
                $("body").css("cursor", "progress");
                $.bug.error_clear();
                $.bug.ajax('POST', $.bug.url + '/index.cgi', {
                    Bugzilla_login: $('.user', element).val(),
                    Bugzilla_password: $('.password', element).val()
                }).pipe(function(data) {
                    $("body").css("cursor", "default");
                    return $.bug.lookup_result(data,
                                               $.bug.state_signin_error_regexps,
                                               $.bug.state_signin_success_regexp);
                }).pipe(function(data) {
                    $('.username').html(data);
                    element.hide();
                    $.bug.state_component();
                });
            });
            $('.login-link', element).attr('href', $.bug.url + '/');
            $('.create-account-link', element).attr('href', $.bug.url + '/createaccount.cgi');
            $.bug.current_step('signin');
            element.show();
        },

        state_component: function() {
            var element = $('.state_component');

            $.bug.current_step('component');
            element.show();
            $('.select', element).select();
            $('.select .choice, img', element).hover(function() {
                var component = $(this).attr('data');
                $('.comment', element).hide();
                $('.comment.' + component, element).show();
            });
            $('.select .choice', element).click(function() {
                $(this).mouseenter();
                var component = $(this).attr('data');
                $('img', element).removeClass('selected');
                $('img[data="' + component + '"]').addClass('selected');
                $.bug.state_subcomponent();
            });
            $('img', element).click(function() {
                var component = $(this).attr('data');
                $(".select .choice[data='" + component + "']", element).click();
            });
            $('.components_icons').mouseleave(function() {
                $('img.selected', element).mouseenter();
            });
        },

        state_subcomponent: function() {
            var element = $('.state_subcomponent');
            var component = $('.state_component .chosen').attr('data');
            var subcomponent = $('.subcomponents .' + component, element).html();
            $('.active_subcomponent', element).html(subcomponent);
            if(!element.hasClass('initialized')) {
                element.addClass('initialized');
                $.bug.current_step('subcomponent');
            }
            element.show();
            $('.active_subcomponent .select', element).select();
            $('.active_subcomponent .select .choice', element).click(function() {
                $.bug.refresh_related_bugs();
                $.bug.state_version();
            });
        },

        state_version: function() {
            var element = $('.state_version');
            if(!element.hasClass('initialized')) {
                element.addClass('initialized');
                $.bug.current_step('version');
                element.show();
                $('.select', element).select();
                $(".select .choice", element).click(function() {
                    $.bug.state_description();
                });
            }
        },

        state_description: function() {
            var element = $('.state_description');
            var template = $(".long", element).val();
            if(!element.hasClass('initialized')) {
                template = template + '\nBrowser: ' + window.navigator.userAgent;
                $(".long", element).val(template);
                var validate = function() {
                    if($(".short", element).val().length > 3 &&
                       $(".long", element).val() != template) {
                        $.bug.state_attach();
                        $.bug.state_submit();
                    }
                };

                $(".short", element).change(validate);
                $(".long", element).keyup(validate);
                element.addClass('initialized');
                $.bug.current_step('description');
                element.show();
            }
        },

        state_attach: function() {
            var element = $('.state_attach');
            if(!element.hasClass('initialized')) {
                var file_input = $("input[type='file']", element);
                var container = $('.attach-file', element);
                container.mousemove(function(e) {
                    file_input.css({
				'left': e.pageX - container.offset().left - file_input.outerWidth() + 10,
				'top': e.pageY - container.offset().top - 10
		    });
                });
                file_input.change(function() {
                    // http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-March/018981.html
                    // in a nutshell : deal with it, it won't go away
                    var path = $(this).val().replace("C:\\fakepath\\","");
                    $("input[name='ignored']", element).val(path);
                });
                element.addClass('initialized');
                $.bug.current_step('attach');
                element.show();
            }
        },
        // Making the double quotes optional caters for differing browser
        // behaviour with jquery .text() - IE8 removes double quotes.
        state_submit_error_regexps: [/CLASS="?THROW_ERROR"?>([^<]*)/i, /FONT SIZE=\"?\+2\"?>([^<]*)/i, // bugzilla < 4
                                     /<DIV CLASS=\"?BOX\"?>\s+<P>([^<]+)/i],                         // bugzilla >= 4
        state_submit_success_regexp: /Bug ([0-9]+)/i,
        state_submit_element: 'html',

        state_submit: function() {
            var element = $('.state_submit');
            if(!element.hasClass('initialized')) {

                if ($.bug.token == '') {
                    $.bug.ajax('GET', $.bug.url + '/enter_bug.cgi?product=LibreOffice&bug_status=UNCONFIRMED').pipe(function(data){
                        $.bug.token = data.match(/<input type="hidden" name="token" value="([A-Za-z0-9]{10})">/)[1];
                    });
                }

                var form = $('.submission_form form');
                $.bug.error_clear();
                form.attr('action', $.bug.url + '/post_bug.cgi');
                form.submit(function() {
                    if($(element).hasClass('inprogress')) {
                        return false;
                    } else {
                        $(element).addClass('inprogress');
                    }
                    var version = $('.state_version .chosen').attr('data');
                    var component = $('.state_component .chosen').attr('data').replace('_',' ');
                    var short_desc = $('.state_subcomponent .active_subcomponent .chosen').attr('data') + ': ' + $('.state_description .short').val();
                    var comment = $('.state_description .long').val();
                    $("body").css("cursor", "progress");
                    $('input[name="token"]', form).val($.bug.token);
                    $('input[name="component"]', form).val(component);
                    $('input[name="version"]', form).val(version);
                    $('input[name="short_desc"]', form).val(short_desc);
                    $('input[name="comment"]', form).val(comment);
                    $.bug.token = '';
                    return true;
                });

                $('#submissionoutput').load(function() {
                    $(element).removeClass('inprogress');
                    $("body").css("cursor", "default");
                    var output = $(this).contents().find($.bug.state_submit_element).html();
                    var data = $.bug.lookup_result(output,
                                                   $.bug.state_submit_error_regexps,
                                                   $.bug.state_submit_success_regexp);
                    $('.bug', element).text(data);
                    $.bug.state_success();
                });
                element.addClass('initialized');
                $.bug.current_step('submit');
                element.show();
            }
        },

        state_success: function() {
            $('.state_submit').hide();
            $('.submission').css('visibility', 'hidden');
            var element = $('.state_success');
            var bug = $('.state_submit .bug').text();
            $('.bug', element).attr('href', $.bug.url + '/show_bug.cgi?id=' + bug);
            element.show();
            window.scrollTo(0,0);
        },

        // if this string is found in the page returned when
        // trying to fill a bug, it means the user is not logged in
        logged_in_false: 'form name="login"',

        logged_in: function() {
            $("body").css("cursor", "progress");
            return $.bug.ajax('GET', $.bug.url + '/enter_bug.cgi').pipe(function(data) {
                $("body").css("cursor", "default");
                return data.indexOf($.bug.logged_in_false) < 0;
            });
        },

        refresh_related_bugs: function() {
            $('.related_bugs').empty();
            var component = $('.state_component .chosen').attr('data').replace('_','%20');
            var subcomponent = $('.state_subcomponent .active_subcomponent .chosen').attr('data');
            var list = $.bug.url + '/buglist.cgi?columnlist=short_desc&component=' + component + '&product=LibreOffice&query_format=advanced&short_desc_type=allwordssubstr&ctype=csv&short_desc=' + subcomponent;
            $.bug.ajax('GET', list).pipe(function(data) {
                var lines = data.split('\n');
                var bug_urls = [];
                for(var i = 1; i < lines.length; i++) {
                    bug_urls.push(lines[i].replace(/([0-9]*),"(.*)"/,'<a href="' + $.bug.url + '/show_bug.cgi?id=$1" target="_blank">$2</a>'));
                }
                $('.related_bugs').html(bug_urls.join('<br>'));
            });
        },

        compatibility: function() {
            $('.left .step:last-child').addClass('last-child'); // cross browser compatibility
        },

        frame: function() {
            if($.bug.window != $.bug.window.top && $.bug.window.parent.bugzilla_url !== undefined) {
                $.bug.url = $.bug.window.parent.bugzilla_url;
            }
        },

        main: function(in_isTest) {
            $.bug.compatibility();
            $.bug.frame();
            $.bug.logged_in().done(function(status) {
                if(status) {
                    $.bug.state_component();
                } else {
                    $.bug.state_signin();
                }
            });
        }
    };

})(jQuery);