Removed ?json in favor of X-Ajax

This commit is contained in:
Marcin Kurczewski 2014-05-15 23:11:53 +02:00
parent a4f7c80fe2
commit 2a6f047c28
7 changed files with 67 additions and 36 deletions

View file

@ -42,8 +42,8 @@ $context->simpleActionName = null;
Assets::setTitle(Core::getConfig()->main->title); Assets::setTitle(Core::getConfig()->main->title);
$context->handleExceptions = false; $context->handleExceptions = false;
$context->json = isset($_GET['json']); $context->layoutName
$context->layoutName = $context->json = isset($_SERVER['HTTP_X_AJAX'])
? 'layout-json' ? 'layout-json'
: 'layout-normal'; : 'layout-normal';
$context->viewName = ''; $context->viewName = '';
@ -87,7 +87,7 @@ catch (SimpleException $e)
catch (Exception $e) catch (Exception $e)
{ {
\Chibi\Util\Headers::setCode(400); \Chibi\Util\Headers::setCode(400);
Messenger::message($e->getMessage()); Messenger::message($e->getMessage(), false);
$context->transport->exception = $e; $context->transport->exception = $e;
$context->transport->queries = \Chibi\Database::getLogs(); $context->transport->queries = \Chibi\Database::getLogs();
$context->viewName = 'error-exception'; $context->viewName = 'error-exception';

View file

@ -20,7 +20,7 @@ $(function()
formDom.addClass('inactive'); formDom.addClass('inactive');
formDom.find(':input').attr('readonly', true); formDom.find(':input').attr('readonly', true);
var url = formDom.attr('action') + '?json'; var url = formDom.attr('action');
var fd = new FormData(formDom[0]); var fd = new FormData(formDom[0]);
var preview = false; var preview = false;
@ -36,7 +36,6 @@ $(function()
data: fd, data: fd,
processData: false, processData: false,
contentType: false, contentType: false,
type: 'POST',
success: function(data) success: function(data)
{ {
@ -51,7 +50,7 @@ $(function()
formDom.find('.preview').hide(); formDom.find('.preview').hide();
var cb = function() var cb = function()
{ {
$.get(window.location.href, function(data) $.get(window.location.href).success(function(data)
{ {
$('.comments-wrapper').replaceWith($(data).find('.comments-wrapper')); $('.comments-wrapper').replaceWith($(data).find('.comments-wrapper'));
$('body').trigger('dom-update'); $('body').trigger('dom-update');
@ -84,7 +83,7 @@ $(function()
} }
}; };
$.ajax(ajaxData); postJSON(ajaxData);
}); });
$('.comment .edit a').bindOnce('edit-comment', 'click', function(e) $('.comment .edit a').bindOnce('edit-comment', 'click', function(e)
@ -100,7 +99,7 @@ $(function()
if (formDom.length == 0) if (formDom.length == 0)
{ {
$.get($(this).attr('href'), function(data) $.get($(this).attr('href')).success(function(data)
{ {
var otherForm = $(data).find('form.edit-comment'); var otherForm = $(data).find('form.edit-comment');
otherForm.hide(); otherForm.hide();

View file

@ -32,6 +32,24 @@ function rememberLastSearchQuery()
} }
//core functionalities, prototypes //core functionalities, prototypes
function getJSON(data)
{
if (typeof(data.headers) === 'undefined')
data.headers = {};
data.headers['X-Ajax'] = '1';
data.type = 'GET';
return $.ajax(data);
};
function postJSON(data)
{
if (typeof(data.headers) === 'undefined')
data.headers = {};
data.headers['X-Ajax'] = '1';
data.type = 'POST';
return $.ajax(data);
};
$.fn.hasAttr = function(name) $.fn.hasAttr = function(name)
{ {
return this.attr(name) !== undefined; return this.attr(name) !== undefined;
@ -83,8 +101,8 @@ $(function()
return; return;
aDom.addClass('inactive'); aDom.addClass('inactive');
var url = $(this).attr('href') + '?json'; var url = $(this).attr('href');
$.post(url).success(function(data) postJSON({ url: url }).success(function(data)
{ {
if (aDom.hasAttr('data-redirect-url')) if (aDom.hasAttr('data-redirect-url'))
window.location.href = aDom.attr('data-redirect-url'); window.location.href = aDom.attr('data-redirect-url');
@ -173,8 +191,13 @@ function split(val)
function retrieveTags(searchTerm, cb) function retrieveTags(searchTerm, cb)
{ {
var options = { search: searchTerm }; var options =
$.getJSON('/tags-autocomplete?json', options, function(data) {
url: '/tags-autocomplete',
data: { search: searchTerm }
};
getJSON(options)
.success(function(data)
{ {
var tags = $.map(data.tags.slice(0, 15), function(tag) var tags = $.map(data.tags.slice(0, 15), function(tag)
{ {
@ -249,8 +272,17 @@ function attachTagIt(target)
{ {
var targetTagit = ui.tag.parents('.tagit'); var targetTagit = ui.tag.parents('.tagit');
var context = target.tagit('assignedTags'); var context = target.tagit('assignedTags');
options = { context: context, tag: ui.tagLabel }; var options =
if (targetTagit.siblings('.related-tags:eq(0)').data('for') == options.tag) {
url: '/tags-related',
data:
{
context: context,
tag: ui.tagLabel
}
};
if (targetTagit.siblings('.related-tags:eq(0)').data('for') == options.data.tag)
{ {
targetTagit.siblings('.related-tags').slideUp(function() targetTagit.siblings('.related-tags').slideUp(function()
{ {
@ -259,7 +291,7 @@ function attachTagIt(target)
return; return;
} }
$.getJSON('/tags-related?json', options, function(data) getJSON(options).success(function(data)
{ {
var list = $('<ul>'); var list = $('<ul>');
$.each(data.tags, function(i, tag) $.each(data.tags, function(i, tag)

View file

@ -15,7 +15,7 @@ function scrolled()
if (pageNext != null && pageNext != pageDone) if (pageNext != null && pageNext != pageDone)
{ {
$(document).data('page-done', pageNext); $(document).data('page-done', pageNext);
$.get(pageNext, [], function(response) $.get(pageNext).success(function(response)
{ {
var dom = $(response); var dom = $(response);
var nextPage = dom.find('.paginator .next:not(.disabled) a').attr('href'); var nextPage = dom.find('.paginator .next:not(.disabled) a').attr('href');

View file

@ -12,9 +12,9 @@ $(function()
aDom.addClass('inactive'); aDom.addClass('inactive');
var enable = !aDom.parents('.post').hasClass('tagged'); var enable = !aDom.parents('.post').hasClass('tagged');
var url = $(this).attr('href') + '?json'; var url = $(this).attr('href');
url = url.replace('_enable_', enable ? '1' : '0'); url = url.replace('_enable_', enable ? '1' : '0');
$.post(url).success(function(data) postJSON({ url: url }).success(function(data)
{ {
aDom.removeClass('inactive'); aDom.removeClass('inactive');
aDom.parents('.post').removeClass('tagged'); aDom.parents('.post').removeClass('tagged');

View file

@ -90,7 +90,7 @@ $(function()
} }
var postDom = posts.first(); var postDom = posts.first();
var url = postDom.find('form').attr('action') + '?json'; var url = postDom.find('form').attr('action');
var fd = new FormData(postDom.find('form').get(0)); var fd = new FormData(postDom.find('form').get(0));
fd.append('file', postDom.data('file')); fd.append('file', postDom.data('file'));
@ -104,7 +104,6 @@ $(function()
processData: false, processData: false,
contentType: false, contentType: false,
dataType: 'json', dataType: 'json',
type: 'POST',
success: function(data) success: function(data)
{ {
postDom.slideUp(function() postDom.slideUp(function()
@ -125,7 +124,7 @@ $(function()
} }
}; };
$.ajax(ajaxData); postJSON(ajaxData);
} }
function uploadFinished() function uploadFinished()
@ -204,7 +203,8 @@ $(function()
if (matches = url.match(/watch.*?=([a-zA-Z0-9_-]+)/)) if (matches = url.match(/watch.*?=([a-zA-Z0-9_-]+)/))
{ {
postDom.find('.file-name strong').text(url); postDom.find('.file-name strong').text(url);
$.getJSON('http://gdata.youtube.com/feeds/api/videos/' + matches[1] + '?v=2&alt=jsonc', function(data) var url = 'http://gdata.youtube.com/feeds/api/videos/' + matches[1] + '?v=2&alt=jsonc';
getJSON({ url: url }).success(function(data)
{ {
postDom.find('.file-name strong') postDom.find('.file-name strong')
.text(data.data.title); .text(data.data.title);

View file

@ -67,7 +67,7 @@ $(function()
$('.comments.unit a.simple-action').data('callback', function() $('.comments.unit a.simple-action').data('callback', function()
{ {
$.get(window.location.href, function(data) $.get(window.location.href).success(function(data)
{ {
$('.comments-wrapper').replaceWith($(data).find('.comments-wrapper')); $('.comments-wrapper').replaceWith($(data).find('.comments-wrapper'));
$('body').trigger('dom-update'); $('body').trigger('dom-update');
@ -76,7 +76,7 @@ $(function()
$('#sidebar a.simple-action').data('callback', function() $('#sidebar a.simple-action').data('callback', function()
{ {
$.get(window.location.href, function(data) $.get(window.location.href).success(function(data)
{ {
$('#sidebar').replaceWith($(data).find('#sidebar')); $('#sidebar').replaceWith($(data).find('#sidebar'));
$('body').trigger('dom-update'); $('body').trigger('dom-update');
@ -97,7 +97,7 @@ $(function()
formDom.addClass('inactive'); formDom.addClass('inactive');
formDom.find(':input').attr('readonly', true); formDom.find(':input').attr('readonly', true);
var url = formDom.attr('action') + '?json'; var url = formDom.attr('action');
var fd = new FormData(formDom[0]); var fd = new FormData(formDom[0]);
var ajaxData = var ajaxData =
@ -112,7 +112,7 @@ $(function()
{ {
disableExitConfirmation(); disableExitConfirmation();
$.get(window.location.href, function(data) $.get(window.location.href).success(function(data)
{ {
$('#sidebar').replaceWith($(data).find('#sidebar')); $('#sidebar').replaceWith($(data).find('#sidebar'));
$('#edit-token').replaceWith($(data).find('#edit-token')); $('#edit-token').replaceWith($(data).find('#edit-token'));
@ -132,7 +132,7 @@ $(function()
} }
}; };
$.ajax(ajaxData); postJSON(ajaxData);
}); });
Mousetrap.bind('a', function() Mousetrap.bind('a', function()