]> sipb.mit.edu Git - ikiwiki.git/blob - underlays/openid-selector/ikiwiki/openid/openid-jquery.js
9f588d54ec77faf1003d73e7a16ea09fe0470261
[ikiwiki.git] / underlays / openid-selector / ikiwiki / openid / openid-jquery.js
1 /*
2 Simple OpenID Plugin
3 http://code.google.com/p/openid-selector/
4
5 This code is licenced under the New BSD License.
6 */
7
8 var providers_large = {
9     verisign: {
10         name: 'Verisign',
11         icon: 'ikiwiki/openid/verisign.png',
12         label: 'Enter your Verisign username:',
13         url: 'http://{username}.pip.verisignlabs.com/'
14     },
15     yahoo: {
16         name: 'Yahoo',      
17         icon: 'ikiwiki/openid/goa-account-yahoo.png',
18         url: 'http://me.yahoo.com/'
19     },    
20     openid: {
21         name: 'Other OpenID',     
22         icon: 'wikiicons/openidlogin-bg.gif',
23         label: 'Enter your OpenID:',
24         url: null
25     }
26 };
27 var providers_small = {
28     livejournal: {
29         name: 'LiveJournal',
30         icon: 'ikiwiki/openid/livejournal.png',
31         label: 'Enter your Livejournal username:',
32         url: 'http://{username}.livejournal.com/'
33     },
34     flickr: {
35         name: 'Flickr',        
36         icon: 'ikiwiki/openid/goa-account-flickr.png',
37         label: 'Enter your Flickr username:',
38         url: 'http://flickr.com/photos/{username}/'
39     },
40     wordpress: {
41         name: 'Wordpress',
42         icon: 'ikiwiki/openid/wordpress.png',
43         label: 'Enter your Wordpress.com username:',
44         url: 'http://{username}.wordpress.com/'
45     },
46     aol: {
47         name: 'AOL',     
48         icon: 'ikiwiki/openid/aol.png',
49         label: 'Enter your AOL username:',
50         url: 'http://openid.aol.com/{username}'
51     }
52 };
53 var providers = $.extend({}, providers_large, providers_small);
54
55 var openid = {
56
57         demo: false,
58         ajaxHandler: null,
59         cookie_expires: 6*30,   // 6 months.
60         cookie_name: 'openid_provider',
61         cookie_path: '/',
62         
63         img_path: 'images/',
64         
65         input_id: null,
66         provider_url: null,
67         provider_id: null,
68         localsignin_id: null,
69         
70     init: function(input_id, localsignin_id) {
71         
72         var openid_btns = $('#openid_btns');
73         
74         this.input_id = input_id;
75         
76         $('#openid_choice').show();
77         $('#openid_input_area').empty();
78         
79         // add box for each provider
80         for (id in providers_large) {
81                 openid_btns.append(this.getBoxHTML(providers_large[id], 'large'));
82         }
83
84         if (providers_small) {
85                 openid_btns.append('<br/>');
86                 
87                 for (id in providers_small) {
88                 
89                         openid_btns.append(this.getBoxHTML(providers_small[id], 'small'));
90                 }
91         }
92         if (localsignin_id != "") {
93                 this.localsignin_id=localsignin_id;
94                 openid_btns.append(
95                         '<a href="javascript: openid.signin(\'localsignin\');"' +
96                         ' style="background: #FFF" ' +
97                         'class="localsignin openid_small_btn">' +
98                         '<img alt="" width="16" height="16" src="favicon.ico" />' +
99                         ' other' +
100                         '</a>'
101                 );
102                 $('#'+this.localsignin_id).hide();
103         }
104         
105         $('#openid_form').submit(this.submit);
106         
107         var box_id = this.readCookie();
108         if (box_id) {
109                 this.signin(box_id, true);
110         }
111     },
112     getBoxHTML: function(provider, box_size) {
113         var label="";
114         var title=""
115         if (box_size == 'large') {
116                 label=' ' + provider["name"];
117         }
118         else {
119                 title=' title="'+provider["name"]+'"';
120         }
121         var box_id = provider["name"].toLowerCase();
122         return '<a' + title +' href="javascript: openid.signin(\''+ box_id +'\');"' +
123                         ' style="background: #FFF" ' + 
124                         'class="' + box_id + ' openid_' + box_size + '_btn">' +
125                         '<img alt="" width="16" height="16" src="' + provider["icon"] + '" />' +
126                         label +
127                         '</a>';
128     
129     },
130     /* Provider image click */
131     signin: function(box_id, onload) {
132
133         if (box_id == 'localsignin') {
134                 this.highlight(box_id);
135                 $('#openid_input_area').empty();
136                 $('#'+this.localsignin_id).show();
137                 this.setCookie(box_id);
138                 return;
139         }
140         else {
141                 if (this.localsignin_id) {
142                         $('#'+this.localsignin_id).hide();
143                 }
144         }
145
146         var provider = providers[box_id];
147                 if (! provider) {
148                         return;
149                 }
150                 
151                 this.highlight(box_id);
152                 
153                 this.provider_id = box_id;
154                 this.provider_url = provider['url'];
155                 
156                 // prompt user for input?
157                 if (provider['label']) {
158                         this.setCookie(box_id);
159                         this.useInputBox(provider);
160                 } else {
161                         this.setCookie('');
162                         $('#openid_input_area').empty();
163                         if (! onload) {
164                                 $('#openid_form').submit();
165                         }
166                 }
167     },
168     /* Sign-in button click */
169     submit: function() {
170         
171         var url = openid.provider_url; 
172         if (url) {
173                 url = url.replace('{username}', $('#openid_username').val());
174                 openid.setOpenIdUrl(url);
175         }
176         if(openid.ajaxHandler) {
177                 openid.ajaxHandler(openid.provider_id, document.getElementById(openid.input_id).value);
178                 return false;
179         }
180         if(openid.demo) {
181                 alert("In client demo mode. Normally would have submitted OpenID:\r\n" + document.getElementById(openid.input_id).value);
182                 return false;
183         }
184         return true;
185     },
186     setOpenIdUrl: function (url) {
187     
188         var hidden = $('#'+this.input_id);
189         if (hidden.length > 0) {
190                 hidden.value = url;
191         } else {
192                 $('#openid_form').append('<input style="display:none" id="' + this.input_id + '" name="' + this.input_id + '" value="'+url+'"/>');
193         }
194     },
195     highlight: function (box_id) {
196         
197         // remove previous highlight.
198         var highlight = $('#openid_highlight');
199         if (highlight) {
200                 highlight.replaceWith($('#openid_highlight a')[0]);
201         }
202         // add new highlight.
203         $('.'+box_id).wrap('<div id="openid_highlight"></div>');
204     },
205     setCookie: function (value) {
206     
207                 var date = new Date();
208                 date.setTime(date.getTime()+(this.cookie_expires*24*60*60*1000));
209                 var expires = "; expires="+date.toGMTString();
210                 
211                 document.cookie = this.cookie_name+"="+value+expires+"; path=" + this.cookie_path;
212     },
213     readCookie: function () {
214                 var nameEQ = this.cookie_name + "=";
215                 var ca = document.cookie.split(';');
216                 for(var i=0;i < ca.length;i++) {
217                         var c = ca[i];
218                         while (c.charAt(0)==' ') c = c.substring(1,c.length);
219                         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
220                 }
221                 return null;
222     },
223     useInputBox: function (provider) {
224         
225                 var input_area = $('#openid_input_area');
226                 
227                 var html = '';
228                 var id = 'openid_username';
229                 var value = '';
230                 var label = provider['label'];
231                 var style = '';
232                 
233                 if (provider['name'] == 'OpenID') {
234                         id = this.input_id;
235                         value = '';
236                         style = 'background:#FFF url(wikiicons/openidlogin-bg.gif) no-repeat scroll 0 50%; padding-left:18px;';
237                 }
238                 if (label) {
239                         html = '<label for="'+ id +'" class="block">' + label + '</label>';
240                 }
241                 html += '<input id="'+id+'" type="text" style="'+style+'" name="'+id+'" value="'+value+'" />' + 
242                                         '<input id="openid_submit" type="submit" value="Login"/>';
243                 
244                 input_area.empty();
245                 input_area.append(html);
246
247                 $('#'+id).focus();
248     },
249     setDemoMode: function (demoMode) {
250         this.demo = demoMode;
251     },
252     setAjaxHandler: function (ajaxFunction) {
253         this.ajaxHandler = ajaxFunction;
254     }
255 };