~autra/webapps-applications/deezer

« back to all changes in this revision

Viewing changes to common/utils.js.in

  • Committer: autra
  • Date: 2013-04-11 12:34:24 UTC
  • mfrom: (406.1.108 trunk)
  • Revision ID: autra@autra-optiplex-380-20130411123424-3b0xa9ppaytxx3hr
sync from parent

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
    return strid;
16
16
}
17
17
 
18
 
var _prevIndicators = [];
 
18
/**
 
19
 * WARNING: this is only kept here for backward compatibility reasons.
 
20
 * This needs to be as soon as the individual scripts are SRUed and
 
21
 * updated to use the latest mechanism
 
22
 */
 
23
var _previousIndicators = [];
19
24
function showIndicators(list) {
20
 
    if (list.length == _prevIndicators.length) {
21
 
        var same = true;
22
 
        for (var i = 0; i < list.length; i++) {
23
 
            if (list[i].name != _prevIndicators[i].name)
24
 
                same = false;
25
 
            if (list[i].count != _prevIndicators[i].count)
26
 
                same = false;
27
 
        }
28
 
        if (same)
29
 
            return;
 
25
    if (list.length == _previousIndicators.length) {
 
26
        var same = true;
 
27
        for (var i = 0; i < list.length; i++) {
 
28
            if (list[i].name != _previousIndicators[i].name)
 
29
                same = false;
 
30
            if (list[i].count != _previousIndicators[i].count)
 
31
                same = false;
 
32
        }
 
33
        if (same)
 
34
            return;
30
35
    }
31
 
    _prevIndicators = list;
 
36
    _previousIndicators = list;
32
37
    Unity.MessagingIndicator.clearIndicators();
33
38
    for (var i = 0; i < list.length; i++) {
34
 
        Unity.MessagingIndicator.showIndicator(list[i].name, { count: list[i].count,
35
 
                                                               callback: list[i].callback });
36
 
    }
37
 
}
 
39
       Unity.MessagingIndicator.showIndicator(list[i].name, { count: list[i].count,
 
40
                                                              callback: list[i].callback });
 
41
    }
 
42
}
 
43
 
 
44
function trim(str) {
 
45
    return str.replace(/^\s+|\s+$/g, "");
 
46
}
 
47
 
 
48
var KEY_NAME = '____unity_indicators_sync';
 
49
function Indicators(getCounters, combineFromMultipleTabs) {
 
50
    this._init(getCounters, combineFromMultipleTabs);
 
51
}
 
