// CytoAjax v0.5 // MacKinley Smith (function($){ $.cytoAjaxResponse = function(message,type,time){ if (message==null) message = "Success!"; if (type==null) type = "success"; if (time==null) time = 0; $('.cytoResponseMessage').hide(); $('html,body').animate({scrollTop:0},400); $('.cytoResponseMessage[data-type="'+type+'"]').html(message).show(400,function(){ var $this = $(this); if (time) setTimeout(function(){$this.hide(400);},time); }); }; $.cytoAjax = function(script,data_to_pass,success,error){ // Set default parameters if (typeof script != "string" || script == "") return console.warn("$.cytoAjax requires a string as the first parameter (script)."); if (data_to_pass==null || typeof data_to_pass != "object") { if (typeof data_to_pass == "function") { if (success!=null && typeof success == "function") error = success; success = data_to_pass; } data_to_pass = {}; } // Make request $.ajax({ url:script, type:'POST', data:data_to_pass, success:function(data,textStatus,jqXHR) { try { data = $.parseJSON(data); } catch (e) { data = $.parseJSON('{"status":"Error","message":"AJAX Return Error: '+e.replace(/"/g,"'")+'
'+data.replace(/"/g,"'")+'"}'); } if (data.status=="OK") { if (typeof success == "function") success.apply(this,[data]); else if (data.message!=null) $.cytoAjaxResponse(data.message); else $.cytoAjaxResponse(((typeof success == "string")?success:"Success!")); } else if (data.status=="Error") { if (data.message != null) $.cytoAjaxResponse(data.message,"error"); if (typeof error == "function") error.apply(this,[data]); } }, error:function(jqXHR,textStatus,errorThrown){ if (typeof error == "function") error.apply(this,[{status:"Error",message:errorThrown}]); } }); }; $.CytoAjax = $.cytoAjax; $.Cytoplasm("ready",function(){ // Set styles var vars = $.Cytoplasm("vars"); $.Cytoplasm("addStyles","cytoajax_styles",{ '.cytoResponseMessage':{ display:'none', padding:"10px", margin:"10px", "border-width":"1px", "border-style":"solid", "border-radius":"5px", font:vars.fonts.sizes.small+" "+vars.fonts.faces.light, '&:first-line':{ font:vars.fonts.sizes.normal+" "+vars.fonts.faces.bold }, '&[data-type=success]':{ "border-color":"green", color:"green", background:"linear-gradient(top, white, #a4ffa4)" }, '&[data-type=loading]':{ "border-color":"yellow", color:"#777700", background:"linear-gradient(top, white, #000077)" }, '&[data-type=error]':{ "border-color":"red", color:"red", background:"linear-gradient(top, white, rgb(255,170,170))" } } }); // Setup callback messages $.each(["success","sending","error"],function(i,type){ var $this = $('.cytoResponseMessage[data-type="'+type+'"]'); if (!$this.is('*')) $this = $('

').addClass('cytoResponseMessage').attr("data-type",type).hide().insertAfter($('header')); }); <% [:success,:notice,:error].each do |msg|%><% if flash[msg] %>$.cytoAjaxResponse("<%=flash[msg]%>","<%=msg.to_s%>");<% end end %> }); })(jQuery);