3 http://code.google.com/p/openid-selector/
5 This code is licenced under the New BSD License.
8 var providers_large = {
11 icon: 'ikiwiki/openid/verisign.png',
12 label: 'Enter your Verisign username:',
13 url: 'http://{username}.pip.verisignlabs.com/'
17 icon: 'ikiwiki/openid/goa-account-yahoo.png',
18 url: 'http://me.yahoo.com/'
22 icon: 'wikiicons/openidlogin-bg.gif',
23 label: 'Enter your OpenID:',
27 var providers_small = {
29 var providers = $.extend({}, providers_large, providers_small);
35 cookie_expires: 6*30, // 6 months.
36 cookie_name: 'openid_provider',
46 init: function(input_id, localsignin_id, localsignin_label) {
48 var openid_btns = $('#openid_btns');
50 this.input_id = input_id;
52 $('#openid_choice').show();
53 $('#openid_input_area').empty();
55 // add box for each provider
56 for (id in providers_large) {
57 openid_btns.append(this.getBoxHTML(providers_large[id], 'large'));
59 if (localsignin_label != "") {
60 this.localsignin_label=localsignin_label;
63 this.localsignin_label="other";
65 if (localsignin_id != "") {
66 this.localsignin_id=localsignin_id;
68 '<a href="javascript: openid.signin(\'localsignin\');"' +
69 ' style="background: #FFF" ' +
70 'class="localsignin openid_large_btn">' +
71 '<img alt="" width="16" height="16" src="favicon.ico" />' +
72 ' ' + this.localsignin_label +
75 $('#'+this.localsignin_id).hide();
78 if (providers_small) {
79 openid_btns.append('<br/>');
81 for (id in providers_small) {
83 openid_btns.append(this.getBoxHTML(providers_small[id], 'small'));
87 $('#openid_form').submit(this.submit);
89 var box_id = this.readCookie();
91 this.signin(box_id, true);
94 getBoxHTML: function(provider, box_size) {
97 if (box_size == 'large') {
98 label=' ' + provider["name"];
101 title=' title="'+provider["name"]+'"';
103 var box_id = provider["name"].toLowerCase();
104 return '<a' + title +' href="javascript: openid.signin(\''+ box_id +'\');"' +
105 ' style="background: #FFF" ' +
106 'class="' + box_id + ' openid_' + box_size + '_btn">' +
107 '<img alt="" width="16" height="16" src="' + provider["icon"] + '" />' +
112 /* Provider image click */
113 signin: function(box_id, onload) {
115 if (box_id == 'localsignin') {
116 this.highlight(box_id);
117 $('#openid_input_area').empty();
118 $('#'+this.localsignin_id).show();
119 this.setCookie(box_id);
123 if (this.localsignin_id) {
124 $('#'+this.localsignin_id).hide();
128 var provider = providers[box_id];
133 this.highlight(box_id);
135 this.provider_id = box_id;
136 this.provider_url = provider['url'];
138 // prompt user for input?
139 if (provider['label']) {
140 this.setCookie(box_id);
141 this.useInputBox(provider);
144 $('#openid_input_area').empty();
146 $('#openid_form').submit();
150 /* Sign-in button click */
153 var url = openid.provider_url;
155 url = url.replace('{username}', $('#openid_username').val());
156 openid.setOpenIdUrl(url);
158 if(openid.ajaxHandler) {
159 openid.ajaxHandler(openid.provider_id, document.getElementById(openid.input_id).value);
163 alert("In client demo mode. Normally would have submitted OpenID:\r\n" + document.getElementById(openid.input_id).value);
168 setOpenIdUrl: function (url) {
170 var hidden = $('#'+this.input_id);
171 if (hidden.length > 0) {
174 $('#openid_form').append('<input style="display:none" id="' + this.input_id + '" name="' + this.input_id + '" value="'+url+'"/>');
177 highlight: function (box_id) {
179 // remove previous highlight.
180 var highlight = $('#openid_highlight');
182 highlight.replaceWith($('#openid_highlight a')[0]);
184 // add new highlight.
185 $('.'+box_id).wrap('<div id="openid_highlight"></div>');
187 setCookie: function (value) {
189 var date = new Date();
190 date.setTime(date.getTime()+(this.cookie_expires*24*60*60*1000));
191 var expires = "; expires="+date.toGMTString();
193 document.cookie = this.cookie_name+"="+value+expires+"; path=" + this.cookie_path;
195 readCookie: function () {
196 var nameEQ = this.cookie_name + "=";
197 var ca = document.cookie.split(';');
198 for(var i=0;i < ca.length;i++) {
200 while (c.charAt(0)==' ') c = c.substring(1,c.length);
201 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
205 useInputBox: function (provider) {
207 var input_area = $('#openid_input_area');
210 var id = 'openid_username';
212 var label = provider['label'];
215 if (provider['name'] == 'OpenID') {
218 style = 'background:#FFF url(wikiicons/openidlogin-bg.gif) no-repeat scroll 0 50%; padding-left:18px;';
221 html = '<label for="'+ id +'" class="block">' + label + '</label>';
223 html += '<input id="'+id+'" type="text" style="'+style+'" name="'+id+'" value="'+value+'" />' +
224 '<input id="openid_submit" type="submit" value="Login"/>';
227 input_area.append(html);
231 setDemoMode: function (demoMode) {
232 this.demo = demoMode;
234 setAjaxHandler: function (ajaxFunction) {
235 this.ajaxHandler = ajaxFunction;