Refactor the bugstatus JavaScript for working with multiple distributions.
[collab-maint/wiki.debian.org.git] / usr / htdocs / bugstatus.js
index 077ced3..b17cd8f 100644 (file)
 //
-// Update DebianBugs interwiki links presentation
+// Update bug interwiki links presentation
 // based on bug status.
 //
 
-function get_json(url) {
-var my_JSON_object = {};
+function get_json(url, links, funct) {
+       var object = {};
        var http_request = new XMLHttpRequest();
-       http_request.open("GET", url, true);
+       http_request.open('GET', url, true);
+       http_request.setRequestHeader('Accept', 'application/json');
        http_request.onreadystatechange = function () {
                var done = 4, ok = 200;
                if (http_request.readyState == done && http_request.status == ok) {
-                       my_JSON_object = JSON.parse(http_request.responseText);
+                       object = JSON.parse(http_request.responseText);
+                       funct(links, object);
                }
        };
        http_request.send(null);
 }
 
-function update_debbugs_links() {
-       var re = new RegExp("^(\\w+://[^/]+)/.*");
-       var loc = re.exec(window.location.href);
-       var url = loc[1] + "/cgi-bin/bugstatus";
-       var buglinks = new Array();
-       // Iterate through DebianBugs
-       for (var i=0; i < document.links.length; i++) {
-               var link = document.links[i];
-               var bugnum = "";
+function update_debian_links(links, statuses){
+       var bug;
 
-               if (link.title == "DebianBug") {
-                       bugnum = link.text;
-                       if (isNaN(parseInt(bugnum))) {
-                               bugnum = bugnum.replace(/^[^0-9]/g, '');
-                               bugnum = bugnum.replace(/^[^0-9].*/g, '');
-                       }
-               }
-               if( isNaN(parseInt(bugnum)) && link.href.indexOf("://bugs.debian.org/") != -1 ) {
-                       bugnum = link.href;
-                       bugnum = bugnum.replace(/^https?:\/\/bugs\.debian\.org\/([0-9]+)$/i, "$1");
-                       bugnum = bugnum.replace(/^https?:\/\/bugs\.debian\.org\/mbox:([0-9]+)$/i, "$1");
-                       bugnum = bugnum.replace(/^https?:\/\/bugs\.debian\.org\/cgi-bin\/bugreport\.cgi\?.*bug=([0-9]+).*$/i, "$1");
-               }
+       for (bug in statuses) {
+               for (var i in links[bug]) {
+                       var link = links[bug][i];
+                       var status = statuses[bug];
 
-               if (isNaN(parseInt(bugnum))) {
-                       continue;
+                       if (status.pending == 'done') {
+                               var fixed_versions = status.fixed_versions.length ? ' in ' + status.fixed_versions.join(', ') : '';
+                               link.className = 'interwiki closed-bug';
+                               link.title = 'Closed' + fixed_versions + ': #' + status.id + ': ' + status.subject;
+                       } else {
+                               var found_versions = status.found_versions.length ? ' in ' + status.found_versions.join(', ') : '';
+                               link.className = 'interwiki open-bug';
+                               link.title = 'Open' + found_versions + ': #' + status.id + ': ' + status.subject;
+                       }
                }
+       }
+}
 
-               bugnum = parseInt(bugnum).toString();
+function update_launchpad_links(links, status){
+       for (var i in links) {
+               var link = links[i];
 
-               if (buglinks[bugnum] == undefined) {
-                       buglinks[bugnum] = new Array();
+               if (status['cgi-status'] != 'ok' ) {
+                       return;
                }
-
-               buglinks[bugnum].push(link);
+               if( status['complete'] ){
+                       link.className = 'interwiki closed-bug';
+                       link.title = 'Closed: #' + status.id + ': ' + status.title;
+               } else {
+                       link.className = 'interwiki open-bug';
+                       link.title = 'Open: #' + status.id + ': ' + status.title;
+               }
+               link.href = status.url;
        }
+}
 
-       if( buglinks.length <= 0 ){
-               return;
+function update_debian(url, links){
+       var bug;
+       var query = [];
+       for (bug in links) {
+               query.push('bug=' + bug);
        }
 
-       var bugnum;
-       var query = new Array();
-       for (bugnum in buglinks) {
-               query.push("bug=" + bugnum);
+       var querystr = query.join(';');
+
+       // Get bug statuses from script
+       get_json(url + '/cgi-bin/bugstatus?' + querystr, links, update_debian_links);
+}
+
+function update_launchpad(url, links){
+       var bug;
+       var query = [];
+       for (bug in links) {
+               get_json(url + '/cgi-bin/launchpad?bug=' + bug, links[bug], update_launchpad_links);
        }
+}
+
+function update_links() {
 
-       var querystr = query.join(";");
-
-       function update_buglinks(bugstatuses) {
-               var bugnum;
-
-               for (bugnum in bugstatuses) {
-                       for (var i in buglinks[bugnum]) {
-                               var buglink = buglinks[bugnum][i];
-                               var bugstatus = bugstatuses[bugnum];
-
-                               if (bugstatus.pending == "done") {
-                                       var fixed_versions = bugstatus.fixed_versions.length ? ' in ' + bugstatus.fixed_versions.join(', ') : '';
-                                       buglink.className = 'interwiki closed-bug';
-                                       buglink.title = 'Closed' + fixed_versions + ': #' + bugstatus.id + ': ' + bugstatus.subject;
-                               } else {
-                                       var found_versions = bugstatus.found_versions.length ? ' in ' + bugstatus.found_versions.join(', ') : '';
-                                       buglink.className = 'interwiki open-bug';
-                                       buglink.title = 'Open' + found_versions + ': #' + bugstatus.id + ': ' + bugstatus.subject;
-                               }
+       // Preparation
+       var re = new RegExp('^(\\w+://[^/]+)/.*');
+       var loc = re.exec(window.location.href);
+       var types = ['debian','launchpad'];
+       var interwiki = {
+               'DebianBug': 'debian',
+               'UbuntuBug':'launchpad',
+               'LaunchpadBug':'launchpad'
+       };
+       var links = undefined;
+
+       // Iterate through the links on the page
+       for (var i=0; i < document.links.length; i++) {
+               var link = document.links[i];
+               var bug = '';
+               var type = '';
+
+               if( link.title in interwiki ){
+                       type = interwiki[link.title];
+                       bug = link.text;
+                       if (isNaN(parseInt(bug))) {
+                               bug = bug.replace(/^[^0-9]/g, '');
+                               bug = bug.replace(/^[^0-9].*/g, '');
                        }
                }
-       }
 
-       // Get bug statuses from script
-       var requrl = url + "?" + querystr
-       var req = new XMLHttpRequest();
-       req.open("GET", requrl, true);
-       req.setRequestHeader("Accept", "application/json");
-       req.onreadystatechange = function () {
-               if (req.readyState == 4) {
-                       if (req.status == 200) {
-                               var statuses = eval('('+req.responseText+')');
-                               update_buglinks(statuses);
+               if( isNaN(parseInt(bug)) ){
+                       if( link.href.indexOf('://bugs.debian.org/') != -1 ){
+                               type = 'debian';
+                               bug = link.href;
+                               bug = bug.replace(/^https?:\/\/bugs\.debian\.org\/([0-9]+)$/i, '$1');
+                               bug = bug.replace(/^https?:\/\/bugs\.debian\.org\/mbox:([0-9]+)$/i, '$1');
+                               bug = bug.replace(/^https?:\/\/bugs\.debian\.org\/cgi-bin\/bugreport\.cgi\?.*bug=([0-9]+).*$/i, '$1');
+                       } else if( link.href.indexOf('launchpad.net/') != -1 ){
+                               type = 'launchpad';
+                               bug = link.href;
+                               bug = bug.replace(/^http[s]?:\/\/(bugs\.)?launchpad\.net\/.*\/([0-9]+)$/i, '$2');
                        }
-                       req = null;
                }
-       };
 
-       req.send(null);
-}
-
-function modify_launchpad_link(url, link) {
-       var my_JSON_object = {};
-       var http_request = new XMLHttpRequest();
-       http_request.open("GET", url, true);
-       http_request.onreadystatechange = function () {
-               var done = 4, ok = 200;
-               if (http_request.readyState == done && http_request.status == ok) {
-                       buginf = JSON.parse(http_request.responseText);
-                       if (buginf['cgi-status'] != "ok" ) {
-                               return;
-                       }
-                       if ( buginf['complete'] ) {
-                               link.className = 'interwiki closed-bug';
-                               link.title = 'Closed: #' + buginf.id + ': ' + buginf.title;
-                       } else {
-                               link.className = 'interwiki open-bug';
-                               link.title = 'Open: #' + buginf.id + ': ' + buginf.title;
-                       }
-                       link.href = buginf.url;
+               if (isNaN(parseInt(bug))) {
+                       continue;
                }
-       };
-       http_request.send(null);
-}
 
-function update_launchpad_links() {
-       var re = new RegExp("^(\\w+://[^/]+)/.*");
-       var loc = re.exec(window.location.href);
-       var url = loc[1] + "/cgi-bin/launchpad";
+               bug = parseInt(bug).toString();
 
-       var buglinks = new Array();
+               if( links == undefined ){
+                       links = {};
+               }
 
-       // Iterate through UbuntuBugs
-       for (var i=0; i < document.links.length; i++) {
-               var link   = document.links[i];
-               var bugnum = link.text;
-               if ( link.title == "UbuntuBug" ) {
-                       if (! isNaN(parseInt(bugnum))) {
-                               modify_launchpad_link(url + "?bugID=" + bugnum, link);
-                       }
+               if( !(type in links) ){
+                       links[type] = {};
                }
-               if( isNaN(parseInt(bugnum)) && link.href.indexOf("launchpad.net/") != -1 ) {
-                       bugnum = link.href;
-                       bugnum = bugnum.replace(/^http[s]?:\/\/(bugs\.)?launchpad\.net\/.*\/([0-9]+)$/i, "$2");
-                       modify_launchpad_link(url + "?bugID=" + bugnum, link);
+
+               if ( !(bug in links[type]) ){
+                       links[type][bug] = [];
                }
 
+               links[type][bug].push(link);
        }
 
-       if( buglinks.length <= 0 ){
+       if( links == undefined ){
                return;
        }
+
+       if( 'debian' in links ){
+               update_debian(loc[1], links['debian']);
+       }
+
+       if( 'launchpad' in links ){
+               update_launchpad(loc[1], links['launchpad']);
+       }
 }
 
 function add_load_event( funct ) {
@@ -169,5 +166,4 @@ function add_load_event( funct ) {
        }
 }
 
-add_load_event( update_debbugs_links );
-add_load_event( update_launchpad_links );
+add_load_event( update_links );