require 'cytoplasm/version' require 'cytoplasm/railtie' require 'yaml' require 'active_support/core_ext/hash/deep_merge' module Cytoplasm # List of all Cytoplasm variables, with their corresponding default values. # Create a YAML file at config/cytoplasm-config.yml to extend the default values. @defaults = { :setup => { :jqueryui => { :version => "1.9.2", # Must be at least 1.9.2 :theme => "ui-darkness" }, :fontloader => { :googlewebfonts_apikey => "AIzaSyDs7hjZSILIAN3T4oFv3qf_DCyy6PfC30E" } }, :layout => { :mode => "fluid", :padding => "0px", :header => { :margin => "0px", :padding => "10px", :logo => { :enabled => true, :position => "absolute", :align => "left", :x => "0px", :y => "0px" }, :nav => { :enabled => true, :align => "right", :background => "transparent", :font => "light" } }, :section => { :margin => "0px", :padding => "20px" }, :footer => { :margin => "0px", :padding => "10px" }, :plugins => { :cytoButton => { :shadow => "inset 0px 0px 10px rgba(0,0,0,0.5)" } } }, :fonts => { :faces => { :light => "Verdana", :regular => "Verdana", :bold => "Verdana" }, :sizes => { :small => "12px", :normal => "14px", :huge => "48px" }, :heading_factor => "1.8" }, :colors => { :global => { :background => "green", :text => "black", :accent => "#aaff7f" }, :header => { :background => "transparent", :text => "white" }, :section => { :background => "white", :text => "black" }, :footer => { :background => "transparent", :text => "white" }, :links => { :unvisited => { :text => "blue" }, :visited => { :text => "purple" }, :hover => { :text => "red" }, :active => { :text => "yellow" } }, :plugins => { :cytoTable => { :background => "#eeeeee", :border => "1px solid #cccccc", :text => "inherit" }, :cytoButton => { :background => "linear-gradient(to bottom, yellow, green)", :border => "1px solid green", :text => "inherit" }, :cytoSelect => { }, :cytoRadio => { }, :cytoSlider => { }, :cytoUpload => { }, :cytoColorPicker => { } } } } @vars = @defaults @cssvars = "" # Dependencies @dependencies = { :css => [ # "cytoplasm" ], :js => [ 'less-1.3.1.min.js', 'cytoplasm.js', 'jquery.ba-throttle-debounce.min.js', 'jquery.color-2.1.0.min.js', 'cytoAjax', 'cytoSelect', 'cytoRadio', 'cytoSlider', 'cytoUpload', 'cytoColorPicker', 'cytoField', 'cytoTable', 'cytoLogo', 'cytoMenu' ] }; def initialize load_vars_from("config/cytoplasm-config.yml") end def self.load_vars(opts={}) @vars = @defaults @vars.deep_merge!(opts_to_sym(opts)) end def self.opts_to_sym(opts) fixed = {} return opts unless opts.is_a? Hash opts.each {|k,v| fixed[k.to_sym] = ((v.is_a?(Hash)) ? opts_to_sym(v) : v)} return fixed end def self.load_vars_from(path_to_yaml_file) begin vars = YAML::load(IO.read(path_to_yaml_file)) rescue Errno::ENOENT puts "YAML configuration file couldn't be found. Using defaults."; return rescue Psych::SyntaxError puts "YAML configuration file contains invalid syntax. Using defaults."; return end load_vars(vars) compile_css() end def self.save_config(opts={}) puts "SAVING CONFIGURATION FILE" load_vars(opts) File.open("config/cytoplasm-config.yml", 'w+') {|f| f.write(@vars.to_yaml) } end # Getters and setters def self.traverse_hash(hash,which=nil,value=nil) return hash if which.nil? if which.is_a? String and which.include? '.' returnvar = hash levels = [] which.split(".").each do |level| levels << level returnvar = returnvar[level.to_sym] unless returnvar[level.to_sym].nil? end else returnvar = hash[which.to_sym] end unless returnvar.nil? if value.nil? return returnvar.to_s else if which.include? "." else hash[which.to_sym] = value end end end end def self.variable(which=nil,value=nil) traverse_hash(@vars,which,value) end def self.vars(which=nil,value=nil) variable(which,value) end def self.defaults(which=nil,value=nil) traverse_hash(@defaults,which,value) end # Dependency loaders def self.dependencies(type="*") deps = [] valid_types = ["*","css","js"] return false unless valid_types.include? type case type when "*" valid_types.each {|t| dependencies(t).each {|d| deps << d} if t!="*"} when "css" deps << "http://ajax.googleapis.com/ajax/libs/jqueryui/"+vars("setup.jqueryui.version")+"/themes/"+vars("setup.jqueryui.theme")+"/jquery-ui.css" deps += load_fonts() @dependencies[:css].each {|stylesheet| deps << "cytoplasm/"+stylesheet} when "js" @dependencies[:js].each {|plugin| deps << "cytoplasm/"+plugin} end return deps end def self.deps(type="*") return dependencies(type) end def self.css() return dependencies("css") end def self.js() return dependencies("js") end def self.compile_css() varfile = ::Rails.root+"public/cytoplasm/cytoplasm.vars.less" # Check for varfile, create one if it doesn't exist unless File.exists?("public/cytoplasm") Dir.chdir("public") Dir.mkdir("cytoplasm") Dir.chdir("../") end File.open(varfile,"wb") do |f| css = "" @vars.each {|k,v| css += hash_to_css(k,v)} f.write(css) end end def self.hash_to_css(k,v,parent="") output = "" if v.is_a? Hash parent += "#{k}_" v.each {|kk,vv| output += hash_to_css(kk,vv,parent)} return output else k = k.to_s v = v.to_s output = "" output += "@#{parent}#{k}:#{v};\n" if k == "background" and v.starts_with? "linear-gradient" args = v[16..-2].split(/,\s*/) direction = args.shift args = direction+", "+args.reverse().join(", ") output += "@#{parent}#{k}_reverse:linear-gradient(#{args});\n" end return output end end def self.load_fonts sheets = [] enabled = {} begin enabled = YAML::load_file("public/fonts/enabled.yml") rescue puts "Failed to parse YAML in public/fonts/enabled.yml!" end if enabled.is_a? Hash and enabled.any? enabled.each do |dir,fonts| if fonts.is_a? Hash and fonts.any? fonts.each do |fam,variants| if dir == "fontsquirrel" sheets << "/fonts/"+fam+"/stylesheet.css" elsif dir == "googlewebfonts" sheet = "http://fonts.googleapis.com/css?family="+fam sheet += ":" + variants.join(",") if variants.any? sheets << sheet end end end end end return sheets end def self.generate_uid (0...50).map{('a'..'z').to_a[rand(26)]}.join end # Instantiate engine module Rails class Engine < ::Rails::Engine end end end