Quellcode durchsuchen

Basic template caching.

Nathaniel van Diepen vor 10 Jahren
Ursprung
Commit
febecfe969
5 geänderte Dateien mit 116 neuen und 4 gelöschten Zeilen
  1. 6 2
      api.php
  2. 2 1
      index.php
  3. 25 0
      js/index.js
  4. 82 0
      js/jquery.storage.js
  5. 1 1
      js/modernizr.js

+ 6 - 2
api.php

@@ -8,7 +8,9 @@
 			$id = $_GET['id'];
 			switch($_GET['type']){
 				case 'user':
-					$ret['template'] = file_get_contents(PATH_DATA.'pages/user.template');
+					if(!isset($_GET['template'])){
+						$ret['template'] = file_get_contents(PATH_DATA.'pages/user.template');
+					}
 					$user = userObj($id);
 					$context = Array(
 						'name'=>$user['name'],
@@ -35,7 +37,9 @@
 				break;
 				case 'page':
 					if(file_exists(PATH_DATA.'pages/'.$id.'.template')){
-						$ret['template'] = file_get_contents(PATH_DATA.'pages/'.$id.'.template');
+						if(!isset($_GET['template'])){
+							$ret['template'] = file_get_contents(PATH_DATA.'pages/'.$id.'.template');
+						}
 						$context = Array();
 						if($LOGGEDIN){
 							$context['key'] = true;

+ 2 - 1
index.php

@@ -71,7 +71,7 @@
 						'history',
 						'rgba',
 						'applicationcache',
-						'mediaquery',
+						//'mediaquery',
 						'fontface'
 					],i,
 					msg = '';
@@ -90,6 +90,7 @@
 		<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
 		<script src="js/handlebars.js"></script>
 		<script src="js/jquery.history.js"></script>
+		<script src="js/jquery.storage.js"></script>
 		<script src="js/jquery.cookie.js"></script>
 		<script src="js/jquery.nicescroll.js"></script>
 		<script src="js/shortcut.js"></script>

+ 25 - 0
js/index.js

@@ -4,6 +4,7 @@
 		Old = {},
 		Key = null,
 		flags = [],
+		templates = [],
 		flag = window.flag = function(name,value){
 			if(exists(value)){
 				flags[name] = value;
@@ -40,11 +41,26 @@
 		getKey = window.getKey = function(){
 			return Key;
 		},
+		template = window.template = function(name,template){
+			if(exists(template)){
+				templates[name] = template;
+				$.localStorage('templates',templates);
+			}else if(exists(templates[name])){
+				console.log('Getting template for: '+name);
+				return templates[name];
+			}else{
+				console.log('No template stored for: '+name);
+				return '';
+			}
+		},
 		apiCall = window.apiCall = function(data,callback){
 			console.log('apiCall('+data.type+'-'+data.id+')');
 			$('#loading').show();
 			data.get = 'api';
 			data.timestamp = +new Date;
+			if(exists(templates[data.type+'-'+data.id])){
+				data.template = false;
+			}
 			$.get(location.href,data,function(d){
 				if(exists(d['error'])){
 					error(d);
@@ -165,6 +181,10 @@
 		setKey(null);
 	}
 	$(document).ready(function(){
+		templates = $.localStorage('templates');
+		if(templates === null){
+			templates = [];
+		}
 		if(!exists($.support.touch)){
 			$.support.touch = 'ontouchstart' in window || 'onmsgesturechange' in window;
 		}
@@ -180,6 +200,11 @@
 									console.log('Context detected logout');
 									setKey(null);
 								}
+								if(exists(d.template)){
+									template(State.data.type+'-'+State.data.id,d.template);
+								}else{
+									d.template = template(State.data.type+'-'+State.data.id);
+								}
 								render.topbar(d.topbar.template,d.topbar.context);
 								render.content(d.template,d.context);
 								$('#loading').hide();

+ 82 - 0
js/jquery.storage.js

@@ -0,0 +1,82 @@
+/*!
+ * jquery.storage.js 0.0.3 - https://github.com/yckart/jquery.storage.js
+ * The client-side storage for every browser, on any device.
+ *
+ * Copyright (c) 2012 Yannick Albert (http://yckart.com)
+ * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
+ * 2013/02/10
+ **/
+;(function($, window, document) {
+    'use strict';
+
+    $.map(['localStorage', 'sessionStorage'], function( method ) {
+        var defaults = {
+            cookiePrefix : 'fallback:' + method + ':',
+            cookieOptions : {
+                path : '/',
+                domain : document.domain,
+                expires : ('localStorage' === method) ? { expires: 365 } : undefined
+            }
+        };
+
+        try {
+            $.support[method] = method in window && window[method] !== null;
+        } catch (e) {
+            $.support[method] = false;
+        }
+
+        $[method] = function(key, value) {
+            var options = $.extend({}, defaults, $[method].options);
+
+            this.getItem = function( key ) {
+                var returns = function(key){
+                    return JSON.parse($.support[method] ? window[method].getItem(key) : $.cookie(options.cookiePrefix + key));
+                };
+                if(typeof key === 'string') return returns(key);
+
+                var arr = [],
+                    i = key.length;
+                while(i--) arr[i] = returns(key[i]);
+                return arr;
+            };
+
+            this.setItem = function( key, value ) {
+                value = JSON.stringify(value);
+                return $.support[method] ? window[method].setItem(key, value) : $.cookie(options.cookiePrefix + key, value, options.cookieOptions);
+            };
+
+            this.removeItem = function( key ) {
+                return $.support[method] ? window[method].removeItem(key) : $.cookie(options.cookiePrefix + key, null, $.extend(options.cookieOptions, {
+                    expires: -1
+                }));
+            };
+
+            this.clear = function() {
+                if($.support[method]) {
+                    return window[method].clear();
+                } else {
+                    var reg = new RegExp('^' + options.cookiePrefix, ''),
+                        opts = $.extend(options.cookieOptions, {
+                            expires: -1
+                        });
+
+                    if(document.cookie && document.cookie !== ''){
+                        $.map(document.cookie.split(';'), function( cookie ){
+                            if(reg.test(cookie = $.trim(cookie))) {
+                                 $.cookie( cookie.substr(0,cookie.indexOf('=')), null, opts);
+                            }
+                        });
+                    }
+                }
+            };
+
+            if (typeof key !== "undefined") {
+                return typeof value !== "undefined" ? ( value === null ? this.removeItem(key) : this.setItem(key, value) ) : this.getItem(key);
+            }
+
+            return this;
+        };
+
+        $[method].options = defaults;
+    });
+}(jQuery, window, document));

Datei-Diff unterdrückt, da er zu groß ist
+ 1 - 1
js/modernizr.js


Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.