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_large = {
11 icon: 'wikiicons/email.png',
12 label: 'Enter your email address:',
17 icon: 'wikiicons/openidlogin-bg.gif',
18 label: 'Enter your OpenID:',
22 var selections_small = {
25 icon: 'ikiwiki/openid/verisign.png',
26 label: 'Enter your Verisign username:',
27 url: 'http://{username}.pip.verisignlabs.com/'
31 icon: 'ikiwiki/openid/goa-account-yahoo.png',
32 url: 'http://me.yahoo.com/'
36 icon: 'ikiwiki/openid/goa-account-flickr.png',
37 label: 'Enter your Flickr username:',
38 url: 'http://flickr.com/photos/{username}/'
42 icon: 'ikiwiki/openid/wordpress.png',
43 label: 'Enter your Wordpress.com username:',
44 url: 'http://{username}.wordpress.com/'
48 icon: 'ikiwiki/openid/aol.png',
49 label: 'Enter your AOL username:',
50 url: 'http://openid.aol.com/{username}'
53 var selections = $.extend({}, selections_large, selections_small);
58 cookie_expires: 6*30, // 6 months.
59 cookie_name: 'openid_selection', // historical name
69 init: function(input_id, othersignin_id, othersignin_label) {
71 var selector_btns = $('#login_btns');
73 this.input_id = input_id;
75 $('#login_choice').show();
76 $('#login_input_area').empty();
78 // add box for each selection
79 for (id in selections_large) {
80 selector_btns.append(this.getBoxHTML(selections_large[id], 'large'));
82 if (othersignin_label != "") {
83 this.othersignin_label=othersignin_label;
86 this.othersignin_label="other";
88 if (othersignin_id != "") {
89 this.othersignin_id=othersignin_id;
90 selector_btns.prepend(
91 '<a href="javascript: selector.signin(\'othersignin\');"' +
92 ' style="background: #FFF" ' +
93 'class="othersignin login_large_btn">' +
94 '<img alt="" width="16" height="16" src="favicon.ico" />' +
95 ' ' + this.othersignin_label +
98 $('#'+this.othersignin_id).hide();
101 if (selections_small) {
102 selector_btns.append('<br/>');
104 for (id in selections_small) {
105 selector_btns.append(this.getBoxHTML(selections_small[id], 'small'));
109 $('#login_selector_form').submit(this.submit);
111 var box_id = this.readCookie();
113 this.signin(box_id, true);
116 getBoxHTML: function(selection, box_size) {
119 if (box_size == 'large') {
120 label=' ' + selection["name"];
123 title=' title="'+selection["name"]+'"';
125 var box_id = selection["name"].toLowerCase();
126 return '<a' + title +' href="javascript: selector.signin(\''+ box_id +'\');"' +
127 ' style="background: #FFF" ' +
128 'class="' + box_id + ' login_' + box_size + '_btn">' +
129 '<img alt="" width="16" height="16" src="' + selection["icon"] + '" />' +
134 /* selection image click */
135 signin: function(box_id, onload) {
137 if (box_id == 'othersignin') {
138 this.highlight(box_id);
139 $('#login_input_area').empty();
140 $('#'+this.othersignin_id).show();
141 this.setCookie(box_id);
145 if (this.othersignin_id) {
146 $('#'+this.othersignin_id).hide();
150 var selection = selections[box_id];
155 this.highlight(box_id);
157 this.selection_id = box_id;
158 this.selection_url = selection['url'];
160 // prompt user for input?
161 if (selection['label']) {
162 this.setCookie(box_id);
163 this.useInputBox(selection);
166 $('#login_input_area').empty();
168 $('#login_selector_form').submit();
172 /* Sign-in button click */
174 var url = selector.selection_url;
176 url = url.replace('{username}', $('#entry').val());
177 selector.setOpenIdUrl(url);
180 selector.setOpenIdUrl("");
182 if(selector.ajaxHandler) {
183 selector.ajaxHandler(selector.selection_id, document.getElementById(selector.input_id).value);
188 setOpenIdUrl: function (url) {
190 var hidden = $('#'+this.input_id);
191 if (hidden.length > 0) {
194 $('#login_selector_form').append('<input style="display:none" id="' + this.input_id + '" name="' + this.input_id + '" value="'+url+'"/>');
197 highlight: function (box_id) {
199 // remove previous highlight.
200 var highlight = $('#login_highlight');
202 highlight.replaceWith($('#login_highlight a')[0]);
204 // add new highlight.
205 $('.'+box_id).wrap('<div id="login_highlight"></div>');
207 setCookie: function (value) {
209 var date = new Date();
210 date.setTime(date.getTime()+(this.cookie_expires*24*60*60*1000));
211 var expires = "; expires="+date.toGMTString();
213 document.cookie = this.cookie_name+"="+value+expires+"; path=" + this.cookie_path;
215 readCookie: function () {
216 var nameEQ = this.cookie_name + "=";
217 var ca = document.cookie.split(';');
218 for(var i=0;i < ca.length;i++) {
220 while (c.charAt(0)==' ') c = c.substring(1,c.length);
221 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
225 useInputBox: function (selection) {
227 var input_area = $('#login_input_area');
232 var label = selection['label'];
235 if (selection['name'] == 'OpenID') {
238 style = 'background:#FFF url(wikiicons/openidlogin-bg.gif) no-repeat scroll 0 50%; padding-left:18px;';
241 html = '<label for="'+ id +'" class="block">' + label + '</label>';
243 html += '<input name="selection" type="hidden" value="' + selection['name'] + '" />'
244 html += '<input id="'+id+'" type="text" style="'+style+'" name="'+id+'" value="'+value+'" />' +
245 '<input id="selector_submit" type="submit" value="Login"/>';
248 input_area.append(html);
252 setAjaxHandler: function (ajaxFunction) {
253 this.ajaxHandler = ajaxFunction;