materio_user.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // @codekit-prepend "gui.js"
  2. // @koala-prepend "gui_ck_fw/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. // trace('get-link click');
  15. // do not show the registration form if already logged-in
  16. if($('body').is('.logged-in'))
  17. return true;
  18. // else show the ajaxified registration form
  19. event.preventDefault();
  20. var $this = $(this),
  21. href = $this.attr("href"),
  22. destination = href.match('/\?destination=([^,]+)');
  23. destination = "/" + destination[1].replace('%23', '#');
  24. destination = destination.replace('//', '/');
  25. // trace('destination', destination);
  26. if($this.parents('.gratos').size()){
  27. loadRegisterBlock(destination);
  28. }else{
  29. loadLoginAndRegisterBlock(destination);
  30. }
  31. return false;
  32. });
  33. };
  34. function loadRegisterBlock (destination) {
  35. // trace("loadRegisterBlock :: dest = "+destination);
  36. $.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_user/registerblock', function(json){formLoaded(json, destination);});
  37. }
  38. function loadLoginAndRegisterBlock(destination){
  39. // trace('loadRegistrationBlock :: dest = '+destination);
  40. $.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_user/loginandregisterblock', function(json){formLoaded(json, destination);});
  41. };
  42. function formLoaded(json, destination){
  43. //trace('formLoaded | json', json);
  44. var $modal = $('<div>').addClass('modal-content').append(json.block);
  45. var id,label, description;
  46. $('input.form-text', $modal).each(function(i) {
  47. id = $(this).attr('id');
  48. label = $('label[for='+id+']').hide().text();
  49. description = $(this).parent().find('.description').hide().text();
  50. $(this).attr('placeholder', label).attr('title', description);
  51. });
  52. // $('.homepage-textfield', $modal).hide();
  53. $('body').append($('<div>').addClass('modal-wrapper').append($modal));
  54. $('.modal-wrapper').bind('click', function(event) {
  55. $(this).remove();
  56. });
  57. $('.modal-content').bind('click', function(event) {
  58. event.stopPropagation();
  59. });
  60. // doesn't work
  61. // Drupal.attachBehaviors('bodi>.modal-wrapper');
  62. $.event.trigger('ajax-register-block-loaded');
  63. $("#user-register-form #edit-submit, #user-register-form #edit-submit--2", $modal).click(function(event){
  64. event.preventDefault();
  65. // disable submit button to avoid duplicate user creation
  66. // console.log('click', this);
  67. $(this).attr('disabled', 'true');
  68. $.ajax({
  69. type: 'POST',
  70. dataType:'json',
  71. url: Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/register/submit",
  72. data: $('#user-register-form', $modal).serialize(),
  73. success: function(data) { onUserLoginRegisterSubmit($('#user-register-form'), data, destination);},
  74. error: function(jqXHR, textStatus, errorThrown) { trace('error : '+textStatus+' | '+errorThrown); }
  75. });
  76. return false;
  77. });
  78. $("#user-login #edit-submit", $modal).click(function(event){
  79. event.preventDefault();
  80. $.ajax({
  81. type: 'POST',
  82. dataType:'json',
  83. url: Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/login/submit",
  84. data: $('#user-login', $modal).serialize(),
  85. success: function(data){ onUserLoginRegisterSubmit($('#user-login'), data, destination); },
  86. error: function(jqXHR, textStatus, errorThrown) { trace('error : '+textStatus+' | '+errorThrown); }
  87. });
  88. return false;
  89. });
  90. // google analytics
  91. // $.event.trigger({
  92. // type : "record-stat",
  93. // categorie : 'Search',
  94. // action : keys,
  95. // label : 'filters : '+ stringTypes.join(' ,'),
  96. // value : json.count
  97. // });
  98. };
  99. function onUserLoginRegisterSubmit($form, data, destination){
  100. // console.log('data', data);
  101. cleanModalErrors();
  102. if(data.errors != null){
  103. for(field in data.errors){
  104. $('input[name='+field+']', $form).addClass('error');
  105. $form.prepend($('<div>').addClass('message-error').html(data.errors[field]));
  106. }
  107. (function($form){
  108. setTimeout(function(){
  109. console.log('enabled');
  110. $('input[type=submit]', $form).removeAttr('disabled');
  111. }, 0.5*1000);
  112. })($form);
  113. }else{
  114. // trace('destination = '+destination);
  115. window.location.pathname = destination;
  116. }
  117. };
  118. function cleanModalErrors() {
  119. $('.message-error', '.modal-content').remove();
  120. $('input', '.modal-content').removeClass('error');
  121. }
  122. init();
  123. };
  124. $(document).ready(function() {
  125. var materiouser = new MaterioUser();
  126. });
  127. })(jQuery);