define(['jquery', 'underscore', 'backbone', 'models/PackageModel', 'views/DownloadButtonView', 'text!templates/downloadContents.html'], function($, _, Backbone, Package, DownloadButtonView, Template) { 'use strict'; var PackageTable = Backbone.View.extend({ template: _.template(Template), type: "PackageTable", tagName : "div", className : "download-contents", events: { "click .expand-control" : "expand", "click .collapse-control" : "collapse" }, initialize: function(options){ if((options === undefined) || (!options)) var options = {}; this.packageId = options.packageId || null; this.memberId = options.memberId || null; this.attributes = options.attributes || null; this.className += options.className || ""; this.currentlyViewing = options.currentlyViewing || null; this.numVisible = options.numVisible || 4; this.parentView = options.parentView || null; this.title = options.title || ""; this.nested = (typeof options.nested === "undefined")? false : options.nested; //Set up the Package model if((typeof options.model === "undefined") || !options.model){ this.model = new Package(); this.model.set("memberId", this.memberId); this.model.set("packageId", this.packageId); } //Get the members if(this.packageId) this.model.getMembers(); else if(this.memberId) this.model.getMembersByMemberID(this.memberId); this.onMetadataView = (this.parentView && this.parentView.type == "Metadata"); this.hasEntityDetails = (this.onMetadataView && (this.model.get("members") && this.model.get("members").length < 150))? this.parentView.hasEntityDetails() : false; this.listenTo(this.model, "changeAll", this.render); }, /* * Creates a table of package/download contents that this metadata doc is a part of */ render: function(){ var view = this, members = this.model.get("members"); //If the model isn't complete, we may be still waiting on a response from the index so don't render anything yet if(!this.model.complete) return false; // Grab all of our URLs var queryServiceUrl = appModel.get('queryServiceUrl'); var packageServiceUrl = appModel.get('packageServiceUrl'); //Start the HTML for the rows var tbody = $(document.createElement("tbody")); //Filter out the packages from the member list members = _.filter(members, function(m){ return(m.type != "Package") }); //Filter the members in order of preferred appearance members = this.sort(members); this.sortedMembers = members; var metadata = this.model.getMetadata(); //Count the number of rows in this table var numRows = members.length; //Cut down the members list to only those that will be visible members = members.slice(0, this.numVisible); this.rowsComplete = false; //Create the HTML for each row _.each(members, function(solrResult){ //Append the row element $(tbody).append(view.getMemberRow(solrResult)); }); var bodyRows = $(tbody).find("tr"); this.numHidden = numRows - this.numVisible; //Draw the footer which will have an expandable/collapsable control if(this.numHidden > 0){ var tfoot = $(document.createElement("tfoot")), tfootRow = $(document.createElement("tr")), tfootCell = $(document.createElement("th")).attr("colspan", "100%"), item = (this.numHidden == 1)? "item" : "items", expandLink = $(document.createElement("a")).addClass("expand-control control").text("Show " + this.numHidden + " more " + item + " in this data set"), expandIcon = $(document.createElement("i")).addClass("icon icon-caret-right icon-on-left"), collapseLink = $(document.createElement("a")).addClass("collapse-control control").text("Show less").css("display", "none"), collapseIcon = $(document.createElement("i")).addClass("icon icon-caret-up icon-on-left"); $(tfoot).append(tfootRow); $(tfootRow).append(tfootCell); $(tfootCell).append(expandLink, collapseLink); $(expandLink).prepend(expandIcon); $(collapseLink).prepend(collapseIcon); } if(bodyRows.length == 0){ tbody.html("