repo init
This commit is contained in:
4
script/jquery.js
vendored
Normal file
4
script/jquery.js
vendored
Normal file
File diff suppressed because one or more lines are too long
0
script/jquery.mods.js
Normal file
0
script/jquery.mods.js
Normal file
185
script/spot.js
Normal file
185
script/spot.js
Normal file
@@ -0,0 +1,185 @@
|
||||
function Spot(asGlobals)
|
||||
{
|
||||
self = this;
|
||||
this.consts = asGlobals.consts;
|
||||
this.consts.hash_sep = '-';
|
||||
this.consts.title = 'Find Me Spot';
|
||||
this.consts.default_page = 'messages';
|
||||
|
||||
/* Initialization */
|
||||
|
||||
this.init = function()
|
||||
{
|
||||
//Variables & constants from php
|
||||
self.vars('tmp', 'object');
|
||||
self.vars('page', 'string');
|
||||
self.updateVars(asGlobals.vars);
|
||||
|
||||
//page elem
|
||||
self.elem = {};
|
||||
self.elem.container = $('#container');
|
||||
|
||||
//on window resize
|
||||
//$(window).resize(self.onResize).resize();
|
||||
|
||||
//Setup menu
|
||||
//self.initMenu();
|
||||
|
||||
//Hash management
|
||||
self.resetTmpFunctions();
|
||||
$(window)
|
||||
.bind('hashchange', self.onHashChange)
|
||||
.trigger('hashchange');
|
||||
};
|
||||
|
||||
this.updateVars = function(asVars)
|
||||
{
|
||||
$.each(asVars, function(sKey, oValue){self.vars(sKey, oValue)});
|
||||
};
|
||||
|
||||
/* Variable Management */
|
||||
|
||||
this.vars = function(oVarName, oValue)
|
||||
{
|
||||
var asVarName = (typeof oVarName == 'object')?oVarName:[oVarName];
|
||||
|
||||
//Set, name & type / default value (init)
|
||||
if(typeof oValue !== 'undefined') setElem(self.vars, copyArray(asVarName), oValue);
|
||||
|
||||
//Get, only name parameter
|
||||
return getElem(self.vars, asVarName);
|
||||
};
|
||||
|
||||
this.tmp = function(sVarName, oValue)
|
||||
{
|
||||
var asVarName = (typeof sVarName == 'object')?sVarName:[sVarName];
|
||||
asVarName.unshift('tmp');
|
||||
return self.vars(asVarName, oValue);
|
||||
};
|
||||
|
||||
/* Page Switch - Trigger & Event catching */
|
||||
|
||||
this.onHashChange = function()
|
||||
{
|
||||
var asHash = self.getHash();
|
||||
if(asHash.hash !='' && asHash.page != '') self.switchPage(asHash); //page switching
|
||||
else if(self.vars('page')=='') self.setHash(self.consts.default_page); //first page
|
||||
};
|
||||
|
||||
this.getHash = function()
|
||||
{
|
||||
var sHash = self.hash();
|
||||
var asHash = sHash.split(self.consts.hash_sep);
|
||||
var sPage = asHash.shift() || '';
|
||||
return {hash:sHash, page:sPage, items:asHash};
|
||||
};
|
||||
|
||||
this.setHash = function(sPage, asItems, bReboot)
|
||||
{
|
||||
bReboot = bReboot || false;
|
||||
sPage = sPage || '';
|
||||
asItems = asItems || [];
|
||||
if(typeof asItems == 'string') asItems = [asItems];
|
||||
if(sPage != '')
|
||||
{
|
||||
var sItems = (asItems.length > 0)?self.consts.hash_sep+asItems.join(self.consts.hash_sep):'';
|
||||
self.hash(sPage+sItems, bReboot);
|
||||
}
|
||||
};
|
||||
|
||||
this.hash = function(hash, bReboot)
|
||||
{
|
||||
bReboot = bReboot || false;
|
||||
if(!hash) return window.location.hash.slice(1);
|
||||
else window.location.hash = '#'+hash;
|
||||
|
||||
if(bReboot) location.reload();
|
||||
};
|
||||
|
||||
/* Page Switch - DOM Replacement */
|
||||
|
||||
this.resetTmpFunctions = function()
|
||||
{
|
||||
self.pageInit = function(asHash){console.log('no init for the page: '+asHash.page)};
|
||||
self.onSamePageMove = function(asHash){return false};
|
||||
self.onQuitPage = function(){return true};
|
||||
self.onFeedback = function(sType, sMsg){feedback(sType, sMsg);};
|
||||
};
|
||||
|
||||
this.switchPage = function(asHash)
|
||||
{
|
||||
var sPageName = asHash.page;
|
||||
var bSamePage = self.vars('page')==sPageName;
|
||||
if(self.onQuitPage(bSamePage) && !bSamePage || self.onSamePageMove(asHash))
|
||||
{
|
||||
//Delete tmp variables
|
||||
self.vars('tmp', {});
|
||||
|
||||
//disable tmp functions
|
||||
self.resetTmpFunctions();
|
||||
|
||||
//Officially a new page
|
||||
var bFirstPage = self.vars('page')=='';
|
||||
self.vars('page', sPageName);
|
||||
|
||||
//Update Page Title
|
||||
var sDetail = asHash.items[0] || '';
|
||||
document.title = self.consts.title+' - '+sPageName+' '+sDetail;
|
||||
|
||||
//Replacing DOM
|
||||
var $Dom = $(self.consts.pages[sPageName]);
|
||||
if(bFirstPage)
|
||||
{
|
||||
self.elem.container.html($(self.consts.pages['template']));
|
||||
self.elem.main = self.elem.container.find('#main');
|
||||
self.splash($Dom, asHash, bFirstPage); //first page
|
||||
}
|
||||
else
|
||||
{
|
||||
self.elem.main.stop().fadeTo('fast', 0, function(){self.splash($Dom, asHash, bFirstPage);}); //Switching page
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.splash = function($Dom, asHash, bFirstPage)
|
||||
{
|
||||
//Switch main content
|
||||
self.elem.main.empty().html($Dom);
|
||||
|
||||
//Page Bootstrap
|
||||
self.pageInit(asHash, bFirstPage);
|
||||
|
||||
//Show main
|
||||
var $FadeInElem = bFirstPage?self.elem.container:self.elem.main;
|
||||
$FadeInElem.hide().fadeTo('slow', 1);
|
||||
};
|
||||
}
|
||||
|
||||
/* Common Functions */
|
||||
|
||||
function copyArray(asArray)
|
||||
{
|
||||
return asArray.slice(0); //trick to copy array
|
||||
}
|
||||
|
||||
function getElem(aoAnchor, asPath)
|
||||
{
|
||||
return (typeof asPath == 'object' && asPath.length > 1)?getElem(aoAnchor[asPath.shift()], asPath):aoAnchor[(typeof asPath == 'object')?asPath.shift():asPath];
|
||||
}
|
||||
|
||||
function setElem(aoAnchor, asPath, oValue)
|
||||
{
|
||||
var asTypes = {boolean:false, string:'', integer:0, int:0, array:[], object:{}};
|
||||
if(typeof asPath == 'object' && asPath.length > 1)
|
||||
{
|
||||
var nextlevel = asPath.shift();
|
||||
if(!(nextlevel in aoAnchor)) aoAnchor[nextlevel] = {}; //Creating a new level
|
||||
if(typeof aoAnchor[nextlevel] !== 'object') debug('Error - setElem() : Already existing path at level "'+nextlevel+'". Cancelling setElem() action');
|
||||
return setElem(aoAnchor[nextlevel], asPath, oValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
var sKey = (typeof asPath == 'object')?asPath.shift():asPath;
|
||||
return aoAnchor[sKey] = (!(sKey in aoAnchor) && (oValue in asTypes))?asTypes[oValue]:oValue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user