/** AA Javascripts library usable on the public pages, just like:
*
*
* (replace "http://actionapps.org/apc-aa" with your server and aa path
*
* @package UserOutput
* @version $Id: aajslib-jquery.php,v 1.4 2006/11/26 21:06:41 honzam Exp $
* @author Honza Malik
* @copyright Econnect, Honza Malik, December 2006
*/
/*
Copyright (C) 2002 Association for Progressive Communications
http://www.apc.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (LICENSE); if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
if (typeof window.AA_GetConf === 'undefined') {
window.AA_GetConf = function(prop) {
if (typeof AA_GetConf.aa_install_path == 'undefined') {
var src = document.querySelector('script[src*="javascript/aajslib"]').getAttribute('src');
AA_GetConf.aa_install_path = src.replace(/javascript\/aajslib.*$/, '');
}
switch (prop) {
case 'path':
return AA_GetConf.aa_install_path;
case 'loader':
return '';
}
}
}
// for backward compatibility - so you can use AA_Config.loader
var AA_Config = {
get AA_INSTAL_PATH() {return AA_GetConf('path');},
get loader() {return AA_GetConf('loader');}
}
// switch current item in gallery
function AA_GalleryGoto(photo_div, viewid, sitemid, galeryid, thumb_id) {
$('div.switcher .active').removeClass('active');
if ($(jqid(thumb_id))) {
$(jqid(thumb_id)).addClass('active');
// $(jqid(thumb_id)).parentNode.scrollTop = $(jqid(thumb_id)).offsetTop - $(jqid(thumb_id)).parentNode.offsetTop - 50;
}
$(jqid(photo_div)).load(AA_GetConf('path') + 'view.php3?vid=' + viewid + '&cmd[' + viewid + ']=x-' + viewid + '-' + sitemid + '&convertto=utf-8&als[GALERYID]=' + galeryid);
}
// now AA specific functions
function AA_HtmlToggle(link_id, link_text_1, div_id_1, link_text_2, div_id_2) {
if ( $(jqid(div_id_1)).is(':visible') ) {
$(jqid(div_id_1)).hide('fast');
$(jqid(div_id_2)).show('fast');
$(jqid(link_id)).html(link_text_2);
$(jqid(link_id)).addClass("is-on");
} else {
$(jqid(div_id_2)).hide('fast');
$(jqid(div_id_1)).show('fast');
$(jqid(link_id)).html(link_text_1);
$(jqid(link_id)).removeClass("is-on");
}
}
function AA_HtmlToggleCss(link_id, link_text_1, link_text_2, selector) {
var link = jqid(link_id);
if ( $(link).hasClass('is-on')) {
$(selector).hide('fast');
$(link).html(link_text_1);
$(link).toggleClass('is-on');
} else {
$(selector).show('fast');
$(link).toggleClass('is-on');
$(link).html(link_text_2);
}
}
function AA_Ajax(div_id, url, param, onload) {
AA_AjaxCss(jqid(div_id), url, param, onload);
}
function AA_AjaxCss(selector, url, param, onload) {
$(selector).hide('fast');
$(selector).html(AA_GetConf('loader'));
$(selector).show('fast');
$(selector).load(url, param, onload);
}
function AA_InsertHtml(into_id, code) {
$(jqid(into_id)).append(code);
}
/** selector_update is optional and is good for updating table rows, where we want to show/hide tr, but update td */
function AA_HtmlAjaxToggleCss(link_id, link_text_1, link_text_2, selector_hide, url, selector_update) {
var link = jqid(link_id);
if ( $(link).hasClass('is-on')) {
$(selector_hide).hide('fast');
$(link).html(link_text_1);
$(link).toggleClass('is-on');
} else {
$(selector_hide).show('fast');
$(link).toggleClass('is-on');
// not loaded from remote url, yet?
if ( !$(link).hasClass('aa-loaded')) {
$(link).addClass('aa-loaded');
AA_AjaxCss(selector_update ? selector_update : selector_hide, url);
}
$(link).html(link_text_2);
}
}
function AA_HtmlAjaxToggle(link_id, link_text_1, div_id_1, link_text_2, div_id_2, url) {
if ( $(jqid(div_id_1)).is(':visible') ) {
$(jqid(div_id_1)).hide('fast');
$(jqid(div_id_2)).show('fast');
// not loaded from remote url, yet?
if ( $(jqid(div_id_2)).attr('data-aa-loaded') != '1') {
$(jqid(div_id_2)).attr('data-aa-loaded', '1');
AA_Ajax(div_id_2, url);
}
$(jqid(link_id)).html(link_text_2);
} else {
$(jqid(div_id_2)).hide('fast');
$(jqid(div_id_1)).show('fast');
$(jqid(link_id)).html(link_text_1);
}
}
/** calls AA responder with permissions of current user and displays returned
* html code into div_id
* Usage:
* FrmSelectEasy('from_slice', $slice_array, $from_slice, 'onchange="DisplayAaResponse(\'fieldselection\', \'Get_Fields\', {slice_id:this.value})"');
* echo '';
**/
function DisplayAaResponse(div_id, method, params) {
AA_AjaxCss(jqid(div_id), AA_GetConf('path') + 'central/responder.php?command='+ method, params);
}
function AA_Response(method, resp_params, ok_func, err_func) {
$.post(AA_GetConf('path') + 'central/responder.php?command='+ method, resp_params, function(data) {
if ( data.substring(0,5) == 'Error' ) {
if (typeof err_func != "undefined") {
err_func(data);
}
} else {
if (typeof ok_func != "undefined") {
ok_func(data);
}
}
});
}
function AA_Refresh(id,inside,ok_func) {
AA_Ajax(id, $(jqid(id)).attr('data-aa-url'));
}
/** Send the form by AJAX and on success displays the ok_html text
* @param id - form id
* @param refresh - id of the html element, which you want to refresh.
* - Such element must have data-aa-url attributes
* @param ok_html - function to call after the page update
* Note, that the form action atribute must be RELATIVE (not with 'http://...')
*/
function AA_SendForm(id, refresh, ok_func) {
var form_id = jqid(id);
// browser supports HTML5 validation
if (typeof $(form_id)[0].checkValidity == 'function') {
if (!$(form_id)[0].checkValidity()) {
// $(form_id)[0].submit();
// AA_StateChange(base_id, 'invalid');
return;
}
}
var url = $(form_id).attr('action');
$(form_id).append(AA_GetConf('loader'));
var code = $(form_id + ' *').serialize();
$.post( url, code).always( function() {
if (typeof refresh != "undefined") {
AA_Refresh(refresh,false,ok_func);
}
})
}
/** Sends the form and replaces the form with the response
* Polls ussage - @see: http://actionapps.org/en/Polls_Module#AJAX_Polls_Design
*/
function AA_AjaxSendForm(form_id, url) {
var filler_url = url || 'modules/polls/poll.php3'; // by default it is used for Polls
if (filler_url.charAt(0)!='/') {
filler_url = AA_GetConf('path') + filler_url; // AA link
}
var valdiv = jqid(form_id);
var code = $(valdiv + ' *').serialize();
$(valdiv).append(AA_GetConf('loader'));
$.post(filler_url, code, function(data) {
$(valdiv).attr("data-aa-edited", "0");
var res = data;
if (typeof data === 'object') { // $.post is parsed if JSON is returned
for (var i in data) {
res = data[i];
break;
}
}
$(valdiv).html(res);
});
}
function displayInput(valdivid, item_id, fid) {
var valdiv = jqid(valdivid);
// already editing ?
switch ($(valdiv).attr('data-aa-edited')) {
case '1': return;
case '2': $(valdiv).attr("data-aa-edited", "0"); // the state 2 is needed for Firefox 3.0 - Storno not works
return;
}
$(valdiv).attr("data-aa-oldval", $(valdiv).html());
var width = $(valdivid).width();
$(valdiv).html(AA_GetConf('loader'));
AA_Response('Get_Widget', { field_id: fid, item_id: item_id }, function(data) {
var valdiv = jqid(valdivid);
$(valdiv).attr('data-aa-edited', '1');
if (width>200) {
data = data.replace('class="ajax_widget aa-ajax-open"', 'class="ajax_widget aa-ajax-open" style="width:'+width+'px;"');
}
$(valdiv).html(data);
var aa_input = $(valdiv).find('select,textarea,input').first();
$(aa_input).focus(); // select the input field (