mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-03 05:09:48 +01:00
added recompilation of templates to NodeBB Reloading - #2010
This commit is contained in:
@@ -2,339 +2,340 @@
|
||||
|
||||
var ajaxify = ajaxify || {};
|
||||
|
||||
(function () {
|
||||
/*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/
|
||||
|
||||
var location = document.location || window.location,
|
||||
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''),
|
||||
templatesConfig = null,
|
||||
availableTemplates = null,
|
||||
apiXHR = null,
|
||||
|
||||
PRELOADER_RATE_LIMIT = 10000;
|
||||
|
||||
window.onpopstate = function (event) {
|
||||
if (event !== null && event.state && event.state.url !== undefined && !ajaxify.initialLoad) {
|
||||
ajaxify.go(event.state.url, function() {
|
||||
$(window).trigger('action:popstate', {url: event.state.url});
|
||||
}, true);
|
||||
}
|
||||
};
|
||||
|
||||
ajaxify.currentPage = null;
|
||||
ajaxify.initialLoad = false;
|
||||
ajaxify.preloader = {};
|
||||
|
||||
function onAjaxError(err, url) {
|
||||
var data = err.data, textStatus = err.textStatus;
|
||||
|
||||
$('#content, #footer').removeClass('ajaxifying');
|
||||
|
||||
if (data) {
|
||||
if (data.status === 404) {
|
||||
return ajaxify.go('404');
|
||||
} else if (data.status === 403) {
|
||||
app.alertError('[[global:please_log_in]]');
|
||||
app.previousUrl = url;
|
||||
return ajaxify.go('login');
|
||||
} else if (data.status === 302) {
|
||||
return ajaxify.go(data.responseJSON.slice(1));
|
||||
}
|
||||
} else if (textStatus !== "abort") {
|
||||
app.alertError(data.responseJSON.error);
|
||||
}
|
||||
}
|
||||
|
||||
ajaxify.go = function (url, callback, quiet) {
|
||||
// "quiet": If set to true, will not call pushState
|
||||
app.enterRoom('global');
|
||||
|
||||
$(window).off('scroll');
|
||||
|
||||
$(window).trigger('action:ajaxify.start', {url: url});
|
||||
|
||||
if ($('#content').hasClass('ajaxifying') && apiXHR) {
|
||||
apiXHR.abort();
|
||||
}
|
||||
|
||||
// Remove trailing slash
|
||||
url = url.replace(/\/$/, "");
|
||||
|
||||
url = ajaxify.removeRelativePath(url);
|
||||
|
||||
var tpl_url = ajaxify.getTemplateMapping(url);
|
||||
|
||||
var hash = '';
|
||||
if(ajaxify.initialLoad) {
|
||||
hash = window.location.hash ? window.location.hash : '';
|
||||
}
|
||||
|
||||
if (ajaxify.isTemplateAvailable(tpl_url) && !!!templatesConfig.force_refresh[tpl_url]) {
|
||||
ajaxify.currentPage = url;
|
||||
|
||||
if (window.history && window.history.pushState) {
|
||||
window.history[!quiet ? 'pushState' : 'replaceState']({
|
||||
url: url + hash
|
||||
}, url, RELATIVE_PATH + '/' + url + hash);
|
||||
}
|
||||
|
||||
translator.load(config.defaultLang, tpl_url);
|
||||
|
||||
$('#footer, #content').removeClass('hide').addClass('ajaxifying');
|
||||
var animationDuration = parseFloat($('#content').css('transition-duration')) || 0.2,
|
||||
startTime = (new Date()).getTime();
|
||||
|
||||
ajaxify.variables.flush();
|
||||
ajaxify.loadData(url, function(err, data) {
|
||||
if (err) {
|
||||
return onAjaxError(err, url);
|
||||
}
|
||||
|
||||
$(window).trigger('action:ajaxify.loadingTemplates', {});
|
||||
|
||||
templates.parse(tpl_url, data, function(template) {
|
||||
translator.translate(template, function(translatedTemplate) {
|
||||
setTimeout(function() {
|
||||
$('#content').html(translatedTemplate);
|
||||
|
||||
ajaxify.variables.parse();
|
||||
|
||||
ajaxify.widgets.render(tpl_url, url, function() {
|
||||
$(window).trigger('action:ajaxify.end', {url: url});
|
||||
});
|
||||
|
||||
$(window).trigger('action:ajaxify.contentLoaded', {url: url});
|
||||
|
||||
ajaxify.loadScript(tpl_url);
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
callback();
|
||||
}
|
||||
|
||||
app.processPage();
|
||||
|
||||
$('#content, #footer').removeClass('ajaxifying');
|
||||
ajaxify.initialLoad = false;
|
||||
|
||||
app.refreshTitle(url);
|
||||
}, animationDuration * 1000 - ((new Date()).getTime() - startTime))
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
ajaxify.removeRelativePath = function(url) {
|
||||
if (url.indexOf(RELATIVE_PATH.slice(1)) === 0) {
|
||||
url = url.slice(RELATIVE_PATH.length);
|
||||
}
|
||||
return url;
|
||||
};
|
||||
|
||||
ajaxify.refresh = function() {
|
||||
ajaxify.go(ajaxify.currentPage);
|
||||
};
|
||||
|
||||
ajaxify.loadScript = function(tpl_url, callback) {
|
||||
require(['forum/' + tpl_url], function(script) {
|
||||
if (script && script.init) {
|
||||
script.init();
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
ajaxify.isTemplateAvailable = function(tpl) {
|
||||
return $.inArray(tpl + '.tpl', availableTemplates) !== -1;
|
||||
};
|
||||
|
||||
ajaxify.getTemplateMapping = function(url) {
|
||||
var tpl_url = ajaxify.getCustomTemplateMapping(url.split('?')[0]);
|
||||
|
||||
if (tpl_url === false && !templates[url]) {
|
||||
tpl_url = url.split('/');
|
||||
|
||||
while(tpl_url.length) {
|
||||
if (ajaxify.isTemplateAvailable(tpl_url.join('/'))) {
|
||||
tpl_url = tpl_url.join('/');
|
||||
break;
|
||||
}
|
||||
tpl_url.pop();
|
||||
}
|
||||
|
||||
if (!tpl_url.length) {
|
||||
tpl_url = url.split('/')[0].split('?')[0];
|
||||
}
|
||||
} else if (templates[url]) {
|
||||
tpl_url = url;
|
||||
}
|
||||
|
||||
return tpl_url;
|
||||
};
|
||||
|
||||
ajaxify.getCustomTemplateMapping = function(tpl) {
|
||||
if (templatesConfig && templatesConfig.custom_mapping && tpl !== undefined) {
|
||||
for (var pattern in templatesConfig.custom_mapping) {
|
||||
if (tpl.match(pattern)) {
|
||||
return (templatesConfig.custom_mapping[pattern]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
ajaxify.loadData = function(url, callback) {
|
||||
url = ajaxify.removeRelativePath(url);
|
||||
|
||||
$(window).trigger('action:ajaxify.loadingData', {url: url});
|
||||
|
||||
if (ajaxify.preloader && ajaxify.preloader[url] && !ajaxify.preloader[url].loading) {
|
||||
callback(null, ajaxify.preloader[url].data);
|
||||
ajaxify.preloader = {};
|
||||
return;
|
||||
}
|
||||
$(document).ready(function() {
|
||||
require(['templates'], function (templatesModule) {
|
||||
/*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/
|
||||
|
||||
var location = document.location || window.location,
|
||||
tpl_url = ajaxify.getCustomTemplateMapping(url.split('?')[0]);
|
||||
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''),
|
||||
apiXHR = null,
|
||||
|
||||
if (!tpl_url) {
|
||||
tpl_url = ajaxify.getTemplateMapping(url);
|
||||
PRELOADER_RATE_LIMIT = 10000;
|
||||
|
||||
window.onpopstate = function (event) {
|
||||
if (event !== null && event.state && event.state.url !== undefined && !ajaxify.initialLoad) {
|
||||
ajaxify.go(event.state.url, function() {
|
||||
$(window).trigger('action:popstate', {url: event.state.url});
|
||||
}, true);
|
||||
}
|
||||
};
|
||||
|
||||
ajaxify.currentPage = null;
|
||||
ajaxify.initialLoad = false;
|
||||
ajaxify.preloader = {};
|
||||
|
||||
function onAjaxError(err, url) {
|
||||
var data = err.data, textStatus = err.textStatus;
|
||||
|
||||
$('#content, #footer').removeClass('ajaxifying');
|
||||
|
||||
if (data) {
|
||||
if (data.status === 404) {
|
||||
return ajaxify.go('404');
|
||||
} else if (data.status === 403) {
|
||||
app.alertError('[[global:please_log_in]]');
|
||||
app.previousUrl = url;
|
||||
return ajaxify.go('login');
|
||||
} else if (data.status === 302) {
|
||||
return ajaxify.go(data.responseJSON.slice(1));
|
||||
}
|
||||
} else if (textStatus !== "abort") {
|
||||
app.alertError(data.responseJSON.error);
|
||||
}
|
||||
}
|
||||
|
||||
apiXHR = $.ajax({
|
||||
url: RELATIVE_PATH + '/api/' + url,
|
||||
cache: false,
|
||||
success: function(data) {
|
||||
if (!data) {
|
||||
ajaxify.go('404');
|
||||
ajaxify.go = function (url, callback, quiet) {
|
||||
// "quiet": If set to true, will not call pushState
|
||||
app.enterRoom('global');
|
||||
|
||||
$(window).off('scroll');
|
||||
|
||||
$(window).trigger('action:ajaxify.start', {url: url});
|
||||
|
||||
if ($('#content').hasClass('ajaxifying') && apiXHR) {
|
||||
apiXHR.abort();
|
||||
}
|
||||
|
||||
// Remove trailing slash
|
||||
url = url.replace(/\/$/, "");
|
||||
|
||||
url = ajaxify.removeRelativePath(url);
|
||||
|
||||
var tpl_url = ajaxify.getTemplateMapping(url);
|
||||
|
||||
var hash = '';
|
||||
if(ajaxify.initialLoad) {
|
||||
hash = window.location.hash ? window.location.hash : '';
|
||||
}
|
||||
|
||||
if (ajaxify.isTemplateAvailable(tpl_url) && !!!templatesModule.config.force_refresh[tpl_url]) {
|
||||
ajaxify.currentPage = url;
|
||||
|
||||
if (window.history && window.history.pushState) {
|
||||
window.history[!quiet ? 'pushState' : 'replaceState']({
|
||||
url: url + hash
|
||||
}, url, RELATIVE_PATH + '/' + url + hash);
|
||||
}
|
||||
|
||||
translator.load(config.defaultLang, tpl_url);
|
||||
|
||||
$('#footer, #content').removeClass('hide').addClass('ajaxifying');
|
||||
var animationDuration = parseFloat($('#content').css('transition-duration')) || 0.2,
|
||||
startTime = (new Date()).getTime();
|
||||
|
||||
ajaxify.variables.flush();
|
||||
ajaxify.loadData(url, function(err, data) {
|
||||
if (err) {
|
||||
return onAjaxError(err, url);
|
||||
}
|
||||
|
||||
$(window).trigger('action:ajaxify.loadingTemplates', {});
|
||||
|
||||
templates.parse(tpl_url, data, function(template) {
|
||||
translator.translate(template, function(translatedTemplate) {
|
||||
setTimeout(function() {
|
||||
$('#content').html(translatedTemplate);
|
||||
|
||||
ajaxify.variables.parse();
|
||||
|
||||
ajaxify.widgets.render(tpl_url, url, function() {
|
||||
$(window).trigger('action:ajaxify.end', {url: url});
|
||||
});
|
||||
|
||||
$(window).trigger('action:ajaxify.contentLoaded', {url: url});
|
||||
|
||||
ajaxify.loadScript(tpl_url);
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
callback();
|
||||
}
|
||||
|
||||
app.processPage();
|
||||
|
||||
$('#content, #footer').removeClass('ajaxifying');
|
||||
ajaxify.initialLoad = false;
|
||||
|
||||
app.refreshTitle(url);
|
||||
}, animationDuration * 1000 - ((new Date()).getTime() - startTime))
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
ajaxify.removeRelativePath = function(url) {
|
||||
if (url.indexOf(RELATIVE_PATH.slice(1)) === 0) {
|
||||
url = url.slice(RELATIVE_PATH.length);
|
||||
}
|
||||
return url;
|
||||
};
|
||||
|
||||
ajaxify.refresh = function() {
|
||||
ajaxify.go(ajaxify.currentPage);
|
||||
};
|
||||
|
||||
ajaxify.loadScript = function(tpl_url, callback) {
|
||||
require(['forum/' + tpl_url], function(script) {
|
||||
if (script && script.init) {
|
||||
script.init();
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
ajaxify.isTemplateAvailable = function(tpl) {
|
||||
return $.inArray(tpl + '.tpl', templatesModule.available) !== -1;
|
||||
};
|
||||
|
||||
ajaxify.getTemplateMapping = function(url) {
|
||||
var tpl_url = ajaxify.getCustomTemplateMapping(url.split('?')[0]);
|
||||
|
||||
if (tpl_url === false && !templates[url]) {
|
||||
tpl_url = url.split('/');
|
||||
|
||||
while(tpl_url.length) {
|
||||
if (ajaxify.isTemplateAvailable(tpl_url.join('/'))) {
|
||||
tpl_url = tpl_url.join('/');
|
||||
break;
|
||||
}
|
||||
tpl_url.pop();
|
||||
}
|
||||
|
||||
if (!tpl_url.length) {
|
||||
tpl_url = url.split('/')[0].split('?')[0];
|
||||
}
|
||||
} else if (templates[url]) {
|
||||
tpl_url = url;
|
||||
}
|
||||
|
||||
return tpl_url;
|
||||
};
|
||||
|
||||
ajaxify.getCustomTemplateMapping = function(tpl) {
|
||||
if (templatesModule.config && templatesModule.config.custom_mapping && tpl !== undefined) {
|
||||
for (var pattern in templatesModule.config.custom_mapping) {
|
||||
if (tpl.match(pattern)) {
|
||||
return (templatesModule.config.custom_mapping[pattern]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
ajaxify.loadData = function(url, callback) {
|
||||
url = ajaxify.removeRelativePath(url);
|
||||
|
||||
$(window).trigger('action:ajaxify.loadingData', {url: url});
|
||||
|
||||
if (ajaxify.preloader && ajaxify.preloader[url] && !ajaxify.preloader[url].loading) {
|
||||
callback(null, ajaxify.preloader[url].data);
|
||||
ajaxify.preloader = {};
|
||||
return;
|
||||
}
|
||||
|
||||
var location = document.location || window.location,
|
||||
tpl_url = ajaxify.getCustomTemplateMapping(url.split('?')[0]);
|
||||
|
||||
if (!tpl_url) {
|
||||
tpl_url = ajaxify.getTemplateMapping(url);
|
||||
}
|
||||
|
||||
apiXHR = $.ajax({
|
||||
url: RELATIVE_PATH + '/api/' + url,
|
||||
cache: false,
|
||||
success: function(data) {
|
||||
if (!data) {
|
||||
ajaxify.go('404');
|
||||
return;
|
||||
}
|
||||
|
||||
data.relative_path = RELATIVE_PATH;
|
||||
|
||||
if (callback) {
|
||||
callback(null, data);
|
||||
}
|
||||
},
|
||||
error: function(data, textStatus) {
|
||||
callback({
|
||||
data: data,
|
||||
textStatus: textStatus
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
ajaxify.loadTemplate = function(template, callback) {
|
||||
if (templates.cache[template]) {
|
||||
callback(templates.cache[template]);
|
||||
} else {
|
||||
$.ajax({
|
||||
url: RELATIVE_PATH + '/templates/' + template + '.tpl' + (config['cache-buster'] ? '?v=' + config['cache-buster'] : ''),
|
||||
type: 'GET',
|
||||
success: function(data) {
|
||||
callback(data.toString());
|
||||
},
|
||||
error: function(error) {
|
||||
throw new Error("Unable to load template: " + template + " (" + error.statusText + ")");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
$('document').ready(function () {
|
||||
if (!window.history || !window.history.pushState) {
|
||||
return; // no ajaxification for old browsers
|
||||
}
|
||||
|
||||
function hrefEmpty(href) {
|
||||
return href === undefined || href === '' || href === 'javascript:;' || href === window.location.href + "#" || href.slice(0, 1) === "#";
|
||||
}
|
||||
|
||||
// Enhancing all anchors to ajaxify...
|
||||
$(document.body).on('click', 'a', function (e) {
|
||||
if (hrefEmpty(this.href) || this.target !== '' || this.protocol === 'javascript:' || $(this).attr('data-ajaxify') === 'false') {
|
||||
return;
|
||||
}
|
||||
|
||||
data.relative_path = RELATIVE_PATH;
|
||||
|
||||
if (callback) {
|
||||
callback(null, data);
|
||||
if(!window.location.pathname.match(/\/(403|404)$/g)) {
|
||||
app.previousUrl = window.location.href;
|
||||
}
|
||||
},
|
||||
error: function(data, textStatus) {
|
||||
callback({
|
||||
data: data,
|
||||
textStatus: textStatus
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
ajaxify.loadTemplate = function(template, callback) {
|
||||
if (templates.cache[template]) {
|
||||
callback(templates.cache[template]);
|
||||
} else {
|
||||
$.ajax({
|
||||
url: RELATIVE_PATH + '/templates/' + template + '.tpl' + (config['cache-buster'] ? '?v=' + config['cache-buster'] : ''),
|
||||
type: 'GET',
|
||||
success: function(data) {
|
||||
callback(data.toString());
|
||||
},
|
||||
error: function(error) {
|
||||
throw new Error("Unable to load template: " + template + " (" + error.statusText + ")");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
if ((!e.ctrlKey && !e.shiftKey && !e.metaKey) && e.which === 1) {
|
||||
if (this.host === window.location.host) {
|
||||
// Internal link
|
||||
var url = this.href.replace(rootUrl + '/', '');
|
||||
|
||||
$('document').ready(function () {
|
||||
if (!window.history || !window.history.pushState) {
|
||||
return; // no ajaxification for old browsers
|
||||
}
|
||||
if(window.location.pathname === this.pathname && this.hash) {
|
||||
if (this.hash !== window.location.hash) {
|
||||
window.location.hash = this.hash;
|
||||
}
|
||||
|
||||
function hrefEmpty(href) {
|
||||
return href === undefined || href === '' || href === 'javascript:;' || href === window.location.href + "#" || href.slice(0, 1) === "#";
|
||||
}
|
||||
|
||||
// Enhancing all anchors to ajaxify...
|
||||
$(document.body).on('click', 'a', function (e) {
|
||||
if (hrefEmpty(this.href) || this.target !== '' || this.protocol === 'javascript:' || $(this).attr('data-ajaxify') === 'false') {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!window.location.pathname.match(/\/(403|404)$/g)) {
|
||||
app.previousUrl = window.location.href;
|
||||
}
|
||||
|
||||
if ((!e.ctrlKey && !e.shiftKey && !e.metaKey) && e.which === 1) {
|
||||
if (this.host === window.location.host) {
|
||||
// Internal link
|
||||
var url = this.href.replace(rootUrl + '/', '');
|
||||
|
||||
if(window.location.pathname === this.pathname && this.hash) {
|
||||
if (this.hash !== window.location.hash) {
|
||||
window.location.hash = this.hash;
|
||||
ajaxify.loadScript(ajaxify.getTemplateMapping(url));
|
||||
e.preventDefault();
|
||||
} else {
|
||||
if (ajaxify.go(url)) {
|
||||
e.preventDefault();
|
||||
}
|
||||
}
|
||||
|
||||
ajaxify.loadScript(ajaxify.getTemplateMapping(url));
|
||||
e.preventDefault();
|
||||
} else {
|
||||
if (ajaxify.go(url)) {
|
||||
} else if (window.location.pathname !== '/outgoing') {
|
||||
// External Link
|
||||
if (config.openOutgoingLinksInNewTab) {
|
||||
window.open(this.href, '_blank');
|
||||
e.preventDefault();
|
||||
} else if (config.useOutgoingLinksPage) {
|
||||
ajaxify.go('outgoing?url=' + encodeURIComponent(this.href));
|
||||
e.preventDefault();
|
||||
}
|
||||
}
|
||||
} else if (window.location.pathname !== '/outgoing') {
|
||||
// External Link
|
||||
if (config.openOutgoingLinksInNewTab) {
|
||||
window.open(this.href, '_blank');
|
||||
e.preventDefault();
|
||||
} else if (config.useOutgoingLinksPage) {
|
||||
ajaxify.go('outgoing?url=' + encodeURIComponent(this.href));
|
||||
e.preventDefault();
|
||||
}
|
||||
});
|
||||
|
||||
$(document.body).on('mouseover', 'a', function (e) {
|
||||
if (hrefEmpty(this.href) || this.target !== '' || this.protocol === 'javascript:' || $(this).attr('data-ajaxify') === 'false') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.host === window.location.host) {
|
||||
// Internal link
|
||||
var url = this.href.replace(rootUrl + '/', ''),
|
||||
currentTime = (new Date()).getTime();
|
||||
|
||||
if (!ajaxify.preloader[url] || (!ajaxify.preloader[url].loading && currentTime - ajaxify.preloader[url].lastFetched > PRELOADER_RATE_LIMIT)) {
|
||||
ajaxify.preloader[url] = {
|
||||
loading: true
|
||||
};
|
||||
ajaxify.loadData(url, function(err, data) {
|
||||
ajaxify.preloader[url] = err ? null : {
|
||||
url: url,
|
||||
data: data,
|
||||
lastFetched: currentTime,
|
||||
loading: false
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
templates.registerLoader(ajaxify.loadTemplate);
|
||||
|
||||
templatesModule.refresh(app.load);
|
||||
// $.getJSON(RELATIVE_PATH + '/api/get_templates_listing', function (data) {
|
||||
// templatesModule.config = data.templatesConfig;
|
||||
// availableTemplates = data.availableTemplates;
|
||||
|
||||
// app.load();
|
||||
// });
|
||||
});
|
||||
|
||||
$(document.body).on('mouseover', 'a', function (e) {
|
||||
if (hrefEmpty(this.href) || this.target !== '' || this.protocol === 'javascript:' || $(this).attr('data-ajaxify') === 'false') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.host === window.location.host) {
|
||||
// Internal link
|
||||
var url = this.href.replace(rootUrl + '/', ''),
|
||||
currentTime = (new Date()).getTime();
|
||||
|
||||
if (!ajaxify.preloader[url] || (!ajaxify.preloader[url].loading && currentTime - ajaxify.preloader[url].lastFetched > PRELOADER_RATE_LIMIT)) {
|
||||
ajaxify.preloader[url] = {
|
||||
loading: true
|
||||
};
|
||||
ajaxify.loadData(url, function(err, data) {
|
||||
ajaxify.preloader[url] = err ? null : {
|
||||
url: url,
|
||||
data: data,
|
||||
lastFetched: currentTime,
|
||||
loading: false
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
templates.registerLoader(ajaxify.loadTemplate);
|
||||
|
||||
$.getJSON(RELATIVE_PATH + '/api/get_templates_listing', function (data) {
|
||||
templatesConfig = data.templatesConfig;
|
||||
availableTemplates = data.availableTemplates;
|
||||
|
||||
app.load();
|
||||
});
|
||||
});
|
||||
|
||||
}());
|
||||
});
|
||||
15
public/src/modules/templates.js
Normal file
15
public/src/modules/templates.js
Normal file
@@ -0,0 +1,15 @@
|
||||
define('templates', function() {
|
||||
var Templates = {};
|
||||
|
||||
Templates.refresh = function(callback) {
|
||||
$.getJSON(RELATIVE_PATH + '/api/get_templates_listing', function (data) {
|
||||
Templates.config = data.templatesConfig;
|
||||
Templates.available = data.availableTemplates;
|
||||
|
||||
if (callback) callback();
|
||||
// app.load();
|
||||
});
|
||||
};
|
||||
|
||||
return Templates;
|
||||
});
|
||||
16
src/meta.js
16
src/meta.js
@@ -17,7 +17,7 @@ var async = require('async'),
|
||||
require('./meta/css')(Meta);
|
||||
require('./meta/sounds')(Meta);
|
||||
require('./meta/settings')(Meta);
|
||||
|
||||
Meta.templates = require('./meta/templates');
|
||||
|
||||
/* Assorted */
|
||||
Meta.userOrGroupExists = function(slug, callback) {
|
||||
@@ -33,10 +33,16 @@ var async = require('async'),
|
||||
plugins.reload(function() {
|
||||
async.parallel([
|
||||
async.apply(Meta.js.minify, false),
|
||||
async.apply(Meta.css.minify)
|
||||
], function() {
|
||||
emitter.emit('nodebb:ready');
|
||||
callback.apply(null, arguments);
|
||||
async.apply(Meta.css.minify),
|
||||
async.apply(Meta.templates.compile)
|
||||
], function(err) {
|
||||
if (!err) {
|
||||
emitter.emit('nodebb:ready');
|
||||
callback.apply(null, arguments);
|
||||
} else {
|
||||
console.log('failed!');
|
||||
emitter.emit('nodebb:reload.failed');
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
89
src/meta/templates.js
Normal file
89
src/meta/templates.js
Normal file
@@ -0,0 +1,89 @@
|
||||
var mkdirp = require('mkdirp'),
|
||||
rimraf = require('rimraf'),
|
||||
winston = require('winston'),
|
||||
async = require('async'),
|
||||
path = require('path'),
|
||||
fs = require('fs'),
|
||||
nconf = require('nconf'),
|
||||
|
||||
emitter = require('../emitter'),
|
||||
plugins = require('../plugins'),
|
||||
utils = require('../../public/src/utils'),
|
||||
|
||||
Templates = {};
|
||||
|
||||
Templates.compile = function(callback) {
|
||||
var baseTemplatesPath = nconf.get('base_templates_path'),
|
||||
viewsPath = nconf.get('views_dir'),
|
||||
themeTemplatesPath = nconf.get('theme_templates_path');
|
||||
|
||||
plugins.getTemplates(function(err, pluginTemplates) {
|
||||
winston.info('[meta/templates] Compiling templates');
|
||||
rimraf.sync(viewsPath);
|
||||
mkdirp.sync(viewsPath);
|
||||
|
||||
async.parallel({
|
||||
baseTpls: function(next) {
|
||||
utils.walk(baseTemplatesPath, next);
|
||||
},
|
||||
themeTpls: function(next) {
|
||||
utils.walk(themeTemplatesPath, next);
|
||||
}
|
||||
}, function(err, data) {
|
||||
var baseTpls = data.baseTpls,
|
||||
themeTpls = data.themeTpls,
|
||||
paths = {};
|
||||
|
||||
if (!baseTpls || !themeTpls) {
|
||||
winston.warn('[meta/templates] Could not find base template files at: ' + baseTemplatesPath);
|
||||
}
|
||||
|
||||
baseTpls = !baseTpls ? [] : baseTpls.map(function(tpl) { return tpl.replace(baseTemplatesPath, ''); });
|
||||
themeTpls = !themeTpls ? [] : themeTpls.map(function(tpl) { return tpl.replace(themeTemplatesPath, ''); });
|
||||
|
||||
baseTpls.forEach(function(el, i) {
|
||||
paths[baseTpls[i]] = path.join(baseTemplatesPath, baseTpls[i]);
|
||||
});
|
||||
|
||||
themeTpls.forEach(function(el, i) {
|
||||
paths[themeTpls[i]] = path.join(themeTemplatesPath, themeTpls[i]);
|
||||
});
|
||||
|
||||
for (var tpl in pluginTemplates) {
|
||||
if (pluginTemplates.hasOwnProperty(tpl)) {
|
||||
paths[tpl] = pluginTemplates[tpl];
|
||||
}
|
||||
}
|
||||
|
||||
async.each(Object.keys(paths), function(relativePath, next) {
|
||||
var file = fs.readFileSync(paths[relativePath]).toString(),
|
||||
matches = null,
|
||||
regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/;
|
||||
|
||||
while(matches = file.match(regex)) {
|
||||
var partial = "/" + matches[1];
|
||||
|
||||
if (paths[partial] && relativePath !== partial) {
|
||||
file = file.replace(regex, fs.readFileSync(paths[partial]).toString());
|
||||
} else {
|
||||
winston.warn('[themes] Partial not loaded: ' + matches[1]);
|
||||
file = file.replace(regex, "");
|
||||
}
|
||||
}
|
||||
|
||||
mkdirp.sync(path.join(viewsPath, relativePath.split('/').slice(0, -1).join('/')));
|
||||
fs.writeFile(path.join(viewsPath, relativePath), file, next);
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
winston.error(err);
|
||||
} else {
|
||||
winston.info('[themes] Successfully compiled templates.');
|
||||
emitter.emit('templates:compiled');
|
||||
if (callback) callback();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = Templates;
|
||||
@@ -24,10 +24,7 @@ var utils = require('./../../public/src/utils'),
|
||||
session = require('express-session'),
|
||||
|
||||
relativePath,
|
||||
viewsPath,
|
||||
themesPath,
|
||||
baseTemplatesPath,
|
||||
themeTemplatesPath;
|
||||
themesPath;
|
||||
|
||||
|
||||
var middleware = {};
|
||||
@@ -68,91 +65,17 @@ function routeCurrentTheme(app, themeId, themesData) {
|
||||
|
||||
// Theme's templates path
|
||||
nconf.set('theme_templates_path', themeObj.templates ? path.join(themesPath, themeObj.id, themeObj.templates) : nconf.get('base_templates_path'));
|
||||
themeTemplatesPath = nconf.get('theme_templates_path');
|
||||
}
|
||||
|
||||
function compileTemplates(pluginTemplates) {
|
||||
var mkdirp = require('mkdirp'),
|
||||
rimraf = require('rimraf');
|
||||
|
||||
winston.info('[themes] Compiling templates');
|
||||
rimraf.sync(viewsPath);
|
||||
mkdirp.sync(viewsPath);
|
||||
|
||||
async.parallel({
|
||||
baseTpls: function(next) {
|
||||
utils.walk(baseTemplatesPath, next);
|
||||
},
|
||||
themeTpls: function(next) {
|
||||
utils.walk(themeTemplatesPath, next);
|
||||
}
|
||||
}, function(err, data) {
|
||||
var baseTpls = data.baseTpls,
|
||||
themeTpls = data.themeTpls,
|
||||
paths = {};
|
||||
|
||||
if (!baseTpls || !themeTpls) {
|
||||
winston.warn('[themes] Could not find base template files at: ' + baseTemplatesPath);
|
||||
}
|
||||
|
||||
baseTpls = !baseTpls ? [] : baseTpls.map(function(tpl) { return tpl.replace(baseTemplatesPath, ''); });
|
||||
themeTpls = !themeTpls ? [] : themeTpls.map(function(tpl) { return tpl.replace(themeTemplatesPath, ''); });
|
||||
|
||||
baseTpls.forEach(function(el, i) {
|
||||
paths[baseTpls[i]] = path.join(baseTemplatesPath, baseTpls[i]);
|
||||
});
|
||||
|
||||
themeTpls.forEach(function(el, i) {
|
||||
paths[themeTpls[i]] = path.join(themeTemplatesPath, themeTpls[i]);
|
||||
});
|
||||
|
||||
for (var tpl in pluginTemplates) {
|
||||
if (pluginTemplates.hasOwnProperty(tpl)) {
|
||||
paths[tpl] = pluginTemplates[tpl];
|
||||
}
|
||||
}
|
||||
|
||||
async.each(Object.keys(paths), function(relativePath, next) {
|
||||
var file = fs.readFileSync(paths[relativePath]).toString(),
|
||||
matches = null,
|
||||
regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/;
|
||||
|
||||
while(matches = file.match(regex)) {
|
||||
var partial = "/" + matches[1];
|
||||
|
||||
if (paths[partial] && relativePath !== partial) {
|
||||
file = file.replace(regex, fs.readFileSync(paths[partial]).toString());
|
||||
} else {
|
||||
winston.warn('[themes] Partial not loaded: ' + matches[1]);
|
||||
file = file.replace(regex, "");
|
||||
}
|
||||
}
|
||||
|
||||
mkdirp.sync(path.join(viewsPath, relativePath.split('/').slice(0, -1).join('/')));
|
||||
fs.writeFile(path.join(viewsPath, relativePath), file, next);
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
winston.error(err);
|
||||
} else {
|
||||
winston.info('[themes] Successfully compiled templates.');
|
||||
emitter.emit('templates:compiled');
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = function(app, data) {
|
||||
middleware = require('./middleware')(app);
|
||||
|
||||
relativePath = nconf.get('relative_path');
|
||||
viewsPath = nconf.get('views_dir');
|
||||
themesPath = nconf.get('themes_path');
|
||||
baseTemplatesPath = nconf.get('base_templates_path');
|
||||
|
||||
|
||||
app.engine('tpl', templates.__express);
|
||||
app.set('view engine', 'tpl');
|
||||
app.set('views', viewsPath);
|
||||
app.set('views', nconf.get('views_dir'));
|
||||
app.set('json spaces', process.env.NODE_ENV === 'development' ? 4 : 0);
|
||||
app.use(flash());
|
||||
|
||||
@@ -205,10 +128,7 @@ module.exports = function(app, data) {
|
||||
routeCurrentTheme(app, data.currentThemeId, data.themesData);
|
||||
routeThemeScreenshots(app, data.themesData);
|
||||
|
||||
plugins.getTemplates(function(err, pluginTemplates) {
|
||||
compileTemplates(pluginTemplates);
|
||||
});
|
||||
|
||||
meta.templates.compile();
|
||||
|
||||
return middleware;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user