summaryrefslogtreecommitdiff
path: root/ikiwiki/login-selector/login-selector.js
blob: f54a944694cd63a6ff4ee2ccc0903a5e79894b8b (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 = $.extend({}, selections_email_large, selections_openid_large);
  23. var selector = {
  24. ajaxHandler: null,
  25. cookie_expires: 6*30, // 6 months.
  26. cookie_name: 'openid_selection', // historical name
  27. cookie_path: '/',
  28. img_path: 'images/',
  29. input_id: null,
  30. selection_url: null,
  31. selection_id: null,
  32. othersignin_id: null,
  33. init: function(input_id, login_methods, othersignin_id, othersignin_label) {
  34. var selector_btns = $('#login_btns');
  35. this.input_id = input_id;
  36. $('#login_choice').show();
  37. $('#login_input_area').empty();
  38. // add box for each selection
  39. if (login_methods['openid']) {
  40. for (id in selections_openid_large) {
  41. selector_btns.append(this.getBoxHTML(selections_openid_large[id], 'large'));
  42. }
  43. }
  44. if (login_methods['email']) {
  45. for (id in selections_email_large) {
  46. selector_btns.prepend(this.getBoxHTML(selections_email_large[id], 'large'));
  47. }
  48. }
  49. if (othersignin_label != "") {
  50. this.othersignin_label=othersignin_label;
  51. }
  52. else {
  53. this.othersignin_label="other";
  54. }
  55. if (othersignin_id != "") {
  56. this.othersignin_id=othersignin_id;
  57. selector_btns.prepend(
  58. '<a href="javascript: selector.signin(\'othersignin\');"' +
  59. ' style="background: #FFF" ' +
  60. 'class="othersignin login_large_btn">' +
  61. '<img alt="" width="16" height="16" src="favicon.ico" />' +
  62. ' ' + this.othersignin_label +
  63. '</a>'
  64. );
  65. $('#'+this.othersignin_id).hide();
  66. }
  67. $('#login_selector_form').submit(this.submit);
  68. var box_id = this.readCookie();
  69. if (box_id) {
  70. this.signin(box_id, true);
  71. }
  72. },
  73. getBoxHTML: function(selection, box_size) {
  74. var label="";
  75. var title=""
  76. if (box_size == 'large') {
  77. label=' ' + selection["name"];
  78. }
  79. else {
  80. title=' title="'+selection["name"]+'"';
  81. }
  82. var box_id = selection["name"].toLowerCase();
  83. return '<a' + title +' href="javascript: selector.signin(\''+ box_id +'\');"' +
  84. ' style="background: #FFF" ' +
  85. 'class="' + box_id + ' login_' + box_size + '_btn">' +
  86. '<img alt="" width="16" height="16" src="' + selection["icon"] + '" />' +
  87. label +
  88. '</a>';
  89. },
  90. /* selection image click */
  91. signin: function(box_id, onload) {
  92. if (box_id == 'othersignin') {
  93. this.highlight(box_id);
  94. $('#login_input_area').empty();
  95. $('#'+this.othersignin_id).show();
  96. this.setCookie(box_id);
  97. return;
  98. }
  99. else {
  100. if (this.othersignin_id) {
  101. $('#'+this.othersignin_id).hide();
  102. }
  103. }
  104. var selection = selections[box_id];
  105. if (! selection) {
  106. return;
  107. }
  108. this.highlight(box_id);
  109. this.selection_id = box_id;
  110. this.selection_url = selection['url'];
  111. // prompt user for input?
  112. if (selection['label']) {
  113. this.setCookie(box_id);
  114. this.useInputBox(selection);
  115. } else {
  116. this.setCookie('');
  117. $('#login_input_area').empty();
  118. if (! onload) {
  119. $('#login_selector_form').submit();
  120. }
  121. }
  122. },
  123. /* Sign-in button click */
  124. submit: function() {
  125. var url = selector.selection_url;
  126. if (url) {
  127. url = url.replace('{username}', $('#entry').val());
  128. selector.setOpenIdUrl(url);
  129. }
  130. else {
  131. selector.setOpenIdUrl("");
  132. }
  133. if (selector.ajaxHandler) {
  134. selector.ajaxHandler(selector.selection_id, document.getElementById(selector.input_id).value);
  135. return false;
  136. }
  137. return true;
  138. },
  139. setOpenIdUrl: function (url) {
  140. var hidden = $('#'+this.input_id);
  141. if (hidden.length > 0) {
  142. hidden.value = url;
  143. } else {
  144. $('#login_selector_form').append('<input style="display:none" id="' + this.input_id + '" name="' + this.input_id + '" value="'+url+'"/>');
  145. }
  146. },
  147. highlight: function (box_id) {
  148. // remove previous highlight.
  149. var highlight = $('#login_highlight');
  150. if (highlight) {
  151. highlight.replaceWith($('#login_highlight a')[0]);
  152. }
  153. // add new highlight.
  154. $('.'+box_id).wrap('<div id="login_highlight"></div>');
  155. },
  156. setCookie: function (value) {
  157. var date = new Date();
  158. date.setTime(date.getTime()+(this.cookie_expires*24*60*60*1000));
  159. var expires = "; expires="+date.toGMTString();
  160. document.cookie = this.cookie_name+"="+value+expires+"; path=" + this.cookie_path;
  161. },
  162. readCookie: function () {
  163. var nameEQ = this.cookie_name + "=";
  164. var ca = document.cookie.split(';');
  165. for(var i=0;i < ca.length;i++) {
  166. var c = ca[i];
  167. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  168. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  169. }
  170. return null;
  171. },
  172. useInputBox: function (selection) {
  173. var input_area = $('#login_input_area');
  174. var html = '';
  175. var id = selection['name']+'_entry';
  176. var value = '';
  177. var label = selection['label'];
  178. var style = '';
  179. if (selection['name'] == 'OpenID') {
  180. id = this.input_id;
  181. value = '';
  182. style = 'background:#FFF url(wikiicons/openidlogin-bg.gif) no-repeat scroll 0 50%; padding-left:18px;';
  183. }
  184. if (label) {
  185. html = '<label for="'+ id +'" class="block">' + label + '</label>';
  186. }
  187. html += '<input id="'+id+'" type="text" style="'+style+'" name="'+id+'" value="'+value+'" />' +
  188. '<input id="selector_submit" type="submit" value="Login"/>';
  189. input_area.empty();
  190. input_area.append(html);
  191. $('#'+id).focus();
  192. },
  193. setAjaxHandler: function (ajaxFunction) {
  194. this.ajaxHandler = ajaxFunction;
  195. }
  196. };