123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- // TODO - Add initial page loading and handlers
- (function($,History){
- var State = History.getState(),
- Old = {},
- Key = null,
- flags = [],
- templates = [],
- flag = window.flag = function(name,value){
- if(exists(value)){
- flags[name] = value;
- }else{
- return exists(flags[name])?flags[name]:false;
- }
- },
- settings = {},
- exists = function(v){
- return typeof v != 'undefined';
- },
- get = window.get = function(s){
- return settings[s];
- },
- set = window.set = function(s,v){
- settings[s] = v;
- return v;
- },
- setKey = window.setKey = function(key){
- if(key !== null){
- console.log('Key change to '+key);
- Key = key;
- var d = new Date();
- d.setTime(d.getTime()+get('expire'));
- $.cookie('key',key,{
- expires: d
- });
- }else{
- console.log('Key deleted');
- Key = null;
- $.removeCookie('key');
- }
- },
- getKey = window.getKey = function(){
- return Key;
- },
- template = window.template = function(name,template){
- var d = +new Date,
- id = (function(name){
- for(var i in templates){
- if(templates[i].name == name){
- return i;
- }
- }
- return false;
- })(name);
- if(exists(template)){
- if(template === null){
- if(id!==false){
- templates.splice(id,1);
- }
- $.localStorage('templates',templates);
- console.log('Dropping template for: '+name);
- return '';
- }else{
- var o = {
- name: name,
- template: template,
- date: get('expire')+d
- }
- if(id===false){
- console.log('Storing new template for: '+name);
- templates.push(o);
- }else{
- console.log('Replacing old template for: '+name);
- templates[id] = o;
- }
- $.localStorage('templates',templates);
- }
- }else if(id!==false){
- console.log('Using cached template for: '+name);
- var template = templates[id].template;
- if(templates[id].date < d){
- delete templates[name];
- $.localStorage('templates',templates);
- }
- return template;
- }else{
- console.log('No cached 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);
- }else{
- if(location.href.substr(location.href.lastIndexOf('/')+1) != d.state.url){
- console.log('Forced redirection to '+d.state.url);
- History.replaceState(d.state.data,d.state.title,d.state.url);
- }
- }
- if(exists(callback)){
- callback(d);
- }
- },'json');
- },
- loadState = window.loadState = function(href,callback){
- console.log('loadState('+href+')');
- $('#loading').show();
- var data = {
- get:'state',
- timestamp: +new Date
- };
- console.log(data,href);
- ajax = $.ajax(href,{
- data: data,
- async: true,
- type: 'GET',
- success: function(d){
- if(exists(d['error'])){
- error(d);
- }else{
- console.log('pushState: '+d.state.title+'['+href+']');
- History.pushState(d.state.data,d.state.title,href);
- getNewState();
- }
- if(exists(callback)){
- callback(d);
- }
- },
- error: function(x,t,e){
- error({
- error: '['+t+'] '+e
- });
- if(exists(callback)){
- callback({
- error: '['+t+'] '+e
- });
- }
- },
- dataType: 'json'
- });
- },
- apiState = window.apiState = function(href,callback){
- console.log('apiState('+href+')');
- $('#loading').show();
- var data = {
- get:'state',
- timestamp: +new Date
- };
- $.ajax(href,{
- data: data,
- async: true,
- type: 'GET',
- success: function(d){
- if(exists(d['error'])){
- error(d);
- }else{
- console.log('pushState: '+d.state.title+'['+href+']');
- History.replaceState(d.state.data,d.state.title,href);
- getNewState();
- }
- if(exists(callback)){
- callback(d);
- }
- },
- error: function(x,t,e){
- error({
- error: '['+t+'] '+e
- });
- if(exists(callback)){
- callback({
- error: '['+t+'] '+e
- });
- }
- },
- dataType: 'json'
- });
- },
- error = function(e){
- var msg = '['+State.url+']'+e.error;
- console.error(msg.trim()+"\n"+(exists(e.state)?JSON.stringify(e.state):''));
- alert(msg.trim());
- $('#loading').hide();
- },
- getNewState = function(){
- State = History.getState();
- console.log("State change. "+JSON.stringify(State.data));
- if (!window.location.origin) {
- window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
- }
- },
- equal = function(o1,o2){
- for(var i in o1){
- if(!exists(o2[i])||o2[i]!=o1[i]){
- return false;
- }
- }
- for(i in o2){
- if(!exists(o1[i])||o2[i]!=o1[i]){
- return false;
- }
- }
- return true;
- },
- render = window.render = {
- topbar: function(t,c){
- $('#topbar').html(Handlebars.compile(t)(c));
- render.links('#topbar');
- render.buttons('#topbar');
- render.menus('#topbar');
- $(window).resize();
- },
- content: function(t,c){
- $('#content').html(
- Handlebars.compile(t)(c)
- );
- render.links('#content');
- render.buttons('#content');
- render.accordions('#content');
- render.menus('#content');
- render.form('#content');
- $(window).resize();
- },
- accordions: function(selector){
- $(selector).find('.accordion').each(function(){
- var icons = {};
- if($(this).children('.icons').length == 1){
- icons = JSON.parse($(this).children('.icons').text());
- $(this).children('.icons').remove();
- }
- $(this).accordion({
- collapsible: true,
- icons: icons,
- active: false
- });
- });
- },
- buttons: function(selector){
- $(selector).find('.button').button();
- $(selector).find('input[type=submit]').button();
- $(selector).find('input[type=button]').button();
- $(selector).find('button').button();
- },
- menus: function(selector){
- $(selector).find('.menu').css({
- 'list-style':'none'
- }).menu({
- icons:{
- submenu: "ui-icon-circle-triangle-e"
- }
- }).removeClass('ui-corner-all').addClass('ui-corner-bottom');
- },
- form: function(selector){
- $(selector).find('#form').position({of:selector,my:'center',at:'center'});
- },
- links: function(selector){
- $(selector).find('a').each(function(){
- var href = this.href;
- if(href.indexOf('#')!=-1&&(href.indexOf(location.origin)!=-1||href.indexOf('#')==0)){
- href = href.substr(href.indexOf('#')+1);
- $(this).click(function(e){
- try{
- if(($(this).hasClass('topbar-home') || $(this).hasClass('topbar-back'))&&$(window).width()<767){
- $('#topbar').children('div.topbar-right,div.topbar-left').toggle();
- $(window).resize();
- }else if($(this).hasClass('topbar-history')){
- History.back();
- }else{
- loadState(href);
- }
- }catch(error){
- console.error(error);
- }
- e.preventDefault();
- return false;
- });
- }
- });
- }
- };
- if(exists($.cookie('key'))){
- setKey($.cookie('key'));
- }else{
- setKey(null);
- }
- $(document).ready(function(){
- templates = $.localStorage('templates');
- if(templates === null){
- templates = [];
- }
- if(!exists($.support.touch)){
- $.support.touch = 'ontouchstart' in window || 'onmsgesturechange' in window;
- }
- $(window).on('statechange',function(){
- getNewState();
- if(!equal(State.data,Old)){
- document.title = State.title;
- switch(State.data.type){
- case 'page':case 'user':case 'project':
- apiCall(State.data,function(d){
- if(exists(d.context)){
- if(!exists(d.context.key)&&Key!==null){
- 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);
- $(window).resize();
- $('#loading').hide();
- }else{
- console.error('No context given');
- History.back();
- }
- });
- break;
- case 'action':break;
- default:
- error({
- url: State.url,
- error: "Something went wrong!"
- });
- }
- Old = State.data;
- }else{
- console.log(State.data,Old);
- console.warn('Stopped double load of '+Old.type+'-'+Old.id);
- $('#loading').hide();
- }
- });
- $('#content').niceScroll({
- cursorwidth: 10,
- nativeparentscrolling: false,
- preservenativescrolling: false
- });
- document.addEventListener('touchmove',function(e){
- e.preventDefault();
- });
- $(window).resize(function(){
- if($(window).width()>767){
- $('#topbar div.topbar-right, #topbar div.topbar-left').css({
- 'display': ''
- });
- }
- $('#content').height($('body').height()-$('#topbar').height());
- $('#content').getNiceScroll().resize();
- render.form('#content');
- });
- var data = {
- get: 'settings',
- timestamp: +new Date
- };
- $.get(location.href,data,function(d){
- settings = d;
- apiState(location.href);
- },'json');
- });
- shortcut.add('f12',function(){
- if(!flag('firebug-lite')){
- $('head').append(
- $('<script>').attr({
- 'type': 'text/javascript',
- 'src': 'https://getfirebug.com/firebug-lite.js#startOpened',
- 'id': 'FirebugLite'
- })
- );
- $('<image>').attr('src','https://getfirebug.com/releases/lite/latest/skin/xp/sprite.png');
- flag('firebug-lite',true);
- }
- });
- shortcut.add('Ctrl+f12',function(){
- if(!flag('manifesto')){
- if(window.applicationCache){
- if(window.applicationCache.status==window.applicationCache.UNCACHED){
- $('head').append(
- $('<script>').attr({
- 'type': 'text/javascript',
- 'src': 'http://manifesto.ericdelabar.com/manifesto.js?x="+(Math.random())'
- })
- );
- (function wait(){
- if($('#cacheStatus').length == 0){
- setTimeout(wait,10);
- }else{
- $('#cacheStatus').niceScroll();
- }
- })();
- }else{
- alert("Manifest file is valid.");
- }
- }else{
- alert("This browser does not support HTML5 Offline Application Cache.");
- }
- flag('manifesto',true);
- }
- });
- $.fn.serializeObject = function(){
- var o = {},
- a = this.serializeArray();
- $.each(a,function(){
- if(o[this.name] !== undefined){
- if(!o[this.name].push){
- o[this.name] = [o[this.name]];
- }
- o[this.name].push(this.value || '');
- }else{
- o[this.name] = this.value || '';
- }
- });
- return o;
- };
- $.ajaxSetup({
- async: false,
- cache: false
- });
- })(jQuery,History);
|