~liu-xiao-guo/debiantrial/html-weibo

« back to all changes in this revision

Viewing changes to www/js/app.js

  • Committer: XiaoGuo, Liu
  • Date: 2015-03-03 11:38:03 UTC
  • Revision ID: xiaoguo.liu@canonical.com-20150303113803-p3tux1bhqxa9k8vx
Initial release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Wait before the DOM has been loaded before initializing the Ubuntu UI layer
 
3
 */
 
4
window.onload = function () {
 
5
    var UI = new UbuntuUI();
 
6
    UI.init();
 
7
    UI.pagestack.push('main');
 
8
    var api = external.getUnityObject('1.0');
 
9
    var oa = api.OnlineAccounts;
 
10
 
 
11
    UI.button('getalbums').click(auth);
 
12
    var photosdiv = document.querySelector('#presults')
 
13
    var lalbums = UI.list('#albums');
 
14
    var token = '';
 
15
 
 
16
    function auth() {
 
17
        UI.pagestack.clear();
 
18
        lalbums.removeAllItems();
 
19
        setResults('');
 
20
        UI.pagestack.push('albumspage');
 
21
        document.getElementById('progress').style.display='block';
 
22
 
 
23
        var filters = {'provider': '', 'service': 'Weibo scope'};
 
24
 
 
25
        oa.api.getAccounts(filters, function(accounts) {
 
26
            console.log("total length: " + accounts.length);
 
27
 
 
28
            document.getElementById('progress').style.display='none';
 
29
            if (accounts.length < 1) {
 
30
                setResults('No Weibo accounts enabled');
 
31
                return;
 
32
            }
 
33
 
 
34
            function authcallback(res){
 
35
                token = res['data']['AccessToken'];
 
36
 
 
37
                getFacebookAlbums(token, function(albums) {
 
38
                    if (albums) {
 
39
                        for (var i = 0; i < albums.length; ++i) {
 
40
                            var name = albums[i]['name'];
 
41
                            var id = albums[i]['id'];
 
42
                            var getPhotos = getFacebookPhotosForAlbum(id, token, function(photos) {
 
43
                                for (i2=0; i2<photos.length; i2++) {
 
44
                                    var shape = document.createElement('div');
 
45
                                    shape.setAttribute('data-role', "shape");
 
46
                                    shape.setAttribute('id', photos[i2]);
 
47
                                    var img = document.createElement('img');
 
48
                                    img.setAttribute('src', photos[i2]);
 
49
                                    img.setAttribute('width', '120');
 
50
                                    shape.appendChild(img);
 
51
                                    photosdiv.appendChild(shape);
 
52
                                    (function(pic){
 
53
                                        UI.shape(pic).click(function(){
 
54
                                        UI.pagestack.push('photopage');
 
55
                                        document.getElementById('photoimg').setAttribute('src', pic);
 
56
                                        });
 
57
                                    })(photos[i2]);
 
58
                                }
 
59
                            });
 
60
                            lalbums.append(
 
61
                                name.slice(1,name.length-1) + ' ' + id.slice(1,id.length-1),
 
62
                                null,
 
63
                                null,
 
64
                                getPhotos);
 
65
                        }
 
66
                    }
 
67
                    else {
 
68
                        setResults('<br><br>ERROR');
 
69
                    }
 
70
                });
 
71
            }
 
72
            accounts[0].authenticate(authcallback);
 
73
        }); //getAccounts
 
74
    } //auth
 
75
 
 
76
    function getFacebookPhotosForAlbum(albumId, accessToken, callback) {
 
77
        return function() {
 
78
            while (photosdiv.hasChildNodes()) {
 
79
                photosdiv.removeChild(photosdiv.lastChild);
 
80
            }
 
81
            var http = new XMLHttpRequest()
 
82
            UI.pagestack.push('albumpage');
 
83
            var url = "https://graph.facebook.com/" +
 
84
                albumId.slice(1,albumId.length-1) +
 
85
                "?fields=photos&access_token=" +
 
86
                accessToken;
 
87
            http.open("GET", url, true);
 
88
            var photos = [];
 
89
            http.onreadystatechange = function() {
 
90
                if (http.readyState === 4){
 
91
                    if (http.status == 200) {
 
92
                        var response = JSON.parse(http.responseText);
 
93
                        for (i = 0; i<response['photos']['data'].length; i++ ) {
 
94
                            var picture = JSON.stringify(response['photos']['data'][i]['images'][0]['source']);
 
95
                            picture = picture.slice(1,picture.length-1);
 
96
                            photos.push(picture);
 
97
                        }
 
98
                        callback(photos);
 
99
                    } else {
 
100
                        console.log("error: " + http.status)
 
101
                        callback(null);
 
102
                    }
 
103
                }
 
104
            };
 
105
            http.send(null);
 
106
        };
 
107
    }
 
108
 
 
109
    function getFacebookAlbums(accessToken, callback) {
 
110
        var http = new XMLHttpRequest()
 
111
        var url = "https://graph.facebook.com/me?fields=albums.fields(id,name)&access_token=" + accessToken;
 
112
        //console.log('url: \n' + url)
 
113
        http.open("GET", url, true);
 
114
        var albums = [];
 
115
        http.onreadystatechange = function() {
 
116
            if (http.readyState === 4){
 
117
                if (http.status == 200) {
 
118
                    var response = JSON.parse(http.responseText);
 
119
                    for (i = 0; i<response['albums']['data'].length; i++ ) {
 
120
                        var name = JSON.stringify(response['albums']['data'][i]['name']);
 
121
                        var id = JSON.stringify(response['albums']['data'][i]['id']);
 
122
                        albums.push({'id': id, 'name': name});
 
123
                    }
 
124
 
 
125
                    callback(albums);
 
126
                } else {
 
127
                    console.log("error: " + http.status)
 
128
                    callback(null);
 
129
                }
 
130
            }
 
131
        };
 
132
        http.send(null);
 
133
    }
 
134
 
 
135
    function setResults(data) {
 
136
        var results = document.getElementById('results');
 
137
        results.innerHTML = data;
 
138
    };
 
139
}; //onload