-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinks.xml
290 lines (221 loc) · 10.6 KB
/
Links.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="Websites"
description="Websites">
<Require feature="opensocial-0.9" />
<Require feature="views" />
<Require feature="dynamic-height" />
<Require feature="pubsub" />
<Require feature="osapi" />
</ModulePrefs>
<!-- ==================== START COMBINED VIEWS ==================== -->
<Content type="html" view="home, profile">
<![CDATA[<!--HTML-->
<!DOCTYPE html>
<!-- #includes -->
<link rel="stylesheet" href="css/gadget.css" type="text/css" media="screen, projection" >
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>
<script type="text/javascript" src="js/os.js" ></script>
<script type="text/javascript" src="js/UCSF.js" ></script>
<style>
.links_title { font-family:Verdana, Arial; font-size: 14px; }
.links_body { font-family:Arial; font-size: 12px; }
.links_credit { font-family:Arial; font-size:10px; }
.links_save_button { height:20px; font-size:11px; }
</style>
<script type="text/javascript">
var g_max_links = 5;
var g_oLinks = []; // declare it like this to make json work
// ========================================
function sort_by (field, reverse, primer) {
reverse = (reverse) ? -1 : 1;
return function(a,b) {
a = a[field];
b = b[field];
if (typeof(primer) != 'undefined') {
a = primer(a);
b = primer(b);
}
if (a<b) return reverse * -1;
if (a>b) return reverse * 1;
return 0;
}
}
// ========================================
// ========================================
function deleteArrayItem (array_index) {
g_oLinks.splice(array_index,1);
// write links data to gadget database
osapi.appdata.update({'userId': '@viewer', 'appId': '@app', 'data': {'links' : gadgets.json.stringify(g_oLinks)} }).execute(function(result) {
if (result.error) {
alert("Error " + result.error.code + " writing application data: " + result.error.message + ". Your edited link list was not saved.");
}
});
// show links w/o deleted item even if data write fails - array already spliced
displayData();
}
// ========================================
// ========================================
function readData(callback) {
osapi.appdata.get({'userId': '@owner', 'appId':'@app', 'fields' : ['links']} ).execute(function(result){
// get incoming link data (in json string format)
var viewer = os.osapi.getViewerFromResult(result);
// convert to json object format
g_oLinks = gadgets.json.parse(viewer.links) || [];
// execute the callback;
callback();
}); /* end osapi.appdata.get */
}
// ========================================
// ========================================
function displayData() {
// if links data exists
if (g_oLinks) {
// sort object by link name, case-insensitive, A-Z
g_oLinks.sort(sort_by('link_name', false, function(a){return a.toUpperCase()}));
if (document.getElementById("edit_links_table")){
// EDIT MODE - build table to hold retrieved app data
var links_table_data = "<table cellspacing='10' cellpadding='0' border='0'><tr>";
var favicon_path_array;
for (i in g_oLinks) {
cell_name = g_oLinks[i].link_name;
cell_url = g_oLinks[i].link_url;
cell_url2 = g_oLinks[i].link_url;
favicon_path_array = cell_url.split("//");
cell_url2 = favicon_path_array[1];
favicon_path_array = cell_url2.split("/");
cell_url2 = favicon_path_array[0];
cell_favicon="<img height='16' width=16' src='http://www.google.com/s2/favicons?domain=" + cell_url2 + "' />";
// build and add table row
links_table_data = links_table_data
+ "<tr>" + "<td>" + cell_favicon + "</td>"
+ "<td>" + "<a href='" + cell_url + "' target='_blank'>" + cell_name + "</a></td>"
+ "<td>" + cell_url + "</td>"
+ "<td><input type='button' class='links_save_button' value='Delete' onClick='deleteArrayItem("
+ i + ")'" + "></td>" + "</tr>";
}
// close the table
links_table_data = links_table_data + "</tr></table>";
// put appdata table markup in designated div
// and set height based on which view view this is
document.getElementById("edit_links_table").innerHTML=links_table_data;
edit_mode_height = 250 + ((g_oLinks.length - 1) * 28) + 10;
gadgets.window.adjustHeight(edit_mode_height);
}
if(document.getElementById("view_links_table")){
// VIEW MODE - build table to hold retrieved app data
links_table_data = "<table cellspacing='10' cellpadding='0' border='0'><tr>";
for (i in g_oLinks) {
cell_name = g_oLinks[i].link_name;
cell_url = g_oLinks[i].link_url;
cell_url2 = g_oLinks[i].link_url;
favicon_path_array = cell_url.split("//");
cell_url2 = favicon_path_array[1];
favicon_path_array = cell_url2.split("/");
cell_url2 = favicon_path_array[0];
cell_favicon="<img height='16' width=16' src='http://www.google.com/s2/favicons?domain=" + cell_url2 + "' />";
// build and add table row
links_table_data = links_table_data
+ "<tr>" + "<td>" + cell_favicon + "</td>"
+ "<td onClick=\"gadgetEventTrack('go_to_website', cell_name)\">" + "<a href='" + cell_url + "' target='_blank'>" + cell_name + "</a></td>" + "</tr>";
}
// close the table
links_table_data = links_table_data + "</tr></table>";
// put appdata table markup in designated div
document.getElementById("view_links_table").innerHTML=links_table_data;
view_mode_height = 12 + ((g_oLinks.length - 1) * 30) + 34;
gadgets.window.adjustHeight(view_mode_height);
}
} /* end if link data exists */
}
// ========================================
// ========================================
function saveData() {
// get link name and url from form
var new_link_name=document.getElementById("linkname").value;
var new_link_url=document.getElementById("linkurl").value;
if (g_oLinks.length < g_max_links || !g_oLinks) {
// check for empty input boxes
if(new_link_name=="" || new_link_url==""){
alert("Please provide both a Link Name and a URL");
return;
}
// prepend http header if missing
if(new_link_url.indexOf("://") == -1){new_link_url = "http://" + new_link_url;}
var newLinkNdx = g_oLinks.length;
g_oLinks[newLinkNdx] = {};
g_oLinks[newLinkNdx].link_name = new_link_name;
g_oLinks[newLinkNdx].link_url = new_link_url;
// write links data to gadget database
osapi.appdata.update({'userId': '@viewer', 'appId': '@app', 'data': {'links' : gadgets.json.stringify(g_oLinks)} }).execute(function(result) {
if (result.error) {
alert("Error " + result.error.code + " writing application data: " + result.error.message);
} else {
// refresh after update, clear input fields - don't need to reset g_oLinks as displayData does this
displayData();
document.getElementById("linkname").value = "";
document.getElementById("linkurl").value = "http://";
alert("Your links information is saved. Don't forget to use the Hide / Show links to make this section visible or hidden on your profile page.");
}
});
} else {
alert("You already have the maximum number of links.");
}
}
// ==============================================================
function gadgetEventTrack(action, label, value) {
var message = {'action' : action};
if (label) {message.label = label;}
if (value) {message.value = value;}
gadgets.pubsub.publish("analytics", message);
}
// ==============================================================
</script>
]]></Content>
<!-- ==================== END COMBINED VIEWS ==================== -->
<!-- ==================== START HOME/EDIT VIEW ==================== -->
<Content type="html" view="default, home" preferred_height="300">
<![CDATA[<!--HTML-->
<div id="hideshow" style="padding:5px 10px 0px 0px;"></div>
<h3 style="padding-left:10px; padding-top: 0px;">Manage Links to Other Websites</h3>
<div style="padding:5px 0px 0px 25px;">
Add up to five websites to your profile.
Enter the website name, as you want it to appear on your profile, and its URL.
Some samples include a link to your lab web site, your research program or your research blog.<br /><br />
</div>
<!-- display the new link input fields -->
<div class='question' style="padding:0px 0px 5px 12px;">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="links_body" valign="top"><b>Website Name</b><br />
e.g. My Lab Site<br />
<input id="linkname" type="linkname" name="linkname" style="width:280px; margin-top:4px;"><br />
(60 characters max)
</td>
<td> </td>
<td class="links_body" valign="top"><b>Website URL</b> (not displayed in profile)<br />
e.g. mylabsite.ucsf.edu<br />
<input id="linkurl" type="linkurl" name="linkurl" style="width:250px; margin-top:4px;" value="http://">
</td>
<td> </td>
<td><br /><br /><input type="button" style="margin-bottom: 8px;" value="Save" onClick="saveData();"></td>
</tr>
</table>
</div>
<h4 style="padding-left:10px; padding-top: 0px;">Your Current Websites:</h4>
<div id="edit_links_table" style="padding:0px 0px 10px 25px;"></div>
<script type="text/javascript">
gadgets.util.registerOnLoadHandler(readData(function(){displayData()}));
</script>
]]></Content>
<!-- ==================== END HOME/EDIT VIEW ==================== -->
<!-- ==================== START PROFILE VIEW ==================== -->
<Content type="html" view="default, profile" preferred_height="100">
<![CDATA[<!--HTML-->
<div id="view_links_table" style="padding:0px 0px 10px 20px;"></div>
<script type="text/javascript">
gadgets.util.registerOnLoadHandler(readData(function(){displayData()}));
</script>
]]></Content>
<!-- ==================== END PROFILE VIEW ==================== -->
</Module>