Add javascript login popup
Add javascript login popup

--- a/include/controller/ControllerBase.class.php
+++ b/include/controller/ControllerBase.class.php
@@ -360,8 +360,10 @@
 		if (!$this->log)
 			return;
 
-		$this->projectList->RemoveObserver($this->log);
-		$this->exe->RemoveObserver($this->log);
+		if ($this->projectList)
+			$this->projectList->RemoveObserver($this->log);
+		if ($this->exe)
+			$this->exe->RemoveObserver($this->log);
 
 		$this->log->SetEnabled(false);
 

--- a/include/controller/Controller_Login.class.php
+++ b/include/controller/Controller_Login.class.php
@@ -74,13 +74,20 @@
 	 */
 	protected function LoadHeaders()
 	{
+		if (isset($this->params['output']) && ($this->params['output'] == 'js')) {
+			$this->headers[] = 'Content-Type: application/json';
+			$this->DisableLogging();
+		}
+
 		if (!empty($_SESSION['gitphpuser'])) {
 			$user = $this->userList->GetUser($_SESSION['gitphpuser']);
 			if ($user) {
-				if (!empty($this->params['redirect']))
-					$this->headers[] = 'Location: ' . $this->params['redirect'];
-				else
-					$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
+				if (!(isset($this->params['output']) && ($this->params['output'] == 'js'))) {
+					if (!empty($this->params['redirect']))
+						$this->headers[] = 'Location: ' . $this->params['redirect'];
+					else
+						$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
+				}
 				$this->loginSuccess = true;
 			} else {
 				unset($_SESSION['gitphpuser']);
@@ -91,10 +98,12 @@
 			$user = $this->userList->GetUser($this->params['username']);
 			if ($user && ($this->params['password'] === $user->GetPassword())) {
 				$_SESSION['gitphpuser'] = $user->GetUsername();
-				if (!empty($this->params['redirect']))
-					$this->headers[] = 'Location: ' . $this->params['redirect'];
-				else
-					$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
+				if (!(isset($this->params['output']) && ($this->params['output'] == 'js'))) {
+					if (!empty($this->params['redirect']))
+						$this->headers[] = 'Location: ' . $this->params['redirect'];
+					else
+						$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
+				}
 				$this->loginSuccess = true;
 			} else {
 				$this->loginSuccess = false;
@@ -107,13 +116,15 @@
 	 */
 	protected function LoadData()
 	{
-		if ($this->loginSuccess === false) {
-			$this->tpl->assign('loginerror', 'Invalid username or password');
-		}
-		if (!empty($this->params['redirect'])) {
-			$this->tpl->assign('redirect', $this->params['redirect']);
-		} else if (!empty($_SERVER['HTTP_REFERER'])) {
-			$this->tpl->assign('redirect', $_SERVER['HTTP_REFERER']);
+		if (!(isset($this->params['output']) && ($this->params['output'] == 'js'))) {
+			if ($this->loginSuccess === false) {
+				$this->tpl->assign('loginerror', 'Invalid username or password');
+			}
+			if (!empty($this->params['redirect'])) {
+				$this->tpl->assign('redirect', $this->params['redirect']);
+			} else if (!empty($_SERVER['HTTP_REFERER'])) {
+				$this->tpl->assign('redirect', $_SERVER['HTTP_REFERER']);
+			}
 		}
 	}
 
@@ -122,6 +133,19 @@
 	 */
 	public function Render()
 	{
+		if (isset($this->params['output']) && ($this->params['output'] == 'js')) {
+			$result = array();
+			if ($this->loginSuccess === true)
+				$result['success'] = true;
+			else {
+				$result['success'] = false;
+				if ($this->loginSuccess === false)
+					$result['message'] = 'Invalid username or password';
+			}
+			echo json_encode($result);
+			return;
+		}
+
 		if ($this->loginSuccess === true)
 			return;				// logged in and redirected, don't render
 

--- a/include/router/Router.class.php
+++ b/include/router/Router.class.php
@@ -243,7 +243,8 @@
 			'mark' => 'm',
 			'prefix' => 'prefix',
 			'sort' => 'sort',
-			'lang' => 'l'
+			'lang' => 'l',
+			'redirect' => 'redirect'
 		);
 	}
 
@@ -356,6 +357,9 @@
 	public function GetController()
 	{
 		$params = $this->QueryVarArrayToParameterArray($_GET);
+		if (!empty($_POST)) {
+			$params = array_merge($params, $this->QueryVarArrayToParameterArray($_POST));
+		}
 
 		if (!empty($_GET['q'])) {
 			$restparams = GitPHP_Router::ReadCleanUrl($_SERVER['REQUEST_URI']);
@@ -486,8 +490,6 @@
 					$controller->SetParam('username', $_POST['username']);
 				if (!empty($_POST['password']))
 					$controller->SetParam('password', $_POST['password']);
-				if (!empty($_POST['redirect']))
-					$controller->SetParam('redirect', $_POST['redirect']);
 				break;
 
 			case 'logout':

--- a/js/common.js
+++ b/js/common.js
@@ -9,8 +9,8 @@
  * @subpackage Javascript
  */
 
-define(["jquery", "modules/getproject", "modules/lang", "modules/tooltip.snapshot", "modules/tooltip.commit", "modules/tooltip.tag", 'modernizr'],
-	function($, project, lang, tooltipSnapshot, tooltipCommit, tooltipTag) {
+define(["jquery", "modules/getproject", "modules/lang", "modules/tooltip.snapshot", "modules/tooltip.commit", "modules/tooltip.tag", 'modules/loginpopup', 'modernizr'],
+	function($, project, lang, tooltipSnapshot, tooltipCommit, tooltipTag, loginpopup) {
 		$(function() {
 			lang($('div.lang_select'));
 			tooltipSnapshot($('a.snapshotTip'));
@@ -21,6 +21,7 @@
       if (!Modernizr.input.autofocus) {
         $('input[autofocus]').filter(':first').focus();
       }
+      loginpopup('a.loginLink');
 		});
 	}
 );

--- /dev/null
+++ b/js/modules/loginpopup.js
@@ -1,1 +1,126 @@
+/*
+ * GitPHP javascript login popup
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Javascript
+ */
 
+define(['jquery', 'modules/geturl', 'qtip'], function($, url) {
+  return function(element) {
+    $(element).qtip({
+      content: {
+        text: function(api) {
+          var container = $(document.createElement('div'));
+          var loginError = $(document.createElement('div')).addClass('loginError').addClass('error').css('padding-top', '0px');
+          var loginDiv = $(document.createElement('div')).addClass('loginForm');
+          var loginForm = $(document.createElement('form'))
+
+          container.append(loginDiv);
+          loginDiv.append(loginForm);
+          loginForm.append(loginError);
+
+          var usernameDiv = $(document.createElement('div')).addClass('field');
+          var usernameLabel = $(document.createElement('label')).attr('for', 'username').text('username:');
+          var usernameField = $(document.createElement('input')).attr('type', 'text').attr('name', 'username');
+          usernameDiv.append(usernameLabel);
+          usernameDiv.append(usernameField);
+          loginForm.append(usernameDiv);
+
+          var passwordDiv = $(document.createElement('div')).addClass('field');
+          var passwordLabel = $(document.createElement('label')).attr('for', 'password').text('password:');
+          var passwordField = $(document.createElement('input')).attr('type', 'password').attr('name', 'password');
+          passwordDiv.append(passwordLabel);
+          passwordDiv.append(passwordField);
+          loginForm.append(passwordDiv);
+
+          var loginButton = $(document.createElement('input')).attr('type', 'submit').attr('value', 'login');
+          loginForm.append(loginButton);
+
+          loginForm.bind('submit', function(event) {
+            var username = $('input[name=username]', this).val();
+            var password = $('input[name=password]', this).val();
+            var errorContainer = $('.loginError', this);
+            if (!username) {
+              errorContainer.text('Username is required');
+              return false;
+            }
+            if (!password) {
+              errorContainer.text('Password is required');
+              return false;
+            }
+            var inputs = $('input', this);
+            $.ajax({
+              url: url + '?a=login&o=js',
+              data: {
+                a: 'login',
+                o: 'js',
+                username: username,
+                password: password
+              },
+              type: 'post',
+              dataType: 'json',
+              success: function(data, status, jqXHR) {
+                if (data) {
+                  if (data.success === true) {
+                    window.location.reload();
+                  } else if (data.message) {
+                    errorContainer.text(data.message);
+                  } else {
+                    errorContainer.text('Error while logging in');
+                  }
+                } else {
+                  errorContainer.text('Error while logging in');
+                }
+              },
+              error: function(jqXHR, message) {
+                  errorContainer.text('Error while logging in');
+              },
+              beforeSend: function() {
+                inputs.attr('disabled', 'disabled');
+              },
+              complete: function() {
+                inputs.removeAttr('disabled');
+              }
+            });
+
+            return false;
+          });
+
+          return container;
+        },
+        title: {
+          text: 'Login',
+          button: true
+        }
+      },
+      position: {
+        my: 'center',
+        at: 'center',
+        target: $(window)
+      },
+      show: {
+        event: 'click',
+        modal: {
+          on: true
+        }
+      },
+      hide: {
+        event: false
+      },
+      style: {
+        classes: 'ui-tooltip-light ui-tooltip-shadow'
+      },
+      events: {
+        visible: function(event, api) {
+          $('input[name=username]', this).focus();
+        }
+      }
+    });
+    $(element).click(function(event) {
+      return false;
+    });
+  };
+});
+

--- a/templates/main.tpl
+++ b/templates/main.tpl
@@ -94,7 +94,7 @@
       {else if $action == 'login'}
         {t}login{/t}
       {else}
-        <a href="{geturl action=login}" />{t}login{/t}</a>
+        <a href="{geturl action=login}" class="loginLink" />{t}login{/t}</a>
       {/if}
       </div>
       {/if}

comments