require 'cytoplasm/version' require 'cytoplasm/railtie' require 'yaml' require 'active_support/core_ext/hash/deep_merge' module Cytoplasm # List of all Cytoplasm configuration settings, with their corresponding default values. # Create a YAML file at config/initializers/cytoplasm.yml to extend the default settings. @conf = { :jqueryui => { :version => "1.9.2", :theme => "ui-darkness" }, :fontloader => { :directory => "fontsquirrel", :googlewebfonts_apikey => "AIzaSyDs7hjZSILIAN3T4oFv3qf_DCyy6PfC30E" } } # List of all Cytoplasm variables, with their corresponding default values. # Create a YAML file at config/initializers/cytoplasm.vars.yml to extend the default values. @vars = { :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" } }, :content => { :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" }, :content => { :background => "white", :text => "inherit" }, :footer => { :background => "transparent", :text => "white" }, :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 => { } } } } @cssvars = "" # Dependencies @dependencies = { :css => [ "cytoplasm" ], :js => [ 'less-1.3.1.min.js', 'cytoplasm.js', 'jquery.ba-throttle-debounce.min.js', 'jquery.color.js', 'cytoAjax', 'cytoSelect', 'cytoRadio', 'cytoSlider', 'cytoUpload', 'cytoColorPicker', 'cytoField', 'cytoTable', 'cytoLogo' ] }; def initialize configure_with("config/initializers/cytoplasm.yml") load_vars_from("config/initializers/cytoplasm.vars.yml") end # Configure through hash def self.configure(opts={}) opts.each {|k,v| @conf[k.to_sym] = v if @valid_settings.include? k.to_sym} end def self.load_vars(opts={}) opts = optsToSym(opts) @vars.deep_merge!(opts) #opts.each {|k,v| @vars[k.to_sym] = v if @valid_vars.include? k.to_sym} end def self.optsToSym(opts) fixed = {} unless opts.is_a? Hash return opts else opts.each do |k,v| fixed[k.to_sym] = ((v.is_a?(Hash)) ? optsToSym(v) : v) end end return fixed end # Configure through yaml file def self.configure_with(path_to_yaml_file) begin conf = 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 configure(conf) 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 # Getters and setters def self.config(which=nil,value=nil) return @conf if which.nil? if which.is_a? String and which.include? '.' returnvar = @conf levels = [] which.split(".").each do |level| levels << level returnvar = returnvar[level.to_sym] unless returnvar[level.to_sym].nil? end else returnvar = @conf[which.to_sym] end unless returnvar.nil? if value.nil? return returnvar else @conf[which.to_sym] = value end end end def self.variable(which=nil,value=nil) return @vars if which.nil? if which.is_a? String and which.include? '.' returnvar = @vars levels = [] which.split(".").each do |level| levels << level returnvar = returnvar[level.to_sym] unless returnvar[level.to_sym].nil? end else returnvar = @vars[which.to_sym] end unless returnvar.nil? if value.nil? return returnvar else @vars[which.to_sym] = value end end end # Shortcuts to the above def self.conf(which=nil,value=nil) return config(which,value) end def self.vars(which=nil,value=nil) return variable(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 do |t| if t!="*" dependencies(t).each do |d| deps << d end end end when "css" deps << "http://ajax.googleapis.com/ajax/libs/jqueryui/"+conf("jqueryui.version")+"/themes/"+conf("jqueryui.theme")+"/jquery-ui.css" deps += load_fonts() #deps << "cytoplasm/cytoplasm-"+Time.now.to_i.to_s @dependencies[:css].each do |stylesheet| deps << "cytoplasm/"+stylesheet end when "js" @dependencies[:js].each do |plugin| deps << "cytoplasm/"+plugin end 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 do |k,v| css += hash_to_css(k,v) end f.write(css) end end def self.hash_to_css(k,v,parent="") output = "" if v.is_a? Hash parent += "#{k}_" v.each do |kk,vv| output += hash_to_css(kk,vv,parent) end 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 = [] begin enabled = YAML::load_file("public/fonts/enabled.yml") rescue puts "Failed to parse YAML in public/fonts/enabled.yml!" enabled = {} end enabled.each do |dir,fonts| 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 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