Skip to content

Commit

Permalink
Revise repeat block view, remove unecessary table wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
guerler committed Aug 10, 2016
1 parent 0052a1c commit 069d44d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 130 deletions.
204 changes: 83 additions & 121 deletions client/galaxy/scripts/mvc/form/form-repeat.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,95 @@
/** This class creates a ui component which enables the dynamic creation of portlets */
define(['utils/utils', 'mvc/ui/ui-table', 'mvc/ui/ui-portlet', 'mvc/ui/ui-misc'],
function(Utils, Table, Portlet, Ui) {
var View = Backbone.View.extend({
initialize : function(options) {
var self = this;
this.options = Utils.merge(options, {
title : 'Section',
empty_text : 'Not available.',
max : null,
min : null
});
this.setElement('<div/>');
define( [ 'utils/utils', 'mvc/ui/ui-table', 'mvc/ui/ui-portlet', 'mvc/ui/ui-misc' ],
function( Utils, Table, Portlet, Ui ) {
var View = Backbone.View.extend({
initialize: function( options ) {
this.list = {};
this.options = Utils.merge( options, {
title : 'Section',
empty_text : 'Not available.',
max : null,
min : null
});
this.button_new = new Ui.ButtonIcon({
icon : 'fa-plus',
title : 'Insert ' + this.options.title_new,
tooltip : 'Add new ' + this.options.title_new + ' block',
floating: 'clear',
onclick : function() { options.onnew && options.onnew() }
});
this.setElement( $( '<div/>' ).append( this.$list = $( '<div/>' ) )
.append( $( '<div/>' ).append( this.button_new.$el ) ) );
},

// create button
this.button_new = new Ui.ButtonIcon({
icon : 'fa-plus',
title : 'Insert ' + this.options.title_new,
tooltip : 'Add new ' + this.options.title_new + ' block',
floating: 'clear',
onclick : function() {
if (options.onnew) {
options.onnew();
}
}
});

// create table
this.table = new Table.View({
cls : 'ui-table-plain',
content : ''
});
this.$el.append(this.table.$el);
this.$el.append($('<div/>').append(this.button_new.$el));

// reset list
this.list = {};
this.n = 0;
},

/** Number of repeat blocks */
size: function() {
return this.n;
},
/** Number of repeat blocks */
size: function() {
return _.size( this.list );
},

/** Add new repeat block */
add: function(options) {
if (!options.id || this.list[options.id]) {
Galaxy.emit.debug('form-repeat::add()', 'Duplicate repeat block id.');
return;
}
this.n++;
var button_delete = new Ui.ButtonIcon({
icon : 'fa-trash-o',
tooltip : 'Delete this repeat block',
cls : 'ui-button-icon-plain',
onclick : function() {
if (options.ondel) {
options.ondel();
}
}
});
var portlet = new Portlet.View({
id : options.id,
title : 'placeholder',
cls : options.cls || 'ui-portlet-repeat',
operations : {
button_delete : button_delete
/** Add new repeat block */
add: function( options ) {
if ( !options.id || this.list[ options.id ] ) {
Galaxy.emit.debug( 'form-repeat::add()', 'Duplicate or invalid repeat block id.' );
return;
}
});
portlet.append(options.$el);
portlet.$el.addClass('section-row');
this.list[options.id] = portlet;
this.table.add(portlet.$el);
this.table.append('row_' + options.id, true);
if (this.options.max > 0 && this.n >= this.options.max) {
this.button_new.disable();
}
this._refresh();
},
var button_delete = new Ui.ButtonIcon({
icon : 'fa-trash-o',
tooltip : 'Delete this repeat block',
cls : 'ui-button-icon-plain',
onclick : function() { options.ondel && options.ondel() }
});
var portlet = new Portlet.View({
id : options.id,
title : 'placeholder',
cls : options.cls || 'ui-portlet-repeat',
operations : { button_delete: button_delete }
});
portlet.append( options.$el );
portlet.$el.addClass( 'section-row' ).hide();
this.list[ options.id ] = portlet;
this.$list.append( portlet.$el.fadeIn( 'fast' ) );
this.options.max > 0 && this.size() >= this.options.max && this.button_new.disable();
this._refresh();
},

/** Delete repeat block */
del: function(id) {
if (!this.list[id]) {
Galaxy.emit.debug('form-repeat::del()', 'Invalid repeat block id.');
return;
}
this.n--;
var table_row = this.table.get('row_' + id);
table_row.remove();
delete this.list[id];
this.button_new.enable();
this._refresh();
},
/** Delete repeat block */
del: function( id ) {
if ( !this.list[ id ] ) {
Galaxy.emit.debug( 'form-repeat::del()', 'Invalid repeat block id.' );
return;
}
this.$list.find( '#' + id ).remove();
delete this.list[ id ];
this.button_new.enable();
this._refresh();
},

/** Remove all */
delAll: function() {
for( var id in this.list ) {
this.del( id );
}
},
/** Remove all */
delAll: function() {
for( var id in this.list ) {
this.del( id );
}
},

/** Hides add/del options */
hideOptions: function() {
this.button_new.$el.hide();
_.each( this.list, function( portlet ) {
portlet.hideOperation('button_delete');
});
if( _.isEmpty( this.list ) ) {
this.$el.append( $('<div/>').addClass( 'ui-form-info' ).html( this.options.empty_text ) );
}
},
/** Hides add/del options */
hideOptions: function() {
this.button_new.$el.hide();
_.each( this.list, function( portlet ) { portlet.hideOperation( 'button_delete' ) } );
_.isEmpty( this.list ) && this.$el.append( $( '<div/>' ).addClass( 'ui-form-info' ).html( this.options.empty_text ) );
},

/** Refresh view */
_refresh: function() {
var index = 0;
for (var id in this.list) {
var portlet = this.list[id];
portlet.title(++index + ': ' + this.options.title);
if (this.n > this.options.min) {
portlet.showOperation('button_delete');
} else {
portlet.hideOperation('button_delete');
/** Refresh view */
_refresh: function() {
var index = 0;
for ( var id in this.list ) {
var portlet = this.list[ id ];
portlet.title( ++index + ': ' + this.options.title );
portlet[ this.size() > this.options.min ? 'showOperation' : 'hideOperation' ]('button_delete');
}
}
}
});

return {
View : View
}
});

return {
View : View
}
});
2 changes: 1 addition & 1 deletion static/maps/mvc/form/form-repeat.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions static/scripts/bundled/analysis.bundled.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion static/scripts/bundled/analysis.bundled.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion static/scripts/bundled/libs.bundled.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion static/scripts/mvc/form/form-repeat.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 069d44d

Please sign in to comment.