Skip to content

Commit

Permalink
Reload listnav explorer trees from explorers using RxJS
Browse files Browse the repository at this point in the history
  • Loading branch information
skateman committed Sep 6, 2017
1 parent b8cfd2e commit 029971b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
14 changes: 12 additions & 2 deletions app/assets/javascripts/controllers/tree_view_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
(function() {
var CONTROLLER_NAME = 'treeViewController';

var TreeViewController = function($http) {
var TreeViewController = function($http, $scope) {
var vm = this;
vm.$http = $http;
vm.$scope = $scope;
vm.selectedNodes = {};
vm.data = {};

ManageIQ.angular.rxSubject.subscribe(function(payload) {
if (payload.reloadTrees && _.isObject(payload.reloadTrees) && Object.keys(payload.reloadTrees).length > 0) {
_.forEach(payload.reloadTrees, function(value, key) {
vm.data[key] = value;
});
vm.$scope.$apply();
}
});

vm.initSelected = function(tree, node) {
vm.selectedNodes[tree] = vm.selectedNodes[tree] || { key: node };
};
Expand Down Expand Up @@ -35,6 +45,6 @@
};
};

TreeViewController.$inject = ['$http'];
TreeViewController.$inject = ['$http', '$scope'];
window.miqHttpInject(angular.module('ManageIQ.treeView')).controller(CONTROLLER_NAME, TreeViewController);
})();
8 changes: 8 additions & 0 deletions app/assets/javascripts/miq_explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ ManageIQ.explorer.updatePartials = function(data) {
}
};

ManageIQ.explorer.reloadTrees = function(data) {
if (_.isObject(data.reloadTrees)) {
sendDataWithRx({reloadTrees: data.reloadTrees});
}
};

ManageIQ.explorer.spinnerOff = function(data) {
if (data.spinnerOff) {
miqSparkle(false);
Expand Down Expand Up @@ -197,6 +203,8 @@ ManageIQ.explorer.processReplaceRightCell = function(data) {

ManageIQ.explorer.replacePartials(data);

ManageIQ.explorer.reloadTrees(data);

if (_.isObject(data.buildCalendar)) { ManageIQ.explorer.buildCalendar(data.buildCalendar); }

if (data.initDashboard) { miqInitDashboardCols(); }
Expand Down
12 changes: 1 addition & 11 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2223,17 +2223,7 @@ def controller_for_common_methods
def replace_trees_by_presenter(presenter, trees)
trees.each_pair do |name, tree|
next unless tree.present?

presenter.replace(
"#{name}_tree_div",
render_to_string(
:partial => 'shared/tree',
:locals => {
:tree => tree,
:name => tree.name.to_s
}
)
)
presenter.reload_tree(name, tree)
end
end

Expand Down
6 changes: 6 additions & 0 deletions app/presenters/explorer_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def initialize(options = {})
:element_updates => {},
:replace_partials => {},
:reload_toolbars => {},
:reload_trees => {},
:exp => {},
:osf_node => '',
:show_miq_buttons => false,
Expand Down Expand Up @@ -141,6 +142,10 @@ def reload_toolbars(toolbars)
self
end

def reload_tree(_name, tree)
@options[:reload_trees][tree.name] = tree.locals_for_render[:bs_tree]
end

def replace(div_name, content)
@options[:replace_partials][div_name] = content
self
Expand Down Expand Up @@ -246,6 +251,7 @@ def for_render_default
data[:updatePartials] = @options[:update_partials] # Replace content of given DOM element (element stays).
data[:updateElements] = @options[:element_updates] # Update element in the DOM with given options
data[:replacePartials] = @options[:replace_partials] # Replace given DOM element (and it's children) (element goes away).
data[:reloadTrees] = @options[:reload_trees] # Replace the data attribute of the given TreeViewComponent
data[:buildCalendar] = format_calendar_dates(@options[:build_calendar])
data[:initDashboard] = !! @options[:init_dashboard]
data[:ajaxUrl] = ajax_action_url(@options[:ajax_action]) if @options[:ajax_action]
Expand Down
5 changes: 3 additions & 2 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@
let(:presenter) { double(:presenter) }

it "calls render and passes data to presenter for each pair w/ value" do
expect(controller).to receive(:render_to_string).with(any_args).twice
expect(presenter).to receive(:replace).with(any_args).twice
allow(tree_1).to receive(:locals_for_render).and_return(:bs_tree => {})
allow(tree_2).to receive(:locals_for_render).and_return(:bs_tree => {})
expect(presenter).to receive(:reload_tree).with(any_args).twice
controller.send(:replace_trees_by_presenter, presenter, trees)
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/miq_policy_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@
end

describe '#replace_right_cell' do
it 'should replace policy_tree_div when replace_trees contains :policy' do
it 'should reload policy tree when reload_trees contains :policy_tree' do
allow(controller).to receive(:params).and_return(:action => 'whatever')
controller.instance_eval { @sb = {:active_tree => :policy_tree} }
allow(controller).to receive(:render).and_return(nil)
presenter = ExplorerPresenter.new(:active_tree => :policy_tree)

controller.send(:replace_right_cell, :nodetype => 'root', :replace_trees => [:policy], :presenter => presenter)
expect(presenter[:replace_partials]).to have_key('policy_tree_div')
expect(presenter[:reload_trees]).to have_key(:policy_tree)
end

it 'should not hide center toolbar while doing searches' do
Expand Down

0 comments on commit 029971b

Please sign in to comment.