@ -5,7 +5,8 @@
var ready _callback ,
config = { } ,
templates ,
fs = null ;
fs = null ,
available _templates = [ ] ;
module . exports = templates = { } ;
@ -29,6 +30,10 @@
return false ;
}
templates . is _available = function ( tpl ) {
return ! ! jQuery . inArray ( tpl , available _templates ) ;
} ;
templates . ready = function ( callback ) {
if ( callback == null && ready _callback ) ready _callback ( ) ;
else ready _callback = callback ;
@ -70,37 +75,11 @@
}
function loadClient ( ) {
var timestamp = new Date ( ) . getTime ( ) ;
var loaded = templatesToLoad . length ;
jQuery . getJSON ( '/templates/config.json' , function ( data ) {
config = data ;
jQuery . when ( jQuery . getJSON ( '/templates/config.json' ) , jQuery . getJSON ( '/api/get_templates_listing' ) ) . done ( function ( config _data , templates _data ) {
config = config _data [ 0 ] ;
available _templates = templates _data [ 0 ] ;
templates . ready ( ) ;
} ) ;
for ( var t in templatesToLoad ) {
( function ( file ) {
jQuery . get ( '/templates/' + file + '.tpl?v=' + timestamp , function ( html ) {
var template = function ( ) {
this . toString = function ( ) {
return this . html ;
} ;
}
template . prototype . parse = parse ;
template . prototype . html = String ( html ) ;
template . prototype . blocks = { } ;
templates [ file ] = new template ;
loaded -- ;
if ( loaded == 0 ) templates . ready ( ) ;
} ) . fail ( function ( ) {
loaded -- ;
if ( loaded == 0 ) templates . ready ( ) ;
} ) ;
} ( templatesToLoad [ t ] ) ) ;
}
}
if ( fs === null ) loadClient ( ) ;
@ -108,17 +87,76 @@
}
templates . init = function ( ) {
loadTemplates ( [
'header' , 'footer' , 'register' , 'home' , 'topic' , 'account' , 'category' , 'users' , 'accountedit' , 'friends' ,
'login' , 'reset' , 'reset_code' , 'account' ,
'confirm' , '403' , 'logout' ,
'emails/reset' , 'emails/reset_plaintext' , 'emails/email_confirm' , 'emails/email_confirm_plaintext' ,
'admin/index' , 'admin/categories' , 'admin/users' , 'admin/topics' , 'admin/settings' , 'admin/themes' , 'admin/twitter' , 'admin/facebook' , 'admin/gplus'
] ) ;
templates . init = function ( templates _to _load ) {
loadTemplates ( templates _to _load || [ ] ) ;
}
templates . load _template = function ( callback , url , template ) {
var location = document . location || window . location ,
rootUrl = location . protocol + '//' + ( location . hostname || location . host ) + ( location . port ? ':' + location . port : '' ) ;
var api _url = ( url === '' || url === '/' ) ? 'home' : url ;
var tpl _url = templates . get _custom _map ( url ) ;
if ( tpl _url == false && ! templates [ api _url ] ) {
tpl _url = api _url . split ( '/' ) [ 0 ] ;
} else {
tpl _url = api _url ;
}
var template _data = null ;
( function ( ) {
var timestamp = new Date ( ) . getTime ( ) ; //debug
if ( ! templates [ tpl _url ] ) {
jQuery . get ( '/templates/' + tpl _url + '.tpl?v=' + timestamp , function ( html ) {
var template = function ( ) {
this . toString = function ( ) {
return this . html ;
} ;
}
template . prototype . parse = parse ;
template . prototype . html = String ( html ) ;
template . prototype . blocks = { } ;
templates [ tpl _url ] = new template ;
parse _template ( ) ;
} ) ;
} else {
parse _template ( ) ;
}
} ( ) ) ;
( function ( ) {
jQuery . get ( API _URL + api _url , function ( data ) {
if ( ! data ) {
window . location . href = '/404' ;
return ;
}
template _data = data ;
parse _template ( ) ;
} ) ;
} ( ) ) ;
function parse _template ( ) {
if ( ! templates [ tpl _url ] || ! template _data ) return ;
document . getElementById ( 'content' ) . innerHTML = templates [ tpl _url ] . parse ( JSON . parse ( template _data ) ) ;
if ( callback ) callback ( true ) ;
}
}
//modified from https://github.com/psychobunny/dcp.templates
var parse = function ( data ) {
var self = this ;
@ -212,30 +250,3 @@
function load _template ( callback , url , template ) {
var location = document . location || window . location ,
rootUrl = location . protocol + '//' + ( location . hostname || location . host ) + ( location . port ? ':' + location . port : '' ) ;
url = ( url === '' || url === '/' ) ? 'home' : url ;
jQuery . get ( API _URL + url , function ( data ) {
if ( ! data ) {
window . location . href = '/403' ;
return ;
}
var tpl = templates . get _custom _map ( url ) ;
if ( tpl == false && ! templates [ url ] ) {
tpl = ( url === '' || url === '/' ) ? 'home' : url . split ( '/' ) [ 0 ] ;
} else if ( templates [ url ] ) {
tpl = url ;
}
document . getElementById ( 'content' ) . innerHTML = templates [ tpl ] . parse ( JSON . parse ( data ) ) ;
if ( callback ) callback ( ) ;
} ) ;
}