Files
NodeBB/public/src/ajaxify.js

266 lines
6.4 KiB
JavaScript
Raw Normal View History

2013-11-21 12:28:10 -05:00
"use strict";
2013-11-21 12:28:10 -05:00
var ajaxify = {};
(function () {
/*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/
2013-08-23 13:45:57 -04:00
var location = document.location || window.location,
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''),
2013-04-22 19:13:39 +00:00
content = null;
var events = [];
ajaxify.register_events = function (new_page_events) {
2013-09-17 13:04:40 -04:00
for (var i = 0, ii = events.length; i < ii; i++) {
socket.removeAllListeners(events[i]); // optimize this to user removeListener(event, listener) instead.
}
events = new_page_events;
};
window.onpopstate = function (event) {
if (event !== null && event.state && event.state.url !== undefined && !ajaxify.initialLoad) {
2014-02-27 23:45:12 -05:00
ajaxify.go(event.state.url, function() {
$(window).trigger('action:popstate', {url: event.state.url});
}, true);
2013-11-21 12:28:10 -05:00
}
};
ajaxify.currentPage = null;
ajaxify.initialLoad = false;
2013-12-07 16:40:14 -05:00
ajaxify.go = function (url, callback, quiet) {
// "quiet": If set to true, will not call pushState
app.enterRoom('global');
2014-01-29 15:19:54 -05:00
$(window).off('scroll');
2014-03-04 17:22:45 -05:00
$(window).trigger('action:ajaxify.start', {url: url});
2013-08-28 01:23:19 +08:00
if ($('#content').hasClass('ajaxifying')) {
templates.cancelRequest();
}
// Remove trailing slash
url = url.replace(/\/$/, "");
2013-09-17 13:04:40 -04:00
if (url.indexOf(RELATIVE_PATH.slice(1)) !== -1) {
2013-07-10 18:42:38 -04:00
url = url.slice(RELATIVE_PATH.length);
}
2014-02-28 16:13:00 -05:00
var tpl_url = ajaxify.getTemplateMapping(url);
2014-02-17 20:57:12 -05:00
var hash = '';
2014-02-18 00:00:42 -05:00
if(ajaxify.initialLoad) {
2014-02-17 20:57:12 -05:00
hash = window.location.hash ? window.location.hash : '';
}
if (templates.is_available(tpl_url) && !templates.force_refresh(tpl_url)) {
ajaxify.currentPage = tpl_url;
if (window.history && window.history.pushState) {
window.history[!quiet ? 'pushState' : 'replaceState']({
2014-02-17 20:57:12 -05:00
url: url + hash
}, url, RELATIVE_PATH + '/' + url + hash);
$.ajax(RELATIVE_PATH + '/plugins/fireHook', {
type: 'PUT',
data: {
_csrf: $('#csrf_token').val(),
hook: 'page.load',
args: {
template: tpl_url,
url: url,
uid: app.uid
2013-11-21 12:28:10 -05:00
}
}
});
}
translator.load(tpl_url);
2014-02-28 16:24:25 -05:00
ajaxify.fadeOut();
templates.flush();
templates.load_template(function () {
2014-03-02 22:47:14 -05:00
ajaxify.loadScript(tpl_url);
2014-02-27 23:45:12 -05:00
if (typeof callback === 'function') {
callback();
}
2013-08-23 13:45:57 -04:00
app.processPage();
2013-09-24 16:46:45 -04:00
2014-02-28 16:17:35 -05:00
ajaxify.renderWidgets(tpl_url, url, function(err) {
2014-02-28 16:24:25 -05:00
ajaxify.fadeIn();
ajaxify.initialLoad = false;
app.refreshTitle(url);
2014-03-04 17:22:45 -05:00
$(window).trigger('action:ajaxify.end', {url: url});
});
}, url);
return true;
}
return false;
};
2014-03-02 22:47:14 -05:00
ajaxify.loadScript = function(tpl_url, callback) {
require(['forum/' + tpl_url], function(script) {
if (script && script.init) {
script.init();
}
if (callback) {
callback();
}
});
};
2014-02-28 16:24:25 -05:00
ajaxify.fadeIn = function() {
$('#content, #footer').stop(true, true).removeClass('ajaxifying');
};
ajaxify.fadeOut = function() {
$('#footer, #content').removeClass('hide').addClass('ajaxifying');
};
2014-02-28 16:13:00 -05:00
ajaxify.getTemplateMapping = function(url) {
var tpl_url = templates.get_custom_map(url.split('?')[0]);
if (tpl_url === false && !templates[url]) {
2014-02-28 16:13:00 -05:00
if (url === '' || url === '/') {
tpl_url = 'home';
2014-03-03 14:43:25 -05:00
} else if (url === 'admin' || url === 'admin/') {
tpl_url = 'admin/index';
2014-02-28 16:13:00 -05:00
} else {
tpl_url = url.split('/');
while(tpl_url.length) {
if (templates.is_available(tpl_url.join('/'))) {
tpl_url = tpl_url.join('/');
break;
}
tpl_url.pop();
}
if (!tpl_url.length) {
tpl_url = url.split('/')[0].split('?')[0];
}
2014-02-28 16:13:00 -05:00
}
} else if (templates[url]) {
tpl_url = url;
}
return tpl_url;
};
2014-02-28 16:13:00 -05:00
2014-03-17 13:38:32 -04:00
ajaxify.repositionNoWidgets = function() {
$('body [no-widget-class]').each(function() {
var $this = $(this);
$this.removeClass();
$this.addClass($this.attr('no-widget-class'));
});
};
2014-02-28 16:17:35 -05:00
ajaxify.renderWidgets = function(tpl_url, url, callback) {
var widgetLocations = [], numLocations;
$('#content [widget-area]').each(function() {
widgetLocations.push($(this).attr('widget-area'));
});
numLocations = widgetLocations.length;
2014-03-17 13:38:32 -04:00
if (!numLocations) {
ajaxify.repositionNoWidgets();
}
function renderWidgets(location) {
var area = $('#content [widget-area="' + location + '"]');
socket.emit('widgets.render', {template: tpl_url + '.tpl', url: url, location: location}, function(err, renderedWidgets) {
if (area.html()) {
area.html(templates.prepare(area.html()).parse({
widgets: renderedWidgets
})).removeClass('hidden');
if (!renderedWidgets.length) {
2014-03-17 13:38:32 -04:00
ajaxify.repositionNoWidgets();
}
}
2014-03-17 15:15:16 -04:00
$('#content [widget-area] img').addClass('img-responsive')
checkCallback();
});
}
function checkCallback() {
numLocations--;
2014-03-17 17:25:22 -04:00
if (numLocations < 0 && callback) {
callback();
}
}
for (var i in widgetLocations) {
if (widgetLocations.hasOwnProperty(i)) {
renderWidgets(widgetLocations[i]);
}
}
checkCallback();
};
2013-12-07 16:40:14 -05:00
ajaxify.refresh = function() {
ajaxify.go(ajaxify.currentPage);
};
$('document').ready(function () {
2013-11-21 12:28:10 -05:00
if (!window.history || !window.history.pushState) {
return; // no ajaxification for old browsers
}
2013-04-22 19:13:39 +00:00
content = content || document.getElementById('content');
// Enhancing all anchors to ajaxify...
$(document.body).on('click', 'a', function (e) {
2013-08-23 13:45:57 -04:00
function hrefEmpty(href) {
2013-11-21 12:28:10 -05:00
return href === 'javascript:;' || href === window.location.href + "#" || href.slice(-1) === "#";
2013-08-23 13:45:57 -04:00
}
2013-11-21 12:28:10 -05:00
if (hrefEmpty(this.href) || this.target !== '' || this.protocol === 'javascript:') {
2013-09-25 13:08:11 -04:00
return;
2013-11-21 12:28:10 -05:00
}
2013-09-25 13:08:11 -04:00
2013-11-21 12:28:10 -05:00
if(!window.location.pathname.match(/\/(403|404)$/g)) {
app.previousUrl = window.location.href;
2013-11-21 12:28:10 -05:00
}
if ($(this).attr('data-ajaxify') === 'false') {
return;
}
if ((!e.ctrlKey && !e.shiftKey) && e.which === 1) {
if (this.host === window.location.host) {
// Internal link
var url = this.href.replace(rootUrl + '/', '');
if (ajaxify.go(url)) {
e.preventDefault();
}
} else if (window.location.pathname !== '/outgoing') {
// External Link
2013-10-01 12:07:58 -04:00
if (config.useOutgoingLinksPage) {
2013-10-01 12:07:58 -04:00
ajaxify.go('outgoing?url=' + encodeURIComponent(this.href));
e.preventDefault();
}
}
}
});
2013-04-22 19:13:39 +00:00
});
}());