/** * Teeko Framework AJAX Javascript library * * @copyright (C) 2009 Webisti Oy. All rights reserved * @version 1.0 * @author Tomi Tuovinen */ var XHR; function Cursor(pointer) { /* try {*/ document.body.style.cursor = pointer; /*} catch(ex) {}*/ } function Refresh(dest) { var lastAction = $("#"+dest).data('lastaction'); var lastArgs = $("#"+dest).data('lastargs'); Request(lastAction, {'dest':dest, method:'post'}, lastArgs ); } function includeJS(id, script) { if(document.getElementById(id)) return false; var oHead = document.getElementsByTagName('HEAD').item(0); var oScript = document.createElement( "script" ); oScript.language = "javascript"; oScript.type = "text/javascript"; if(id !== false) oScript.id = id; oScript.defer = true; oScript.text = script; oHead.appendChild( oScript ); } function Request(act, parms, args, additional_args, returnXHR) { // Force Change hash cause otherwise visitor clicks link and hash change thinks that // history used and it double click link. // And timeout 100 that hash is changed before force hash.clicked(); if(act === undefined) return false; var args_str = ""; if(typeof parms === 'string') { parms = eval('(' + parms + ')'); } if(typeof args === 'string') { // Check if it is json array if(args.substring(0,1) == '{' && args.substring(args.length-1,args.length) == '}') { args = eval('(' + args + ')'); } else if($('#'+args).length > 0) { // if its element // If its FORM if($('#'+args).attr('tagName') == 'FORM') { //console.log($('#'+args).serialize()); args = $('#'+args).serializeArray(); //console.log(args); } else { var svalue = $('#'+args).attr('value'); var sname = $('#'+args).attr('name'); var sid = $('#'+args).attr('id'); args = '{"'+sname+'":"'+svalue.replace(/(\")/g, '”').replace(/<(.|\n)*?>/g, '')+'"}'; } } } if(parms !== undefined) { var smethod = parms['method']; var destination = parms['dest']; var append = parms['append']; var get_dataType = parms['datatype']; var vasync = parms['async']; var abort = parms['abort']; } if(smethod === undefined) var smethod = 'get'; if(!get_dataType) get_dataType = 'html'; if(abort === true && typeof XHR !== 'undefined' && XHR !== null) { XHR.abort(); XHR = undefined; } if(/*$(document).data('preloader') !== undefined && */typeof XHR !== 'undefined' && XHR !== null) { // DEBUG: Ei voi tehdä kahta loaditemii esim. showssa kun toinen aborttaa toisen // XHR.abort(); // XHR = undefined; // DEBUG: Tässä tulee virhe jostain syystä!! mutta pitää laittaa toimimaan //$(destination).preloader(); } // $("#"+destination).data('lastaction', act); // $("#"+destination).data('lastargs', args); /*try {*/ if(typeof additional_args === 'object') { $.each(additional_args,function(i,item) { args[args.length] = item; }); } if(typeof args === 'object') { //Check is values element id's and change value $.each(args,function(i,item) { if(typeof item !== 'undefined') if(item[0] == '#' && $(item).length > 0) args[i] = $(item).attr('value'); }); /* var arrHash = hash.current(); arrHash = {'name':'settings.hash','value':JSON.stringify(arrHash)}; args[args.length] = arrHash; */ //alert(args); args = JSON.stringify(args); } else { /* var args = new Array(); var arrHash = hash.current(); arrHash = {'name':'settings.hash','value':JSON.stringify(arrHash)}; args[0] = arrHash; args = JSON.stringify(args);*/ } if(typeof args !== 'undefined') { args_str = "args=" + encodeURIComponent($.base64.encode(args)); } var arrHash = hash.current(); var arrStr = ""; $.each(arrHash,function(i,item) { arrStr += item+'/'; }); args_str += "&test="+encodeURIComponent($.base64.encode(arrStr)); var surl = ""; if(act.indexOf('http://') == -1) { surl = 'template/'+act; } else { surl = act; } var success_function = function(data, status) { try { if(data === '') return false; var data = (typeof data === 'object') ? data : eval('(' + data + ')'); if(typeof data === 'object') { if(data.destination !== null) destination = data.destination; if($(destination).length > 0 && typeof destination !== 'undefined') { if(data.css == undefined) data.css = ''; if(data.html == undefined) data.html = ''; if(append !== 'true') $(destination).find('*').remove(); if($(data.element).length > 0) { /// Jos templatessa on määritelty pois lähtö efekti niin käytetään sitä sitten tässä!!! $(data.element).remove(); } $(destination).append(data.html + data.css).show(); if(append !== 'true') $(destination).scrollTop(0); if(data.scrollto) $('body').scrollTo(data.scrollto,200); if(data.element && data.show) { if(data.show =='fade') { $(data.element).hide().fadeIn(1000); } else { $(data.element).hide().delay(500).show(data.show,{},1000); } } if(data.js_classes !== null) { $.each(data.js_classes,function(i,item) { if(item.length > 0) includeJS('external_class_'+i, item); }); } if(data.js_files !== null) { $.each(data.js_files,function(i,item) { if(item.length > 0) includeJS(false, item); }); } includeJS(false, data.js_events); // CHECK ANCHOR and scroll to it var anchor = hash.anchor(); if(typeof anchor !== 'undefined') { var pos = $('[name='+anchor+']').offset().top; var anchor_target = $('[name='+anchor+']').attr('parent'); $(anchor_target).scrollTop(pos); } EmailProtection(); hash.changeLinks(destination); $(destination).find('.draggable').draggable(); data = null; } } else { //Error('CRITICAL','success_function', "Destination: '"+destination+"' doesn't exists"); } preloader.unload(); // If custom callback is defined then eval it if(parms !== undefined) { if(typeof parms['callback'] == 'function') { parms['callback'].call('foo', data); } else if(parms['callback']) { if(parms['callback'].substr(parms['callback'].length - 1) == ')') eval(parms['callback']); else eval(parms['callback']+'()'); } } if(editor.exists() === true) { $.teeko.editable( $(destination) ); } XHR = undefined; } catch(ex) { // Error('CRITICAL','success_function', ex.message); } } switch(smethod) { case 'get': case 'post': Cursor('Wait'); // DEBUG: Tässä tulee virhe jostain syystä!! mutta pitää laittaa toimimaan preloader.load(); break; } if(args_str) surl += "?" + args_str; XHR = $.ajax({ async: vasync, type: smethod, url: surl, // data: args_str, contentType: "text/html; charset=utf-8", dataType: get_dataType, success: success_function, error: function (XMLHttpRequest, textStatus, errorThrown) { // if(XMLHttpRequest.status != 200) // { //Error('CRITICAL','error', "ajax.js .ajax function Error - HTML status code: " + XMLHttpRequest.status); // } } }); Cursor('auto'); /*} catch(ex) { Error('CRITICAL','success_function',ex.message); }*/ }