Explorar o código

Better caching.

First load time and any updates are slower, but overall the site will be
faster. Everything will be cached initially.
Nathaniel van Diepen %!s(int64=11) %!d(string=hai) anos
pai
achega
54025b961b
Modificáronse 5 ficheiros con 83 adicións e 38 borrados
  1. 4 1
      api.php
  2. 1 1
      bugs.appcache
  3. 2 4
      data/pages/index.template
  4. 1 1
      data/version
  5. 75 31
      js/index.js

+ 4 - 1
api.php

@@ -111,6 +111,7 @@
 					$title = $id;
 					if(file_exists(PATH_DATA.'pages/'.$id.'.template')){
 						$context = array();
+						$ret['template'] = $id;
 						if($LOGGEDIN){
 							$context['key'] = true;
 							$context['user'] = userObj($_SESSION['username']);
@@ -188,7 +189,8 @@
 				case 'template':
 					if(isset($_GET['name'])){
 						retj(array(
-							'template'=>file_get_conetents(PATH_DATA.'/'.$_GET['id'].'/'.$_GET['name'])
+							'template'=>file_get_contents(PATH_DATA.'/'.$_GET['id'].'/'.$_GET['name'].'.template'),
+							'name'=>$_GET['name']
 						));
 					}else{
 						retj(array(
@@ -334,6 +336,7 @@
 			));
 		}
 	}else{
+		$_GET['type'] = '';
 		retj(array(
 		'error'=>'Type missing.'
 	));

+ 1 - 1
bugs.appcache

@@ -111,4 +111,4 @@ NETWORK:
 *
 
 FALLBACK:
-#Version Sat Mar 29 19:55:22 MDT 2014
+#Version Tue Apr  1 23:13:27 MDT 2014

+ 2 - 4
data/pages/index.template

@@ -1,4 +1,4 @@
-<button id="install" class="recommend">
+<button id="install" style="display:none;" class="recommend">
   Install this App
 </button>
 <script>
@@ -21,12 +21,10 @@
 							error(request.error.name);
 						};
 						return false;
-					});
+					}).show();
 				};
 			};
 		})(navigator.mozApps.checkInstalled(location.origin+"/bugs.webapp"));
-	}else{
-		$('button#install').hide();
 	}
 </script>
 <h3>

+ 1 - 1
data/version

@@ -1 +1 @@
-Sat Mar 29 19:55:22 MDT 2014
+Tue Apr  1 23:13:27 MDT 2014

+ 75 - 31
js/index.js

@@ -96,16 +96,13 @@
 				data.get = 'api';
 				data.back = State.data.back;
 				data.timestamp = +new Date;
-				if(''!=template(data.type+'-'+data.id)){
-					data.template = false;
-				}
 				$.get(location.href,data,function(d){
 					if(exists(d['error'])){
 						error(d);
 					}else{
 						if(exists(d.state)){
 							d.state.title = d.state.title.capitalize();
-							if(location.href.substr(location.href.lastIndexOf('/')+1) != d.state.url && d.state.url != ''){
+							if(location.href.substr(location.href.lastIndexOf('/')+1) != d.state.url && d.state.url !== ''){
 								console.log('Forced redirection to '+d.state.url);
 								History.replaceState(d.state.data,d.state.title,d.state.url);
 								getNewState();
@@ -115,7 +112,11 @@
 					}
 					if(exists(callback)){
 						console.log('Running apiCall callback');
-						callback(d);
+						try{
+							callback(d);
+						}catch(e){
+							error(e);
+						}
 					}
 				},'json');
 			}
@@ -241,13 +242,14 @@
 										console.log('Context detected console.logout');
 										setKey(null);
 									}
+									render.topbar(d.topbar.template,d.topbar.context);
 									if(exists(d.template)){
-										template(State.data.type+'-'+State.data.id,d.template);
+										console.log('Using template: '+d.template);
+										d.template = template(d.template);
+										render.content(d.template,d.context);
 									}else{
-										d.template = template(State.data.type+'-'+State.data.id);
+										console.log('No template used');
 									}
-									render.topbar(d.topbar.template,d.topbar.context);
-									render.content(d.template,d.context);
 									$(window).resize();
 									loading(false);
 								}else{
@@ -636,6 +638,46 @@
 					flag('firebug-lite',true);
 				}
 			}
+		},
+		getTemplates = window.getTemplates = function(callback){
+			$.get('api.php',{
+					type: 'manifest',
+					id: 'pages'
+				},function(d){
+				if(!exists(d.error)){
+					var count = d.manifest.length;
+					for(var i in d.manifest){
+						console.log('Loading template('+(Number(i)+1)+'/'+d.manifest.length+'): '+d.manifest[i]);
+						if(template(d.manifest[i]) === ''){
+							$.get('api.php',{
+								type: 'template',
+								id: 'pages',
+								name: d.manifest[i]
+							},function(d){
+								templates.push({
+									name: d.name,
+									template: d.template
+								});
+								$.localStorage('templates',templates);
+								count--;
+								console.log('Loaded template('+count+' left): '+d.name);
+							},'json');
+						}else{
+							count--;
+						}
+					}
+					setTimeout(function wait_for_templates(){
+						if(count === 0){
+							console.log('getTemplates callback');
+							callback();
+						}else{
+							setTimeout(wait_for_templates,10);
+						}
+					},10);
+				}else{
+					error(d.error);
+				}
+			},'json');
 		};
 	if(exists($.cookie('key'))){
 		setKey($.cookie('key'));
@@ -697,7 +739,30 @@
 						templates = [];
 					}
 				}
-				replaceState(location.href);
+				getTemplates(function(){
+					replaceState(location.href);
+					(function notifications(){
+						var context = State;
+						context.type = 'action';
+						context.id = 'notifications';
+						context.url = State.url;
+						context.title = State.title;
+						context.topbar = false;
+						context.no_state = true;
+						apiCall(context,function(d){
+							if(!exists(d.error)){
+								if(d.count>0 && $.localStorage('last_pm_check') < d.timestamp){
+									notify('Alert','You have '+d.count+' new message'+(d.count>1?'s':''),function(){
+										loadState('page-messages');
+									});
+								}
+								$('.topbar-notifications').css('display',d.count>0?'block':'').text('('+d.count+')');
+								$.localStorage('last_pm_check',d.timestamp);
+							}
+							setTimeout(notifications,5*1000); // every 5 seconds
+						},true);
+					})();
+				});
 			}else{
 				error(d.error);
 			}
@@ -717,27 +782,6 @@
 			}
 		};
 		$('#notification-container').notify();
-		(function notifications(){
-			var context = State;
-			context.type = 'action';
-			context.id = 'notifications';
-			context.url = State.url;
-			context.title = State.title;
-			context.topbar = false;
-			context.no_state = true;
-			apiCall(context,function(d){
-				if(!exists(d.error)){
-					if(d.count>0 && $.localStorage('last_pm_check') < d.timestamp){
-						notify('Alert','You have '+d.count+' new message'+(d.count>1?'s':''),function(){
-							loadState('page-messages');
-						});
-					}
-					$('.topbar-notifications').css('display',d.count>0?'block':'').text('('+d.count+')');
-					$.localStorage('last_pm_check',d.timestamp);
-				}
-				setTimeout(notifications,5*1000); // every 5 seconds
-			},true);
-		})();
 	});
 	shortcut.add('f12',function(){
 		debug.firebug();