Top Menu

Jump to content
Home
    Modules
      • Projects
      • Activity
      • Work packages
      • Gantt charts
      • Calendars
      • Team planners
      • Boards
      • News
    • Getting started
    • Introduction video
      Welcome to OpenProject Community
      Get a quick overview of project management and team collaboration with OpenProject. You can restart this video from the help menu.

    • Help and support
    • Upgrade to Enterprise edition
    • User guides
    • Videos
    • Shortcuts
    • Community forum
    • Enterprise support

    • Additional resources
    • Data privacy and security policy
    • Digital accessibility (DE)
    • OpenProject website
    • Security alerts / Newsletter
    • OpenProject blog
    • Release notes
    • Report a bug
    • Development roadmap
    • Add and edit translations
    • API documentation
  • Sign in
      Forgot your password?

      or sign in with your existing account

      Google

Side Menu

  • Overview
  • Activity
    Activity
  • Roadmap
  • Work packages
    Work packages
  • Gantt charts
    Gantt charts
  • Calendars
    Calendars
  • Team planners
    Team planners
  • Boards
    Boards
  • News
  • Forums

Content

Development
  1. OpenProject
  2. Forums
  3. Development
  4. Include a JS tag in the plugin view

Include a JS tag in the plugin view

Added by micail com over 7 years ago

I am trying to include the JS file in the plugin view. I can not get the right file url, instead-of base_path/assets/file_name.js I have base_path/javascript/file_name.js. I am attaching my code bellow.

index.html.erb

@ <%= javascript_include_tag ‘mega_calendar/jquery-serialize-object.js’, plugin: :openproject_mega_calendar %> @

hooks.rb

module OpenProject::MegaCalendar
  ##
  # Given a hook name as defined in the core the main way to call it is defining
  # a method with the same name in your Hook class (e.g. view_layouts_base_sidebar here).
  #
  # Alternatively you can use the `render_on` helper as shown for the `homescreen_after_links`
  # and the `view_layouts_base_html_head` hooks.
  class Hooks < Redmine::Hook::ViewListener
    # here we render a partial
    render_on :homescreen_after_links, partial: 'hooks/mega_calendar/homescreen_after_links'

    # you can use inline partials as well:
    render_on :view_layouts_base_html_head, inline: <<-VIEW
<% content_for :header_tags do %>
  <%= stylesheet_link_tag 'mega_calendar/jquery-serialize-object.js', plugin: :openproject_mega_calendar %>
  <%= javascript_include_tag 'mega_calendar/jquery-serialize-object.js', plugin: :openproject_mega_calendar %>
<% end %>
VIEW

    ##
    # Any string returned by this method will be rendered at the position of
    # the `view_layouts_base_sidebar` hook. Here we want to use a partial
    # which is why we use the controller's `render_to_string` method.
    #
    # You may also just return a string directly, though.
    def view_layouts_base_sidebar(context={})
      context[:controller].send(:render_to_string, {
        :partial => "hooks/mega_calendar/view_layouts_base_sidebar",
        :locals => context
      })
    end

    ##
    # This is a controller hook. It doesn't render anything. Its return value
    # may be used by the callsite of the hook. Though it's not used most of the time.
    def controller_account_success_authentication_after(context={})
      context[:controller].flash[:MegaCalendar] = "Yay! Welcome #{context[:user].firstname}!"
    end
  end
end

Replies (1)

RE: Include a JS tag in the plugin view - Added by micail com over 7 years ago

I managed to solve it, in :
plugins/openproject-YOUR_PLUGIN_NAME/lib/open_project/YOUR_PLUGIN_NAME/engine.rb

JS or CSS has to be specify by adding it’s path to an array example:

assets %w( mega_calendar/main.css mega_calendar/main.js mega_calendar/jquery-serialize-object.js datetimepicker/jquery.datetimepicker.css datetimepicker/jquery.datetimepicker.js qtip2/jquery.qtip.min.css qtip2/jquery.qtip.min.js fullcalendar/fullcalendar.js fullcalendar/fullcalendar.css fullcalendar/lib/jquery.min.js fullcalendar/lib/moment.min.js fullcalendar/lang-all.js )

Then you can easily include it in any *.html.erb in this plugin by :

<%= javascript_include_tag 'mega_calendar/jquery-serialize-object', plugin: :openproject_mega_calendar %> <%= javascript_include_tag 'datetimepicker/jquery.datetimepicker', plugin: :openproject_mega_calendar %>

  • (1 - 1/1)
Loading...