// CytoPlasm jQuery Setup v0.1 // By MacKinley Smith // Initialize global JS variables var Cytoplasm; (function($){ // Ensure that jQuery and jQuery UI get loaded $.each(["/assets/jquery.js","/assets/jquery-ui.js"],function(i,script){if (!$('script[src="'+script+'"], script[src="'+script+'?body=1"]').is('*')) $('head').append("");}); var fixInputs = function(){$('.cytoField').width("100%").each(function(){$(this).width($(this).width()-16);});}; $(window).resize(fixInputs); $(document).ready(fixInputs); $(document).ready(function(){ // Setup header var header = $("header"); header.find('h1').click(function(){window.location = "<%=((defined? root_url) ? root_url : "/")%>";}); // Setup callback messages $.each(["success","sending","error"],function(i,v){ var $this = $('#'+v+'_message'); if (!$this.is('*')) { $this = $('

').addClass('callback_message').attr("id",v+"_message"); if (header.children(':first-child').is('h1')) $this.insertAfter(header.children(':first-child')); else $this.prependTo(header); $this.hide(); } }); }); })(jQuery); // Cytoplasm plugin (function($){ var defaults = { }; var conf = {}; var vars = {}; var ready = false; var reverseGradient = function(orig){ if (typeof orig != "string") return console.warn("Cytoplasm.reverseGradient only accepts string arguments!"); // Return untouched if not gradient if (orig.indexOf("gradient")==-1) return orig; // Get direction var dir = orig.split("(")[1].split(",")[0]; // Reverse direction var newdir = dir; if (dir.indexOf("top")>-1) newdir = newdir.replace("top","bottom"); else if (dir.indexOf("bottom")>-1) newdir = newdir.replace("bottom","top"); if (dir.indexOf("left")>-1) newdir = newdir.replace("left","right"); else if (dir.indexOf("right")>-1) newdir = newdir.replace("right","left"); return orig.replace(dir,newdir) }; var compileStyles = function(element,styles){ output = ""; output += element+" { "; $.each(styles,function(prop,value){output += (typeof value == "object") ? compileStyles(prop,value) : prop+" : "+value+"; ";}); output += " } "; return output; }; var methods = { init:function(options){ var settings = $.extend(true,{},defaults,options); // Fetch latest settings via AJAX $.cytoAjax("/cytoplasm/settings/fetch",{},function(data){ conf = data.conf; vars = data.vars; methods.setStyles.apply(this); $('body').data('cytoplasm',settings); ready = true; }); }, setStyles:function(){ // Remove existing cytoplasm styles if ($('#cytoplasm_dynamic_styles').is("*")) $('#cytoplasm_dynamic_styles').remove(); // Set variables var boldweight = (vars.fonts.faces.bold==vars.fonts.faces.regular)?"bold":"normal"; var lightweight = (vars.fonts.faces.light==vars.fonts.faces.regular)?"lighter":"normal"; var styletag = ""; var styles = { body:{ background:vars.colors.global.background, font:vars.fonts.sizes.normal+" "+vars.fonts.faces.regular, color:vars.colors.global.text }, '::selection':{ background:vars.colors.global.accent, color:"contrast("+vars.colors.global.accent+",black,white)" }, '::-moz-selection':{ background:vars.colors.global.accent, color:"contrast("+vars.colors.global.accent+",black,white)" }, a:{ "font-family":vars.fonts.faces.light, "&:hover":{color:vars.colors.global.accent} }, 'h1,h3,h5,b,strong':{ "font-family":vars.fonts.faces.bold, "font-weight":boldweight }, 'h2,h4,h6,table.cytoTable,.cytoButton':{ "font-family":vars.fonts.faces.light, "font-weight":lightweight }, header:{ '#logo':{ position:vars.layout.header.logo.position, top:vars.layout.header.logo.y+10 }, h1:{ color:"contrast("+vars.colors.global.background+",black,white)", "&:hover":{ color:vars.colors.global.accent } } }, 'table.cytoTable':{ background:vars.colors.plugins.cytoTable.background, td:{ border:vars.colors.plugins.cytoTable.border, "&.labelcell":{"font-size":vars.fonts.sizes.small} } }, 'input.cytoField':{"font-family":vars.fonts.faces.regular}, '.cytoButton':{ border:vars.colors.plugins.cytoButton.border, color:vars.colors.plugins.cytoButton.text, "&:not(.active),&:not(:active)":{background:vars.colors.plugins.cytoButton.background}, "&.active,&:active":{background:reverseGradient(vars.colors.plugins.cytoButton.background)} } }; // Heading sizes $.each([1,2,3,4,5,6],function(i,num){$('h'+num.toString()).css("font-size",parseInt(vars.fonts.sizes.huge) - (parseInt(vars.fonts.sizes.heading_factor)*i));}); // Logo positioning if (vars.layout.header.logo.align == "center") styles.header["#logo"].left = (($(window).width()-$('header #logo').width())/2) + vars.layout.header.logo.x; else styles.header["#logo"][vars.layout.header.logo.align] = vars.layout.header.logo.x+10; // Compile the styles for HTML and add to $.each(styles,function(e,s){styletag += compileStyles(e,s);}); var parser = new(less.Parser)({}); parser.parse(styletag,function(e,tree){$("").appendTo("head");}); }, conf:function(which,value){ var returnvar; if (which==null) return conf; if (typeof which != "string") return console.warn("Incorrect argument type passed to $.Cytoplasm(\"conf\")!"); if (which.indexOf(".")>-1) { returnvar = conf; var levels = []; $.each(which.split("."),function(i,level){ levels.push(level); if (returnvar[level]!=null) returnvar = returnvar[level]; }); } else returnvar = conf[which]; if (returnvar==null) return console.warn("Failed to find Cytoplasm config variable "+which+"!"); if (value==null) return returnvar; else { if (levels==null || levels==[]) return conf[which] = value; var exp = "conf"; $.each(levels,function(i,level){exp += "["+level.toString()+"]"}); exp += " = "+value.toString(); return eval(exp); } }, vars:function(which,value){ var returnvar; if (which==null) return vars; if (typeof which != "string") return console.warn("Incorrect argument type passed to $.Cytoplasm(\"vars\")!"); if (which.indexOf(".")>-1) { returnvar = vars; var levels = []; $.each(which.split("."),function(i,level){ levels.push(level); if (returnvar[level]!=null) returnvar = returnvar[level]; }); } else returnvar = vars[which]; if (returnvar==null) return console.warn("Failed to find Cytoplasm variable "+which+"!"); if (value==null) { return returnvar; } else { if (levels==null || levels==[]) return vars[which] = value; var exp = "vars"; $.each(levels,function(i,level){exp += "["+level.toString()+"]"}); exp += " = "+value.toString(); return eval(exp); } }, style:function(){ }, refresh:function(){ }, destroy:function(){ $('body').data('cytoplasm',null); }, ready:function(callback){ var interval = setInterval(function(){ if (ready) { callback.apply(); clearInterval(interval) } },10); } }; $.Cytoplasm = function(method){ if (methods[method]) return methods[method].apply(this,Array.prototype.slice.call(arguments,1)); else if (typeof method == 'object' || !method) return methods.init.apply(this,arguments); else $.error('Method ' + method + ' does not exist on jQuery.Cytoplasm!'); }; $(document).ready(function(){$.Cytoplasm()}); })(jQuery);