Skip to content

Commit

Permalink
Push logic from sidebar into child views.
Browse files Browse the repository at this point in the history
Moving logic into the views that actually need/use the search and home
controls.
  • Loading branch information
mitechie authored and Kyle Adams committed Apr 25, 2014
1 parent 36fdddb commit 2bab254
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 197 deletions.
36 changes: 10 additions & 26 deletions app/subapps/browser/browser.js
Expand Up @@ -419,18 +419,15 @@ YUI.add('subapp-browser', function(Y) {
if (detailsNode) { detailsNode.hide(); }
// If there is no provided metadata show the defaults.
if (!metadata || !metadata.search) {
this._sidebar.showSearch();
this.renderEditorial();
}
if (metadata && metadata.search) {
this._sidebar.showSearch();
this.renderSearchResults();
}
if (metadata && metadata.id) {
// The entity rendering views need to handle the new state format
// before this can be hooked up.
if (detailsNode) { detailsNode.show(); }
this._sidebar.showSearch();
this.renderEntityDetails();
}
},
Expand Down Expand Up @@ -633,25 +630,29 @@ YUI.add('subapp-browser', function(Y) {
*/
renderSearchResults: function(req, res, next) {
var container = this.get('container'),
extraCfg = {};
extraCfg = {},
metadata;

extraCfg.renderTo = container.one('.bws-content');

// If there's a selected charm we need to pass that info onto the View
// to render it selected.
if (window.flags && window.flags.il) {
extraCfg.activeID = this.state.getState('current', 'sectionA', 'id');
var metadata = this.state.getState('current', 'sectionA', 'metadata');
extraCfg.query = metadata.search;
this._sidebar.set('withHome', true);
metadata = this.state.getState('current', 'sectionA', 'metadata');
} else {
if (this.state.getCurrent('charmID')) {
extraCfg.activeID = this.state.getCurrent('charmID');
}
}

this._search = new views.BrowserSearchView(
this._getViewCfg(extraCfg));
var viewCfg = this._getViewCfg(extraCfg);
if (window.flags && window.flags.il) {
viewCfg.filters.text = metadata.search;
}

this._search = new views.BrowserSearchView(viewCfg);
this._search.set('withHome', true);

// Prepare to handle cache
this._search.on(this._search.EV_CACHE_UPDATED, function(ev) {
Expand Down Expand Up @@ -1029,23 +1030,6 @@ YUI.add('subapp-browser', function(Y) {
req.params = {};
}

// Support redirecting the minimized view.
if (req.params.viewmode === 'minimized') {
// This setTimeout is required because the double dispatch events
// happen in an unpredictable order so we simply let them complete
// then navigate away to avoid issues where we are trying to render
// while other views are in the middle of being torn down.
setTimeout(function() {
self.fire('viewNavigate', {
change: {
viewmode: 'sidebar'
}
});
}, 0);
return;
}


if (!req.params.viewmode) {
req.params.viewmode = 'sidebar';
}
Expand Down
4 changes: 3 additions & 1 deletion app/subapps/browser/views/charmresults.js
Expand Up @@ -45,7 +45,7 @@ YUI.add('subapp-browser-charmresults', function(Y) {
@extends {juju.browser.views.CharmResults}
@event EV_CACHE_UPDATED when the cache has been updated this is fired
*/
ns.CharmResults = Y.Base.create('browser-view-charmresults', Y.View, [
ns.CharmResults = Y.Base.create('browser-view-charmresults', ns.MainView, [
views.utils.apiFailingView,
widgets.browser.IndicatorManager,
Y.Event.EventTracker
Expand Down Expand Up @@ -261,10 +261,12 @@ YUI.add('subapp-browser-charmresults', function(Y) {
}, '0.1.0', {
requires: [
'base',
'browser-search-widget',
'browser-overlay-indicator',
'event-tracker',
'juju-models',
'juju-view-utils',
'subapp-browser-mainview',
'view'
]
});
4 changes: 4 additions & 0 deletions app/subapps/browser/views/editorial.js
Expand Up @@ -173,6 +173,10 @@ YUI.add('subapp-browser-editorial', function(Y) {
*/
render: function(cachedResults) {
var store = this.get('store');

var tplNode = Y.one('.charmbrowser .bws-header');
this._renderSearchWidget(tplNode);
this._bindSearchWidgetEvents();
this.showIndicator(this.get('renderTo'));

// By default we grab the editorial content from the api to use for
Expand Down
4 changes: 4 additions & 0 deletions app/subapps/browser/views/search.js
Expand Up @@ -149,6 +149,10 @@ YUI.add('subapp-browser-searchview', function(Y) {
@method render
*/
render: function(cachedResults) {
var tplNode = Y.one('.charmbrowser .bws-header');
this._renderSearchWidget(tplNode);
this._bindSearchWidgetEvents();

this.showIndicator(this.get('renderTo'));
// This is only rendered once from the subapp and so the filters is
// the initial set from the application. All subsequent renders go
Expand Down
39 changes: 1 addition & 38 deletions app/subapps/browser/views/sidebar.js
Expand Up @@ -38,7 +38,7 @@ YUI.add('subapp-browser-sidebar', function(Y) {
* @extends {juju.browser.views.MainView}
*
*/
ns.Sidebar = Y.Base.create('browser-view-sidebar', ns.MainView, [], {
ns.Sidebar = Y.Base.create('browser-view-sidebar', Y.View, [], {
template: views.Templates.sidebar,

events: {
Expand All @@ -54,29 +54,6 @@ YUI.add('subapp-browser-sidebar', function(Y) {
this.get('container').setHTML('');
},

/**
Shows the sidebar search widget and removes the class on the sidebar
container.
@method showSearch
*/
showSearch: function() {
this.search.show();
this.get('container').removeClass('no-search');
},

/**
Hides the sidebar search widget and adds the class on the sidebar
container.
@method hideSearch
*/
hideSearch: function() {
this.search.hide();
// addClass() is idempotent.
this.get('container').addClass('no-search', true);
},

/**
* Render out the view to the DOM.
*
Expand All @@ -87,24 +64,12 @@ YUI.add('subapp-browser-sidebar', function(Y) {
var tpl = this.template(this.getAttrs()),
tplNode = Y.Node.create(tpl);

if (window.flags && window.flags.il) {
// Render then immediately hide the search widget to allow the state
// to control the show/hide of the search widget.
this._renderSearchWidget(tplNode);
this.search.hide();
} else {
this._renderSearchWidget(tplNode);
}


if (typeof container !== 'object') {
container = this.get('container');
} else {
this.set('container', container);
}
container.setHTML(tplNode);
// Bind our view to the events from the search widget used for controls.
this._bindSearchWidgetEvents();
}

}, {
Expand All @@ -113,9 +78,7 @@ YUI.add('subapp-browser-sidebar', function(Y) {

}, '0.1.0', {
requires: [
'browser-search-widget',
'juju-templates',
'subapp-browser-mainview',
'view'
]
});
3 changes: 1 addition & 2 deletions app/subapps/browser/views/view.js
Expand Up @@ -29,7 +29,6 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
YUI.add('subapp-browser-mainview', function(Y) {
var ns = Y.namespace('juju.browser.views'),
models = Y.namespace('juju.models'),
views = Y.namespace('juju.views'),
widgets = Y.namespace('juju.widgets');

/**
Expand Down Expand Up @@ -188,7 +187,7 @@ YUI.add('subapp-browser-mainview', function(Y) {
filters: this.get('filters'),
withHome: this.get('withHome')
});
this.search.render(node.one('.bws-header'));
this.search.render(node);
}
},

Expand Down
1 change: 0 additions & 1 deletion app/widgets/charm-search.js
Expand Up @@ -677,7 +677,6 @@ YUI.add('browser-search-widget', function(Y) {
'event-valuechange',
'juju-browser-models',
'juju-templates',
'juju-views',
'widget'
]
});
125 changes: 7 additions & 118 deletions test/test_browser_app.js
Expand Up @@ -63,117 +63,6 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
delete window.juju_config;
});

it('must correctly render the initial browser ui', function(done) {
var container = Y.one('#subapp-browser');
view = new Sidebar({
container: container,
store: new Y.juju.charmworld.APIv3({
apiHost: 'http://localhost'
})
});

// mock out the data source on the view so that it won't actually make a
// request.
var emptyData = {
responseText: Y.JSON.stringify({
result: {
'new': [],
slider: []
}
})
};

// Override the store to not call the dummy localhost address.
view.get('store').set(
'datasource',
new Y.DataSource.Local({source: emptyData}));
view.render();

// And the hide button is rendered to the container node.
assert.isTrue(Y.Lang.isObject(container.one('#bws-sidebar')));
// Also verify that the search widget has rendered into the view code.
assert.isTrue(Y.Lang.isObject(container.one('input')));

// The home buttons are not visible by default.
assert.isFalse(view.get('withHome'));
assert.isTrue(container.one('.browser-nav').hasClass('hidden'));

// Yet changing the attribute triggers it to go.
view.search.showHome = function() {
// The only way to exit the test is that we hit this callback bound to
// the change event we trigger below.
done();
};
view.set('withHome', true);
});

it('shows the home icon when instructed', function() {
var container = Y.one('#subapp-browser');
view = new Sidebar({
store: new Y.juju.charmworld.APIv3({
apiHost: 'http://localhost'
}),
withHome: true
});

// mock out the data source on the view so that it won't actually make a
// request.
var emptyData = {
responseText: Y.JSON.stringify({
result: {
'new': [],
slider: []
}
})
};

// Override the store to not call the dummy localhost address.
view.get('store').set(
'datasource',
new Y.DataSource.Local({source: emptyData}));
view.render(container);

// The home buttons are not visible by default.
assert.isTrue(view.get('withHome'));
assert.isFalse(container.one('.browser-nav').hasClass('hidden'));

});

it('routes home when it catches a gohome event', function(done) {
var container = Y.one('#subapp-browser'),
fakeStore = new Y.juju.charmworld.APIv3({});
view = new Sidebar({
store: fakeStore
});
view.on('viewNavigate', function(ev) {
assert.equal(ev.change.search, false);
assert.equal(ev.change.filter.clear, true);
done();
});

view.render(container);
view.search._onHome({
halt: function() {}
});
});

it('picks up the search widget deploy event', function(done) {
var container = utils.makeContainer(this, 'subapp-browser'),
fakeStore = new Y.juju.charmworld.APIv3({});
view = new Sidebar({
charmID: 'precise/jenkins-13',
store: fakeStore
});

view._deployEntity = function() {
container.remove(true);
done();
};

view.render(container);
view.search.fire(view.search.EVT_DEPLOY);
});

});
})();

Expand Down Expand Up @@ -235,16 +124,16 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
context._cleanups.push(searchStub.reset);
entityStub = utils.makeStubMethod(app, 'renderEntityDetails');
context._cleanups.push(entityStub.reset);
showSearchStub = utils.makeStubMethod(app._sidebar, 'showSearch');
context._cleanups.push(showSearchStub.reset);
}

function assertions(
editorialCount, searchCount, entityCount, showSearchCount) {
assert.equal(editorialStub.callCount(), editorialCount);
assert.equal(searchStub.callCount(), searchCount);
assert.equal(entityStub.callCount(), entityCount);
assert.equal(showSearchStub.callCount(), showSearchCount);
editorialCount, searchCount, entityCount) {
assert.equal(editorialStub.callCount(), editorialCount,
'editorial not rendered');
assert.equal(searchStub.callCount(), searchCount,
'search not rendered');
assert.equal(entityStub.callCount(), entityCount,
'entity details not rendered');
}

it('renders the editorial when no metadata is provided', function() {
Expand Down
7 changes: 5 additions & 2 deletions test/test_browser_editorial.js
Expand Up @@ -43,8 +43,11 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
beforeEach(function() {
container = utils.makeContainer(this, 'container');
var testcontent = [
'<div id=testcontent><div class="bws-view-data">',
'</div><div class="bws-content"></div></div>'
'<div id="testcontent" class="charmbrowser">',
'<div class="bws-view-data"></div>',
'<div class="bws-header"></div>',
'<div class="bws-content"></div>',
'</div>'
].join();

Y.Node.create(testcontent).appendTo(container);
Expand Down

0 comments on commit 2bab254

Please sign in to comment.