Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
1,401 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// dependencies | ||
|
||
// grid model | ||
export default Backbone.Model.extend({ | ||
defaults: { | ||
url_base: "", | ||
async: false, | ||
async_ops: [], | ||
categorical_filters: [], | ||
filters: {}, | ||
sort_key: null, | ||
show_item_checkboxes: false, | ||
advanced_search: false, | ||
cur_page: 1, | ||
num_pages: 1, | ||
operation: undefined, | ||
item_ids: undefined | ||
}, | ||
|
||
/** | ||
* Add filtering criterion. | ||
*/ | ||
add_filter: function(key, value, append) { | ||
// Update URL arg with new condition. | ||
if (append) { | ||
// Update or append value. | ||
var cur_val = this.attributes.filters[key]; | ||
|
||
var new_val; | ||
if (cur_val === null || cur_val === undefined) { | ||
new_val = value; | ||
} else if (typeof cur_val == "string") { | ||
if (cur_val == "All" || cur_val == value) { | ||
new_val = value; | ||
} else { | ||
// Replace string with array. | ||
var values = []; | ||
values[0] = cur_val; | ||
values[1] = value; | ||
new_val = values; | ||
} | ||
} else { | ||
// Current value is an array. | ||
new_val = cur_val; | ||
if (new_val.indexOf(value) === -1) { | ||
new_val.push(value); | ||
} | ||
} | ||
this.attributes.filters[key] = new_val; | ||
} else { | ||
// Replace value. | ||
this.attributes.filters[key] = value; | ||
} | ||
}, | ||
|
||
/** | ||
* Remove filtering criterion. | ||
*/ | ||
remove_filter: function(key, condition) { | ||
var cur_val = this.attributes.filters[key]; | ||
if (cur_val === null || cur_val === undefined) { | ||
return false; | ||
} | ||
|
||
if (typeof cur_val === "string") { | ||
// overwrite/remove condition. | ||
this.attributes.filters[key] = ""; | ||
} else { | ||
// filter contains an array of conditions. | ||
var condition_index = _.indexOf(cur_val, condition); | ||
if (condition_index !== -1) { | ||
cur_val[condition_index] = ""; | ||
} | ||
} | ||
}, | ||
|
||
/** | ||
* Returns URL data for obtaining a new grid. | ||
*/ | ||
get_url_data: function() { | ||
var url_data = { | ||
async: this.attributes.async, | ||
sort: this.attributes.sort_key, | ||
page: this.attributes.cur_page, | ||
show_item_checkboxes: this.attributes.show_item_checkboxes, | ||
advanced_search: this.attributes.advanced_search | ||
}; | ||
|
||
// Add operation, item_ids only if they have values. | ||
if (this.attributes.operation) { | ||
url_data.operation = this.attributes.operation; | ||
} | ||
if (this.attributes.item_ids) { | ||
url_data.id = this.attributes.item_ids; | ||
} | ||
|
||
// Add filter arguments to data, placing "f-" in front of all arguments. | ||
var self = this; | ||
_.each(_.pairs(self.attributes.filters), k => { | ||
url_data[`f-${k[0]}`] = k[1]; | ||
}); | ||
return url_data; | ||
}, | ||
|
||
// Return URL for obtaining a new grid | ||
get_url: function(args) { | ||
return `${this.get("url_base")}?${$.param(this.get_url_data())}&${$.param(args)}`; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** This class renders the grid list with shared section. */ | ||
import GridView from "mvc/grid/grid-view"; | ||
var View = Backbone.View.extend({ | ||
initialize: function(options) { | ||
var self = this; | ||
this.setElement($("<div/>")); | ||
this.model = new Backbone.Model(options); | ||
this.item = this.model.get("item"); | ||
this.title = this.model.get("plural"); | ||
$.ajax({ | ||
url: `${Galaxy.root + this.item}/${this.model.get("action_id")}?${$.param(Galaxy.params)}`, | ||
success: function(response) { | ||
self.model.set(response); | ||
self.render(); | ||
} | ||
}); | ||
}, | ||
|
||
render: function() { | ||
var grid = new GridView(this.model.attributes); | ||
this.$el.empty().append(grid.$el); | ||
this.$el.append(this._templateShared()); | ||
}, | ||
|
||
_templateShared: function() { | ||
var self = this; | ||
var $tmpl = $(`<div><h2>${this.model.get("plural")} shared with you by others</h2></div>`); | ||
var options = this.model.attributes; | ||
if (options.shared_by_others && options.shared_by_others.length > 0) { | ||
var $table = $( | ||
'<table class="colored" border="0" cellspacing="0" cellpadding="0" width="100%">' + | ||
'<tr class="header">' + | ||
"<th>Title</th>" + | ||
"<th>Owner</th>" + | ||
"</tr>" + | ||
"</table>" | ||
); | ||
_.each(options.shared_by_others, (it, index) => { | ||
var display_url = `${Galaxy.root + self.item}/display_by_username_and_slug?username=${ | ||
it.username | ||
}&slug=${it.slug}`; | ||
$table.append( | ||
`<tr><td><a href="${display_url}">${_.escape(it.title)}</a></td><td>${_.escape( | ||
it.username | ||
)}</td></tr>` | ||
); | ||
}); | ||
$tmpl.append($table); | ||
} else { | ||
$tmpl.append(`No ${this.model.get("plural").toLowerCase()} have been shared with you.`); | ||
} | ||
return $tmpl; | ||
} | ||
}); | ||
|
||
export default { | ||
View: View | ||
}; |
Oops, something went wrong.