forked from slopjong/OpenSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openspace.js
157 lines (131 loc) · 5.37 KB
/
openspace.js
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
var openspace = {
/**
* Loads some modules and populates the listbox with spaces known
* in the space directory. After the initialization this object
* is registered as an observer in the OpenSpace javascript module.
*/
onLoad: function() {
// load the openspace module
try{
Components.utils.import("resource://openspace/openspace.jsm");
}catch(e){
Components.utils.reportError("The module 'openspace.jsm' could not be loaded");
return;
}
// load jQuery
var jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].
getService(Components.interfaces.mozIJSSubScriptLoader);
jsLoader.loadSubScript("resource://openspace/jquery-1.7.1.min.js");
jQuery.noConflict();
// populate the listbox with known supported hackerspaces
var listbox = document.getElementById("spaces-list");
//directory = openspace.sortObject(directory);
jQuery.each(directory, function(space, url){
listbox.appendItem(space);
});
// load the preference manager
this.prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch( "extensions.openspace." );
// attach event handlers to the panel
jQuery("#spaces-list").select(this.saveMyspace);
//jQuery("#refresh-interval").change(this.saveRefreshInterval);
registerOpenSpaceObserver(this);
this.initialized = true;
},
/**
* Unregisters this object as an observer in the OpenSpace javascript module.
*/
onUnLoad: function(){
unregisterOpenSpaceObserver(this);
},
/**
* Displays a green, red or a grey dot. The colors have the following meaning:
*
* green: space is open
* red: space is closed
* grey: the space json could not be loaded
*
* This method is called by the javascript module 'openspace.jsm' whenever the timer
* triggers.
*
* @argument {boolean, undefined} status The space status. Undefined if it could not be determined.
* @see openspace.jsm
*/
setStatus: function(status){
if(typeof status == "undefined") {
jQuery("#openspace-status-image").attr("src","chrome://openspace/skin/grey.png");
return;
}
if(typeof status == "string") {
if(status == "true")
status = true;
else
status = false;
}
if(status == true)
jQuery("#openspace-status-image").attr("src","chrome://openspace/skin/green.png");
else
jQuery("#openspace-status-image").attr("src","chrome://openspace/skin/red.png");
},
/**
* Initialize the panel with the space and refresh interval according to the user's configuration.
*/
initPanel: function(){
// select the user's hackerspace
var myspace = this.prefs.getCharPref("myspace");
var list = document.getElementById("spaces-list");
jQuery.each(list.childNodes, function(index,item){
if(myspace === item.label)
list.selectItem(item);
});
var refresh_interval = this.prefs.getIntPref("refresh_interval");
document.getElementById("refresh-interval").value = refresh_interval;
},
/**
* Saves the chosen hackerspace to the preferences.
*/
saveMyspace: function(data){
//jQuery("#spaces-list");
var listbox = document.getElementById("spaces-list");
var myspace = listbox.selectedItem.label;
// this refers to the listbox since saveMyspace is bound
// to it as an event handler, so openspace must be used
openspace.prefs.setCharPref("myspace", myspace);
},
/**
* Saves the refresh interval to the preferences.
*/
saveRefreshInterval: function(){
// doesn't work with jQuery
//jQuery("#refresh-interval").attr("value");
var seconds = document.getElementById("refresh-interval").value;
this.prefs.setIntPref("refresh_interval", seconds);
},
showPanel: function(){
var spanel = document.getElementById('openspace-panel');
var panel = document.getElementById('thepanel');
panel.openPopup(spanel, "before_end", -28, -1, false, false);
},
showNotification: function(that){
PopupNotifications.show(
//gBrowser.selectedBrowser,
//document.getElementById("openspace-panel"),
//document.getElementById("openspace-status-image"),
null,
"sample-popup",
"This is a sample popup notification.",
null, /* anchor ID */
{
label: "Do Something",
accessKey: "D",
callback: function() {
alert("Doing something awesome!");
}
},
null /* secondary action */
);
}
};
window.addEventListener("load", function(e) { openspace.onLoad(e); }, false);
window.addEventListener("unload", function(e){ openspace.onUnLoad(e); }, false);