Simplify storage handling

This commit is contained in:
guerler
2016-09-01 01:44:07 -04:00
parent 494141c260
commit 61839637f0
4 changed files with 53 additions and 97 deletions
@@ -1,8 +1,8 @@
/**
* Main application class.
*/
define( [ 'mvc/ui/ui-modal', 'mvc/ui/ui-portlet', 'mvc/ui/ui-misc', 'utils/utils', 'plugin/components/storage', 'plugin/components/model', 'utils/deferred', 'plugin/views/viewer', 'plugin/views/editor' ],
function( Modal, Portlet, Ui, Utils, Storage, Chart, Deferred, Viewer, Editor ) {
define( [ 'mvc/ui/ui-modal', 'mvc/ui/ui-portlet', 'mvc/ui/ui-misc', 'utils/utils', 'plugin/components/model', 'utils/deferred', 'plugin/views/viewer', 'plugin/views/editor' ],
function( Modal, Portlet, Ui, Utils, Chart, Deferred, Viewer, Editor ) {
return Backbone.View.extend({
initialize: function( options ) {
var self = this;
@@ -37,14 +37,13 @@ define( [ 'mvc/ui/ui-modal', 'mvc/ui/ui-portlet', 'mvc/ui/ui-misc', 'utils/utils
_build: function( options ) {
this.options = options;
this.modal = parent.Galaxy && parent.Galaxy.modal || new Modal.View();
this.chart = new Chart();
this.storage = new Storage( this.chart, this.types, options );
this.chart = new Chart( options );
this.deferred = new Deferred();
this.viewer = new Viewer( this );
this.editor = new Editor( this );
this.$el.append( this.viewer.$el );
this.$el.append( this.editor.$el );
if ( !this.storage.load() ) {
if ( !this.chart.load() ) {
this.go( 'editor' );
} else {
this.go( 'viewer' );
@@ -1,4 +1,4 @@
define( [ 'utils/utils' ], function( Utils ) {
define( [ 'utils/utils', 'mvc/visualization/visualization-model' ], function( Utils ) {
return Backbone.Model.extend({
defaults : {
id : null,
@@ -13,17 +13,17 @@ define( [ 'utils/utils' ], function( Utils ) {
},
initialize: function( options ) {
this.groups = new Backbone.Collection();
this.settings = new Backbone.Model();
this.definition = {};
this.vis_id = options.id;
this.groups = new Backbone.Collection();
this.settings = new Backbone.Model();
this.definition = {};
},
reset: function( options ) {
this.set({
id : Utils.uid(),
title : 'New Chart',
type : '__first',
dataset_id : options.config.dataset_id,
title : 'New Chart'
dataset_id : this.get( 'config' ).dataset_id
});
this.settings.clear();
this.groups.reset();
@@ -34,6 +34,45 @@ define( [ 'utils/utils' ], function( Utils ) {
this.set( { state : value, state_info : info } );
this.trigger( 'set:state' );
console.debug( 'Chart:state() - ' + info + ' (' + value + ')' );
},
/** Pack and save nested chart model */
save: function() {
var chart_dict = {
attributes : this.attributes,
settings : this.settings.attributes,
groups : []
};
this.groups.each( function( group ) {
chart_dict.groups.push( group.attributes );
});
var vis = new Visualization({
id : this.vis_id,
type : 'charts',
title : this.get( 'title' ) || '',
config : {
dataset_id : this.get( 'config' ).dataset_id,
chart_dict : chart_dict
}
});
vis.save();
},
/** Load nested models/collections from packed dictionary */
load: function() {
var chart_dict = this.get( 'config' ).chart_dict;
if ( chart_dict.attributes ) {
this.set( chart_dict.attributes );
this.state( 'ok', 'Loading saved visualization...' );
this.settings.set( chart_dict.settings );
this.groups.reset();
this.groups.add( chart_dict.groups );
this.set( 'modified', false );
console.debug( 'Model::load() - Loading chart model ' + chart_dict.attributes.type + '.' );
return true;
}
console.debug( 'Model::load() - Chart attributes unavailable.' );
return false;
}
});
});
@@ -1,82 +0,0 @@
/** This class saves and loads a chart through the api. */
define( [ 'utils/utils', 'mvc/ui/ui-modal', 'plugin/components/model', 'mvc/visualization/visualization-model' ], function( Utils, Modal, Chart ) {
return Backbone.Model.extend({
initialize: function( chart, types, options ) {
var self = this;
this.chart = chart;
this.options = options;
this.id = options.id;
this.types = types;
this.modal = parent.Galaxy && parent.Galaxy.modal || new Modal.View();
this.vis = new Visualization({
id : this.id,
type : 'charts',
config : {
dataset_id : this.options.config.dataset_id,
chart_dict : {}
}
});
var chart_dict = this.options.config.chart_dict;
if ( chart_dict ) {
this.vis.get( 'config' ).chart_dict = chart_dict;
}
this.chart.on( 'change:dataset_id_job', function() { self.save() } );
},
/** Pack and save nested chart model */
save: function() {
var self = this;
var chart = this.chart;
var chart_dict = {
attributes : chart.attributes,
settings : chart.settings.attributes,
groups : []
};
chart.groups.each( function( group ) {
chart_dict.groups.push( group.attributes );
});
this.vis.set( 'title', chart.get( 'title' ) || '' );
this.vis.get( 'config' ).chart_dict = chart_dict;
this.vis.save()
.fail(function( xhr, status, message ) {
self.modal.show( { title: 'Saving failed.', body: 'An attempt to save this chart to the server failed. Please try again and contact the administrator.', buttons: { 'Close': function() { self.modal.hide() } } } );
})
.then( function( response ) {
if ( response && response.id ) {
self.id = response.id;
} else {
console.debug( 'Storage::save() - Unrecognized response. Saving may have failed.' );
}
});
},
/** Load nested models/collections from packed dictionary */
load: function() {
var chart_dict = this.vis.get( 'config' ).chart_dict;
if ( !chart_dict.attributes ) {
console.debug( 'Storage::load() - Chart attributes not found. Invalid format.' );
return false;
}
var chart_type = chart_dict.attributes[ 'type' ];
if ( !chart_type ) {
console.debug( 'Storage::load() - Chart type not provided. Invalid format.' );
return false;
}
var chart_definition = this.types[ chart_type ];
if ( !chart_definition ) {
console.debug( 'Storage::load() - Chart type not supported. Please re-configure the chart.' );
return false;
}
this.chart.definition = chart_definition;
this.chart.set( chart_dict.attributes );
this.chart.state( 'ok', 'Loading saved visualization...' );
this.chart.settings.set( chart_dict.settings );
this.chart.groups.reset();
this.chart.groups.add( chart_dict.groups );
this.chart.set( 'modified', false );
console.debug( 'Storage::load() - Loading chart type ' + chart_type + '.' );
return true;
}
});
});
@@ -28,7 +28,7 @@ define( [ 'mvc/ui/ui-tabs', 'mvc/ui/ui-misc', 'mvc/ui/ui-portlet', 'utils/utils'
title : 'Cancel',
onclick : function() {
self.app.go( 'viewer' );
self.app.storage.load();
self.chart.load();
}
})
}
@@ -95,7 +95,7 @@ define( [ 'mvc/ui/ui-tabs', 'mvc/ui/ui-misc', 'mvc/ui/ui-portlet', 'utils/utils'
this.chart.on( 'change:title', function( chart ) { self._refreshTitle() } );
this.chart.on( 'change:type', function( chart ) { self.types.value( chart.get( 'type' ) ) } );
this.chart.on( 'redraw', function( chart ) { self.portlet.showOperation( 'back' ) } );
this.chart.reset( this.app.options );
this.chart.reset();
},
/** Show editor */
@@ -144,7 +144,7 @@ define( [ 'mvc/ui/ui-tabs', 'mvc/ui/ui-misc', 'mvc/ui/ui-portlet', 'utils/utils'
if ( valid ) {
this.app.go( 'viewer' );
this.app.deferred.execute( function() {
self.app.storage.save();
self.chart.save();
self.chart.trigger( 'redraw' );
});
}