~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/javascript/lp-links.js

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-09-16 08:56:39 UTC
  • mfrom: (13943.1.13 bug-title-849121)
  • Revision ID: launchpad@pqm.canonical.com-20110916085639-jrypx5sxptdzgtg6
[r=allenap, rvb][bug=849121] For bug links in Launchpad,
 add the bug's title to the title attribute of the URL if the bug is
 valid.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
    function harvest_links(links_holder, link_class, link_type) {
13
13
        // Get any links of the specified link_class and store them as the
14
14
        // specified link_type in the specified links_holder
15
 
        var link_info = new Array();
 
15
        var link_info = [];
16
16
        Y.all('.'+link_class).each(function(link) {
17
17
            var href = link.getAttribute('href');
18
18
            if( link_info.indexOf(href)<0 ) {
24
24
        }
25
25
    }
26
26
 
27
 
    function process_invalid_links(link_info, link_class, link_type) {
28
 
        // We have a collection of invalid links possibly containing links of
 
27
    function process_links(link_info, link_class, link_type) {
 
28
        // We have a collection of valid and invalid links containing links of
29
29
        // type link_type, so we need to remove the existing link_class,
30
30
        // replace it with an invalid-link class, and set the link title.
31
 
        var invalid_links = link_info['invalid_'+link_type];
32
 
 
33
 
        if( Y.Object.size(invalid_links) == 0 )
34
 
            return;
 
31
        var invalid_links = link_info[link_type].invalid || {};
 
32
        var valid_links = link_info[link_type].valid || {};
35
33
 
36
34
        Y.all('.'+link_class).each(function(link) {
37
35
            var href = link.getAttribute('href');
38
 
            if( !(href in invalid_links) )
39
 
                return;
40
 
            var invalid_link_msg = invalid_links[href];
41
 
            link.removeClass(link_class);
42
 
            link.addClass('invalid-link');
43
 
            link.setAttribute('title', invalid_link_msg);
44
 
            link.on('click', function(e) {
45
 
                e.halt();
46
 
                alert(invalid_link_msg);
47
 
            });
 
36
            if(invalid_links.hasOwnProperty(href)) {
 
37
                var invalid_link_msg = invalid_links[href];
 
38
                link.removeClass(link_class);
 
39
                link.addClass('invalid-link');
 
40
                link.setAttribute('title', invalid_link_msg);
 
41
                link.on('click', function(e) {
 
42
                    e.halt();
 
43
                    alert(invalid_link_msg);
 
44
                });
 
45
            } else if(valid_links.hasOwnProperty(href)) {
 
46
                var valid_link_msg = valid_links[href];
 
47
                link.setAttribute('title', valid_link_msg);
 
48
            }
 
49
 
48
50
        });
49
51
    }
50
52
 
51
 
    Y.lp.app.links.check_valid_lp_links = function() {
 
53
    Y.lp.app.links.check_valid_lp_links = function(io_provider) {
52
54
        // Grabs any lp: style links on the page and checks that they are
53
55
        // valid. Invalid ones have their class changed to "invalid-link".
54
 
        // ATM, we only handle +branch links.
 
56
        // ATM, we only handle +branch and bug links.
55
57
 
56
 
        var links_to_check = {}
 
58
        var links_to_check = {};
57
59
 
58
60
        // We get all the links with defined css classes.
59
61
        // At the moment, we just handle branch links, but in future...
61
63
        harvest_links(links_to_check, 'bug-link', 'bug_links');
62
64
 
63
65
        // Do we have anything to do?
64
 
        if( Y.Object.size(links_to_check) == 0 ) {
 
66
        if( Y.Object.size(links_to_check) === 0 ) {
65
67
            return;
66
68
        }
67
69
 
74
76
            on: {
75
77
                failure: function(id, response, args) {
76
78
                    // If we have firebug installed, log the error.
77
 
                    if( console != undefined ) {
 
79
                    if( Y.Lang.isValue(console) ) {
78
80
                        console.log("Link Check Error: " + args + ': '
79
 
                                + response.status + ' - ' +
80
 
                                response.statusText + ' - '
81
 
                                + response.responseXML);
 
81
                            + response.status + ' - ' +
 
82
                            response.statusText + ' - '
 
83
                            + response.responseXML);
82
84
                    }
83
85
                },
84
86
                success: function(id, response) {
85
 
                    var link_info = Y.JSON.parse(response.responseText)
86
 
                    // ATM, we just handle branch links, but in future...
87
 
                    process_invalid_links(link_info, 'branch-short-link',
88
 
                            'branch_links');
89
 
                    process_invalid_links(link_info, 'bug-link',
90
 
                            'bug_links');
 
87
                    var link_info = Y.JSON.parse(response.responseText);
 
88
                    // We handle bug and branch links. The future is here.
 
89
                    process_links(link_info, 'branch-short-link',
 
90
                        'branch_links');
 
91
                    process_links(link_info, 'bug-link',
 
92
                        'bug_links');
91
93
                }
92
94
            }
93
 
        }
 
95
        };
94
96
        var uri = '+check-links';
95
97
        var on = Y.merge(config.on);
96
98
        var client = this;
99
101
                         on: on,
100
102
                         'arguments': [client, uri],
101
103
                         data: qs};
102
 
        Y.io(uri, y_config);
 
104
        Y.lp.client.get_io_provider(io_provider).io(uri, y_config);
103
105
    };
104
106
 
105
107
}, "0.1", {"requires": ["base", "node", "io", "dom", "json", "lp.client"]});