-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Getting close to guilder object being ready. Added ancillary classes …
…that guiler uses.
- Loading branch information
Jason Harrelson
committed
Feb 28, 2009
1 parent
5c9c17b
commit 22a831d
Showing
18 changed files
with
377 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
$:.unshift(File.dirname(__FILE__)) unless | ||
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) | ||
|
||
require 'guilded/base' | ||
require 'guilded/component_def' | ||
require 'guilded/guilder' | ||
require 'guilded/base/view_helpers' | ||
require 'guilded/exceptions' | ||
|
||
module Guilded | ||
VERSION = '0.0.1' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Guilded | ||
module Base | ||
|
||
# A place to define common stylesheets for all guilded components | ||
COMMON_CSS = [] | ||
|
||
# A place to define common stylesheets for all guilded components | ||
COMMON_JS = [] | ||
|
||
JQUERY_JS = 'jquery/jquery-1.2.6.min.js' | ||
GUILDED_JS = 'guilded/guilded.min.js' | ||
|
||
# The reset stylesheet guilded will use. | ||
RESET_CSS = 'guilded/reset-min.css' | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Guilded | ||
module Base | ||
module ViewHelpers | ||
|
||
# Generates the javascript includes for each Guilded element that is used. Also | ||
# generates the initGuildedElements function and includes a call to each GUIlded | ||
# elements Init method. | ||
# | ||
# Must be called once per rendered page. You can include it just before the closing body | ||
# tag of your application layout. If no Guilded elements were called in the template, the | ||
# call to g_apply_behavior will not output anything. | ||
# | ||
def g_apply_behavior( options={} ) | ||
|
||
@g_elements.each_value do |guilded_def| | ||
#TODO get stylesheet (skin) stuff using rails caching | ||
combine_css_sources( guilded_def.kind, guilded_def.options[:skin], guilded_def.styles ) unless guilded_def.exclude_css? | ||
|
||
# Combine all JavaScript sources so that the caching option can be used on | ||
# the javascript_incldue_tag helper. | ||
combine_js_sources( guilded_def.kind, guilded_def.libs ) unless guilded_def.exclude_js? | ||
end | ||
|
||
# Add default styles | ||
Guilded::Base::COMMON_CSS.each do |style| | ||
@combined_css_srcs.push( style ) unless @combined_css_srcs.include?( style ) | ||
end | ||
|
||
# Add default JavaScripts | ||
Guilded::Base::COMMON_JS.each do |item| | ||
@combined_js_srcs.push( item ) unless @combined_js_srcs.include?( item ) | ||
end | ||
|
||
to_init << stylesheet_link_tag( @combined_css_srcs, :cache => "cache/#{generate_css_cache_name( @combined_js_srcs )}" ) | ||
to_init << javascript_include_tag( @combined_js_srcs, :cache => "cache/#{generate_js_cache_name( @combined_js_srcs )}" ) | ||
|
||
to_init << "<script type=\"text/javascript\">" | ||
to_init << "var initGuildedElements = function(){" | ||
|
||
@g_elements.each_value do |guilded_def| | ||
unless guilded_def.exclude_js? | ||
to_init << " g.#{guilded_def.kind.to_s.camelize( :lower )}Init( #{guilded_def.options.to_json} );" | ||
end | ||
end | ||
|
||
to_init << "}; jQuery('document').ready( initGuildedElements ); </script>" | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Guilded | ||
|
||
class ComponentDef | ||
|
||
attr_reader :kind, :options, :libs, :styles | ||
attr_accessor :additional_js | ||
|
||
def initialize( kind, options={}, libs=[], styles=[], additional_js='' ) | ||
@kind = kind | ||
@options = options | ||
@libs = libs | ||
@styles = styles | ||
@additional_js = additional_js | ||
end | ||
|
||
def exclude_css? | ||
options.include?( :exclude_css ) && ( options[:exclude_css] == 'true' || options[:exclude_css] == true ) | ||
end | ||
|
||
def exclude_js? | ||
options.include?( :exclude_js ) && ( options[:exclude_js] == 'true' || options[:exclude_js] == true ) | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'guilded/exceptions/guilded_exception' | ||
require 'guilded/exceptions/id_missing' | ||
require 'guilded/exceptions/duplicate_element_id' | ||
|
||
module Guilded | ||
module Exceptions | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Guilded | ||
module Exceptions | ||
|
||
class DuplicateElementId < GuildedException | ||
def initialize( id='' ) #:nodoc: | ||
@msg = ":id #{id} for element is already in use on current page" | ||
end | ||
end | ||
|
||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Guilded | ||
module Exceptions | ||
|
||
class GuildedException < RuntimeError | ||
|
||
def initialize( msg="" ) #:nodoc: | ||
@msg = msg | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Guilded | ||
module Exceptions | ||
|
||
class IdMissing < GuildedException | ||
def initialize #:nodoc: | ||
@msg = ":id for element must be present in the options hash" | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Guilded | ||
module Exceptions | ||
|
||
class IdMissingException < GuildedException | ||
def initialize #:nodoc: | ||
@msg = ":id for element must be present in the options hash" | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
module Guilded | ||
class Guilder | ||
|
||
# The folder name for guilded JavaScript files. Must include trailing '/'. | ||
GUILDED_JS_FOLDER = "guilded/" | ||
# The folder name for guilded css files. Must include trailing '/'. | ||
GUILDED_CSS_FOLDER = "guilded/" | ||
GUILDED_NS = "guilded." | ||
|
||
attr_reader :g_elements, :combined_js_srcs, :combined_css_srcs | ||
|
||
def initialize( options={} ) #:nodoc: | ||
@g_elements = Hash.new #unless @g_elements | ||
@combined_js_srcs = Array.new #if @combined_js_srcs.nil? | ||
@combined_css_srcs = Array.new #if @combined_css_srcs.nil? | ||
|
||
# Make sure that the css reset file is first so that other files can override the reset, | ||
# unless the user specified no reset to be included. | ||
@combined_css_srcs.insert( 0, Guilded::Base::RESET_CSS ) unless options[:reset] == false | ||
@combined_js_srcs.insert( 0, Guilded::Base::GUILDED_JS ) unless @combined_js_srcs.include?( Guilded::Base::GUILDED_JS ) | ||
@combined_js_srcs.insert( 0, Guilded::Base::JQUERY_JS ) unless @combined_js_srcs.include?( Guilded::Base::JQUERY_JS ) | ||
#TODO find a way to get the resolve js libs to work on these two so we get the non-min version in development | ||
end | ||
|
||
# Adds an element with its options to the @g_elements hash to be used later. | ||
# | ||
def add_to_inits( element, options={}, libs=[], styles=[] ) | ||
raise Guilded::Exceptions::IdMissing unless options.has_key?( :id ) | ||
raise Guilded::Exceptions::DuplicateElementId( options[:id] ) if @g_elements && @g_elements.has_key?( options[:id] ) | ||
@g_elements = Hash.new unless @g_elements | ||
@g_elements[ options[:id] ] = Guilded::ComponentDef.new( element, options, libs, styles ) | ||
end | ||
|
||
# Generates the markup required to include all the assets necessary for the Guilded compoents in | ||
# @g_elements collection. | ||
# | ||
def apply | ||
to_init = "" | ||
|
||
@g_elements.each_value do |guilded_def| | ||
#TODO get stylesheet (skin) stuff using rails caching | ||
combine_css_sources( guilded_def.kind, guilded_def.options[:skin], guilded_def.styles ) unless guilded_def.exclude_css? | ||
|
||
# Combine all JavaScript sources so that the caching option can be used on | ||
# the javascript_incldue_tag helper. | ||
combine_js_sources( guilded_def.kind, guilded_def.libs ) unless guilded_def.exclude_js? | ||
end | ||
|
||
# Add default styles | ||
Guilded::Base::COMMON_CSS.each do |style| | ||
@combined_css_srcs.push( style ) unless @combined_css_srcs.include?( style ) | ||
end | ||
|
||
# Add default JavaScripts | ||
Guilded::Base::COMMON_JS.each do |script| | ||
@combined_js_srcs.push( script ) unless @combined_js_srcs.include?( script ) | ||
end | ||
|
||
to_init << stylesheet_link_tag( @combined_css_srcs, :cache => "cache/#{generate_css_cache_name( @combined_js_srcs )}" ) | ||
to_init << javascript_include_tag( @combined_js_srcs, :cache => "cache/#{generate_js_cache_name( @combined_js_srcs )}" ) | ||
|
||
to_init << "<script type=\"text/javascript\">" | ||
to_init << "var initGuildedElements = function(){" | ||
|
||
@g_elements.each_value do |guilded_def| | ||
unless guilded_def.exclude_js? | ||
to_init << " g.#{guilded_def.kind.to_s.camelize( :lower )}Init( #{guilded_def.options.to_json} );" | ||
end | ||
end | ||
|
||
to_init << "}; jQuery('document').ready( initGuildedElements ); </script>" | ||
end | ||
|
||
protected | ||
|
||
# Helper method that takes the libs and component specific js files and puts them | ||
# into one array so that the javascript_include_tag can correctly cache them. Automatically | ||
# ignores files that have already been inlcuded. | ||
# | ||
# *parameters* | ||
# combined_src (required) An array of the combined sorces for the page being renderred. | ||
# component (required) The name of a guilded component. | ||
# libs An array of JavaScript libraries that this component depends on. More than likely | ||
# a jQuery plugin, etc. | ||
# | ||
def combine_js_sources( component, libs=[] ) #:doc: | ||
#combined_src = Array.new | ||
resolve_js_libs( libs ) | ||
|
||
comp_src = add_guilded_js_path( component ) | ||
@combined_js_srcs.push( comp_src ) unless @combined_js_srcs.include?( comp_src ) | ||
end | ||
|
||
# Helper method that adds the aditional JavaScript library icludes to the include set. | ||
# | ||
# If running development mode, it will try to remove any .pack, .min, or.compressed | ||
# parts fo the name to try and get the debug version of the library. If it cannot | ||
# find the debug version of the file, it will just remain what was initially provded. | ||
# | ||
def resolve_js_libs( libs ) | ||
if Rails.env.development? | ||
# Try to use an unpacked or unminimized version | ||
libs.each do |lib| | ||
debug_lib = lib.gsub( /.pack/, '' ).gsub( /.min/, '' ).gsub( /.compressed/, '' ) | ||
path = "#{RAILS_ROOT}/public/javascripts/#{debug_lib}" | ||
if File.exist?( path ) | ||
@combined_js_srcs.push( debug_lib ) unless @combined_js_srcs.include?( debug_lib ) | ||
else | ||
@combined_js_srcs.push( lib ) unless @combined_js_srcs.include?( lib ) | ||
end | ||
end | ||
else | ||
libs.each do |lib| | ||
@combined_js_srcs.push( lib ) unless @combined_js_srcs.include?( lib ) | ||
end | ||
end | ||
end | ||
|
||
# Helper method that takes an array of js sources and adds the correct guilded | ||
# path to them. Returns an array with the new path resolved sources. | ||
# | ||
def map_guilded_js_paths( *sources ) | ||
sources.map { |source| add_guilded_js_path( source ) } | ||
end | ||
|
||
# Adds the guilded JS path to the | ||
# | ||
def add_guilded_js_path( source ) | ||
part = "#{GUILDED_JS_FOLDER}#{GUILDED_NS}#{source.to_s}" | ||
ext = 'js' | ||
|
||
if Rails.env.development? | ||
"#{part}.#{ext}" | ||
else | ||
|
||
possibles = { | ||
"#{RAILS_ROOT}/public/javascripts/#{part}.pack.#{ext}" => "#{part}.pack.#{ext}", | ||
"#{RAILS_ROOT}/public/javascripts/#{part}.min.#{ext}" => "#{part}.min.#{ext}", | ||
"#{RAILS_ROOT}/public/javascripts/#{part}.compressed#{ext}" => "#{part}.compressed#{ext}", | ||
"#{RAILS_ROOT}/public/javascripts/#{part}.#{ext}" => "#{part}.#{ext}" | ||
} | ||
|
||
possibles.each do |full_path, part_path| | ||
return part_path if File.exists?( full_path ) | ||
end | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require File.dirname(__FILE__) + '/spec_helper.rb' | ||
|
||
# Time to add your specs! | ||
# http://rspec.info/ | ||
describe "Place your specs here" do | ||
|
||
it "find this spec in spec directory" do | ||
violated "Be sure to write your specs" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require File.dirname(__FILE__) + '/spec_helper.rb' | ||
|
||
# Time to add your specs! | ||
# http://rspec.info/ | ||
describe "Place your specs here" do | ||
|
||
it "find this spec in spec directory" do | ||
violated "Be sure to write your specs" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--colour |
Oops, something went wrong.