~launchpad-pqm/launchpad/devel

11644.2.8 by Ian Booth
Next round of work
1
/**
2
 * Launchpad utilities for manipulating links.
3
 *
4
 * @module app
5
 * @submodule links
6
 */
11644.2.18 by Ian Booth
Clean up some ajax, windmill mods
7
11644.2.21 by Ian Booth
Changes as per code review
8
YUI.add('lp.app.links', function(Y) {
9
11912.2.1 by Ian Booth
Add back good changes after testfix revert
10
    var namespace = Y.namespace('lp.app.links');
11
12
    function harvest_links(links_holder, link_class, link_type) {
11644.2.21 by Ian Booth
Changes as per code review
13
        // Get any links of the specified link_class and store them as the
14
        // specified link_type in the specified links_holder
13943.1.5 by Nigel Babu
Lint fixes
15
        var link_info = [];
11810.1.4 by Ian Booth
Revert use of Array.unique()
16
        Y.all('.'+link_class).each(function(link) {
17
            var href = link.getAttribute('href');
18
            if( link_info.indexOf(href)<0 ) {
19
                link_info.push(href);
20
            }
21
        });
11644.2.21 by Ian Booth
Changes as per code review
22
        if( link_info.length > 0 ) {
23
            links_holder[link_type] = link_info;
24
        }
25
    }
26
13943.1.1 by Nigel Babu
Add a "title" attribute with bug title
27
    function process_links(link_info, link_class, link_type) {
28
        // We have a collection of valid and invalid links containing links of
29
        // type link_type, so we need to remove the existing link_class,
30
        // replace it with an invalid-link class, and set the link title.
13978.1.1 by Nigel Babu
Fixes regression and adds more test-cases
31
        if(!Y.Lang.isValue(link_info[link_type])) {
32
            return;
33
        }
13943.1.13 by Nigel Babu
Better code!
34
        var invalid_links = link_info[link_type].invalid || {};
35
        var valid_links = link_info[link_type].valid || {};
13943.1.3 by Nigel Babu
First try at Javascript tests!
36
13943.1.1 by Nigel Babu
Add a "title" attribute with bug title
37
        Y.all('.'+link_class).each(function(link) {
38
            var href = link.getAttribute('href');
13943.1.13 by Nigel Babu
Better code!
39
            if(invalid_links.hasOwnProperty(href)) {
13943.1.1 by Nigel Babu
Add a "title" attribute with bug title
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);
13943.1.5 by Nigel Babu
Lint fixes
47
                });
13943.1.13 by Nigel Babu
Better code!
48
            } else if(valid_links.hasOwnProperty(href)) {
13943.1.1 by Nigel Babu
Add a "title" attribute with bug title
49
                var valid_link_msg = valid_links[href];
50
                link.setAttribute('title', valid_link_msg);
51
            }
52
53
        });
54
    }
55
13943.1.3 by Nigel Babu
First try at Javascript tests!
56
    Y.lp.app.links.check_valid_lp_links = function(io_provider) {
11644.2.18 by Ian Booth
Clean up some ajax, windmill mods
57
        // Grabs any lp: style links on the page and checks that they are
58
        // valid. Invalid ones have their class changed to "invalid-link".
13943.1.11 by Nigel Babu
Changes per review
59
        // ATM, we only handle +branch and bug links.
13943.1.5 by Nigel Babu
Lint fixes
60
61
        var links_to_check = {};
11644.2.18 by Ian Booth
Clean up some ajax, windmill mods
62
11644.2.12 by Ian Booth
Add linkchecker implementation and add unit test
63
        // We get all the links with defined css classes.
64
        // At the moment, we just handle branch links, but in future...
13570.1.3 by Nigel Babu
merged devel in
65
        harvest_links(links_to_check, 'branch-short-link', 'branch_links');
66
        harvest_links(links_to_check, 'bug-link', 'bug_links');
11644.2.18 by Ian Booth
Clean up some ajax, windmill mods
67
68
        // Do we have anything to do?
13943.1.5 by Nigel Babu
Lint fixes
69
        if( Y.Object.size(links_to_check) === 0 ) {
11644.2.18 by Ian Booth
Clean up some ajax, windmill mods
70
            return;
71
        }
11644.2.12 by Ian Booth
Add linkchecker implementation and add unit test
72
73
        // Get the final json to send
74
        var json_link_info = Y.JSON.stringify(links_to_check);
11644.2.6 by Ian Booth
Working
75
        var qs = '';
12421.1.12 by Tim Penhey
Fix up references to methods now in Y.lp.client instead of LP.client.
76
        qs = Y.lp.client.append_qs(qs, 'link_hrefs', json_link_info);
11644.2.18 by Ian Booth
Clean up some ajax, windmill mods
77
78
        var config = {
11644.2.3 by Ian Booth
More hacking - hook in some ajax
79
            on: {
11644.2.14 by Ian Booth
Fix javascript on windmill test
80
                failure: function(id, response, args) {
11644.2.18 by Ian Booth
Clean up some ajax, windmill mods
81
                    // If we have firebug installed, log the error.
13943.1.11 by Nigel Babu
Changes per review
82
                    if( Y.Lang.isValue(console) ) {
11644.2.18 by Ian Booth
Clean up some ajax, windmill mods
83
                        console.log("Link Check Error: " + args + ': '
13943.1.12 by Nigel Babu
A little more cleaner JSON
84
                            + response.status + ' - ' +
85
                            response.statusText + ' - '
86
                            + response.responseXML);
11644.2.18 by Ian Booth
Clean up some ajax, windmill mods
87
                    }
11644.2.13 by Ian Booth
Add some tests
88
                },
11644.2.3 by Ian Booth
More hacking - hook in some ajax
89
                success: function(id, response) {
13943.1.5 by Nigel Babu
Lint fixes
90
                    var link_info = Y.JSON.parse(response.responseText);
91
                    // We handle bug and branch links. The future is here.
13943.1.1 by Nigel Babu
Add a "title" attribute with bug title
92
                    process_links(link_info, 'branch-short-link',
13943.1.11 by Nigel Babu
Changes per review
93
                        'branch_links');
13943.1.1 by Nigel Babu
Add a "title" attribute with bug title
94
                    process_links(link_info, 'bug-link',
13943.1.11 by Nigel Babu
Changes per review
95
                        'bug_links');
11644.2.3 by Ian Booth
More hacking - hook in some ajax
96
                }
97
            }
13943.1.5 by Nigel Babu
Lint fixes
98
        };
11644.2.18 by Ian Booth
Clean up some ajax, windmill mods
99
        var uri = '+check-links';
100
        var on = Y.merge(config.on);
101
        var client = this;
102
        var y_config = { method: "POST",
103
                         headers: {'Accept': 'application/json'},
104
                         on: on,
105
                         'arguments': [client, uri],
106
                         data: qs};
13943.1.3 by Nigel Babu
First try at Javascript tests!
107
        Y.lp.client.get_io_provider(io_provider).io(uri, y_config);
11644.2.8 by Ian Booth
Next round of work
108
    };
11644.2.2 by Ian Booth
More prototyping
109
12421.1.12 by Tim Penhey
Fix up references to methods now in Y.lp.client instead of LP.client.
110
}, "0.1", {"requires": ["base", "node", "io", "dom", "json", "lp.client"]});
11644.2.3 by Ian Booth
More hacking - hook in some ajax
111