52
 
 
53
Indicators.prototype = {
 
54
    _prevIndicators: null,
 
55
 
 
56
    _getCountersCb: null,
 
57
 
 
58
    _init: function(getCounters, combineFromMultipleTabs) {
 
59
        this._last = {};
 
60
        this._timestamp = new Date();
 
61
        this._prevIndicators = [];
 
62
        this._getCountersCb = getCounters;
 
63
        this._combineFromMultipleTabs = combineFromMultipleTabs;
 
64
 
 
65
        window.addEventListener('blur', this._onBlur.bind(this));
 
66
        window.addEventListener('focus', this._onFocus.bind(this));
 
67
 
 
68
        setInterval(this._updateIndicators.bind(this), 8000);
 
69
        this._updateIndicators(true);
 
70
    },
 
71
 
 
72
    _onBlur: function() {
 
73
    },
 
74
 
 
75
    _onFocus: function() {
 
76
        this._timestamp = new Date();
 
77
        this.visited(this._currentLabel);
 
78
    },
 
79
 
 
80
    _updateTotal: function(total) {
 
81
        if (this._combineFromMultipleTabs) {
 
82
            return;
 
83
        }
 
84
        if (total) {
 
85
            Unity.Launcher.setCount(Number(total));
 
86
        } else {
 
87
            Unity.Launcher.clearCount();
 
88
        }
 
89
    },
 
90
 
 
91
    _updateIndicators: function(firstRun) {
 
92
        var state = localStorage.getItem(KEY_NAME);
 
93
        var updateState = false;
 
94
 
 
95
        try {
 
96
            var list = this._getCountersCb();
 
97
        } catch (e) {
 
98
            return;
 
99
        }
 
100
 
 
101
        if (state) {
 
102
            if (!this._combineFromMultipleTabs) {
 
103
                state = JSON.parse(state);
 
104
 
 
105
                if (state.timestamp > this._timestamp) {
 
106
                    this._timestamp = state.timestamp;
 
107
                    this._prevIndicators = state.prevIndicators;
 
108
                    this._last = state.lastValue;
 
109
                } else {
 
110
                    updateState = true;
 
111
                }
 
112
            }
 
113
        } else {
 
114
            updateState = true;
 
115
        }
 
116
        if (!firstRun && !this._forceUpdate && list.length == this._prevIndicators.length) {
 
117
            var same = true;
 
118
            for (var i = 0; i < list.length; i++) {
 
119
                if (list[i].name != this._prevIndicators[i].name)
 
120
                    same = false;
 
121
                if (list[i].count != this._prevIndicators[i].count)
 
122
                    same = false;
 
123
            }
 
124
            if (same)
 
125
                return;
 
126
        }
 
127
 
 
128
        this._forceUpdate = false;
 
129
 
 
130
        var total = 0;
 
131
        if (!this._combineFromMultipleTabs) {
 
132
            Unity.MessagingIndicator.clearIndicators();
 
133
        }
 
134
        for (var i = 0; i < list.length; i++) {
 
135
            var count = 0;
 
136
            if (!this._last[list[i].name]) {
 
137
                this._last[list[i].name] = 0;
 
138
            }
 
139
            if (this._last[list[i].name]) {
 
140
                if (this._last[list[i].name] < Number(list[i].count)) {
 
141
                    count = Number(list[i].count) - this._last[list[i].name];
 
142
                } else {
 
143
                    count = 0;
 
144
                }
 
145
            } else {
 
146
                this._last[list[i].name] = list[i].count;
 
147
            }
 
148
            if (!count) {
 
149
                if (this._combineFromMultipleTabs) {
 
150
                    Unity.MessagingIndicator.clearIndicator(list[i].name);
 
151
                }
 
152
                continue;
 
153
            }
 
154
 
 
155
            total += count;
 
156
            if (!firstRun) {
 
157
                Unity.MessagingIndicator.showIndicator(list[i].name, { count: count,
 
158
                                                                       callback: list[i].callback });
 
159
            } else {
 
160
                this._last[list[i].name] = Number(list[i].count);
 
161
            }
 
162
        }
 
163
        if (!firstRun) {
 
164
            this._updateTotal(total);
 
165
        }
 
166
        this._prevIndicators = list;
 
167
        try {
 
168
            if (updateState) {
 
169
                this._saveState();
 
170
            } else {
 
171
                localStorage.setItem(KEY_NAME, JSON.stringify(state));
 
172
            }
 
173
        } catch (e) {
 
174
            setTimeout((function () {this._updateIndicators(firstRun)}).bind(this), 500);
 
175
        }
 
176
    },
 
177
 
 
178
    _saveState: function() {
 
179
        localStorage.setItem(KEY_NAME, JSON.stringify({
 
180
            timestamp: new Date(),
 
181
            prevIndicators: this._prevIndicators,
 
182
            lastValue: this._last
 
183
        }));
 
184
    },
 
185
 
 
186
    visited: function(labelName) {
 
187
        this._currentLabel = labelName;
 
188
        var count = 0;
 
189
        for (var i = 0; i < this._prevIndicators.length; i++) {
 
190
            if (this._prevIndicators[i].name == labelName) {
 
191
                count = this._prevIndicators[i].count;
 
192
            }
 
193
        }
 
194
        this._forceUpdate = true;
 
195
        this._last[labelName] = count;
 
196
        this._saveState();
 
197
        this._updateIndicators(false);
 
198
    }
 
199
};
38
200
 
39
201
/**
40
202
 * On Chrommium v18 (and maybe earlier versions) the click() function