Project

General

Profile

Download (5.05 KB) Statistics
| Branch: | Revision:
1
define([
2
'dojo/_base/array',
3
'dojo/_base/xhr',
4
'dojo/topic',
5
'steam2/user'
6
], function(arrayUtil, xhr, topic, user){
7

    
8
var basewait = 500;
9
var timeout = 0;
10
var ui_update = {
11

    
12
    init: function(){
13
        this.subscribe();
14
    },
15

    
16
    wait: basewait,
17
    url: "/stabile/ui_update/" + user.username + "~ui_update",
18
    session: null,
19
    lasttimestamp: null,
20
    lastev: null,
21

    
22
    subscribe: function(){
23
        var self = this;
24
        if (!user.username) {
25
            ui_update.logout();
26
        } else if (ui_update.logged_out) {
27
            // do nothing
28
        } else {
29
//            console.log("GETTING", ui_update.url, "basewait", basewait, "timeout", timeout);
30
// Using jQuery
31
/*
32
            $.ajax({
33
                url: ui_update.url,
34
                success: function (tasks) {
35
                    ui_update.wait = basewait;
36
                    if (ui_update.publish(tasks)) ui_update.subscribe();
37
                },
38
                dataType: "json",
39
                timeout: timeout,
40
                error: function(tasks) {
41
                    console.log("ui_update error", tasks);
42
                    setTimeout(ui_update.subscribe, ui_update.wait);
43
                    ui_update.wait = ui_update.wait * 1.5;
44
                }
45
            })
46
*/
47
// Unsing Dojo
48
            xhr.get({
49
                "handleAs":"json",
50
                "timeout":timeout,
51
                "url":ui_update.url,
52
                "load": function(tasks){
53
                    ui_update.wait = basewait;
54
                    if (ui_update.publish(tasks)) ui_update.subscribe();
55
                },
56
                "error": function(tasks){
57
                    console.log("ui_update error", tasks);
58
                    setTimeout(ui_update.subscribe, ui_update.wait);
59
                    ui_update.wait = ui_update.wait * 1.5;
60
                }
61
            });
62
        }
63
    },
64

    
65
    publish: function(tasks){
66
        var loggedin = true;
67
        var dup = false;
68
        if (tasks && tasks.length && tasks.length>0) {
69
            arrayUtil.forEach(tasks, function(ev){
70
                console.log("received", ev);
71
                if (dup || (ui_update.lastev && ev.type == "serial" && ev.serial == ui_update.lastev)
72
                ) {
73
                    console.log("got duplicate task");
74
                    dup = true;
75
                } else if(ev.type == "update") {
76
                    if (ev.message) {
77
                        console.log("Also got ui message", ev);
78
                        IRIGO.toaster([{message: ev.message}]);
79
                    }
80
                    console.log("publishing", ev.tab + ":update", ev);
81
                    topic.publish(ev.tab + ":update", ev);
82
                } else if(ev.type == "removal"){
83
                    topic.publish(ev.tab + ":removal", ev);
84
                } else if(ev.type == "session") {
85
                    ui_update.url = ev.url;
86
                    ui_update.session = ev.session;
87
                } else if(ev.type == "logout"){
88
                    ui_update.logout(ev.message);
89
                    loggedin = false;
90
                } else if(ev.type == "message" && ev.message){
91
                    console.log("Got ui message", ev.message);
92
                    IRIGO.toaster([{message: ev.message}]);
93
                } else if (ev.type == "serial" && ev.serial) {
94
                    ui_update.lastev = ev.serial;
95
                }
96
                if (ev.download) {
97
                    console.log("Starting downloader UI...");
98
                    if (!upload.inited) upload.init();
99
                    var nfile = new plupload.File({name: ev.name, size: ev.size, destroy: function(){;}  });
100
                    nfile.url = "http://download";
101
                    nfile.path = ev.download;
102
                    uploader.files.push(nfile);
103
                    upload.updateList();
104
                    var download = new plupload.Downloader(nfile);
105
                    download.start();
106
                    uploader.start();
107
                }
108
                if ( ev.backup ) {
109
                    console.log("Updating missing backups...");
110
                    images.updateMissingBackups();
111
                }
112
//            if (ev.timestamp) ui_update.lastev = ev;
113
            });
114
        } else {
115
            console.log("network error", tasks);
116
            setTimeout(ui_update.subscribe, ui_update.wait);
117
            ui_update.wait = ui_update.wait * 1.5;
118
            return false;
119
        }
120
        return loggedin;
121
    },
122

    
123
    logout: function(msg) {
124
        var loginUrl = '/stabile/login';
125
        if (location.pathname != '' || location.hash!='') loginUrl += "?back=" + location.pathname + location.hash;
126
        if (!msg) msg = "Your session has timed out!";
127
        topic.publish("message", {
128
            message: msg,
129
            duration: 2000,
130
            type:"warning"
131
        });
132
        var to_login_page = function(){
133
            window.location.href = loginUrl;
134
        };
135
        setTimeout(to_login_page, 2000);
136
    },
137
    
138
    // hook: pull style observer pattern.
139
    // used from grid and gridDialog.
140
    onUpdate: function(task){}
141
    
142
};
143

    
144
ui_update.init();
145
window.ui_update = ui_update;
146

    
147
});
(20-20/23)