/** * 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 Request(act, parms, args, copyElementValue, returnXHR) { if(act === undefined) return false; hash.clicked = true; 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') { args = $('#'+args).serializeArray(); } 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']; } if(smethod === undefined) var smethod = 'get'; if(!get_dataType) get_dataType = 'html'; if($(document).data('preloader') !== undefined && XHR !== null) { XHR.abort(); XHR = null; // DEBUG: Tässä tulee virhe jostain syystä!! mutta pitää laittaa toimimaan //$(destination).preloader(); } // $("#"+destination).data('lastaction', act); // $("#"+destination).data('lastargs', args); /*try {*/ if(typeof args === 'object') { args = JSON.stringify(args); } if(typeof args !== 'undefined') { args_str = "args=" + encodeURIComponent($.base64.encode(args)); } var surl = ""; if(act.indexOf('http://') == -1) { // surl = 'index.php?ajax&act='+act; surl = 'template/'+act; } else { surl = act; } // args_str += '&ajax&act='+act; var success_function = function(data, status) { try { if(data === '') return false; hash.clicked = 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') { //$(destination).addClass('editor-container'); if(append == 'true') { $(destination).append(data.css).append(data.html).append(data.js).show(); } else { // $("#"+destination).empty(); /// Transition /* if($(destination).find('*').length > 0) { // $(destination).fadeOut(function() // { $(destination).html(json_data.css).append(json_data.html).append(json_data.js).show(); //}); } else {*/ //$(destination).html(data.html).append(data.css).append(data.js).scrollTop(0).show(); if(data.js == undefined) data.js = ''; if(data.css == undefined) data.css = ''; if(data.html == undefined) data.html = ''; $(destination).html(data.html + data.css + data.js).scrollTop(0).show(); /// DEBUG: uus jquery jostain syystä ei enää kolmannella kerralla // lataa request functiota jos se on laitettu sivustolle niin // että heti alisivuston lataamisessa se ajetaan suoraan.. // eval:illa asia hoitu mutta sitten piti view luokasta ottaa // pois session tarkistus luokkien kannalta et annetaanko luokkaa vai ei // ja firefox alkaa taas viemään paljon muistia //if(data.js !== undefined) eval(data.js); } hash.changeLinks(destination); // DEBUG: tän ehkä vois tehä jotenki toisella tavalla?!?!? ehkä niin // et kun ladataan alussa teeko luokkia niin jos teeko core luokka on ladattu // niin sitte ollaan editorissa? sen vois tehä try catch sisällä niin // et kutsutaan jotain teeko core status metodia ja jos menee catchiin niin ei olla editoris! // Toi teeko.editable:han ei toimi myöskään ilman editoria koska se sijaitsee teeko coressa // ja sitähän ei oo olemassa jos ei oo editoria!! //if($("#site_editor").length > 0) $.teeko.editable($(destination)); /* try {*/ //$.teeko.editable($(destination)); /*} catch(ex){} */ // DEBUG: Tässä tulee virhe jostain syystä!! mutta pitää laittaa toimimaan //$(destination).preloader(); 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(parms['callback']) { if(parms['callback'].substr(parms['callback'].length - 1) == ')') eval(parms['callback']); else eval(parms['callback']+'()'); } } } 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); }*/ }