Project

General

Profile

Custom JS and CSS plugin

Added by Alexander Sapozhnikov over 13 years ago

Hello!

I wrote plugin which allows to use custom JavaScript and Cascade Stylesheets on certain projects.
You can get/install/clone it from http://github.com/shoorick/redmine_custom_js_and_css

I use hook:

class Hooks  < Redmine::Hook::ViewListener

  ASSETS = File.dirname(__FILE__) + '/../../assets'
  PLUGIN = 'redmine_custom_js_and_css'

  # Custom styles
  def view_layouts_base_html_head(context = { })
    prj = context[:project] || return
    return if prj.nil? 
    id = prj.identifier
    if File.file?( "#{ASSETS}/stylesheets/by_project/#{id}.css" )
      stylesheet_link_tag "by_project/#{id}", :plugin => PLUGIN
    end
  end

  # Call javascript if found corresponding file for every project
  def view_layouts_base_body_bottom(context = { })
    prj = context[:project] || return
    hookname = 'layouts_base_body_bottom'
    if User.current.logged?
      id = prj.identifier
      if File.file?( "#{ASSETS}/javascripts/by_project/#{id}/#{hookname}.js")
        javascript_include_tag "by_project/#{id}/#{hookname}", :plugin => PLUGIN
      end
    end
  end

  # Try to add hook to bottom of issue form
  def view_issues_form_details_bottom(context = { })
    prj = context[:project] || return
    id = prj.identifier
    hookname = 'issues_form_details_bottom'
    if File.file?( "#{ASSETS}/javascripts/by_project/#{id}/#{hookname}.js")
        javascript_include_tag "by_project/#{id}/#{hookname}", :plugin => PLUGIN
    end
  end

end
But I do not like I must write same lines in other defs. I want to write one common code, but I can't get current project's name outside of def. I saw Hooks and Plugin Internals but found no solution. What can I do?

P.S. I don't write in Ruby. I know Perl, CSS and JavaScript.