summaryrefslogtreecommitdiffstats
path: root/extensions/MyDashboard/web/js/query.js
blob: 42dfca56104d032885d14335c7420697a7a0c283 (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
/* 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/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0.
 */

if (typeof(MyDashboard) == 'undefined') {
    var MyDashboard = {};
}

// Main query code
YUI({
    base: 'js/yui3/',
    combine: false, 
    groups: {
        gallery: {
            combine: false,
            base: 'js/yui3/',
            patterns: { 'gallery-': {} }
        }
    }
}).use("node", "datatable", "datatable-sort", "datatable-message", "json-stringify",
       "datatable-datasource", "datasource-io", "datasource-jsonschema", "cookie",
       "gallery-datatable-row-expansion-bmo", "handlebars", "escape", function(Y) {
    var counter          = 0,
        bugQueryTable    = null,
        bugQuery         = null,
        lastChangesQuery = null,
        lastChangesCache = {},
        default_query    = "assignedbugs";

    // Grab last used query name from cookie or use default
    var query_cookie = Y.Cookie.get("my_dashboard_query");
    if (query_cookie) {
        var cookie_value_found = 0;
        Y.one("#query").get("options").each( function() {
            if (this.get("value") == query_cookie) {
                this.set('selected', true);
                default_query = query_cookie;
                cookie_value_found = 1;
            }
        });
        if (!cookie_value_found) {
            Y.Cookie.set("my_dashboard_query", "");
        }
    }

    var bugQuery = new Y.DataSource.IO({ source: 'jsonrpc.cgi' });

    bugQuery.plug(Y.Plugin.DataSourceJSONSchema, {
        schema: {
            resultListLocator: "result.result.bugs",
            resultFields: ["bug_id", "changeddate", "changeddate_fancy",
                           "bug_status", "short_desc", "changeddate_api" ],
            metaFields: {
                description: "result.result.description",
                heading:     "result.result.heading",
                buffer:      "result.result.buffer",
                mark_read:   "result.result.mark_read"
            }
        }
    });

    bugQuery.on('error', function(e) {
        try {
            var response = Y.JSON.parse(e.data.responseText);
            if (response.error)
                e.error.message = response.error.message;
        } catch(ex) {
            // ignore
        }
    });

    var bugQueryCallback = {
        success: function(e) {
            if (e.response) {
                Y.one('#query_count_refresh').removeClass('bz_default_hidden');
                Y.one("#query_container .query_description").setHTML(e.response.meta.description);
                Y.one("#query_container .query_heading").setHTML(e.response.meta.heading);
                Y.one("#query_bugs_found").setHTML(
                    '<a href="buglist.cgi?' + e.response.meta.buffer +
                    '" target="_blank">' + e.response.results.length + ' bugs found</a>');
                bugQueryTable.set('data', e.response.results);

                var mark_read = e.response.meta.mark_read;
                if (mark_read) {
                    Y.one('#query_markread').setHTML( mark_read );
                    Y.one('#bar_markread').removeClass('bz_default_hidden');
                    Y.one('#query_markread_text').setHTML( mark_read );
                    Y.one('#query_markread').removeClass('bz_default_hidden');
                }
                else {
                    Y.one('#bar_markread').addClass('bz_default_hidden');
                    Y.one('#query_markread').addClass('bz_default_hidden');
                }
                Y.one('#query_markread_text').addClass('bz_default_hidden');
            }
        },
        failure: function(o) {
            if (o.error) {
                alert("Failed to load bug list from Bugzilla:\n\n" + o.error.message);
            } else {
                alert("Failed to load bug list from Bugzilla.");
            }
        }
    };

    var updateQueryTable = function(query_name) {
        if (!query_name) return;

        counter = counter + 1;
        lastChangesCache = {};

        Y.one('#query_count_refresh').addClass('bz_default_hidden');
        bugQueryTable.set('data', []);
        bugQueryTable.render("#query_table");
        bugQueryTable.showMessage('loadingMessage');

        var bugQueryParams = {
            version: "1.1",
            method:  "MyDashboard.run_bug_query",
            id:      counter,
            params:  { query : query_name }
        };

        bugQuery.sendRequest({
            request: Y.JSON.stringify(bugQueryParams),
            cfg: {
                method:  "POST",
                headers: { 'Content-Type': 'application/json' }
            },
            callback: bugQueryCallback
        });
    };

    var updatedFormatter = function(o) {
        return '<span title="' + Y.Escape.html(o.value) + '">' +
               Y.Escape.html(o.data.changeddate_fancy) + '</span>';
    };


    lastChangesQuery = new Y.DataSource.IO({ source: 'jsonrpc.cgi' });

    lastChangesQuery.plug(Y.Plugin.DataSourceJSONSchema, {
        schema: {
            resultListLocator: "result.results",
            resultFields: ["last_changes"],
        }
    });

    lastChangesQuery.on('error', function(e) {
        try {
            var response = Y.JSON.parse(e.data.responseText);
            if (response.error)
                e.error.message = response.error.message;
        } catch(ex) {
            // ignore
        }
    });

    bugQueryTable = new Y.DataTable({
        columns: [
            { key: Y.Plugin.DataTableRowExpansion.column_key, label: ' ', sortable: false },
            { key: "bug_id", label: "Bug", allowHTML: true, sortable: true,
              formatter: '<a href="show_bug.cgi?id={value}" target="_blank">{value}</a>' },
            { key: "changeddate", label: "Updated", formatter: updatedFormatter,
              allowHTML: true, sortable: true },
            { key: "bug_status", label: "Status", sortable: true },
            { key: "short_desc", label: "Summary", sortable: true },
        ],
    });

    var last_changes_source   = Y.one('#last-changes-template').getHTML(),
        last_changes_template = Y.Handlebars.compile(last_changes_source);

    var stub_source           = Y.one('#last-changes-stub').getHTML(),
        stub_template         = Y.Handlebars.compile(stub_source);


    bugQueryTable.plug(Y.Plugin.DataTableRowExpansion, {
        uniqueIdKey: 'bug_id',
        template: function(data) {
            var bug_id = data.bug_id;

            var lastChangesCallback = {
                success: function(e) {
                    if (e.response) {
                        var last_changes = e.response.results[0].last_changes;
                        last_changes['bug_id'] = bug_id;
                        lastChangesCache[bug_id] = last_changes;
                        Y.one('#last_changes_stub_' + bug_id).setHTML(last_changes_template(last_changes));
                    }
                },
                failure: function(o) {
                    if (o.error) {
                        alert("Failed to load last changes from Bugzilla:\n\n" + o.error.message);
                    } else {
                        alert("Failed to load last changes from Bugzilla.");
                    }
                }
            };

            if (!lastChangesCache[bug_id]) {
                var lastChangesParams = {
                    version: "1.1",
                    method:  "MyDashboard.run_last_changes",
                    params:  { bug_id: data.bug_id, changeddate_api: data.changeddate_api  }
                };

                lastChangesQuery.sendRequest({
                    request: Y.JSON.stringify(lastChangesParams),
                    cfg: {
                        method:  "POST",
                        headers: { 'Content-Type': 'application/json' }
                    },
                    callback: lastChangesCallback
                });

                return stub_template({bug_id: bug_id});
            }
            else {
                return last_changes_template(lastChangesCache[bug_id]);
            }

        }
    });

    bugQueryTable.plug(Y.Plugin.DataTableSort);

    bugQueryTable.plug(Y.Plugin.DataTableDataSource, {
        datasource: bugQuery
    });

    // Initial load
    Y.on("contentready", function (e) {
        updateQueryTable(default_query);
    }, "#query_table");

    Y.one('#query').on('change', function(e) {
        var index = e.target.get('selectedIndex');
        var selected_value = e.target.get("options").item(index).getAttribute('value');
        updateQueryTable(selected_value);
        Y.Cookie.set("my_dashboard_query", selected_value, { expires: new Date("January 12, 2025") });
    });

    Y.one('#query_refresh').on('click', function(e) {
        var query_select = Y.one('#query');
        var index = query_select.get('selectedIndex');
        var selected_value = query_select.get("options").item(index).getAttribute('value');
        updateQueryTable(selected_value);
    });

    Y.one('#query_markread').on('click', function(e) {
        var data = bugQueryTable.data;
        var bug_ids = [];

        Y.one('#query_markread').addClass('bz_default_hidden');
        Y.one('#query_markread_text').removeClass('bz_default_hidden');

        for (var i = 0, l = data.size(); i < l; i++) {
            bug_ids.push(data.item(i).get('bug_id'));
        }
        YAHOO.bugzilla.bugUserLastVisit.update(bug_ids);
        YAHOO.bugzilla.bugInterest.unmark(bug_ids);
    });

    Y.one('#query_buglist').on('click', function(e) {
        var data = bugQueryTable.data;
        var ids = [];
        for (var i = 0, l = data.size(); i < l; i++) {
            ids.push(data.item(i).get('bug_id'));
        }
        var url = 'buglist.cgi?bug_id=' + ids.join('%2C');
        window.open(url, '_blank');
    });
});