1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 20:58:30 +01:00

Ajax API enhancements.

This commit is contained in:
Lóna Lore 2016-02-10 22:49:57 +01:00
parent ec414893e2
commit 14aa88d74c

View File

@ -6,6 +6,8 @@ var e107 = e107 || {'settings': {}, 'behaviors': {}};
(function ($) {
e107.callbacks = e107.callbacks || {};
/**
* Attach all registered behaviors to a page element.
*
@ -114,6 +116,169 @@ var e107 = e107 || {'settings': {}, 'behaviors': {}};
e107.attachBehaviors(document, e107.settings);
});
/**
* Behavior to attach a click event to links with .e-ajax class.
*
* @type {{attach: Function}}
*/
e107.behaviors.eAjaxLink = {
attach: function (context, settings) {
$(context).find('a.e-ajax').once('e-ajax-link').each(function () {
$(this).click(function () {
var id = $(this).attr("href");
var target = $(this).attr("data-target"); // support for input buttons etc.
var loading = $(this).attr('data-loading'); // image to show loading.
var nav = $(this).attr('data-nav-inc');
if (nav != null) {
e107.callbacks.eNav(this, '.e-ajax'); //modify data-src value for next/prev. 'from='
}
var src = $(this).attr("data-src");
if (target != null) {
id = '#' + target;
}
if (loading != null) {
$(id).html("<img src='" + loading + "' alt='' />");
}
if (src === null) // old way - href='myscript.php#id-to-target
{
var tmp = src.split('#');
id = tmp[1];
src = tmp[0];
}
// var effect = $(this).attr("data-effect");
// alert(id);
$(id).load(src, function () {
// alert(src);
//$(this).hide();
// $(this).fadeIn();
});
return false;
});
});
}
};
/**
* Behavior to attach a change event to selects with .e-ajax class.
*
* @type {{attach: Function}}
*/
e107.behaviors.eAjaxSelect = {
attach: function (context, settings) {
$(context).find('select.e-ajax').once('e-ajax-select').each(function () {
$(this).on('change', function ()
{
var form = $(this).closest("form").attr('id');
var target = $(this).attr("data-target"); // support for input buttons etc.
var loading = $(this).attr('data-loading'); // image to show loading.
var handler = $(this).attr('data-src');
var method = $(this).attr('data-method');
var data = $('#' + form).serialize();
var $target = $("#" + target);
if(loading != null)
{
$target.html("<img src='" + loading + "' alt='' />");
}
$.ajax({
type: 'post',
url: handler,
data: data,
success: function (data)
{
switch(method)
{
case 'replaceWith':
$target.replaceWith($(data));
break;
case 'append':
$target.append($(data));
break;
case 'prepend':
$target.prepend($(data));
break;
case 'before':
$target.before($(data));
break;
case 'after':
$target.after($(data));
break;
case 'html':
default:
$target.html(data).hide().show("slow");
break;
}
// Attach all registered behaviors to the new content.
e107.attachBehaviors();
}
});
return false;
});
});
}
};
/**
* Dynamic next/prev.
*
* @param e object (eg. from selector)
* @param navid - class with data-src that needs 'from=' value updated. (often 2 of them eg. next/prev)
*/
e107.callbacks.eNav = function (e, navid) {
var src = $(e).attr("data-src");
var inc = parseInt($(e).attr("data-nav-inc"));
var dir = $(e).attr("data-nav-dir");
var tot = parseInt($(e).attr("data-nav-total"));
var val = src.match(/from=(\d+)/);
var amt = parseInt(val[1]);
var oldVal = 'from=' + amt;
var newVal = null;
var sub = amt - inc;
var add = amt + inc;
$(e).show();
if (add > tot) {
add = amt;
// $(e).hide();
}
if (sub < 0) {
sub = 0
}
if (dir == 'down') {
newVal = 'from=' + sub;
}
else {
newVal = 'from=' + add;
}
if (newVal) {
src = src.replace(oldVal, newVal);
$(navid).attr("data-src", src);
}
};
})(jQuery);
$.ajaxSetup({
@ -760,92 +925,7 @@ $(document).ready(function()
$("a.e-ajax").click(function(){
var id = $(this).attr("href");
var target = $(this).attr("data-target"); // support for input buttons etc.
var loading = $(this).attr('data-loading'); // image to show loading.
var nav = $(this).attr('data-nav-inc');
if(nav != null)
{
eNav(this,'.e-ajax'); //modify data-src value for next/prev. 'from='
}
var src = $(this).attr("data-src");
if(target != null)
{
id = '#' + target;
}
if(loading != null)
{
$(id).html("<img src='"+loading+"' alt='' />");
}
if(src === null) // old way - href='myscript.php#id-to-target
{
var tmp = src.split('#');
id = tmp[1];
src = tmp[0];
}
// var effect = $(this).attr("data-effect");
// alert(id);
$(id).load(src,function() {
// alert(src);
//$(this).hide();
// $(this).fadeIn();
});
return false;
});
$("select.e-ajax").on('change', function(){
var form = $(this).closest("form").attr('id');
var target = $(this).attr("data-target"); // support for input buttons etc.
var loading = $(this).attr('data-loading'); // image to show loading.
var handler = $(this).attr('data-src');
var data = $('#'+form).serialize();
if(loading != null)
{
$("#"+target).html("<img src='"+loading+"' alt='' />");
}
$.ajax({
type: 'post',
url: handler,
data: data,
success: function(data)
{
// console.log(data);
$("#"+target).html(data).hide().show("slow");
}
});
return false;
});
// Does the same as externalLinks();
$('a').each(function() {
var href = $(this).attr("href");