aboutsummaryrefslogtreecommitdiff
path: root/extensions/Dashboard/web/js/widgets.js
blob: f0f5a712defb0012cacb16d4db8bf7e1c46608fb (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright (C) 2012 Jolla Ltd.
 * Contact: Pami Ketolainen <pami.ketolainen@jollamobile.com>
 *
 * The Initial Developer of the Original Code is "Nokia Corporation"
 * Portions created by the Initial Developer are Copyright (C) 2011 the
 * Initial Developer. All Rights Reserved.
 */


/**
 * URL widget implementation.
 */
Widget.addClass('url', Widget.extend({
    // See Widget.render().
    render: function()
    {
        this.base();
        this._iframe = this._child('iframe')
        this._iframe.load(this._onIframeLoad.bind(this));
    },

    /**
     * Handle completion of IFRAME load by attempting to modify (and replace)
     * the child document using the elements matched by the configured CSS
     * selector, if any. This may fail due to browser same-origin policy (e.g.
     * different domain).
     */
    _onIframeLoad: function()
    {
        try {
            // Any property access will throw if same origin policy in effect.
            var location = this._iframe[0].contentDocument.location;
        } catch(e) {
            if(window.console) {
                console.error('_onIframeLoad: can\'t apply CSS: %o', e);
            }
            return;
        }

        var body = $('body', this._iframe[0].contentDocument);
        if(this.state.data.selector) {
            var matched = $(this.state.data.selector, body);
            body.children().remove();
            matched.appendTo(body);
            matched.css('padding', '0px');
            body.css('margin', '0px');
        }
        body.find('a').attr('target', '_blank');
        $('html', this._iframe).css('margin', '0px');
    },

    onClickLoadurl: function()
    {
        var url = this.settingsDialog.find("[name='url']").val();
        if (url) {
            this._iframe.attr("src", url);
        }
    },

    reload: function()
    {
        var url = !url ? this.state.data.url : url;
        this._iframe.attr("src", this.state.data.url);
    }
}));


/**
 * RSS widget implementation.
 */
Widget.addClass('rss', Widget.extend({

    // See Widget.reload().
    reload: function()
    {
        this.loader(true);
        if(! this.state.data.url) {
            this.error('Please set a feed URL.');
            return;
        }
        var rpc = new Rpc('Dashboard', 'get_feed', { url: this.state.data.url });
        rpc.fail($.proxy(this, "error"));
        rpc.done($.proxy(this, "_onReloadDone"));
    },

    /**
     * Populate our template with the feed contents.
     *
     * @param feed
     *      Feed JSON object, as returned by get_feed RPC.
     */
    _onReloadDone: function(feed)
    {
        this.loader(false);
        var items = this.contentElement.find(".feed-items");
        items.empty();

        if(feed.link) {
            $('h2 a', this.contentElement).attr('href', feed.link);
        } else {
            $('h2 a', this.contentElement).attr('href', "");
        }
        $('h2 a', this.contentElement).text(feed.title);

        var length = Math.min(feed.items.length,
            DASHBOARD_CONFIG.rss_max_items);
        for(var i = 0; i < length; i++) {
            items.append(this._formatItem(feed.items[i]));
        }
    },

    /**
     * Format a single item.
     *
     * @param item
     *      Item JSON object as returned by get_feed RPC.
     */
    _formatItem: function(item)
    {
        var template = cloneTemplate('#rss-template-item');
        $('h3 a', template).text(item.title);
        $('h3 a', template).attr('href', item.link);
        $('.updated-text', template).text(item.modified);
        $('.description-text', template).text(this._sanitize(item.description));
        return template;
    },

    _sanitize: function(html)
    {
        // TODO
        html = html || '';
        var s = html.replace(/^<.+>/, '');
        return s.replace(/<.+/g, '');
    }
}));


/**
 * Text widget implementation.
 */
Widget.addClass('text', Widget.extend({
    reload: function()
    {
        this.contentElement.find("div.text").html(this.state.data.text);
    }
}));

/**
 * Confirmation support to colorbox.close()
 *
 * Adds new configuration option to colorbox
 *
 *      onCloseConfirm: callback
 *
 * Where callback is a function which should return true if it is ok to close
 * the box.
 */
$.colorbox.originalClose = $.colorbox.close;
$.colorbox.close = function() {
    element = $.colorbox.element();
    var confirmClose = element.data().colorbox.onCloseConfirm;
    if (typeof confirmClose == "undefined") {
        $.colorbox.originalClose();
    } else {
        if (confirmClose() == true) $.colorbox.originalClose();
    }
}

/**
 * Helper function to hide the header and footer from bugzilla page
 *
 * @param frame
 *      The iframe element
 */
function stripBugzillaPage(frame)
{
    var contents = $(frame).contents();
    contents.find("div#header").hide();
    contents.find("div#footer").hide();
}

/**
 * Generic bugs widget class
 */
var BugsWidget = Widget.extend(
{
    DEFAULT_COLUMNS: ["bug_id", "bug_status", "short_desc"],
    DEFAULT_SORT: [0, -1 , -1],

    // See Widget.render().
    render: function()
    {
        this.base();
        this._queryField = $("input[name='query']", this.settingsDialog);
        this._queryButton = $("input[name='editquery']", this.settingsDialog);
        this._queryButton.click($.proxy(this, '_openQueryEditor'));

        this._columnList = $("ul.buglist-column-select", this.settingsDialog);
        for (var name in BUGLIST_COLUMNS) {
            var item = $("<li/>");
            var check = $("<input type='checkbox'/>");
            check.attr("name", name);
            item.append(check).append(BUGLIST_COLUMNS[name]);
            item.append(cloneTemplate("#buglist-sort-template"));
            this._columnList.append(item);
        }
        this._columnList.sortable();
    },

    _sortOrder: function()
    {
        return $.isEmptyObject(this.state.data.sort) ?
                this.DEFAULT_SORT : this.state.data.sort;
    },
    _columnNames: function()
    {
        return $.isEmptyObject(this.state.data.columns) ?
                this.DEFAULT_COLUMNS : this.state.data.columns;
    },

    _setCustomSetting: function()
    {
        this.base();
        var columns = this._columnNames();
        var sort = this._sortOrder();

        // Iterate backwards so we can easily push the selected on top of the
        // list in right order
        for (var i = columns.length - 1; i >= 0; i--) {
            var check = this._columnList.find("input[name='" + columns[i] + "']");
            check.prop("checked", true);
            var item = check.parent();
            try {
                $("select", item).val(sort[i]);
            } catch(e) {
            }
            item.remove();
            this._columnList.prepend(item);
        }
    },

    _getCustomSettings: function()
    {
        var data = this.base();
        data.columns = [];
        data.sort = [];
        this._columnList.find("input:checked").each(function(){
            var check = $(this);
            data.columns.push(check.attr("name"));
            data.sort.push(Number(check.siblings("select").val()) || 0);
        });
        return data;
    },

    /**
     * See Widget._updateStateData()
     */
    _updateStateData: function(changes)
    {
        var changed = false;
        var changes = $.extend({}, changes);
        // columns and sort in data are arrays so they need special checking
        for (var key in {columns:1, sort:1}) {
            var list = changes[key];
            if(list == undefined) continue;
            delete changes[key];

            var orig = this.state.data[key] || [];
            if (list.length != orig.length) {
                this.state.data[key] = list;
                changed = true;
                continue;
            }

            for (var i = 0; i < list.length; i++) {
                if (list[i] != orig[i]) {
                    this.state.data[key] = list;
                    changed = true;
                    continue;
                }
            }
        }
        // Call the base implementation for remaining simple values
        return this.base(changes) || changed;
    },

    /**
     * Open query editor box when edit query button is clicked
     */
    _openQueryEditor: function()
    {
        $.colorbox({
            close: "Apply",
            width: "90%",
            height: "90%",
            iframe: true,
            fastIframe: false,
            href: "query.cgi" + this._queryField.val(),
            onCloseConfirm: $.proxy(this, '_confirmQueryClose'),
            onCleanup: $.proxy(this, '_getSearchQuery'),
            onComplete: $.proxy(this, '_onEditBoxReady')
        });
    },

    /**
     * Hide unneeded elements from the page in edit box
     */
    _onEditBoxReady: function()
    {
        var frame = $("#cboxContent iframe")
        stripBugzillaPage(frame);
        frame.load(function(event){stripBugzillaPage(event.target);});
    },

    /**
     * Get the query string from buglist page open in edit box
     */
    _getSearchQuery: function()
    {
        try {
            var loc = $("#cboxContent iframe").contents()[0].location;
            if (loc.pathname.match("buglist.cgi")) {
                this._queryField.val(loc.search);
            }
        } catch(e) {
            if (window.console) console.error(e);
            alert("Failed to get the query string");
        }
    },

    /**
     * Confirm that query edit box is on buglist page before closing
     */
    _confirmQueryClose: function()
    {
        var path = "";
        try {
            path = $("#cboxContent iframe").contents()[0].location.pathname;
        } catch(e) {
            if (window.console) console.error(e);
            return true;
        }
        if (path.match("buglist.cgi") == null) {
            return confirm(
                "After entering the search parameters, "
                + "you need to click 'search' to open "
                + "the buglist before closing. "
                + "Do you really want to close?");
        } else {
            return true;
        }
    },

    // See Widget.reload().
    reload: function()
    {
        this.loader(true);
        if (this.state.data.query) {
            // set ctype to csv in query parameters
            params = getQueryParams(this.state.data.query);
            params.ctype = "csv";

            // Set columns
            var columns = this._columnNames();
            if ($.isEmptyObject(columns)) columns = this.DEFAULT_COLUMNS;
            params.columnlist = columns.join(",");

            // Create request to fetch the data and set result callbacks
            var jqxhr = $.get("buglist.cgi" + getQueryString(params), {});
            jqxhr.success(this._onReloadDone.bind(this));
            jqxhr.error(this._onReloadFail.bind(this));
        } else {
            this.error("Set the query string in widget options");
        }
    },

    /**
     * Display an error message when bug list fetching
     *
     * @param error
     *      String error from backend.
     */
    _onReloadFail: function(error)
    {
        this.error(error);
    },

    /**
     * Create the bug list table
     *
     * @param result
     *      Result JSON object, as returned by search RPC.
     */
    _onReloadDone: function(data)
    {
        this.loader(false);
        try {
            var buglist = $.csv()(data);
        } catch(e) {
            this.error("Failed to parse bug list");
            return;
        }
        if (buglist.length == 1) {
            var content = $("<p>Sorry, no bugs found</p>");
        } else {
            var tableorder = [];
            var sort = this._sortOrder();
            var columns = this._columnNames();
            // Create table
            var content = $("<table class='buglist tablesorter'/>");
            content.append("<thead/>");
            content.append("<tbody/>");
            // Create header
            var header = $("<tr/>");
            for (var i = 0; i < buglist[0].length; i++)
            {
                var name = buglist[0][i];
                var index = columns.indexOf(name);
                if (sort[index] > -1) {
                    tableorder.push([i, sort[index]]);
                }
                header.append("<th>" + BUGLIST_COLUMNS[name] + "</th>");
            }
            $("thead", content).append(header);
            // Create rows
            for(var i = 1; i < buglist.length; i++)
            {
                var row = $("<tr/>");
                for(var j = 0; j < buglist[i].length; j++)
                {
                    var value = buglist[i][j];
                    var formatter = this["_format_" + buglist[0][j]];
                    if (formatter != undefined) {
                        value = formatter(value);
                    }
                    var cell = $("<td/>");
                    cell.append(value);
                    row.append(cell);
                }
                $("tbody", content).append(row);
            }
            // Make it pretty and sortable
            content.tablesorter({sortList: tableorder, useUI: true});
        }
        this.contentElement.html(content);
    },

    /**
     * Formatter for bug_id in data table
     */
    _format_bug_id: function(value)
    {
        var link = $("<a target='_blank'></a>");
        link.text(value);
        link.attr("href", "show_bug.cgi?id=" + value);
        return link;
    }
});
Widget.addClass('bugs', BugsWidget);

/**
 * My bugs widget implementation
 */
var MyBugsWidget = BugsWidget.extend({

    TEMPLATE_TYPE: "bugs",

    constructor: function(dashboard, state)
    {
        this.base(dashboard, state);
        // Exact same query as the default "My bugs" search
        this.state.data.query = getQueryString({
            bug_status: ['UNCONFIRMED', 'NEW', 'ASSIGNED', 'REOPENED'],
            email1: this._dashboard.config.user_login,
            emailassigned_to1: 1,
            emailreporter1: 1,
            emailtype1: 'exact',
            'field0-0-0': 'bug_status',
            'type0-0-0': 'notequals',
            'value0-0-0': 'UNCONFIRMED',
            'field0-0-1': 'reporter',
            'type0-0-1': 'equals',
            'value0-0-1': this._dashboard.config.user_login
        });
    },
    render: function()
    {
        this.base();
        this.settingsDialog.find(".buglist-query-entry").hide();
    }
});
Widget.addClass('mybugs', MyBugsWidget);