[#42,view][m]: allow configuring of DataExplorer with views to show.

* TODO: Some issues regarding configuring the FlotGraph from query string
This commit is contained in:
Rufus Pollock
2012-02-17 20:30:26 +00:00
parent 23e46e9c6f
commit a9cfd4412c
2 changed files with 86 additions and 46 deletions

View File

@@ -2,9 +2,26 @@ $(function() {
var $el = $('<div />'); var $el = $('<div />');
$el.appendTo($('.data-explorer-here')); $el.appendTo($('.data-explorer-here'));
var dataset = demoDataset(); var dataset = demoDataset();
var views = [
{
id: 'grid',
label: 'Grid',
view: new recline.View.DataTable({
model: dataset
})
},
{
id: 'graph',
label: 'Graph',
view: new recline.View.FlotGraph({
model: dataset
})
}
];
window.dataExplorer = new recline.View.DataExplorer({ window.dataExplorer = new recline.View.DataExplorer({
el: $el el: $el
, model: dataset , model: dataset
, views: views
}); });
Backbone.history.start(); Backbone.history.start();
setupLoadFromWebstore(function(dataset) { setupLoadFromWebstore(function(dataset) {

View File

@@ -1,30 +1,57 @@
this.recline = this.recline || {}; this.recline = this.recline || {};
this.recline.View = this.recline.View || {}; this.recline.View = this.recline.View || {};
// Views module following classic module pattern
(function($, my) { (function($, my) {
// ## DataExplorer // ## DataExplorer
// //
// The primary view for the entire application. // The primary view for the entire application. Usage:
//
// It should be initialized with a recline.Model.Dataset object and an existing
// dom element to attach to (the existing DOM element is important for
// rendering of FlotGraph subview).
// //
// To pass in configuration options use the config key in initialization hash // <pre>
// e.g. // var myExplorer = new model.recline.DataExplorer({
// model: {{recline.Model.Dataset instance}}
// el: {{an existing dom element}}
// views: {{page views}}
// config: {{config options -- see below}}
// });
// </pre>
// //
// var explorer = new DataExplorer({ // ### Parameters
// config: {...} //
// **:param model:** (required) Dataset instance.
//
// **:param el:** (required) DOM element.
//
// **:param views:** (optional) the views (Grid, Graph etc) for DataExplorer to
// show. This is an array with the key used in routing. If not provided
// just initialize a DataTable with key 'grid'. Example:
//
// <pre>
// var views = [
// {
// id: 'grid',
// label: 'Grid',
// view: new recline.View.DataTable({
// model: dataset
// }) // })
// },
// {
// id: 'graph',
// label: 'Graph',
// view: new recline.View.FlotGraph({
// model: dataset
// })
// }
// ];
// </pre>
// //
// Config options: // **:param config:** Config options like:
// //
// * displayCount: how many documents to display initially (default: 10) // * displayCount: how many documents to display initially (default: 10)
// * readOnly: true/false (default: false) value indicating whether to // * readOnly: true/false (default: false) value indicating whether to
// operate in read-only mode (hiding all editing options). // operate in read-only mode (hiding all editing options).
// //
// All other views as contained in this one. // NB: the element already being in the DOM is important for rendering of
// FlotGraph subview.
my.DataExplorer = Backbone.View.extend({ my.DataExplorer = Backbone.View.extend({
template: ' \ template: ' \
<div class="data-explorer"> \ <div class="data-explorer"> \
@@ -32,8 +59,9 @@ my.DataExplorer = Backbone.View.extend({
\ \
<div class="header"> \ <div class="header"> \
<ul class="navigation"> \ <ul class="navigation"> \
<li class="active"><a href="#grid" class="btn">Grid</a> \ {{#views}} \
<li><a href="#graph" class="btn">Graph</a></li> \ <li><a href="#{{id}}" class="btn">{{label}}</a> \
{{/views}} \
</ul> \ </ul> \
<div class="pagination"> \ <div class="pagination"> \
<form class="display-count"> \ <form class="display-count"> \
@@ -67,15 +95,17 @@ my.DataExplorer = Backbone.View.extend({
this.setReadOnly(); this.setReadOnly();
} }
// Hash of 'page' views (i.e. those for whole page) keyed by page name // Hash of 'page' views (i.e. those for whole page) keyed by page name
this.pageViews = { if (options.views) {
grid: new my.DataTable({ this.pageViews = options.views;
model: this.model, } else {
config: this.config this.pageViews = [{
}) id: 'grid',
, graph: new my.FlotGraph({ label: 'Grid',
model: this.model view: new my.DataTable({
}) model: this.model
}; })
}];
}
// this must be called after pageViews are created // this must be called after pageViews are created
this.render(); this.render();
@@ -123,32 +153,25 @@ my.DataExplorer = Backbone.View.extend({
render: function() { render: function() {
var tmplData = this.model.toTemplateJSON(); var tmplData = this.model.toTemplateJSON();
tmplData.displayCount = this.config.displayCount; tmplData.displayCount = this.config.displayCount;
tmplData.views = this.pageViews;
var template = $.mustache(this.template, tmplData); var template = $.mustache(this.template, tmplData);
$(this.el).html(template); $(this.el).html(template);
var $dataViewContainer = this.el.find('.data-view-container'); var $dataViewContainer = this.el.find('.data-view-container');
_.each(this.pageViews, function(view, pageName) { _.each(this.pageViews, function(view, pageName) {
$dataViewContainer.append(view.el) $dataViewContainer.append(view.view.el)
}); });
}, },
setupRouting: function() { setupRouting: function() {
var self = this; var self = this;
this.router.route('', 'grid', function() { // Default route
self.updateNav('grid'); this.router.route('', this.pageViews[0].id, function() {
self.updateNav(self.pageViews[0].id);
}); });
this.router.route(/grid(\?.*)?/, 'view', function(queryString) { $.each(this.pageViews, function(idx, view) {
self.updateNav('grid', queryString); self.router.route(/^([^?]+)(\?.*)?/, 'view', function(viewId, queryString) {
}); self.updateNav(viewId, queryString);
this.router.route(/graph(\?.*)?/, 'graph', function(queryString) { });
self.updateNav('graph', queryString);
// we have to call here due to fact plot may not have been able to draw
// if it was hidden until now - see comments in FlotGraph.redraw
qsParsed = parseQueryString(queryString);
if ('graph' in qsParsed) {
var chartConfig = JSON.parse(qsParsed['graph']);
_.extend(self.pageViews['graph'].chartConfig, chartConfig);
}
self.pageViews['graph'].redraw();
}); });
}, },
@@ -157,11 +180,11 @@ my.DataExplorer = Backbone.View.extend({
var $el = this.el.find('.navigation li a[href=#' + pageName + ']'); var $el = this.el.find('.navigation li a[href=#' + pageName + ']');
$el.parent().addClass('active'); $el.parent().addClass('active');
// show the specific page // show the specific page
_.each(this.pageViews, function(view, pageViewName) { _.each(this.pageViews, function(view, idx) {
if (pageViewName === pageName) { if (view.id === pageName) {
view.el.show(); view.view.el.show();
} else { } else {
view.el.hide(); view.view.el.hide();
} }
}); });
} }