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