summaryrefslogtreecommitdiff
path: root/ikiwiki/login-selector/login-selector.js
blob: 71ae0466e351387458d6da75a2ec9db86ceb0548 (plain)
  1. /*
  2. Based on the Simple OpenID Plugin
  3. http://code.google.com/p/openid-selector/
  4. This code is licenced under the New BSD License.
  5. */
  6. var selections_email_large = {
  7. email: {
  8. name: 'Email',
  9. icon: 'wikiicons/email.png',
  10. label: 'Enter your email address:',
  11. url: null
  12. }
  13. };
  14. var selections_openid_large = {
  15. openid: {
  16. name: 'OpenID',
  17. icon: 'wikiicons/openidlogin-bg.gif',
  18. label: 'Enter your OpenID:',
  19. url: null
  20. }
  21. };
  22. var selections_openid_small = {
  23. verisign: {
  24. name: 'Verisign',
  25. icon: 'ikiwiki/login-selector/verisign.png',
  26. label: 'Enter your Verisign username:',
  27. url: 'http://{username}.pip.verisignlabs.com/'
  28. },
  29. yahoo: {
  30. name: 'Yahoo',
  31. icon: 'ikiwiki/login-selector/goa-account-yahoo.png',
  32. url: 'http://me.yahoo.com/'
  33. },
  34. flickr: {
  35. name: 'Flickr',
  36. icon: 'ikiwiki/login-selector/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/login-selector/wordpress.png',
  43. label: 'Enter your Wordpress.com username:',
  44. url: 'http://{username}.wordpress.com/'
  45. },
  46. aol: {
  47. name: 'AOL',
  48. icon: 'ikiwiki/login-selector/aol.png',
  49. label: 'Enter your AOL username:',
  50. url: 'http://openid.aol.com/{username}'
  51. }
  52. };
  53. var selections = $.extend({}, selections_email_large, selections_openid_large, selections_openid_small);
  54. var selector = {
  55. ajaxHandler: null,
  56. cookie_expires: 6*30, // 6 months.
  57. cookie_name: 'openid_selection', // historical name
  58. cookie_path: '/',
  59. img_path: 'images/',
  60. input_id: null,
  61. selection_url: null,
  62. selection_id: null,
  63. othersignin_id: null,
  64. init: function(input_id, login_methods, othersignin_id, othersignin_label) {
  65. var selector_btns = $('#login_btns');
  66. this.input_id = input_id;
  67. $('#login_choice').show();
  68. $('#login_input_area').empty();
  69. // add box for each selection
  70. if (login_methods['openid']) {
  71. for (id in selections_openid_large) {
  72. selector_btns.append(this.getBoxHTML(selections_openid_large[id], 'large'));
  73. }
  74. }
  75. if (login_methods['email']) {
  76. for (id in selections_email_large) {
  77. selector_btns.prepend(this.getBoxHTML(selections_email_large[id], 'large'));
  78. }
  79. }
  80. if (othersignin_label != "") {
  81. this.othersignin_label=othersignin_label;
  82. }
  83. else {
  84. this.othersignin_label="other";
  85. }
  86. if (othersignin_id != "") {
  87. this.othersignin_id=othersignin_id;
  88. selector_btns.prepend(
  89. '<a href="javascript: selector.signin(\'othersignin\');"' +
  90. ' style="background: #FFF" ' +
  91. 'class="othersignin login_large_btn">' +
  92. '<img alt="" width="16" height="16" src="favicon.ico" />' +
  93. ' ' + this.othersignin_label +
  94. '</a>'
  95. );
  96. $('#'+this.othersignin_id).hide();
  97. }
  98. if (login_methods['openid'] && selections_openid_small) {
  99. selector_btns.append('<br/>');
  100. for (id in selections_openid_small) {
  101. selector_btns.append(this.getBoxHTML(selections_openid_small[id], 'small'));
  102. }
  103. }
  104. $('#login_selector_form').submit(this.submit);
  105. var box_id = this.readCookie();
  106. if (box_id) {
  107. this.signin(box_id, true);
  108. }
  109. },
  110. getBoxHTML: function(selection, box_size) {
  111. var label="";
  112. var title=""
  113. if (box_size == 'large') {
  114. label=' ' + selection["name"];
  115. }
  116. else {
  117. title=' title="'+selection["name"]+'"';
  118. }
  119. var box_id = selection["name"].toLowerCase();
  120. return '<a' + title +' href="javascript: selector.signin(\''+ box_id +'\');"' +
  121. ' style="background: #FFF" ' +
  122. 'class="' + box_id + ' login_' + box_size + '_btn">' +
  123. '<img alt="" width="16" height="16" src="' + selection["icon"] + '" />' +
  124. label +
  125. '</a>';
  126. },
  127. /* selection image click */
  128. signin: function(box_id, onload) {
  129. if (box_id == 'othersignin') {
  130. this.highlight(box_id);
  131. $('#login_input_area').empty();
  132. $('#'+this.othersignin_id).show();
  133. this.setCookie(box_id);
  134. return;
  135. }
  136. else {
  137. if (this.othersignin_id) {
  138. $('#'+this.othersignin_id).hide();
  139. }
  140. }
  141. var selection = selections[box_id];
  142. if (! selection) {
  143. return;
  144. }
  145. this.highlight(box_id);
  146. this.selection_id = box_id;
  147. this.selection_url = selection['url'];
  148. // prompt user for input?
  149. if (selection['label']) {
  150. this.setCookie(box_id);
  151. this.useInputBox(selection);
  152. } else {
  153. this.setCookie('');
  154. $('#login_input_area').empty();
  155. if (! onload) {
  156. $('#login_selector_form').submit();
  157. }
  158. }
  159. },
  160. /* Sign-in button click */
  161. submit: function() {
  162. var url = selector.selection_url;
  163. if (url) {
  164. url = url.replace('{username}', $('#entry').val());
  165. selector.setOpenIdUrl(url);
  166. }
  167. else {
  168. selector.setOpenIdUrl("");
  169. }
  170. if (selector.ajaxHandler) {
  171. selector.ajaxHandler(selector.selection_id, document.getElementById(selector.input_id).value);
  172. return false;
  173. }
  174. return true;
  175. },
  176. setOpenIdUrl: function (url) {
  177. var hidden = $('#'+this.input_id);
  178. if (hidden.length > 0) {
  179. hidden.value = url;
  180. } else {
  181. $('#login_selector_form').append('<input style="display:none" id="' + this.input_id + '" name="' + this.input_id + '" value="'+url+'"/>');
  182. }
  183. },
  184. highlight: function (box_id) {
  185. // remove previous highlight.
  186. var highlight = $('#login_highlight');
  187. if (highlight) {
  188. highlight.replaceWith($('#login_highlight a')[0]);
  189. }
  190. // add new highlight.
  191. $('.'+box_id).wrap('<div id="login_highlight"></div>');
  192. },
  193. setCookie: function (value) {
  194. var date = new Date();
  195. date.setTime(date.getTime()+(this.cookie_expires*24*60*60*1000));
  196. var expires = "; expires="+date.toGMTString();
  197. document.cookie = this.cookie_name+"="+value+expires+"; path=" + this.cookie_path;
  198. },
  199. readCookie: function () {
  200. var nameEQ = this.cookie_name + "=";
  201. var ca = document.cookie.split(';');
  202. for(var i=0;i < ca.length;i++) {
  203. var c = ca[i];
  204. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  205. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  206. }
  207. return null;
  208. },
  209. useInputBox: function (selection) {
  210. var input_area = $('#login_input_area');
  211. var html = '';
  212. var id = selection['name']+'_entry';
  213. var value = '';
  214. var label = selection['label'];
  215. var style = '';
  216. if (selection['name'] == 'OpenID') {
  217. id = this.input_id;
  218. value = '';
  219. style = 'background:#FFF url(wikiicons/openidlogin-bg.gif) no-repeat scroll 0 50%; padding-left:18px;';
  220. }
  221. if (label) {
  222. html = '<label for="'+ id +'" class="block">' + label + '</label>';
  223. }
  224. html += '<input id="'+id+'" type="text" style="'+style+'" name="'+id+'" value="'+value+'" />' +
  225. '<input id="selector_submit" type="submit" value="Login"/>';
  226. input_area.empty();
  227. input_area.append(html);
  228. $('#'+id).focus();
  229. },
  230. setAjaxHandler: function (ajaxFunction) {
  231. this.ajaxHandler = ajaxFunction;
  232. }
  233. };