Browse Source

State history updates. Load/API optimizations

Nathaniel van Diepen 11 years ago
parent
commit
87b92f6a98
4 changed files with 127 additions and 28 deletions
  1. 3 3
      .htaccess
  2. 12 1
      data/login.template.html
  3. 31 0
      index.php
  4. 81 24
      js/index.js

+ 3 - 3
.htaccess

@@ -9,9 +9,9 @@
 	RewriteRule ^\~([^/]*)$ /index.php?type=user&id=$1 [QSA,L]
 	RewriteRule ^\+([^/]*)$ /index.php?type=group&id=$1 [QSA,L]
 	RewriteRule ^\!([^/]*)$ /index.php?type=issue&id=$1 [QSA,L]
-	RewriteRule ^scrum-([^/]*)$ /index.php?type=scrum&id=$2 [QSA,L]
-	RewriteRule ^admin-([^/]*)$ /index.php?type=admin&id=$2 [QSA,L]
-	RewriteRule ^page-([^/]*)$ /index.php?type=template&id=$2 [QSA,L]
+	RewriteRule ^scrum-([^/]*)$ /index.php?type=scrum&id=$1 [QSA,L]
+	RewriteRule ^admin-([^/]*)$ /index.php?type=admin&id=$1 [QSA,L]
+	RewriteRule ^page-([^/]*)$ /index.php?type=template&id=$1 [QSA,L]
 </IfModule>
 # File security
 <FilesMatch ".(bak|config|sql|fla|psd|ini|log|sh|inc|~|swp)$">

+ 12 - 1
data/login.template.html

@@ -8,4 +8,15 @@
 	<div>
 		Password: <input name="password" type="password"/>
 	</div>
-</form>
+	<input type="submit" value="login"/>
+	<input type="button" value="cancel" class="cancel"/>
+</form>
+<script>
+	$('#login').submit(function(){
+		// TODO - Handle login
+		return false;
+	}).children('.cancel').click(function(){
+		loadState('page-index');
+		return false;
+	});
+</script>

+ 31 - 0
index.php

@@ -1,3 +1,33 @@
+<?php
+	if(isset($_GET['get'])){
+		$get = $_GET['get'];
+		unset($_GET['get']);
+		if(!isset($_GET['type']) || !isset($_GET['id'])){
+			$type = 'template';
+			$id = 'index';
+		}else{
+			$type = $_GET['type'];
+			$id = $_GET['id'];
+		}
+		if($get == 'state'){
+			$json = Array();
+			$json['state'] = Array();
+			$json['state']['data'] = $_GET;
+			switch($type){
+				case 'user':$url='~'.$id;break;
+				case 'group':$url='+'.$id;break;
+				case 'issue':$url='!'.$id;break;
+				case 'template':$url='page-'.$id;break;
+				default:$url=$type.'-'.$id;
+			}
+			$json['state']['url'] = $url;
+			die(json_encode($json));
+			
+		}elseif($get == 'api'){
+			require_once('api.php');
+		}
+	}
+?>
 <!doctype html>
 <html>
 	<head>
@@ -15,6 +45,7 @@
 		<link href="css/style.css" rel="stylesheet" type="text/css"/>
 	</head>
 	<body>
+		<div id="topbar"></div>
 		<div id="content"></div>
 	</body>
 </html>

+ 81 - 24
js/index.js

@@ -1,41 +1,98 @@
 // TODO - Add initial page loading and handlers
 (function($,History){
+	History._replaceState = History.replaceState;
+	History.replaceState = function(){
+		console.log('replaceState'+JSON.stringify(arguments));
+		History._replaceState.apply(this,arguments);
+	};
+	History._pushState = History.pushState;
+	History.pushState = function(){
+		console.log('pushState'+JSON.stringify(arguments));
+		History._pushState.apply(this,arguments);
+	};
 	var State = History.getState(),
+		flag = false,
+		exists = function(v){
+			return typeof v != 'undefined';
+		},
 		api = function(data,callback){
-			$.get('api.php',data,function(d){
-				if(location.href.substr(location.href.lastInstanceOf('/')) != d.state.url){
+			data.get = 'api';
+			$.get(location.href,data,function(d){
+				if(location.href.substr(location.href.lastIndexOf('/')+1) != d.state.url){
 					History.pushState(d.state.data,d.state.title,d.state.url);
 				}
-				callback(d);
+				if(exists(callback)){
+					callback(d);
+				}
+			},'json');
+		},
+		loadState = window.loadState = function(href,callback){
+			$.get(href,{get:'state'},function(d){
+				History.pushState(d.state.data,document.title,href);
+				State = History.getState();
+				if(exists(callback)){
+					callback();
+				}
+			},'json');
+		},
+		apiState = window.apiState = function(href,callback){
+			$.get(href,{get:'state'},function(d){
+				History.replaceState(d.state.data,document.title,href);
+				State = History.getState();
+				if(exists(callback)){
+					callback();
+				}
 			},'json');
 		};
 	$(document).ready(function(){
+		$(window).on('statechange',function(){
+			var Old = State;
+			State = History.getState();
+			if(State.data.type != Old.data.type || State.data.id != Old.data.id){
+				console.log("State change. "+JSON.stringify(State));
+				switch(State.data.type){
+					case 'template':
+						api(State.data,function(d){
+							$('#content').html(Handlebars.compile(d.template)(d.context)).mCustomScrollbar('destroy');
+							$('#content,.scroll').mCustomScrollbar({
+								theme: 'dark-2',
+								scrollInertia: 0
+							});
+							$('#content').find('a').each(function(){
+								var href = this.href;
+								if(href.indexOf(location.origin) != -1 && href.indexOf('#') != -1){
+									href = href.substr(href.indexOf('#')+1);
+									console.log('Setting up link to '+href);
+									$(this).click(function(){
+										loadState(href);
+										return false;
+									});
+								}
+							});
+						});
+					break;
+					default:
+						alert("Something went wrong!\nYour current state:\n"+JSON.stringify(State));
+				}
+			}
+		});
 		if($.isEmptyObject(State.data)){
-			History.pushState({
+			History.replaceState({
 				type: 'template',
 				id: 'index'
 			},'Bugs','page-index');
+			console.log('Forcing default state.');
+		}else{
+			flag = true;
 		}
-		$(window).on('statechange',function(){
-			State = History.getState();
-			switch(State.data.type){
-				case 'template':
-					$.get('api.php',State.data,function(d){
-						$('#content').html(Handlebars.compile(d.template)(d.context)).mCustomScrollbar('destroy');
-						$('#content,.scroll').mCustomScrollbar({
-							theme: 'dark-2',
-							scrollInertia: 0
-						});
-						$('#content').find('a').each(function(){
-							if(this.href[0] == '#'){
-								
-							}
-						});
-					},'json');
-				break;
-				default:
-					alert("Something went wrong!\nYour current state:\n"+JSON.stringify(State));
+		apiState(location.href,function(){
+			if(flag){
+				State.data = {
+					type: '',
+					data: ''
+				};
 			}
-		}).trigger('statechange');
+			$(window).trigger('statechange');
+		});
 	});
 })(jQuery,History);