materio_user.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // @codekit-prepend "gui.js"
  2. // @koala-prepend "gui.js"
  3. (function($) {
  4. MaterioUser = function(){
  5. var _isAdhesion = $('body').is('.page-node-11187') || $('body').is('.page-node-11186');
  6. function init() {
  7. //trace('MaterioUser init compile test 3');
  8. if(_isAdhesion)
  9. initAdhesion();
  10. };
  11. function initAdhesion(){
  12. //trace('initAdhesion');
  13. $('.get-link a').bind('click', function(event) {
  14. // do not show the registration form if already logged-in
  15. if($('body').is('.logged-in'))
  16. return true;
  17. // else show the ajaxified registration form
  18. event.preventDefault();
  19. var $this = $(this),
  20. href = $this.attr("href"),
  21. destination = href.match('/\?destination=([^,]+)');
  22. destination = "/" + destination[1].replace('%23', '#');
  23. trace('destination', destination);
  24. loadRegistrationBlock(destination);
  25. return false;
  26. });
  27. };
  28. function loadRegistrationBlock(destination){
  29. //trace('loadRegistrationBlock :: dest = '+dest);
  30. $.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_user/register',
  31. // {'types':types,'current_path':document.location.href, 'keys':keys, 'searchmode':searchmode},
  32. function(json){
  33. //trace('json', json);
  34. var $modal = $('<div>').addClass('modal-content').append(json.block);
  35. // $.event.trigger({
  36. // type:"form-loaded",
  37. // context:"modal-content"
  38. // });
  39. var id,label, description;
  40. $('input.form-text', $modal).each(function(i) {
  41. id = $(this).attr('id');
  42. label = $('label[for='+id+']').hide().text();
  43. description = $(this).parent().find('.description').hide().text();
  44. $(this).attr('placeholder', label).attr('title', description);
  45. });
  46. // $('.homepage-textfield', $modal).hide();
  47. $('body').append($('<div>').addClass('modal-wrapper').append($modal));
  48. // doesn't work
  49. // Drupal.attachBehaviors('bodi>.modal-wrapper');
  50. $.event.trigger('ajax-register-block-loaded');
  51. $("#user-register-form #edit-submit--2", $modal).click(function(event){
  52. event.preventDefault();
  53. $.ajax({
  54. type: 'POST',
  55. dataType:'json',
  56. url: Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/register/submit",
  57. data: $('#user-register-form', $modal).serialize(),
  58. success: function(data) { onUserLoginRegisterSubmit(data, destination);},
  59. error: function() {trace('error');}
  60. });
  61. return false;
  62. });
  63. $("#user-login #edit-submit", $modal).click(function(event){
  64. event.preventDefault();
  65. $.ajax({
  66. type: 'POST',
  67. dataType:'json',
  68. url: Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/login/submit",
  69. data: $('#user-login', $modal).serialize(),
  70. success: function(data){ onUserLoginRegisterSubmit(data, destination); },
  71. error: function() { trace('error'); }
  72. });
  73. return false;
  74. });
  75. // google analytics
  76. // $.event.trigger({
  77. // type : "record-stat",
  78. // categorie : 'Search',
  79. // action : keys,
  80. // label : 'filters : '+ stringTypes.join(' ,'),
  81. // value : json.count
  82. // });
  83. });
  84. };
  85. function onUserLoginRegisterSubmit(data, destination){
  86. cleanModalErrors();
  87. if(data.errors != null){
  88. for(field in data.errors){
  89. $('#user-login input[name='+field+']', '.modal-content').addClass('error');
  90. $('.modal-content').prepend($('<div>').addClass('message-error').html(data.errors[field]));
  91. }
  92. }else{
  93. window.location.pathname = destination;
  94. }
  95. };
  96. function cleanModalErrors() {
  97. $('.message-error', '.modal-content').remove();
  98. $('input', '.modal-content').removeClass('error');
  99. }
  100. init();
  101. };
  102. $(document).ready(function() {
  103. var materiouser = new MaterioUser();
  104. });
  105. })(jQuery);