kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
3.9 KiB
149 lines
3.9 KiB
12 years ago
|
#-- encoding: UTF-8
|
||
|
#-- copyright
|
||
12 years ago
|
# OpenProject is a project management system.
|
||
12 years ago
|
#
|
||
12 years ago
|
# Copyright (C) 2012-2013 the OpenProject Team
|
||
12 years ago
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
12 years ago
|
# modify it under the terms of the GNU General Public License version 3.
|
||
12 years ago
|
#
|
||
|
# See doc/COPYRIGHT.rdoc for more details.
|
||
|
#++
|
||
|
require 'singleton'
|
||
|
require 'active_support/descendants_tracker'
|
||
12 years ago
|
|
||
12 years ago
|
module OpenProject
|
||
12 years ago
|
module Themes
|
||
12 years ago
|
class Theme
|
||
|
class SubclassResponsibility < StandardError
|
||
|
end
|
||
|
|
||
|
class << self
|
||
|
include ActiveSupport::DescendantsTracker
|
||
|
|
||
|
def inherited(base)
|
||
|
super # call to ActiveSupport::DescendantsTracker
|
||
|
base.send :include, Singleton # make all theme classes singletons
|
||
|
clear_cache # clear the themes cache
|
||
|
|
||
|
# register the theme's stylesheet manifest with rails' asset pipeline
|
||
|
# we need to wrap the call to #stylesheet_manifest in a Proc,
|
||
|
# because when this code is executed the theme class (base) hasn't had
|
||
|
# a chance to override the method yet
|
||
|
Rails.application.config.assets.precompile << Proc.new {
|
||
|
base.instance.stylesheet_manifest unless base.abstract?
|
||
|
}
|
||
|
end
|
||
|
|
||
|
def new_theme(identifier = nil)
|
||
|
theme = Class.new(self).instance
|
||
12 years ago
|
theme.identifier = identifier
|
||
12 years ago
|
theme
|
||
|
end
|
||
|
|
||
|
def themes
|
||
|
@_themes ||= (descendants - abstract_themes).map(&:instance)
|
||
|
end
|
||
|
alias_method :all, :themes
|
||
|
|
||
|
def registered_themes
|
||
|
@_registered_themes ||= \
|
||
12 years ago
|
themes.each_with_object(Hash.new) do |theme, themes|
|
||
12 years ago
|
themes[theme.identifier] = theme
|
||
|
end
|
||
|
end
|
||
|
delegate :fetch, to: :registered_themes
|
||
12 years ago
|
|
||
12 years ago
|
def clear
|
||
|
direct_descendants.clear && clear_cache
|
||
|
end
|
||
|
|
||
|
def clear_cache
|
||
|
@_themes = @_registered_themes = nil
|
||
|
end
|
||
|
|
||
|
def abstract!
|
||
|
Theme.abstract_themes << self
|
||
|
|
||
|
# undefine methods responsible for creating instances
|
||
12 years ago
|
singleton_class.send :remove_method, *[:new, :allocate, :instance]
|
||
12 years ago
|
end
|
||
|
|
||
|
def abstract?
|
||
|
self.in?(Theme.abstract_themes)
|
||
|
end
|
||
|
|
||
|
def abstract_themes
|
||
|
@_abstract_themes ||= Array.new
|
||
|
end
|
||
|
|
||
|
include Enumerable
|
||
|
delegate :each, to: :themes
|
||
12 years ago
|
end
|
||
|
|
||
12 years ago
|
# 'OpenProject::Themes::GoofyTheme' => :'goofy-theme'
|
||
12 years ago
|
def identifier
|
||
|
@identifier ||= self.class.to_s.demodulize.underscore.dasherize.to_sym
|
||
12 years ago
|
end
|
||
12 years ago
|
attr_writer :identifier
|
||
12 years ago
|
|
||
12 years ago
|
# 'OpenProject::Themes::GoofyTheme' => 'Goofy Theme'
|
||
12 years ago
|
def name
|
||
|
@name ||= self.class.to_s.demodulize.titleize
|
||
12 years ago
|
end
|
||
|
|
||
12 years ago
|
def stylesheet_manifest
|
||
|
"#{identifier}.css"
|
||
12 years ago
|
end
|
||
|
|
||
12 years ago
|
def assets_prefix
|
||
|
identifier.to_s
|
||
12 years ago
|
end
|
||
|
|
||
12 years ago
|
def assets_path
|
||
|
raise SubclassResponsibility, "override this method to point to your theme's assets folder"
|
||
|
end
|
||
|
|
||
|
def overridden_images_path
|
||
|
@overridden_images_path ||= File.join(assets_path, 'images', assets_prefix)
|
||
12 years ago
|
end
|
||
|
|
||
12 years ago
|
def overridden_images
|
||
|
@overridden_images ||= \
|
||
|
begin
|
||
|
Dir.chdir(overridden_images_path) { Dir.glob('**/*') }
|
||
12 years ago
|
rescue Errno::ENOENT # overridden_images_path missing
|
||
12 years ago
|
[]
|
||
|
end.to_set
|
||
12 years ago
|
end
|
||
|
|
||
12 years ago
|
def image_overridden?(source)
|
||
12 years ago
|
source.in?(overridden_images)
|
||
12 years ago
|
end
|
||
|
|
||
|
URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}
|
||
|
|
||
|
def path_to_image(source)
|
||
|
return source if source =~ URI_REGEXP
|
||
|
return source if source[0] == ?/
|
||
|
|
||
|
if image_overridden?(source)
|
||
12 years ago
|
File.join(assets_prefix, source)
|
||
12 years ago
|
else
|
||
|
source
|
||
|
end
|
||
12 years ago
|
end
|
||
12 years ago
|
|
||
|
def default?
|
||
|
false
|
||
|
end
|
||
|
|
||
|
include Comparable
|
||
|
delegate :'<=>', to: :'self.class'
|
||
|
|
||
|
include Singleton
|
||
|
abstract!
|
||
12 years ago
|
end
|
||
|
end
|
||
|
end
|