Tuesday, February 3, 2015

Register/login



Jeff Atwood wrote a great post on creating the simplest login page. Read it!

Let`s use JQueryMobile for the widgests and Parse for the backed.
Insert this html to the JQueryMobile page:

<h3>Login/Register</h3>
<label for="usrnm" class="ui-hidden-accessible">Email:</label>
<input type="text" name="user" id="loginEmail" placeholder="Email">
<label for="pswd" class="ui-hidden-accessible">Password:</label>
<input type="password" data-inline="true" name="passw" id="loginPassword" placeholder="Password">
<input type="submit" data-inline="true" onclick="myForgotPassword();" value="reset pass"> 
<label id="loginMessage" style="color:red"></label>
<input type="submit" data-inline="true" onclick="myLogin();"       value="Log in">
<input type="submit" data-inline="true" onclick="myRegister();"    value="Create new account">


Use JQuery to extract user values and fill login message ($("#input_id".val() , or $(#message_id").text("new text")
We will use Parse user.signUp , Parse.User.logIn and Parse.User.requestUserPassword methods for the backend.



function myRegister() {
    console.log("myRegister");
    $('#loginMessage').text(""); //clean
    var email = $('#loginEmail').val();
    var password= $('#loginPassword').val();
    
    var user= new Parse.User();
    user.set("username",email);
    user.set("email" , email);
    user.set("password", password);    

    user.signUp(null, {
       success: function(user)  {
           console.log("register success");
           $('#loginMessage').text("success");
       },
       error: function(user,error) {
           console.log("register failed "+ error.code +" " +error.message );
           $('#loginMessage').text("failed "+error.message);
       }
       
    });
}

function myLogin() {
    console.log("myLogin");
    $('#loginMessage').text(""); //clean
    var email = $('#loginEmail').val();
    var password= $('#loginPassword').val();
    Parse.User.logIn(email, password, {
       success: function(user)  {
           $('#loginMessage').text("login success");
       },
       error: function(user,error) {
           $('#loginMessage').text("login failed "+error.message);
       }
    });
    
}

function myForgotPassword() {
    console.log("myLogin");
    var email = $('#loginEmail').val();
    
    Parse.User.requestPasswordReset(email , {
       success: function() {
           $('#loginMessage').text("password reset sent");
       },
       error: function(error) {
           $('#loginMessage').text("failed to reset password "+ error.message);
       }
    });
}

Tinder-clone in javascript

Tinder-like moving images:

Have a look at the end result here.
And now let`s go step by step:

[Step 1] http://jsfiddle.net/3y7fogdv/1/embedded/result/
  • Check if it actually doing anything: Use requeststAnimationFrame (+big function to make it better)
  • The actual moving of the element is done in js using: e.style.transform = 'translate3d('+newX+'px,'+ newY+'px,5px)';
  • To return it to first position, add style{transition:all 0.3s;}
  • To make sure the image is not dragged by itself:   user-drag: none;  -moz-user-select: none;  -webkit-user-drag: none;
[Step 2] http://jsfiddle.net/3y7fogdv/7/embedded/result/
Lets use a card-style (see here) and move the entire card.

[Step 3] http://jsfiddle.net/3y7fogdv/18/embedded/result/
Add tinder buttons  (V,X) below, add z-index to the .card ( z-index:1;    position: relative; ) so that it will move above them.
Also note that when we move the card down, it causes scrolling (solve by adding overflow:hidden to the main content) , and that the end of the content ends with the end of the buttons instead to extend to the full view. This is solved by a small js, to change main content size according to full size- header height.
Last change is addition of rotation to the transform  rotate( startX-newX)*0.05 + deg

[Step 4] http://jsfiddle.net/3y7fogdv/19/embedded/result/
check the release location on hammer "panend" event, and a check of ev.isFinal and ev.deltaX ev.deltaY.  If it is above a threshold, move it far away.
Also , let`s use multiple cards, so we will use multiple Hammer instances, one for each card. Luckily hammer supports it and hammer events are separated per card.


[Final notes]
On mobile, panning may cause scrolling the window down. To avoid this, add:
        $(document).bind('touchmove', function(e) {
            e.preventDefault();
        });