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
|
|
|
|
2013-09-20 16:01:52 -04:00
|
|
|
(function ($) {
|
|
|
|
|
/*global app, templates, utils*/
|
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-23 19:38:48 +00:00
|
|
|
var current_state = null;
|
2013-04-25 19:24:49 +00:00
|
|
|
var executed = {};
|
2013-04-22 19:27:56 +00:00
|
|
|
|
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) {
|
2013-12-17 21:53:50 -05:00
|
|
|
ajaxify.go(event.state.url, null, 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-02-02 15:10:37 -05:00
|
|
|
$(window).trigger('action:ajaxify.start', { url: url });
|
2014-02-02 15:11:33 -05:00
|
|
|
$('body').trigger('action:ajaxifying', {url: url}); // Deprecated as of v0.4.0
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-23 15:06:31 +08:00
|
|
|
var tpl_url = templates.get_custom_map(url.split('?')[0]);
|
2013-08-23 13:45:57 -04:00
|
|
|
|
2013-05-09 03:33:53 +00:00
|
|
|
if (tpl_url == false && !templates[url]) {
|
2013-09-17 13:04:40 -04:00
|
|
|
if (url === '' || url === '/') {
|
2013-07-10 18:42:38 -04:00
|
|
|
tpl_url = 'home';
|
|
|
|
|
} else {
|
2013-07-23 15:06:31 +08:00
|
|
|
tpl_url = url.split('/')[0].split('?')[0];
|
2013-07-10 18:42:38 -04:00
|
|
|
}
|
2013-08-23 13:45:57 -04:00
|
|
|
|
2013-05-09 03:33:53 +00:00
|
|
|
} else if (templates[url]) {
|
|
|
|
|
tpl_url = 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-19 17:23:17 -05:00
|
|
|
$('#footer, #content').removeClass('hide').addClass('ajaxifying');
|
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-02-19 16:44:36 -05:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
require(['forum/' + tpl_url], function(script) {
|
2013-11-21 12:28:10 -05:00
|
|
|
if (script && script.init) {
|
|
|
|
|
script.init();
|
|
|
|
|
}
|
2013-10-03 15:04:25 -04:00
|
|
|
});
|
2013-05-06 20:23:38 +00:00
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
|
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-19 17:23:17 -05:00
|
|
|
var widgetLocations = [];
|
2013-11-21 12:28:10 -05:00
|
|
|
|
2014-02-19 17:23:17 -05:00
|
|
|
require(['vendor/async'], function(async) {
|
|
|
|
|
$('#content [widget-area]').each(function() {
|
|
|
|
|
widgetLocations.push(this.getAttribute('widget-area'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async.each(widgetLocations, function(location, next) {
|
|
|
|
|
var area = $('#content [widget-area="' + location + '"]');
|
|
|
|
|
|
2014-02-19 19:28:21 -05:00
|
|
|
socket.emit('widgets.render', {template: tpl_url + '.tpl', url: url, location: location}, function(err, renderedWidgets) {
|
2014-02-20 17:57:41 -05:00
|
|
|
area.html(templates.prepare(area.html()).parse({
|
|
|
|
|
widgets: renderedWidgets
|
|
|
|
|
})).removeClass('hidden');
|
2014-02-20 21:41:56 -05:00
|
|
|
|
|
|
|
|
if (!renderedWidgets.length) {
|
|
|
|
|
$('body [no-widget-class]').each(function() {
|
|
|
|
|
this.className = this.getAttribute('no-widget-class');
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-02-20 17:57:41 -05:00
|
|
|
|
2014-02-19 18:15:19 -05:00
|
|
|
next(err);
|
2014-02-19 17:23:17 -05:00
|
|
|
});
|
2014-02-19 18:15:19 -05:00
|
|
|
}, function(err) {
|
|
|
|
|
$('#content, #footer').stop(true, true).removeClass('ajaxifying');
|
|
|
|
|
ajaxify.initialLoad = false;
|
2014-02-19 17:23:17 -05:00
|
|
|
|
2014-02-19 18:15:19 -05:00
|
|
|
app.refreshTitle(url);
|
|
|
|
|
$(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
|
|
|
|
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
|
|
|
|
2013-11-21 12:28:10 -05:00
|
|
|
if (this.getAttribute('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
|
|
|
});
|
|
|
|
|
|
2013-10-06 04:17:06 -04:00
|
|
|
}(jQuery));
|