materio_user.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. destination = destination.replace('//', '/');
  24. // trace('destination', destination);
  25. if($this.parents('.gratos').size()){
  26. loadRegisterBlock(destination);
  27. }else{
  28. loadLoginAndRegisterBlock(destination);
  29. }
  30. return false;
  31. });
  32. };
  33. function loadRegisterBlock (destination) {
  34. trace("loadRegisterBlock :: dest = "+destination);
  35. $.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_user/registerblock', function(json){formLoaded(json, destination);});
  36. }
  37. function loadLoginAndRegisterBlock(destination){
  38. //trace('loadRegistrationBlock :: dest = '+destination);
  39. $.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_user/loginandregisterblock', function(json){formLoaded(json, destination);});
  40. };
  41. function formLoaded(json, destination){
  42. //trace('formLoaded | json', json);
  43. var $modal = $('<div>').addClass('modal-content').append(json.block);
  44. var id,label, description;
  45. $('input.form-text', $modal).each(function(i) {
  46. id = $(this).attr('id');
  47. label = $('label[for='+id+']').hide().text();
  48. description = $(this).parent().find('.description').hide().text();
  49. $(this).attr('placeholder', label).attr('title', description);
  50. });
  51. // $('.homepage-textfield', $modal).hide();
  52. $('body').append($('<div>').addClass('modal-wrapper').append($modal));
  53. $('.modal-wrapper').bind('click', function(event) {
  54. $(this).remove();
  55. });
  56. $('.modal-content').bind('click', function(event) {
  57. event.stopPropagation();
  58. });
  59. // doesn't work
  60. // Drupal.attachBehaviors('bodi>.modal-wrapper');
  61. $.event.trigger('ajax-register-block-loaded');
  62. $("#user-register-form #edit-submit, #user-register-form #edit-submit--2", $modal).click(function(event){
  63. event.preventDefault();
  64. $.ajax({
  65. type: 'POST',
  66. dataType:'json',
  67. url: Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/register/submit",
  68. data: $('#user-register-form', $modal).serialize(),
  69. success: function(data) { onUserLoginRegisterSubmit($('#user-register-form'), data, destination);},
  70. error: function() {trace('error');}
  71. });
  72. return false;
  73. });
  74. $("#user-login #edit-submit", $modal).click(function(event){
  75. event.preventDefault();
  76. $.ajax({
  77. type: 'POST',
  78. dataType:'json',
  79. url: Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/login/submit",
  80. data: $('#user-login', $modal).serialize(),
  81. success: function(data){ onUserLoginRegisterSubmit($('#user-login'), data, destination); },
  82. error: function() { trace('error'); }
  83. });
  84. return false;
  85. });
  86. // google analytics
  87. // $.event.trigger({
  88. // type : "record-stat",
  89. // categorie : 'Search',
  90. // action : keys,
  91. // label : 'filters : '+ stringTypes.join(' ,'),
  92. // value : json.count
  93. // });
  94. };
  95. function onUserLoginRegisterSubmit($form, data, destination){
  96. cleanModalErrors();
  97. if(data.errors != null){
  98. for(field in data.errors){
  99. $('input[name='+field+']', $form).addClass('error');
  100. $form.prepend($('<div>').addClass('message-error').html(data.errors[field]));
  101. }
  102. }else{
  103. // trace('destination = '+destination);
  104. window.location.pathname = destination;
  105. }
  106. };
  107. function cleanModalErrors() {
  108. $('.message-error', '.modal-content').remove();
  109. $('input', '.modal-content').removeClass('error');
  110. }
  111. init();
  112. };
  113. $(document).ready(function() {
  114. var materiouser = new MaterioUser();
  115. });
  116. })(jQuery);