Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/site/app/webroot/js/stats/plot-selection.js
blob: 805e3b9720e12f3977327dfe30fc5040b8605482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
var plotSelection = {
    summary: {}, // JSON summary retrieved with field names and other data
    dropdowns: {},
    
    /**************************************
     *       Dropdown Data Utilities      *
     *************************************/
    
    /**
     * Loads plotSelection.summary data from JSON
     */
    loadSummary: function() {
        $.getJSON(statsURL + 'json/' + addonID + '/summary', function(data) {
            plotSelection.summary = data;
        });
        
    },
    
    /**************************************
     *    Dropdown Creation Utilities     *
     *************************************/
    
    addPlotSelector: function() {
        var dropdown = new Dropdown({
                'id': 'plot-selector',
                'type': 'big-menu',
                'onChange': 'Plots.determinePlot();',
                'hasColorbox': false,
                'parentDOM': 'plot-selector-area',
                'removeOnNewTimeplot': false
            });
        
        this.dropdowns[dropdown.config.id] = dropdown;
        
        var menu = this.dropdowns[dropdown.config.id].addMenu({
                'name': 'plotselectionmenu',
                'showNoneForLevel1': false
            });
        
        menu.addItem({'value': 'summary', 'name': localized['statistics_js_plotselection_selector_summary']}).select();
        menu.addItem({'value': 'downloads', 'name': localized['statistics_js_plotselection_selector_downloads']});
        menu.addItem({'value': 'updatepings', 'name': localized['statistics_js_plotselection_selector_adu']});
        menu.addItem({'value': 'version', 'name': localized['statistics_js_plotselection_selector_version'], 'indented': true});
        menu.addItem({'value': 'application', 'name': localized['statistics_js_plotselection_selector_application'], 'indented': true});
        menu.addItem({'value': 'status', 'name': localized['statistics_js_plotselection_selector_status'], 'indented': true});
        menu.addItem({'value': 'os', 'name': localized['statistics_js_plotselection_selector_os'], 'indented': true});
        //menu.addItem({'value': 'custom', 'name': localized['statistics_js_plotselection_selector_custom']});
    },

    addGroupBySelector: function() {
        var group_drop = new Dropdown({
            'id':          'group-by-selector',
            'type':        'group-by',
            'onChange':    'Plots.determinePlot();',
            'hasColorbox': false,
            'parentDOM':   'plot-selector-area',
            'removeOnNewTimeplot': false
        });
        this.dropdowns['group-by-selector'] = group_drop;
        var menu = group_drop.addMenu({
            'name': 'group-by-selection-menu',
            'showNoneForLevel1': false
        });

        menu.addItem({'value': 'date',  'name': localized['statistics_js_groupby_selector_date']}).select();
        menu.addItem({'value': 'week',  'name': localized['statistics_js_groupby_selector_week']});
        menu.addItem({'value': 'month', 'name': localized['statistics_js_groupby_selector_month']});
        menu.addItem({'value': 'week_over_week', 'name':localized['statistics_js_groupby_selector_week_over_week']});

        $('#group-by-selector').hide();
    },

    getGroupByValue: function() {
        return (!this.dropdowns['group-by-selector']) ? 'date' :
            this.dropdowns['group-by-selector'].selectedItem.value;
    },
    
    addAdditionalDropdown: function() {
        var type = plotSelection.dropdowns['plot-selector'].selectedItem.value;
        
        if (type == 'custom')
            this.addCustomDropdown();
        else
            this.addDefinedDropdown();
    },
    
    /**
     * Adds a custom dropdown with menus and submenus from summary
     */
    addCustomDropdown: function() {
        // Add dropdown
        var dropdown = new Dropdown({
                'type': 'custom', 'removeOnNewTimeplot': true
            });
        
        this.dropdowns[dropdown.config.id] = dropdown;
        
        // Add data type menu
        var typeMenu = this.dropdowns[dropdown.config.id].addMenu({
                'type': 'custom'
            });
        
        $.each(this.summary.updatepings.plotFields, function(field, values) {
            // Add data type items
            var typeItem = typeMenu.addItem({
                    'value': field,
                    'name': plotSelection.summary.prettyNames[field],
                    'isSubmenu': true
                });
            
            // Loop through values (versions, statuses, app names, etc)
            $.each(values, function(item, count) {
                if (typeof count != 'object') {
                    // If not an object, regular menu item
                    typeItem.submenu.addItem({
                            'value': item,
                            'name': item,
                            'tooltip': sprintf(localized['statistics_js_plotselection_foundinrange'], count)
                        });
                }
                else {
                    // If an object, it's an application
                    
                    // Filter GUID to make for better id=""
                    var filtered = item.replace(/[\s\@\.\?\{\}\-\"%<>]/g, '_');
                    
                    // Add application name item
                    var appName = (item in plotSelection.summary.prettyNames ? plotSelection.summary.prettyNames[item] : plotSelection.summary.prettyNames['unknown'] + ' ' + item);
                    
                    var appItem = typeItem.submenu.addItem({
                            'value': filtered,
                            'name': appName,
                            'tooltip': 'GUID: ' + item,
                            'isSubmenu': true
                        });
                    
                    // Loop through application versions
                    $.each(count, function(appVersion, appCount) {
                        // Add item for application version
                        appItem.submenu.addItem({
                            'value': appVersion,
                            'name': appVersion,
                            'tooltip': sprintf(localized['statistics_js_plotselection_foundinrange'], appCount)
                            });
                    });
                }
            });
        });
    },
    
    addDefinedDropdowns: function() {
        this.addOptionsDropdown();
        
        for (var i = 2; i < 8; i++) {
            if (Plots.availableFields[i]) {
                this.addDefinedDropdown(i);
            }
        }
        
    },
    
    addDefinedDropdown: function(selectedIndex) {
        var type = this.dropdowns['plot-selector'].selectedItem.value;
        
        var dropdown = new Dropdown({
                'type': type,
                'onChange': 'plotSelection.definedChanged(this);'
            });
        
        this.dropdowns[dropdown.config.id] = dropdown;
        
        var menu = this.dropdowns[dropdown.config.id].addMenu({
                'name': type,
                'scrolling': true
            });
        
        var item;
        
        if (type == 'application') {
            var appMenus = {};
        }
        
        for (var j = 2; j < Plots.availableFields.length; j++) {
            if (type == 'application') {
                var appInfo = this.getApplicationName(Plots.availableFields[j]);
                
                if (!appMenus[appInfo.guid]) {
                    // Filter GUID to make for better id=""
                    var filtered = appInfo.guid.replace(/[\s\@\.\?\{\}\-\"%<>]/g, '_');
                    
                    // Add application name item
                    var appName = (appInfo.guid in plotSelection.summary.prettyNames ? plotSelection.summary.prettyNames[appInfo.guid] : plotSelection.summary.prettyNames['unknown'] + ' ' + appInfo.guid);
                    
                    appMenus[appInfo.guid] = menu.addItem({
                            'value': filtered,
                            'name': appName,
                            'tooltip': 'GUID: ' + appInfo.guid,
                            'isSubmenu': true
                        });
                }
                
                item = appMenus[appInfo.guid].submenu.addItem({
                        'value': Plots.availableFields[j],
                        'name': appInfo.itemName,
                        'tooltip': appInfo.itemTooltip,
                        'prefix': (plotSelection.summary.shortNames[type] ? plotSelection.summary.shortNames[type] + ':' : '')
                    });
            }
            else
                item = menu.addItem({
                        'value': Plots.availableFields[j],
                        'name': Plots.availableFields[j],
                        'prefix': (plotSelection.summary.shortNames[type] ? plotSelection.summary.shortNames[type] + ':' : '')
                    });
            
            
            if (selectedIndex == j)
                item.select();
        }
    },
    
    getApplicationName: function(guid) {
        var appParts = guid.split('/');
        if (plotSelection.summary.shortNames[plotSelection.summary.prettyNames[appParts[0]]]) {
            var itemName = plotSelection.summary.shortNames[plotSelection.summary.prettyNames[appParts[0]]] + '&nbsp;' + appParts[1];
            var itemTooltip = appParts[0] + ' (' + plotSelection.summary.prettyNames[appParts[0]] + ')';
        }
        else {
            var itemName = plotSelection.summary.shortNames['unknown'] + '&nbsp;' + appParts[1];
            var itemTooltip = appParts[0] + ' (' + plotSelection.summary.prettyNames['unknown'] + ')';
        }
        
        return {'guid': appParts[0], 'version': appParts[1], 'itemName': itemName, 'itemTooltip': itemTooltip};
    },
    
    definedChanged: function(dropdown) {
        // Remove existing plot
        Plots.defined.removePlot(dropdown.config.id);
        
        // If not clearing the plot, add the new selected plot
        if (dropdown.selectedItem.value != '') {
            Plots.defined.addPlot(dropdown.config.id, Plots.availableFields.indexOf(dropdown.selectedItem.value), dropdown.config.color);
        }
    },
    
    addOptionsDropdown: function() {
        var type = this.dropdowns['plot-selector'].selectedItem.value;
        
        var dropdown = new Dropdown({
                'id': 'options',
                'type': 'options',
                'title': '<img src="' + $('#options-cog').attr('src') + '" />',
                'removeOnNewTimeplot': true,
                'removable': false,
                'hasColorbox': false,
                'itemsToggle': true,
                'parentDOM': 'options-area'
            });
        
        this.dropdowns[dropdown.config.id] = dropdown;
        
        var menu = this.dropdowns[dropdown.config.id].addMenu({
                'name': 'options',
                'showNoneForLevel1': false
            });
        
        if (type != 'summary' && type != 'downloads') {
            menu.addItem({
                    'value': 'count',
                    'name': localized['statistics_js_plotselection_options_count_name_checked'],
                    'nameChecked': localized['statistics_js_plotselection_options_count_name_checked'],
                    'nameUnchecked': localized['statistics_js_plotselection_options_count_name_unchecked'],
                    'tooltip': localized['statistics_js_plotselection_options_count_tooltip'],
                    'checked': true,
                    'onSelect': 'plotSelection.togglePlot(this);',
                    'addValueToClass': true
                });
        }
        
        if (type != 'summary') {
            menu.addItem({
                    'value': 'events-firefox',
                    'name': localized['statistics_js_plotselection_options_events_firefox_name_checked'],
                    'nameChecked': localized['statistics_js_plotselection_options_events_firefox_name_checked'],
                    'nameUnchecked': localized['statistics_js_plotselection_options_events_firefox_name_unchecked'],
                    'tooltip': localized['statistics_js_plotselection_options_events_firefox_tooltip'],
                    'checked': true,
                    'onSelect': 'plotSelection.toggleEventPlot(this);',
                    'addValueToClass': true
                });
            
            var addonItem = menu.addItem({
                    'value': 'events-addon',
                    'name': sprintf(localized['statistics_js_plotselection_options_events_addon_name_checked'], addonName),
                    'nameChecked': sprintf(localized['statistics_js_plotselection_options_events_addon_name_checked'], addonName),
                    'nameUnchecked': sprintf(localized['statistics_js_plotselection_options_events_addon_name_unchecked'], addonName),
                    'tooltip': localized['statistics_js_plotselection_options_events_addon_tooltip'],
                    'checked': true,
                    'onSelect': 'plotSelection.toggleEventPlot(this);',
                    'addValueToClass': true
                });
            $('#' + addonItem.config.id + ' .item-toggle-icon').html('<img src="' + $('#addon-icon').attr('src') + '" width="16" height="16" />');
        }
        
        if (type != 'summary' && type != 'downloads') {
            menu.addItem({
                    'value': 'add-plot',
                    'name': localized['statistics_js_plotselection_options_addplot_name'],
                    'tooltip': localized['statistics_js_plotselection_options_addplot_tooltip'],
                    'onSelect': 'plotSelection.addAdditionalDropdown();',
                    'addValueToClass': true
                });
        }
        
        if (type != 'summary')
            menu.addDivider();
        
        menu.addItem({
                'value': 'resize',
                'name': localized['statistics_js_plotselection_options_resize_name_unchecked'],
                'nameChecked': localized['statistics_js_plotselection_options_resize_name_checked'],
                'nameUnchecked': localized['statistics_js_plotselection_options_resize_name_unchecked'],
                'tooltip': localized['statistics_js_plotselection_options_resize_tooltip'],
                'onSelect': 'plotSelection.resizePlot(this);',
                'addValueToClass': true
            });
        
        /*menu.addItem({
                'value': 'rss',
                'name': 'Subscribe to Graph',
                'tooltip': 'Subscribe to this graph for daily updates',
                'addValueToClass': true
            });*/
        
        var csvItem = menu.addItem({
                'value': 'csv',
                'name': localized['statistics_js_plotselection_options_csv_name'],
                'tooltip': localized['statistics_js_plotselection_options_csv_tooltip'],
                'addValueToClass': true
            });
        $('#' + csvItem.config.id + ' a').attr('href', Plots.currentCSV);
    },
    
    togglePlot: function(item) {
        if (item.config.checked)
            Plots.defined.addPlot(item.config.value, 1, '#000000');
        else
            Plots.defined.removePlot(item.config.value);
    },
    
    toggleEventPlot: function(item) {
        if (item.config.checked)
            Plots.defined.addEventPlot(item.config.value);
        else
            Plots.defined.removePlot(item.config.value);
    },
    
    resizePlot: function(item) {
        if (item.config.checked)
            Plots.resizePlot(350);
        else
            Plots.resizePlot(150);
    },
    
    remove: function(dropdown) {
        Plots.defined.removePlot(dropdown.config.id);
        plotSelection.dropdowns[dropdown.config.id] = null;
    },
    
    removeAll: function() {
        colors.resetCounter();
        
        $.each(plotSelection.dropdowns, function(dropdown_id, object) {
            if (plotSelection.dropdowns[dropdown_id] != null) {
                if (plotSelection.dropdowns[dropdown_id].remove(true))
                    plotSelection.dropdowns[dropdown_id] = null;
            }
        });
    }
};