Browse Source

Login is now saved in the state

Nathaniel van Diepen 10 years ago
parent
commit
b7867d538e
4 changed files with 33 additions and 2 deletions
  1. 0 0
      README.md
  2. 2 0
      data/login.template.html
  3. 4 0
      index.php
  4. 27 2
      js/index.js

+ 0 - 0
README.md


+ 2 - 0
data/login.template.html

@@ -22,8 +22,10 @@
 		data.id = 'login';
 		apiCall(data,function(d){
 			if(!d.error){
+				setKey(d.key);
 				loadState('page-index');
 			}else{
+				setKey(null);
 				loadState('page-login');
 			}
 		});

+ 4 - 0
index.php

@@ -24,6 +24,10 @@
 			$json = Array();
 			$json['state'] = Array();
 			$json['state']['data'] = $_GET;
+			if(isset($_GET['key'])){
+				$json['key'] = $_GET['key'];
+				$json['state']['data']['key'] = $_GET['key'];
+			}
 			switch($type){
 				case 'user':$url='~'.$id;break;
 				case 'group':$url='+'.$id;break;

+ 27 - 2
js/index.js

@@ -1,10 +1,17 @@
 // TODO - Add initial page loading and handlers
 (function($,History){
 	var State = History.getState(),
+		Key = null,
 		flag = false,
 		exists = function(v){
 			return typeof v != 'undefined';
 		},
+		setKey = window.setKey = function(key){
+			Key = key;
+		},
+		getKey = window.getKey = function(){
+			return Key;
+		},
 		api = window.apiCall = function(data,callback){
 			data.get = 'api';
 			data.timestamp = +new Date;
@@ -25,7 +32,11 @@
 			},'json');
 		},
 		loadState = window.loadState = function(href,callback){
-			$.get(href,{get:'state',timestamp:+new Date},function(d){
+			var data = {get:'state',timestamp:+new Date};
+			if(Key !== null){
+				data.key = Key;
+			}
+			$.get(href,data,function(d){
 				History.pushState(d.state.data,document.title,href);
 				State = History.getState();
 				if(exists(callback)){
@@ -34,7 +45,11 @@
 			},'json');
 		},
 		apiState = window.apiState = function(href,callback){
-			$.get(href,{get:'state',timestamp:+new Date},function(d){
+			var data = {get:'state',timestamp:+new Date};
+			if(Key !== null){
+				data.key = Key;
+			}
+			$.get(href,data,function(d){
 				History.replaceState(d.state.data,document.title,href);
 				State = History.getState();
 				if(exists(callback)){
@@ -46,6 +61,16 @@
 		$(window).on('statechange',function(){
 			var Old = State;
 			State = History.getState();
+			if(Key !== null){
+				State.key = Key;
+				State.data.key = Key;
+			}else{
+				if(exists(State.data['key'])){
+					Key = State.data.key;
+				}else if(exists(State['key'])){
+					Key = State.key;
+				}
+			}
 			if(State.data.type != Old.data.type || State.data.id != Old.data.id){
 				console.log("State change. "+JSON.stringify(State));
 				switch(State.data.type){