2 Based on the Simple OpenID Plugin
3 http://code.google.com/p/openid-selector/
5 This code is licenced under the New BSD License.
8 var selections_email_large = {
11 icon: 'wikiicons/emaillogin.png',
12 label: 'Enter your email address:',
16 var selections_openid_large = {
19 icon: 'wikiicons/openidlogin-bg.gif',
20 label: 'Enter your OpenID:',
24 var selections_openid_small = {
27 icon: 'ikiwiki/login-selector/verisign.png',
28 label: 'Enter your Verisign username:',
29 url: 'http://{username}.pip.verisignlabs.com/'
33 icon: 'ikiwiki/login-selector/goa-account-yahoo.png',
34 url: 'http://me.yahoo.com/'
38 icon: 'ikiwiki/login-selector/goa-account-flickr.png',
39 label: 'Enter your Flickr username:',
40 url: 'http://flickr.com/photos/{username}/'
44 icon: 'ikiwiki/login-selector/wordpress.png',
45 label: 'Enter your Wordpress.com username:',
46 url: 'http://{username}.wordpress.com/'
50 icon: 'ikiwiki/login-selector/aol.png',
51 label: 'Enter your AOL username:',
52 url: 'http://openid.aol.com/{username}'
55 var selections = $.extend({}, selections_email_large, selections_openid_large, selections_openid_small);
60 cookie_expires: 6*30, // 6 months.
61 cookie_name: 'openid_selection', // historical name
71 init: function(input_id, login_methods, othersignin_id, othersignin_label) {
73 var selector_btns = $('#login_btns');
75 this.input_id = input_id;
77 $('#login_choice').show();
78 $('#login_input_area').empty();
80 // add box for each selection
81 if (login_methods['openid']) {
82 for (id in selections_openid_large) {
83 selector_btns.append(this.getBoxHTML(selections_openid_large[id], 'large'));
86 if (login_methods['email']) {
87 for (id in selections_email_large) {
88 selector_btns.prepend(this.getBoxHTML(selections_email_large[id], 'large'));
92 if (othersignin_label != "") {
93 this.othersignin_label=othersignin_label;
96 this.othersignin_label="other";
98 if (othersignin_id != "") {
99 this.othersignin_id=othersignin_id;
100 selector_btns.prepend(
101 '<a href="javascript: selector.signin(\'othersignin\');"' +
102 ' style="background: #FFF" ' +
103 'class="othersignin login_large_btn">' +
104 '<img alt="" width="16" height="16" src="favicon.ico" />' +
105 ' ' + this.othersignin_label +
108 $('#'+this.othersignin_id).hide();
111 if (login_methods['openid'] && selections_openid_small) {
112 selector_btns.append('<br/>');
114 for (id in selections_openid_small) {
115 selector_btns.append(this.getBoxHTML(selections_openid_small[id], 'small'));
119 $('#login_selector_form').submit(this.submit);
121 var box_id = this.readCookie();
123 this.signin(box_id, true);
126 getBoxHTML: function(selection, box_size) {
129 if (box_size == 'large') {
130 label=' ' + selection["name"];
133 title=' title="'+selection["name"]+'"';
135 var box_id = selection["name"].toLowerCase();
136 return '<a' + title +' href="javascript: selector.signin(\''+ box_id +'\');"' +
137 ' style="background: #FFF" ' +
138 'class="' + box_id + ' login_' + box_size + '_btn">' +
139 '<img alt="" width="16" height="16" src="' + selection["icon"] + '" />' +
144 /* selection image click */
145 signin: function(box_id, onload) {
147 if (box_id == 'othersignin') {
148 this.highlight(box_id);
149 $('#login_input_area').empty();
150 $('#'+this.othersignin_id).show();
151 this.setCookie(box_id);
155 if (this.othersignin_id) {
156 $('#'+this.othersignin_id).hide();
160 var selection = selections[box_id];
165 this.highlight(box_id);
167 this.selection_id = box_id;
168 this.selection_url = selection['url'];
170 // prompt user for input?
171 if (selection['label']) {
172 this.setCookie(box_id);
173 this.useInputBox(selection);
176 $('#login_input_area').empty();
178 $('#login_selector_form').submit();
182 /* Sign-in button click */
184 var url = selector.selection_url;
186 url = url.replace('{username}', $('#entry').val());
187 selector.setOpenIdUrl(url);
190 selector.setOpenIdUrl("");
192 if (selector.ajaxHandler) {
193 selector.ajaxHandler(selector.selection_id, document.getElementById(selector.input_id).value);
198 setOpenIdUrl: function (url) {
200 var hidden = $('#'+this.input_id);
201 if (hidden.length > 0) {
204 $('#login_selector_form').append('<input style="display:none" id="' + this.input_id + '" name="' + this.input_id + '" value="'+url+'"/>');
207 highlight: function (box_id) {
209 // remove previous highlight.
210 var highlight = $('#login_highlight');
212 highlight.replaceWith($('#login_highlight a')[0]);
214 // add new highlight.
215 $('.'+box_id).wrap('<div id="login_highlight"></div>');
217 setCookie: function (value) {
219 var date = new Date();
220 date.setTime(date.getTime()+(this.cookie_expires*24*60*60*1000));
221 var expires = "; expires="+date.toGMTString();
223 document.cookie = this.cookie_name+"="+value+expires+"; path=" + this.cookie_path;
225 readCookie: function () {
226 var nameEQ = this.cookie_name + "=";
227 var ca = document.cookie.split(';');
228 for(var i=0;i < ca.length;i++) {
230 while (c.charAt(0)==' ') c = c.substring(1,c.length);
231 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
235 useInputBox: function (selection) {
237 var input_area = $('#login_input_area');
240 var id = selection['name']+'_entry';
242 var label = selection['label'];
245 if (selection['name'] == 'OpenID') {
248 style = 'background:#FFF url(wikiicons/openidlogin-bg.gif) no-repeat scroll 0 50%; padding-left:18px;';
251 html = '<label for="'+ id +'" class="block">' + label + '</label>';
253 html += '<input id="'+id+'" type="text" style="'+style+'" name="'+id+'" value="'+value+'" />' +
254 '<input id="selector_submit" type="submit" value="Login"/>';
257 input_area.append(html);
261 setAjaxHandler: function (ajaxFunction) {
262 this.ajaxHandler = ajaxFunction;