Project

General

Profile

« Previous | Next » 

Revision c1a8ccf1

Added by Daniel Lobato Garcia over 8 years ago

Fixes #10929 - Add removed widgets to dashboard

This commit allows users to re-add widgets. At this point it's only
possible to recover removed widgets by clicking on "Reset to default".

To do this I populate the 'Add widgets' list by getting a diff
between the current widgets and the available ones. The code calls any
available widget 'default_widget', so keep that in mind.

After removing a widget, we reload the page, which is fairly fast thanks
to turbolinks. This is so that we populate the 'Add widgets' list.

Clicking on the widget to add will trigger a POST call that will add the
widget to User.current.widgets and reload the page. The reason why I
need to reload the page is to get the data in the new widget, otherwise
we would load an empty widget. It's not optimal but turbolinks makes it
fast.

(cherry picked from commit 24fc15c59e0acb589ef46a8f60e8ce76e72fd8d2)

View differences:

app/helpers/dashboard_helper.rb
def dashboard_actions
[_("Generated at %s") % Time.zone.now.to_s(:short),
select_action_button(_("Manage dashboard"), {},
link_to_function(_("Save dashboard"), "save_position('#{save_positions_widgets_path}')"),
link_to(_("Reset to default"), reset_default_widgets_path, :method => :put),
content_tag(:li,'',:class=>'divider'),
content_tag(:li,_("Restore widgets"), :class=>'nav-header', :id=>'restore_list' )
select_action_button(
_("Manage dashboard"), {},
link_to_function(_("Save dashboard"), "save_position('#{save_positions_widgets_path}')"),
link_to(_("Reset to default"), reset_default_widgets_path, :method => :put),
content_tag(:li, '', :class=>'divider'),
content_tag(:li, _("Restore widgets"), :class=>'nav-header', :id=>'restore_list'),
content_tag(:li, '', :class=>'divider'),
content_tag(:li, _("Add widgets"), :class=>'nav-header'),
content_tag(:li, '', :class=>'widget-add') do
widgets_to_add
end
)]
end
def removed_widgets
Dashboard::Manager.default_widgets - User.current.widgets.map(&:to_hash)
end
def widgets_to_add
return link_to(_('Nothing to add'), '#') unless removed_widgets.present?
removed_widgets.each do |removed_widget|
concat(link_to_function(_(removed_widget[:name]),
"add_widget('#{removed_widget[:name]}')"))
end
end
def render_widget(widget)
render(:partial => widget.template, :locals => widget.data)
end

Also available in: Unified diff