summaryrefslogtreecommitdiff
path: root/static/js/home.js
blob: 441a9925a6674cddc07b45180cd5c5d3615042d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
function ifEnter(fn) {
    return function(e) {
        if (e.keyCode == 13) { fn(); }
    };
}

function initLoginForm() {

	var nick = "#nickInput", nickFiller = "username"
	var pass = "#passwordInput", passLabel = "#passwordInputLabel", passFiller = "password"
	var submit = "#signin-submit"
	
	// username input logic:
	// set filler text for the login form only if empty
	if ($(nick).val() == "") $(nick).val(nickFiller);
	
	// erase the form text when clicked only if it is filler text, otherwise select it
  $(nick).focus(function(){
    if($(nick).val() == nickFiller) $(nick).val("")
    else $(nick).select()
  })
  
  // restore filler text on blur if field is empty
  $(nick).blur(function(){
    if($(nick).val() == "") $(nick).val(nickFiller);
  })
  
  // password input logic:
  
  var showPassHideLabel = function(){
    $(passLabel).addClass("invisible")
    $(pass).removeClass("invisible")
    $(pass).focus()
  }

  var hidePassShowLabel = function(){
    $(passLabel).val(passFiller)
    $(pass).addClass("invisible")
    $(passLabel).removeClass("invisible")
  }
	
	// only show password label if input is empty
  if ($(pass).val() == "") hidePassShowLabel()

  $(passLabel).click(showPassHideLabel)
  $(passLabel).focus(showPassHideLabel)
  
  $(pass).blur(function(){
    if($(pass).val() == "") hidePassShowLabel()
  })
	
	$(nick).keypress(ifEnter(login));
  $(pass).keypress(ifEnter(login));
  $(submit).click(login);
    
  initBigHand(submit)
}

function initExpandableLoginForm() {
  
  initLoginForm()
  
  $(".signin").click( function(e){          
    e.preventDefault();
    $("fieldset#signin_menu").toggle();
    $(".signin").toggleClass("menu-open");
    $("#nickInput").select()
  });
  
  $("fieldset#signin_menu").mouseup( function(){
    return false
  });
  
  $(document).mouseup(function(e) {
    if ($(e.target).parent("a.signin").length == 0) {
      $(".signin").removeClass("menu-open");
      $("fieldset#signin_menu").hide();
    }
  });			
}

function initBigHand(id){
  var cursor = "#cursor-big"
  
  if (!$(cursor).length) // add cursor image to page if it doesn't exist already...    
    $('<img src="/static/img/cursors/osx.hand.gif" class="invisible no-cursor" id="cursor-big">').appendTo('body')

  $(id).addClass("no-cursor")

  // i have to do this weirdly bc putting the cursor image where the mouse cursor is causes problems with mouse events:
  // * it stops mousemove events on the image below the mouse cursor
  // * it fucks up mouseover/out and even mouseenter/leave events, as well as click
  
  // so i am doing this:
  // on mousing over the image:
  //    make cursor visible
  //    find image co-ords
  //    bind a global mousemove func
  //    bind cursor click event
  // mousemove func:
  //    move image to mouse co-ords
  //    if mouse co-ords are outside the image co-ords:
  //        make cursor invisible
  //        unbind mousemove func
  //        unbind cursor click event

  var mousemove = function(e){
    $(cursor).css({
      "top": e.pageY + "px",
      "left": e.pageX - 32 + "px" // 32: (4 pixels * 8 pixels per big pixel) to line up pointy finger with cursor
    })
    $(id).unbind('mouseover', imageMouseOver)
    if (e.pageY < initBigHand.coords.top || 
        e.pageY > initBigHand.coords.bottom ||
        e.pageX < initBigHand.coords.left || 
        e.pageX > initBigHand.coords.right) {
          $(cursor).addClass('invisible')
          $(cursor).unbind('click', cursorClick)
          $('body').unbind('mousemove', mousemove)
          $(id).mouseover(imageMouseOver)
      }    
  }
  
  var cursorClick = function(){ $(id).click() }
  
  var imageMouseOver = function(){
    initBigHand.coords = {
      "left":   $(id).offset().left,
      "top":    $(id).offset().top,
      "right":  $(id).offset().left + $(id).width(),
      "bottom": $(id).offset().top + $(id).height()
    }
    $('body').mousemove(mousemove)
    $(cursor).click(cursorClick)
    $(cursor).removeClass('invisible')
  }
  
  $(id).mouseover(imageMouseOver)
  
}

function login() {
    //$('#passwordInput, #loginSubmit').blur();
    var nick = $('#nickInput').val();
    var password = $('#passwordInput').val();
    var hash = hex_sha1(nick + '$' + password + '$dumpfm');

    var onSuccess = function(json) {
        location.href = "/chat";
    };

    var onError = function(resp, textStatus, errorThrown) {
        alert("Error logging in!");
    };

    $.ajax({
        type: 'GET',
        timeout: 5000,
        url: '/login',
        data: {'nick': nick, ts: '', 'hash': hash },
        cache: false,
        dataType: 'json',
        success: onSuccess,
        error: onError
    });
};