2013-11-21 12:28:10 -05:00
|
|
|
"use strict";
|
2013-04-22 22:19:16 +00:00
|
|
|
|
2013-11-21 12:28:10 -05:00
|
|
|
var ajaxify = {};
|
2013-04-22 22:19:16 +00:00
|
|
|
|
2014-02-26 21:55:29 -05:00
|
|
|
(function () {
|
2014-03-12 15:05:07 -04:00
|
|
|
/*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/
|
2013-08-23 13:45:57 -04:00
|
|
|
|
2013-04-23 19:38:48 +00: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;
|
|
|
|
|
|
2013-04-25 19:35:14 +00:00
|
|
|
var events = [];
|
2013-09-20 16:01:52 -04:00
|
|
|
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++) {
|
2013-04-25 19:35:14 +00:00
|
|
|
socket.removeAllListeners(events[i]); // optimize this to user removeListener(event, listener) instead.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
events = new_page_events;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-09-20 16:01:52 -04:00
|
|
|
window.onpopstate = function (event) {
|
2014-01-24 10:09:28 -05:00
|
|
|
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
|
|
|
}
|
2013-05-04 07:17:05 +00:00
|
|
|
};
|
|
|
|
|
|
2013-12-07 16:33:42 -05:00
|
|
|
ajaxify.currentPage = null;
|
2014-01-24 10:09:28 -05:00
|
|
|
ajaxify.initialLoad = false;
|
2013-12-07 16:40:14 -05:00
|
|
|
|
2013-12-17 21:53:50 -05:00
|
|
|
ajaxify.go = function (url, callback, quiet) {
|
2014-01-24 09:48:41 -05:00
|
|
|
// "quiet": If set to true, will not call pushState
|
2013-11-23 17:07:31 -05:00
|
|
|
app.enterRoom('global');
|
2013-08-28 02:32:38 +08:00
|
|
|
|
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
|
|
|
|
2014-01-24 08:48:40 -05:00
|
|
|
if ($('#content').hasClass('ajaxifying')) {
|
|
|
|
|
templates.cancelRequest();
|
|
|
|
|
}
|
2014-01-08 14:53:32 -05:00
|
|
|
|
2013-09-29 13:47:27 -04:00
|
|
|
// Remove trailing slash
|
|
|
|
|
url = url.replace(/\/$/, "");
|
2013-05-08 17:28:22 +00:00
|
|
|
|
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);
|
2013-08-11 16:12:20 -04:00
|
|
|
|
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 : '';
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-17 15:20:08 -04:00
|
|
|
if (templates.is_available(tpl_url) && !templates.force_refresh(tpl_url)) {
|
2013-12-07 16:33:42 -05:00
|
|
|
ajaxify.currentPage = tpl_url;
|
|
|
|
|
|
2013-11-25 15:56:16 -05:00
|
|
|
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);
|
2013-11-25 15:56:16 -05:00
|
|
|
|
|
|
|
|
$.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
|
|
|
}
|
2013-11-25 15:56:16 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-05-06 03:41:22 +00:00
|
|
|
|
2013-09-20 16:01:52 -04:00
|
|
|
translator.load(tpl_url);
|
|
|
|
|
|
2014-02-28 16:24:25 -05:00
|
|
|
ajaxify.fadeOut();
|
2013-06-05 17:14:10 -04:00
|
|
|
|
2013-06-05 17:00:58 -04:00
|
|
|
templates.flush();
|
2013-09-20 16:01:52 -04:00
|
|
|
templates.load_template(function () {
|
2014-03-02 22:47:14 -05:00
|
|
|
ajaxify.loadScript(tpl_url);
|
2013-05-06 20:23:38 +00:00
|
|
|
|
2014-02-27 23:45:12 -05:00
|
|
|
if (typeof callback === 'function') {
|
2013-05-06 20:23:38 +00:00
|
|
|
callback();
|
|
|
|
|
}
|
2013-08-23 13:45:57 -04:00
|
|
|
|
2013-11-23 17:07:31 -05: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();
|
2014-02-28 16:08:25 -05:00
|
|
|
ajaxify.initialLoad = false;
|
2014-02-19 17:23:17 -05:00
|
|
|
|
2014-02-28 16:08:25 -05:00
|
|
|
app.refreshTitle(url);
|
2014-03-04 17:22:45 -05:00
|
|
|
$(window).trigger('action:ajaxify.end', {url: url});
|
2014-02-19 17:23:17 -05:00
|
|
|
});
|
2013-12-17 21:53:50 -05:00
|
|
|
}, url);
|
2013-08-11 16:12:20 -04:00
|
|
|
|
2013-04-22 22:19:16 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2013-09-20 16:01:52 -04:00
|
|
|
};
|
2013-04-22 22:19:16 +00:00
|
|
|
|
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]);
|
|
|
|
|
|
2014-03-12 15:05:07 -04:00
|
|
|
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 {
|
2014-03-06 16:00:17 -05:00
|
|
|
tpl_url = url.split('/');
|
|
|
|
|
|
2014-03-09 20:48:15 -04:00
|
|
|
while(tpl_url.length) {
|
2014-03-09 21:05:01 -04:00
|
|
|
if (templates.is_available(tpl_url.join('/'))) {
|
|
|
|
|
tpl_url = tpl_url.join('/');
|
2014-03-09 20:48:15 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
tpl_url.pop();
|
|
|
|
|
}
|
2014-03-09 21:05:01 -04:00
|
|
|
|
|
|
|
|
if (!tpl_url.length) {
|
|
|
|
|
tpl_url = url.split('/')[0].split('?')[0];
|
2014-03-06 16:00:17 -05:00
|
|
|
}
|
2014-02-28 16:13:00 -05:00
|
|
|
}
|
|
|
|
|
} else if (templates[url]) {
|
|
|
|
|
tpl_url = url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tpl_url;
|
2014-03-12 15:05:07 -04:00
|
|
|
};
|
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) {
|
2014-03-12 15:05:07 -04:00
|
|
|
var widgetLocations = [], numLocations;
|
2014-02-28 16:08:25 -05:00
|
|
|
|
2014-03-12 15:05:07 -04:00
|
|
|
$('#content [widget-area]').each(function() {
|
|
|
|
|
widgetLocations.push($(this).attr('widget-area'));
|
2014-02-28 16:08:25 -05:00
|
|
|
});
|
2014-03-12 15:05:07 -04:00
|
|
|
|
|
|
|
|
numLocations = widgetLocations.length;
|
|
|
|
|
|
2014-03-17 13:38:32 -04:00
|
|
|
if (!numLocations) {
|
|
|
|
|
ajaxify.repositionNoWidgets();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 17:11:13 -04:00
|
|
|
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-12 17:11:13 -04:00
|
|
|
}
|
2014-03-12 16:51:59 -04:00
|
|
|
}
|
2014-03-12 15:05:07 -04:00
|
|
|
|
2014-03-17 15:15:16 -04:00
|
|
|
$('#content [widget-area] img').addClass('img-responsive')
|
2014-03-12 17:11:13 -04:00
|
|
|
checkCallback();
|
|
|
|
|
});
|
2014-03-12 15:05:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function checkCallback() {
|
|
|
|
|
numLocations--;
|
2014-03-17 17:25:22 -04:00
|
|
|
if (numLocations < 0 && callback) {
|
2014-03-12 15:05:07 -04:00
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i in widgetLocations) {
|
|
|
|
|
if (widgetLocations.hasOwnProperty(i)) {
|
2014-03-12 17:11:13 -04:00
|
|
|
renderWidgets(widgetLocations[i]);
|
2014-03-12 15:05:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkCallback();
|
2014-02-28 16:08:25 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-12-07 16:40:14 -05:00
|
|
|
ajaxify.refresh = function() {
|
|
|
|
|
ajaxify.go(ajaxify.currentPage);
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-20 16:01:52 -04:00
|
|
|
$('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');
|
|
|
|
|
|
2013-07-05 10:59:15 -04:00
|
|
|
// Enhancing all anchors to ajaxify...
|
2013-09-20 16:01:52 -04:00
|
|
|
$(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)) {
|
2013-10-06 04:17:06 -04:00
|
|
|
app.previousUrl = window.location.href;
|
2013-11-21 12:28:10 -05:00
|
|
|
}
|
2013-07-05 10:59:15 -04:00
|
|
|
|
2014-02-26 21:55:29 -05:00
|
|
|
if ($(this).attr('data-ajaxify') === 'false') {
|
2013-10-12 17:43:29 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2013-10-28 15:49:12 -04:00
|
|
|
|
2013-12-07 16:18:01 -05:00
|
|
|
if ((!e.ctrlKey && !e.shiftKey) && e.which === 1) {
|
2013-09-22 15:27:10 -04:00
|
|
|
if (this.host === window.location.host) {
|
2013-09-25 11:25:48 -04:00
|
|
|
// Internal link
|
2013-09-22 15:27:10 -04:00
|
|
|
var url = this.href.replace(rootUrl + '/', '');
|
|
|
|
|
|
|
|
|
|
if (ajaxify.go(url)) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
2013-09-25 11:25:48 -04:00
|
|
|
} else if (window.location.pathname !== '/outgoing') {
|
|
|
|
|
// External Link
|
2013-10-01 12:07:58 -04:00
|
|
|
|
2014-01-15 02:33:20 -05:00
|
|
|
if (config.useOutgoingLinksPage) {
|
2013-10-01 12:07:58 -04:00
|
|
|
ajaxify.go('outgoing?url=' + encodeURIComponent(this.href));
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
2013-07-05 10:59:15 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-04-22 19:13:39 +00:00
|
|
|
});
|
|
|
|
|
|
2014-02-26 21:55:29 -05:00
|
|
|
}());
|