From 2db859ea78608a9262e2e4dc9bdb5f2b28f5bf66 Mon Sep 17 00:00:00 2001 From: Nicolas PROCUREUR Date: Tue, 8 Nov 2016 16:29:34 +0100 Subject: [PATCH] Inject on toolbars --- app/helpers/toolbar_helper.rb | 21 +++++++++++++++++--- lib/ekylibre/plugin.rb | 36 ++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/app/helpers/toolbar_helper.rb b/app/helpers/toolbar_helper.rb index e122a3cad8..5b4323eff0 100644 --- a/app/helpers/toolbar_helper.rb +++ b/app/helpers/toolbar_helper.rb @@ -41,7 +41,7 @@ def export(*natures) modal_id = nature.name.to_s + '-exporting' if Document.of(nature.name, key).any? @template.content_for :popover, @template.render('backend/shared/export', nature: nature, key: key, modal_id: modal_id) - menu.item nature.human_name, '#' + modal_id, data: { toggle: 'modal' } + menu.item nature.human_name, '#' + modal_id, data: {toggle: 'modal'} else DocumentTemplate.of_nature(nature.name).each do |template| menu.item(template.name, @template.params.merge(format: :pdf, template: template.id, key: key)) @@ -78,10 +78,10 @@ def menu(name, options = {}, &block) def destroy(options = {}) if @template.resource if @template.resource.destroyable? - tool(options[:label] || :destroy.ta, { action: :destroy, id: @template.resource.id, redirect: options[:redirect] }, method: :delete, data: { confirm: :are_you_sure_you_want_to_delete.tl }) + tool(options[:label] || :destroy.ta, {action: :destroy, id: @template.resource.id, redirect: options[:redirect]}, method: :delete, data: {confirm: :are_you_sure_you_want_to_delete.tl}) end else - tool(options[:label] || :destroy.ta, { action: :destroy, redirect: options[:redirect] }, { method: :delete }.merge(options.except(:redirect, :label))) + tool(options[:label] || :destroy.ta, {action: :destroy, redirect: options[:redirect]}, {method: :delete}.merge(options.except(:redirect, :label))) end end @@ -112,6 +112,17 @@ def action(name, *args) end end + def view_addons(options = {}) + return nil unless options[:controller].present? + + options[:action] ||= :index + options[:context] = :toolbar + + Ekylibre::Plugin.find_addons(options).collect do |addon| + @template.render partial: addon, locals: {t: self} + end + end + def method_missing(method_name, *args) raise ArgumentError, 'Block can not be accepted' if block_given? options = args.extract_options! @@ -135,6 +146,10 @@ def toolbar(options = {}, &block) end end end + html << capture(toolbar) do |t| + t.view_addons(controller: controller_name, action: action_name).join.html_safe + end + unless options[:wrap].is_a?(FalseClass) html = content_tag(:div, html, class: 'toolbar' + (options[:class] ? ' ' << options[:class].to_s : '')) end diff --git a/lib/ekylibre/plugin.rb b/lib/ekylibre/plugin.rb index 9765208306..0b359a4178 100644 --- a/lib/ekylibre/plugin.rb +++ b/lib/ekylibre/plugin.rb @@ -106,10 +106,9 @@ def generate_themes_stylesheets plugin.themes_assets.each do |name, addons| next unless name == theme || name == '*' || (name.respond_to?(:match) && theme.match(name)) stylesheet << "// #{plugin.name}\n" - next unless addons[:stylesheets] addons[:stylesheets].each do |file| stylesheet << "@import \"#{file}\";\n" - end + end if addons[:stylesheets] end end # /themes//plugins.scss @@ -143,9 +142,25 @@ def after_login_plugin def after_login_path(resource) after_login_plugin.name.to_s.camelize.constantize.after_login_path(resource) end + + def find_addons(options = {}) + return unless options[:context] + + view_addons = [] + search_path = "backend/#{options[:controller]}##{options[:action]}" + + each do |plugin| + availables_addons = plugin.view_addons + view_addons << availables_addons[options[:context]][search_path] if availables_addons.key? options[:context] and availables_addons[options[:context]].key? search_path + end + + view_addons + + end + end - attr_reader :root, :themes_assets, :routes, :javascripts, :initializers + attr_reader :root, :themes_assets, :routes, :javascripts, :initializers, :view_addons field_accessor :name, :summary, :description, :url, :author, :author_url, :version # Links plugin into app @@ -155,6 +170,7 @@ def initialize(plugfile_path) @themes_assets = {}.with_indifferent_access @javascripts = [] @initializers = {} + @view_addons = {} lib = @root.join('lib') if File.directory?(lib) @@ -315,6 +331,20 @@ def add_routes(&block) @routes = block end + def add_toolbar_addon(partial_path, options = {}) + return unless options[:to] + context = :toolbar + @view_addons[context] ||= {} + @view_addons[context][options[:to]] = partial_path + end + + def add_cobble_addon(partial_path, options = {}) + return unless options[:to] + context = :cobble + @view_addons[context] ||= {} + @view_addons[context][options[:to]] = partial_path + end + # Adds menus with DSL in Ekylibre backend nav def extend_navigation(&block) Ekylibre::Navigation.exec_dsl(&block)