Permalink
Browse files

first comit

  • Loading branch information...
0 parents commit a4a38c6abb2b8429f3d087faf2dbb6a81aafeb1d @gbaygon committed Apr 11, 2012
Showing with 358 additions and 0 deletions.
  1. 0 README.md
  2. +4 −0 jquery-1.7.1.min.js
  3. +235 −0 jquery.kajax.js
  4. +119 −0 kajax.php
No changes.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,235 @@
+var kajax_registered_error_handler = false;
+
+
+(function( $ ) {
+ $.fn.kajax = function(options) {
+
+ var defaultOptions = {
+ target : '#kajax-target', /*target to load the content*/
+ loading_class : 'kajax-loading', /*override loading div class*/
+ loading_sub:'', /*selector, to insert loading in a subelement*/
+ eval:false,
+ debug: true /*show debug*/
+ };
+
+ var opt = $.extend(defaultOptions, options);
+
+ var LOADING_DIV = '<div class="'+opt.loading_class+'"><div class="'+opt.loading_class+'-image"></div></div>';
+
+
+ function init_pushState()
+ {
+ if(history.pushState)
+ {
+ // needed to recreate the 'first' page with AJAX
+ history.replaceState({ path: window.location.href }, '');
+
+ $(window).bind('popstate', function(event) {
+ // if the event has our history data on it, load the page fragment with AJAX
+ var state = event.originalEvent.state;
+ if (state) {
+ ajax_load(state.path);
+ }
+ });
+ }
+ }
+
+ /*raises a javascript overlay on the page, to show an unrecoverable exception*/
+ function raiseOverlay(content)
+ {
+ if(opt.debug)
+ {
+ $("body").append(
+ $("<div id='overlay'><a href=\"javascript:$('#overlay').remove();\">close</a><pre id='debug_content'></pre></div>")
+ );
+ $("#debug_content").html(content);
+ $("#overlay").css({
+ 'position': 'absolute',
+ 'top': 0,
+ 'right':0,
+ 'left':0,
+ 'overflow': 'hidden',
+ 'backgroundColor':'#000000',
+ 'color':'gray',
+ 'fontFamily':'verdana',
+ 'padding':'40px 100px 40px 100px',
+ 'opacity': 0.8,
+ 'z-index': 5000
+ }
+ );
+ }
+ }
+
+ function htmlspecialchars(string) { return $
+ ('<span>').text(string).html() };
+
+ function error2html(errobj)
+ {
+ var s = '';
+ $.each(
+ errobj, function(k,v)
+ {
+ v = htmlspecialchars(v);
+ if(k!='')
+ {
+ s += '\n---------------\n';
+ s += '<span style="color:red">'+ k + '</span>' + v;
+ }
+ else{
+ s += '<span style="color:red; font-size:2.0em;">'+v+ '</span>';
+ }
+ }
+ )
+ return s;
+ }
+
+ function error2txt(errobj)
+ {
+ var s = '';
+ $.each(
+ errobj, function(k,v)
+ {
+ s += '\n---------------\n';
+ s += k + v;
+ }
+ )
+ return s;
+ }
+
+ function ajax_load( load_url )
+ {
+ if(history.pushState)
+ {
+ where = $(opt.target);
+
+ history.pushState({path:load_url}, '', load_url);
+ beginLoading(where);
+
+ where.load(load_url,{'kajax':true},
+ function(){
+ endLoading(where);
+ }
+ );
+ }
+ else
+ {
+ window.location = load_url;
+ }
+ }
+
+ function ajax_eval(url, data /*this can be null*/, onSuccess /*this can be null*/)
+ {
+ $.post(url,data,
+ function(res)
+ {
+ try
+ {
+ eval(res);
+ }
+ catch(err)
+ {
+ msg = {
+ '':'KAJAX Exception while evaluating response',
+ 'Url: ':url,
+ 'Sent Data: ':decodeURIComponent(data),
+ 'Error Type: ':err.name,
+ 'Debug message: ':err.message,
+ 'Received Script: \n\n':res
+ };
+
+ raiseOverlay(error2html(msg));
+ throw error2txt(msg);
+ }
+ if(onSuccess)
+ {
+ onSuccess();
+ }
+ }
+ );
+ }
+
+
+ function beginLoading(el){
+ if(opt.loading_sub != '')
+ {
+ $(opt.loading_sub,el).append(LOADING_DIV);
+ }
+ else
+ {
+ $(el).append(LOADING_DIV);
+ }
+ }
+
+ function endLoading(el)
+ {
+ $('.'+opt.loading_class, el).remove();
+ }
+
+
+
+ function register_ErrorHandler()
+ {
+ if(kajax_registered_error_handler)
+ {
+ return; //only once
+ }
+
+ /*catches all ajax errors*/
+ $(document).ajaxError(function(event, xhr, ajaxOptions, errorThrown) {
+
+ msg = {
+ '':'KAJAX Error while executing ajax request',
+ 'URL: ':ajaxOptions.url,
+ 'Status: ':xhr.status+' ('+errorThrown+')',
+ 'Response Text: ':xhr.responseText
+ }
+
+ raiseOverlay(error2html(msg));
+ throw error2txt(msg);
+ });
+
+ kajax_registered_error_handler = true;
+ }
+
+ /****/
+ init_pushState();
+ register_ErrorHandler();
+
+ $(document).on('click',this.selector, function(event){
+ var el = $(this);
+ if(el.is('a'))
+ {
+ if(event.which == 1)
+ {
+ var load_url = el.attr('href');
+ if(opt.eval)
+ {
+ ajax_eval(load_url);
+ }
+ else
+ {
+ ajax_load(load_url);
+ }
+ event.preventDefault();
+ }
+ }
+ })
+
+ $(document).on('submit',this.selector, function(event){
+ var el = $(this);
+ if(el.is('form'))
+ {
+ data = el.serialize();
+ url = el.attr('action');
+ beginLoading(el);
+ ajax_eval(url,data, function(){
+ endLoading(el);
+ });
+ event.preventDefault();
+ }
+ })
+
+
+
+ };
+})( jQuery );
119 kajax.php
@@ -0,0 +1,119 @@
+<?php
+
+
+/*------------UTILITIES--------------*/
+
+/*creates a jquery element selector*/
+function jelem($target)
+{
+ return "$('" . $target . "')";
+}
+
+/*creates a jquery method call*/
+function jmethod($target,$func_name,$paramstr)
+{
+ return jelem($target).".".$func_name."(".$paramstr.");";
+}
+
+/*creates a javascritp string*/
+function jstr($str)
+{
+ $str = str_replace( "\n", '', $str );
+ return "'".$str."'";
+}
+
+/*---------------KAJAX----------------*/
+class Kajax
+{
+ protected $buffer = '';
+
+ public function __construct()
+ {
+ }
+
+ /** manually adds script */
+ public function rawscript( $js )
+ {
+ $this->buffer .= $js;
+ }
+
+
+ /*logs an object to the console*/
+ public function olog( $obj )
+ {
+ $this->buffer .= $this->log( json_encode($obj) );
+ }
+
+ /*logs a string to the console*/
+ public function log( $text )
+ {
+ $this->buffer .= sprintf('console.log("%s");', $text);
+ }
+
+ public function html($target, $content)
+ {
+ $this->buffer .= jmethod($target,'html',jstr($content));
+ }
+
+ public function append($target, $content)
+ {
+ $this->buffer .= jmethod($target,'append',jstr($content));
+ }
+
+ public function prepend($target, $content)
+ {
+ $this->buffer .= jmethod($target,'prepend',jstr($content));
+ }
+
+ public function remove($target)
+ {
+ $this->buffer .= jmethod($target,'remove','');
+ }
+
+ public function empty_elem($target)
+ {
+ $this->buffer .= jmethod($target,'empty','');
+ }
+
+ public function toggle($target)
+ {
+ $this->buffer .= jmethod($target,'toggle','');
+ }
+
+ public function attr($target, $attr, $value)
+ {
+ $this->buffer .= jmethod($target,'attr', jstr($attr).','.jstr($value) );
+ }
+
+ public function css($target, $prop, $value)
+ {
+ $this->buffer .= jmethod($target,'css', jstr($prop).','.jstr($value) );
+ }
+
+ public function addClass($target, $class)
+ {
+ $this->buffer .= jmethod($target,'addClass', jstr($class));
+ }
+
+ public function removeClass($target, $class)
+ {
+ $this->buffer .= jmethod($target,'removeClass', jstr($class));
+ }
+
+ public function toggleClass($target, $class)
+ {
+ $this->buffer .= jmethod($target,'toggleClass', jstr($class));
+ }
+
+ public function redirect($url)
+ {
+ $this->buffer .= 'window.location = "'.$url.'"';
+ }
+
+ public function render()
+ {
+ return $this->buffer;
+ }
+}
+
+

0 comments on commit a4a38c6

Please sign in to comment.