Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

* Abstracted out list component. List now handles all bank functionality that does: -> Rendering -> Sorting -> Visibility -> Scroll Bars -> Add/Remove * Mostly integrated with bank.js

Joshi503dcbff0a33
diff --git a/src/index.html b/src/index.html
index 348a11e5b..c74f70327 100644
--- a/src/index.html
+++ b/src/index.html
@@ -125,6 +125,7 @@
<!-- link.dev --> <script type="text/javascript" src="js/imageDownload.js"></script>
<!-- link.dev --> <script type="text/javascript" src="js/menuPicker.js"></script>
<!-- link.dev --> <script type="text/javascript" src="js/accordionPicker.js"></script>
+ <!-- link.dev --> <script type="text/javascript" src="js/list.js"></script>
<!-- link.dev --> <script type="text/javascript" src="js/bank.js"></script>
<!-- link.dev --> <script type="text/javascript" src="js/switch.js"></script>
<!-- link.dev --> <script type="text/javascript" src="js/selector.js"></script>
diff --git a/src/js/bank.js b/src/js/bank.js
index 56c7389f0..30b014a51 100644
--- a/src/js/bank.js
+++ b/src/js/bank.js
@@ -58,19 +58,14 @@ SOTE.widget.Bank = function(containerId, config){
this.state = config.state;
this.selected = config.selected;
- this.values = this.unserialize(this.selected[this.state]);
this.dataSourceUrl = config.dataSourceUrl;
this.title = config.title;
this.callback = config.callback;
this.selector = config.selector;
- this.meta = new Object;
+ this.data = new Object;
this.buildMetaDone = false;
- /*this.meta["MODIS_Terra_CorrectedReflectance_TrueColor"] = {label:"Corrected Reflectance (True Color)",sublabel:"Terra / MODIS"};
- this.meta["fires48"] = {label:"Fires (Past 48 Hours)",sublabel:"MODIS Fire and Thermal Anomalies"};
- this.meta["AIRS_Dust_Score"] = {label:"Dust Score",sublabel:"Aqua / AIRS"};
- this.meta["OMI_Aerosol_Index"] = {label:"Aerosol Index",sublabel:"Aura / OMI"};*/
this.categories = config.categories;
this.initRenderComplete = false;
this.dataSourceUrl = config.dataSourceUrl;
@@ -80,9 +75,8 @@ SOTE.widget.Bank = function(containerId, config){
this.queryString = "";
this.noFireVal = null;
this.init();
- //this.updateComponent(this.id+"=baselayers.MODIS_Terra_CorrectedReflectance_TrueColor-overlays.fires48.AIRS_Dust_Score.OMI_Aerosol_Index")
-}
+};
SOTE.widget.Bank.prototype.buildMeta = function(cb,val){
this.buildMetaDone = false;
@@ -92,29 +86,33 @@ SOTE.widget.Bank.prototype.buildMeta = function(cb,val){
callback: cb,
val: val
});
- /*
- SOTE.util.getJSON(
- "data/" + this.state + "_" + this.dataSourceUrl,
- {self:this,callback:cb,val:val},
- SOTE.widget.Bank.handleMetaSuccess,
- SOTE.widget.Bank.handleUpdateFailure
- );
- */
+
};
SOTE.widget.Bank.handleMetaSuccess = function(data,status,xhr,args){
var self = args.self;
+ var dupes = new Object;
+ var count = 0;
+
for(var i in data){
if(i != "palettes") {
- for(var j=0; j<data[i].length; j++){
-
- self.meta[data[i][j].value] = {label:data[i][j].label,sublabel:data[i][j].sublabel,type:data[i][j].type};
-
+
+ for(var j=0; j<data[i].length; j++){
+ if(!self.data[data[i][j].category]){
+ self.data[data[i][j].category] = new Object;
+ self.data[data[i][j].category].title = self.categories[count++];
+ self.data[data[i][j].category].items = [];
+ }
+
+ if(! (data[i][j].value in dupes) ){
+ self.data[data[i][j].category].items.push({label:data[i][j].label,sublabel:data[i][j].sublabel,value:data[i][j].value});
+ dupes[data[i][j].value] = 1;
+ }
}
}
}
- $.each(self.meta, function(name, meta) {
+ /*$.each(self.meta, function(name, meta) {
if ( name in self.config.layers ) {
var layer = self.config.layers[name];
if ( "rendered" in layer ) {
@@ -130,13 +128,12 @@ SOTE.widget.Bank.handleMetaSuccess = function(data,status,xhr,args){
meta.palette = self.config.palettes[layer.rendered];
}
}
- });
+ });*/
+
if(args.callback){
args.callback({self:self,val:args.val});
}
else{
- self.values = null;
- self.values = self.unserialize(self.selected[self.state]);
self.render();
self.fire();
}
@@ -175,129 +172,33 @@ SOTE.widget.Bank.prototype.init = function(){
*/
SOTE.widget.Bank.prototype.render = function(){
- this.container.innerHTML = "";
+ $(this.container).empty();
-/* var tabs = document.createElement("ul");
- tabs.innerHTML = "<li><a href='#bank' class='activetab'>Active</a></li><li><a class='addlayerstab' href='#selectorbox'>Add Layers</a></li>";
- this.container.appendChild(tabs);
- */
var tabs_height = $(".ui-tabs-nav").outerHeight(true);
$('#'+this.id).addClass('bank');
- $('#'+this.id).height($('#'+this.id).parent().outerHeight() - tabs_height); /*var container = document.createElement("div");
- container.setAttribute("id","bank");
- container.setAttribute("class","bank");*/
-
- //var titleContainer = document.createElement("div");
- //titleContainer.setAttribute("class","tabContainer");
- //var title = document.createElement("h2");
- //title.innerHTML = "Active"; //this.title;
- /*var ext = document.createElement("a");
- ext.setAttribute("id","callSelectorLink");
- ext.setAttribute("class","callSelectorLink");
- ext.setAttribute("title","Show Product Listing");
- */
- //titleContainer.appendChild(title);
- //titleContainer.appendChild(ext);
+ $('#'+this.id).height($('#'+this.id).parent().outerHeight() - tabs_height);
- //container.appendChild(titleContainer);
+ this.list = new SOTE.widget.List(this.id,{
+ data: this.data,
+ selected: this.selected[this.state]
+ });
- //$('#'+this.id).delegate('.callSelectorLink','click',{selector:this.selector},this.callback);
+ $("#"+this.id).bind("listchange", {self:this}, SOTE.widget.Bank.handleFire);
- for(var i=0; i<this.categories.length; i++){
- var formattedCategoryName = this.categories[i].replace(/\s/g, "");
- var category = document.createElement("ul");
- category.setAttribute("id",formattedCategoryName.toLowerCase());
- category.setAttribute("class",this.id+"category category");
-
- var categoryContainer = document.createElement("div");
- categoryContainer.setAttribute("id",this.id + formattedCategoryName);
- categoryContainer.setAttribute("class","categoryContainer");
-
- var categoryTitle = document.createElement("h3");
- categoryTitle.setAttribute("class","head");
- categoryTitle.innerHTML = this.categories[i];
-
- categoryContainer.appendChild(categoryTitle);
-
-
- if(this.values !== null && this.values[formattedCategoryName.toLowerCase()]){
- for(var j=0; j<this.values[formattedCategoryName.toLowerCase()].length; j++){
- var myVal = this.values[formattedCategoryName.toLowerCase()][j].value
- var item = document.createElement("li");
- item.setAttribute("id",formattedCategoryName.toLowerCase()+"-"+myVal);
- item.setAttribute("class",this.id+"item item");
- item.innerHTML = "<a><img class='close bank-item-img' id='close"+myVal.replace(/:/g,"colon")+"' title='Remove Layer' src='images/close-red-x.png' /></a>";
- if(this.meta !== null && this.meta[myVal]){
- if(myVal in this.hidden){
- item.innerHTML += "<a class='hdanchor'><img class='hide hideReg bank-item-img' title='Show Layer' id='hide"+myVal.replace(/:/g,"colon")+"' src='images/invisible.png' /></a>";
- }
- else {
- item.innerHTML += "<a class='hdanchor'><img class='hide hideReg bank-item-img' title='Hide Layer' id='hide"+myVal.replace(/:/g,"colon")+"' src='images/visible.png' /></a>";
- }
- item.innerHTML += "<h4>"+this.meta[myVal].label+"</h4>";
- item.innerHTML += "<p>"+this.meta[myVal].sublabel+"</p>";
- var m = this.meta[myVal];
-
- if(m && m.palette){
- var paletteString = "<div><span class='palette'><span class='p-min' style='margin-right:10px;'>"+m.min+"</span>" +
- "<canvas class='colorBar' id='canvas"+this.values[formattedCategoryName.toLowerCase()][j].value+"' width=100px height=14px'></canvas>" +
- "<span class='p-max' style='margin-left:10px;'>"+m.max+"</span>";
- if(m.units && m.units != ""){
- paletteString += "<span class='units' style='margin-left:3px;'>("+m.units+")</span></span></div>";
- }
- item.innerHTML += paletteString;
- }
-
- }
- else{
- item.innerHTML += "<h4>"+this.values[formattedCategoryName.toLowerCase()][j].value+"</h4>";
- }
- category.appendChild(item);
- }
+ /*var m = this.meta[myVal];
+
+ if(m && m.palette){
+ var paletteString = "<div><span class='palette'><span class='p-min' style='margin-right:10px;'>"+m.min+"</span>" +
+ "<canvas class='colorBar' id='canvas"+this.values[formattedCategoryName.toLowerCase()][j].value+"' width=100px height=14px'></canvas>" +
+ "<span class='p-max' style='margin-left:10px;'>"+m.max+"</span>";
+ if(m.units && m.units != ""){
+ paletteString += "<span class='units' style='margin-left:3px;'>("+m.units+")</span></span></div>";
}
-
- categoryContainer.appendChild(category);
- this.container.appendChild(categoryContainer);
-
+ item.innerHTML += paletteString;
}
- /*
- var selectorbox = document.createElement("div");
- selectorbox.setAttribute("id","selectorbox");
- this.container.appendChild(selectorbox);*/
-
- this.renderCanvases();
- /*var accordionToggler = document.createElement("a");
- accordionToggler.setAttribute("class","accordionToggler atcollapse");
- accordionToggler.setAttribute("title","Hide Products");
- this.isCollapsed = false;
- this.container.appendChild(accordionToggler);
- $('.accordionToggler').bind('click',{self:this},SOTE.widget.Bank.toggle);*/
- $("#"+this.id).undelegate(".close" ,'click');
- $("#"+this.id).undelegate(".hideReg" ,'click');
- $("#"+this.id).undelegate(".hideSingle" ,'click');
- $("#"+this.id).delegate(".close" ,'click',{self:this},SOTE.widget.Bank.removeValue);
- $("#"+this.id).delegate(".hideReg" ,'click',{self:this},SOTE.widget.Bank.toggleValue);
- $("#"+this.id).delegate(".hideSingle" ,'click',{self:this},SOTE.widget.Bank.toggleValue);
- $( "." + this.id + "category" ).sortable({items: "li:not(.head)"});
- if($(window).width() > 720)
- {
- if(this.jsp){
- var api = this.jsp.data('jsp');
- if(api) api.destroy();
- }
- this.jsp = $( "." + this.id + "category" ).jScrollPane({autoReinitialise: false, verticalGutter:0});
- }
- $( "." + this.id + "category li" ).disableSelection();
- $( "." + this.id + "category" ).bind('sortstart',{self:this},SOTE.widget.Bank.initiateSort);
- $( "." + this.id + "category" ).bind('sortstop',{self:this},SOTE.widget.Bank.handleSort);
-
- setTimeout(SOTE.widget.Bank.adjustCategoryHeights,1,{self:this});
-
- //this.hideAllRadioExceptTop();
+
+ this.renderCanvases();*/
- /*$('#'+this.id).tabs();
- //this.b = new SOTE.widget.Bank("products",{paletteWidget: window.palettes, dataSourceUrl:"ap_products.php",title:"My Layers",selected:{antarctic:"baselayers,MODIS_Terra_CorrectedReflectance_TrueColor~overlays,antarctic_coastlines", arctic:"baselayers,MODIS_Terra_CorrectedReflectance_TrueColor~overlays,arctic_coastlines",geographic:"baselayers,MODIS_Terra_CorrectedReflectance_TrueColor~overlays,sedac_bound"},categories:["Base Layers","Overlays"],config:config});
- this.s = new SOTE.widget.Selector("selectorbox",{dataSourceUrl:"ap_products.php",categories:["Base Layers","Overlays"]}); */
// Mark the component as ready in the registry if called via init()
if ((this.initRenderComplete === false) && REGISTRY) {
@@ -307,48 +208,9 @@ SOTE.widget.Bank.prototype.render = function(){
};
-SOTE.widget.Bank.adjustCategoryHeights = function(args){
- var self = args.self;
- var heights = new Array;
- var container_height = $("#"+self.id).outerHeight(true);
- var labelHeight = 0;
- $('#'+self.id+' .head').each(function(){
- labelHeight += $(this).outerHeight(true);
- });
- container_height -= labelHeight;
- //console.log("This.id: " + container_height);
- for(var i=0; i<self.categories.length; i++){
- var formattedCategoryName = self.categories[i].replace(/\s/g, "");
- var actual_height = 0;
- var count = 0;
- $('#' + formattedCategoryName.toLowerCase() + ' li').each(function(){
- actual_height += $(this).outerHeight(true);
- count++;
- });
-
- heights.push({name:formattedCategoryName.toLowerCase(),height:actual_height,count:count});
- }
-
- if(heights[0].height + heights[1].height > container_height){
- if(heights[0].height > container_height/2) {
- heights[0].height = container_height/2;
- }
-
- heights[1].height = container_height - heights[0].height;
-
- }
-
- $("#" + heights[0].name).css("height",heights[0].height+"px");
- $("#" + heights[1].name).css("height",heights[1].height+"px");
-
- SOTE.widget.Bank.reinitializeScrollbars({self:self});
-};
-
-SOTE.widget.Bank.reinitializeScrollbars = function(o) {
- var pane = $("." + o.self.id + "category").each(function(){
- var api = $(this).data('jsp');
- if(api) api.reinitialise();
- });
+SOTE.widget.Bank.handleFire = function(e){
+ var self = e.data.self;
+ self.fire();
};
SOTE.widget.Bank.prototype.renderCanvases = function(){
@@ -360,7 +222,7 @@ SOTE.widget.Bank.prototype.renderCanvases = function(){
} else {
self.paletteWidget.displaySelector(name);
}
- }
+ };
};
var self = this;
@@ -394,174 +256,6 @@ SOTE.widget.Bank.prototype.renderCanvases = function(){
}
};
-SOTE.widget.Bank.toggle = function(e,ui){
- var self = e.data.self;
- if(self.isCollapsed){
- $('.accordionToggler').removeClass('atexpand').addClass('atcollapse');
- $('.accordionToggler').attr("title","Hide Products");
- $('.bank').css('display','block');
- self.isCollapsed = false;
- }
- else{
- $('.accordionToggler').removeClass('atcollapse').addClass('atexpand');
- $('.accordionToggler').attr("title","Show Products");
- $('.bank').css('display','none');
- self.isCollapsed = true;
- }
-};
-
-SOTE.widget.Bank.initiateSort = function(e,ui){
- $(ui.item).css({
- 'background-color':'rgba(255,245,218,0.85)',
- 'border':'1px #000 dashed'
- });
-
-};
-
-SOTE.widget.Bank.handleSort = function(e,ui){
- var self = e.data.self;
- self.values = new Object;
- $(ui.item).css({
- 'background-color':'',
- 'border':'none'
- });
- for(var i=0; i<self.categories.length; i++){
- var formatted = self.categories[i].replace(/\s/g, "");
- formatted = formatted.toLowerCase();
- self.values[formatted] = new Array();
- jQuery('li[id|="'+formatted+'"]').each(function() {
- var item = jQuery( this );
- var id = item.attr("id");
- var vals = id.split("-");
- var val = vals.splice(0,1);
- val = vals.join("-");
- self.values[formatted].push({value:val});
- });
- }
- self.fire();
-
-};
-
-SOTE.widget.Bank.removeValue = function(e){
- var self = e.data.self;
- var val = e.target.id.replace(/colon/g,":");
- val = val.replace(/close/g,"");
- for(var i=0; i<self.categories.length; i++){
- var formatted = self.categories[i].replace(/\s/g, "");
- formatted = formatted.toLowerCase();
- for(var j=0; j<self.values[formatted].length; j++){
- if(self.values[formatted][j].value == val){
- self.values[formatted].splice(j,1);
- }
- }
- }
- var formattedVal = "close"+val.replace(/:/g,"colon");
- //self.render();
- $("#"+self.id+" #"+formattedVal).parent().parent().remove();
- self.count--;
- self.fire();
-};
-
-SOTE.widget.Bank.toggleValue = function(e){
- var self = e.data.self;
- var val = e.target.id.replace(/colon/g,":");
- val = val.replace(/hide/g,"");
- for(var i=0; i<self.categories.length; i++){
- var formatted = self.categories[i].replace(/\s/g, "");
- formatted = formatted.toLowerCase();
- if(self.values[formatted]){
- for(var j=0; j<self.values[formatted].length; j++){
- if(self.values[formatted][j].value == val){
- if(val in self.hidden){
- delete self.hidden[val];
- e.target.src = 'images/visible.png';
- $("#"+e.target.id).attr("title","Hide Layer");
- }
- else {
- self.hidden[val] = 1;
- e.target.src = 'images/invisible.png';
- $("#"+e.target.id).attr("title","Show Layer");
- }
-
-
- }
- }
- }
- }
- //var formattedVal = val.replace(/:/g,"colon");
- //self.render();
- //$("#"+self.id+" #"+formattedVal).parent().parent().remove();
- self.fire();
-};
-
-SOTE.widget.Bank.prototype.hideAllRadioExceptTop = function(){
- for(var i=0; i<this.categories.length - 1; i++){
- var formatted = this.categories[i].replace(/\s/g, "");
- formatted = formatted.toLowerCase();
- for(var j=1; j<this.values[formatted].length; j++){
-
- this.hidden[this.values[formatted][j].value] = 1;
- $('#hide'+this.values[formatted][j].value).attr("src",'images/invisible.png');
-
- }
- }
-};
-
-SOTE.widget.Bank.radioToggleValue = function(e){
- var self = e.data.self;
- var val = e.target.id.replace(/colon/g,":");
- var cat = -1;
- var hidden = false;
- val = val.replace(/hide/g,"");
-
- for(var i=0; i<self.categories.length; i++){
- var formatted = self.categories[i].replace(/\s/g, "");
- formatted = formatted.toLowerCase();
- if(self.values[formatted]){
- for(var j=0; j<self.values[formatted].length; j++){
- if(self.values[formatted][j].value == val){
- if(val in self.hidden){
- delete self.hidden[val];
- e.target.src = 'images/visible.png';
- for(var k=0; k<self.values[formatted].length; k++){
- if(self.values[formatted][k].value != val){
- self.hidden[self.values[formatted][k].value] = 1;
- $('#hide'+self.values[formatted][k].value).attr("src",'images/invisible.png');
- }
- }
- }
- else {
- self.hidden[val] = 1;
- e.target.src = 'images/invisible.png';
- if(j+1 < self.values[formatted].length){
- if(self.hidden[self.values[formatted][j+1].value]){
- delete self.hidden[self.values[formatted][j+1].value];
- $('#hide'+self.values[formatted][j+1].value).attr("src",'images/visible.png');
- }
- }
- else {
- if(self.hidden[self.values[formatted][0].value]){
- delete self.hidden[self.values[formatted][0].value];
- $('#hide'+self.values[formatted][0].value).attr("src",'images/visible.png');
-
- }
- }
- }
-
- }
- }
- }
- }
-
-
- //var formattedVal = val.replace(/:/g,"colon");
- //self.render();
- //$("#"+self.id+" #"+formattedVal).parent().parent().remove();
- self.fire();
-};
-
-
-
/**
* Fires an event to the registry when the state of the component is changed
*
@@ -569,7 +263,6 @@ SOTE.widget.Bank.radioToggleValue = function(e){
*
*/
SOTE.widget.Bank.prototype.fire = function(){
- setTimeout(SOTE.widget.Bank.adjustCategoryHeights,1,{self:this});
$("#"+this.id).trigger("fire");
if(REGISTRY){
@@ -593,18 +286,11 @@ SOTE.widget.Bank.prototype.fire = function(){
*
*/
SOTE.widget.Bank.prototype.setValue = function(valString, selector){
-
- this.values = this.unserialize(valString, selector);
- var valid = true;
- var self = this;
-
- this.render();
- this.fire();
-
+ this.list.setValue(valString, selector);
};
SOTE.widget.Bank.prototype.currCount = function(){
- return this.count;
+ return this.list.count;
};
/**
@@ -617,77 +303,12 @@ SOTE.widget.Bank.prototype.currCount = function(){
*/
SOTE.widget.Bank.prototype.getValue = function(){
- var value = this.serialize(this.values);
- return this.id + "=" + value;
+ return this.id + "=" + this.list.getValue();
};
SOTE.widget.Bank.prototype.value = function(){
- var value = this.serialize(this.values);
- return value;
-};
-
-SOTE.widget.Bank.prototype.serialize = function(values){
- var serialized = "";
- for(var i=0; i<this.categories.length; i++){
- if(i > 0){
- serialized += "~";
- }
- var formatted = this.categories[i].replace(/\s/g, "");
- formatted = formatted.toLowerCase();
-
- serialized += formatted;
- if(values[formatted]){
- for(var j=0; j<values[formatted].length; j++){
- if(!(values[formatted][j].value in this.hidden)){
- serialized += "," + values[formatted][j].value;
- }else {
- serialized += "," + "!" + values[formatted][j].value;
- }
- }
- }
- }
- return serialized;
-};
-
-SOTE.widget.Bank.prototype.unserialize = function(string, selector){
- var unserialized = new Object;
- var hideIndicator = /^!/;
- var categories = string.split("~");
- var values = [];
- this.count = 0;
- for(var i=0; i<categories.length; i++){
- var items = categories[i].split(/[\.,]/);
- unserialized[items[0]] = new Array;
- for(var j=1; j<items.length; j++){
- if ( this.meta && !this.meta[items[j]] && !hideIndicator.test(items[j]) ) {
- this.log.warn("No such product: " + items[j]);
- } else {
- if(hideIndicator.test(items[j])){
- items[j] = items[j].replace(/!/g, "");
- this.hidden[items[j]] = 1;
- }
- else {
- if(!selector){
- delete this.hidden[items[j]];
- }
- }
-
- unserialized[items[0]].push({"value":items[j]});
- values.push(items[j]);
- this.count++;
- }
- }
-
- }
-
- for(var key in this.hidden){
- if( values.indexOf(key) < 0 ){
- delete this.hidden[key];
- }
- }
-
- return unserialized;
+ return this.list.getValue();
};
/**
@@ -724,7 +345,7 @@ SOTE.widget.Bank.handleUpdateSuccess = function(self,qs){
projection = self.VALID_PROJECTIONS[0];
}
if(projection == self.state){
- var vals = SOTE.util.extractFromQuery("selectorbox",qs)
+ var vals = SOTE.util.extractFromQuery("selectorbox",qs);
self.setValue(vals, "selector");
}
else {
@@ -764,19 +385,16 @@ SOTE.widget.Bank.prototype.loadFromQuery = function(qs){
this.updateComponent(qs);
}
else {
- var layers = SOTE.util.extractFromQuery(this.id, qs);
- if ( layers ) {
- SOTE.widget.Bank.loadValue({
- self: this,
- val: SOTE.util.extractFromQuery(this.id, qs)
- });
- }
+ SOTE.widget.Bank.loadValue({
+ self: this,
+ val: SOTE.util.extractFromQuery(this.id, qs)
+ });
}
};
SOTE.widget.Bank.prototype.sleep = function(v){
setTimeout(SOTE.widget.Bank.loadValue,100,{self:this,val:v});
-}
+};
SOTE.widget.Bank.loadValue = function(args){
if(args.self.buildMetaDone == true) {
diff --git a/src/js/list.js b/src/js/list.js
new file mode 100644
index 000000000..9398f3d68
--- /dev/null
+++ b/src/js/list.js
@@ -0,0 +1,395 @@
+SOTE.namespace("SOTE.widget.List");
+
+SOTE.widget.List.prototype = new SOTE.widget.Component;
+
+/**
+ * A selection device classifying radio buttons/checkboxes into categories that are displayed in Accordion form.
+ * Radio buttons allow single selection accross categories. Checkboxes allow multiple selections accross categories.
+ * Radio selections cannot span multiple categories.
+ *
+ * @module SOTE.widget
+ * @class List
+ * @constructor
+ * @this {List}
+ * @param {String} containerId is the container id of the div in which to render the object
+ * @param {Object} [config] is a hash allowing configuration of this component
+ * @config {Object[]} [items] a JS Array of JS Objects: items.category[i..n].key, items.category[i..n].value
+ * , items.category[i..n].action, items.type[i..n] representing the available options
+ * @config {String} [selected] the key of the initially selected option(s)
+ * @augments SOTE.widget.Component
+ *
+*/
+SOTE.widget.List = function(containerId, config){
+ this.log = Logging.getLogger("Worldview.Widget.List");
+
+ //Get the ID of the container element
+ this.container=document.getElementById(containerId);
+ if (this.container==null){
+ this.setStatus("Error: element '"+containerId+"' not found!",true);
+ return;
+ }
+ this.id = containerId;
+ //Store the container's ID
+ this.containerId=containerId;
+
+ //Define an object for holding configuration
+ if (config===undefined){
+ config={};
+ }
+
+ if(config.title === undefined){
+ config.title = "My List";
+ }
+
+ if(config.data === undefined){
+ SOTE.util.throwError("List data is not defined.");
+ }
+
+ this.hidden = new Object;
+ this.values = new Object;
+
+ this.data = config.data;
+ this.selected = this.unserialize(config.selected);
+ this.selected = this.selected[1];
+
+ this.init();
+
+};
+
+/**
+ * Displays all options in HTML in an Accordion format (see JQuery UI Accordion) with the selected states being indicated
+ * by radio button and checkbox selection. All callbacks should be set. The component UI should be rendered
+ * with controllers to call the events.
+ *
+ *
+ * @this {List}
+ *
+*/
+SOTE.widget.List.prototype.init = function(){
+
+ this.render();
+ this.trigger();
+};
+
+SOTE.widget.List.prototype.trigger = function(){
+ $("#"+this.id).trigger("listchange");
+};
+
+/**
+ * Renders the UI accordion from this.items
+ *
+ *
+ * @this {List}
+ *
+*/
+SOTE.widget.List.prototype.render = function(){
+
+ // options: category (id: txt, title:txt, notification:txt, checkbox:bool),
+ // item (id: txt, label:txt, value: txt, hide:bool, remove:bool, checkbox:bool)
+
+ // setNotification: id,txt
+
+ $(this.container).empty().addClass('list');
+
+ for(var key in this.data){
+ var title = this.data[key].title;
+
+ $(this.container).append($("<h3 class='head'></h3>").html(title));
+ $category = $("<ul id='"+key+"'></ul>").addClass('category');
+
+ this.values[key] = new Object;
+
+ for(var i=0; i<this.data[key].items.length; ++i){
+
+ var item = this.data[key].items[i];
+ var label = item.label;
+ var fLabel = item.label.replace(" ","-").toLowerCase();
+
+ if(item.value in this.selected) {
+
+ this.values[key][item.value] = 1;
+
+ $item = $("<li id='"+key+"-"+item.value+"' class='item'></li>");
+ $item.append("<a><img class='close bank-item-img' id='close"+key+"-"+item.value.replace(/:/g,"colon")+"' title='Remove Layer' src='images/close-red-x.png' /></a>");
+ if (item.value in this.hidden){
+ $item.append("<a class='hdanchor'><img class='hide bank-item-img' title='Show Layer' id='hide"+key+"-"+item.value.replace(/:/g,"colon")+"' src='images/invisible.png' /></a>");
+ }
+ else {
+ $item.append("<a class='hdanchor'><img class='hide bank-item-img' title='Hide Layer' id='hide"+key+"-"+item.value.replace(/:/g,"colon")+"' src='images/visible.png' /></a>");
+ }
+ $item.append("<h4>"+label+"</h4>");
+ $item.append("<p>"+item.sublabel+"</p>");
+
+ $category.append($item);
+
+ }
+ }
+
+ $(this.container).append($category);
+ }
+
+ $("#"+this.id).undelegate(".close" ,'click');
+ $("#"+this.id).undelegate(".hide" ,'click');
+ $("#"+this.id).delegate(".close" ,'click',{self:this},SOTE.widget.List.removeItem);
+ $("#"+this.id).delegate(".hide" ,'click',{self:this},SOTE.widget.List.hideItem);
+ $( ".category" ).sortable({items: "li:not(.head)"});
+
+ if($(window).width() > 720)
+ {
+ if(this.jsp){
+ var api = this.jsp.data('jsp');
+ if(api) api.destroy();
+ }
+ this.jsp = $( "." + this.id + "category" ).jScrollPane({autoReinitialise: false, verticalGutter:0});
+ }
+
+ $( "." + this.id + "category li" ).disableSelection();
+ $( ".category" ).bind('sortstart',{self:this},SOTE.widget.List.initiateSort);
+ $( ".category" ).bind('sortstop',{self:this},SOTE.widget.List.handleSort);
+
+ setTimeout(SOTE.widget.List.adjustCategoryHeights,1,{self:this});
+
+};
+
+SOTE.widget.List.adjustCategoryHeights = function(args){
+ var self = args.self;
+ var heights = new Array;
+ var container_height = $("#"+self.id).outerHeight(true);
+ var labelHeight = 0;
+ $('#'+self.id+' .head').each(function(){
+ labelHeight += $(this).outerHeight(true);
+ });
+ container_height -= labelHeight;
+
+ for(var key in self.values){
+ var actual_height = 0;
+ var count = 0;
+ $('#' + key + ' li').each(function(){
+ actual_height += $(this).outerHeight(true);
+ count++;
+ });
+
+ heights.push({name:key,height:actual_height,count:count});
+ }
+
+ if(heights[0].height + heights[1].height > container_height){
+ if(heights[0].height > container_height/2) {
+ heights[0].height = container_height/2;
+ }
+
+ heights[1].height = container_height - heights[0].height;
+
+ }
+
+ $("#" + heights[0].name).css("height",heights[0].height+"px");
+ $("#" + heights[1].name).css("height",heights[1].height+"px");
+
+ SOTE.widget.List.reinitializeScrollbars(self);
+};
+
+SOTE.widget.List.reinitializeScrollbars = function(self) {
+ var pane = $(".category").each(function(){
+ var api = $(this).data('jsp');
+ if(api) api.reinitialise();
+ });
+};
+
+SOTE.widget.List.initiateSort = function(e,ui){
+ $(ui.item).css('background-color','#fff5da');
+};
+
+SOTE.widget.List.handleSort = function(e,ui){
+ var self = e.data.self;
+ self.values = new Object;
+ $(ui.item).css('background-color','rgba(238, 238, 238, 0.85)');
+ for(var key in self.data){
+ self.values[key] = new Array();
+ jQuery('li[id|="'+key+'"]').each(function() {
+ var item = jQuery( this );
+ var id = item.attr("id");
+ var vals = id.split("-");
+ var val = vals.splice(0,1);
+ val = vals.join("-");
+ self.values[key][val] = 1;
+ });
+ }
+ self.trigger();
+};
+
+SOTE.widget.List.removeItem = function(e){
+ var self = e.data.self;
+ var srcData = e.target.id.replace(/colon/g,":").replace(/close/g,"");
+ var t = srcData.split(/-/);
+ var category = t[0];
+ var val = t[1];
+
+ delete self.values[category][val];
+
+ var formattedVal = "close"+val.replace(/:/g,"colon");
+ $("#"+self.id+" #"+e.target.id).parent().parent().remove();
+
+ self.count--;
+ setTimeout(SOTE.widget.List.adjustCategoryHeights,1,{self:self});
+ self.trigger();
+};
+
+SOTE.widget.List.hideItem = function(e){
+ var self = e.data.self;
+ var srcData = e.target.id.replace(/colon/g,":").replace(/hide/g,"");
+ var t = srcData.split(/-/);
+ var category = t[0];
+ var val = t[1];
+
+ if(val in self.hidden){
+ delete self.hidden[val];
+ e.target.src = 'images/visible.png';
+ $("#"+e.target.id).attr("title","Hide Layer");
+ }
+ else {
+ self.hidden[val] = 1;
+ e.target.src = 'images/invisible.png';
+ $("#"+e.target.id).attr("title","Show Layer");
+ }
+ self.trigger();
+};
+
+/**
+ * Sets the selected option(s) in the List from the passed in value(s), if valid
+ * [containerId]=[options] (options is a dot delimited string with the first item containing
+ * a single select item, and the remaining items containing the multi-select items).
+ *
+ * @this {List}
+ * @param {String} value is a dot delimited string of the key(s) of the option(s) to be set as selected
+ * @returns {boolean} true or false depending on if the new value(s) validates
+ *
+*/
+SOTE.widget.List.prototype.setValue = function(valString, selector){
+
+ this.selected = this.unserialize(valString, selector)[1];
+
+ this.render();
+ this.trigger();
+};
+
+SOTE.widget.List.prototype.currCount = function(){
+ return this.count;
+};
+
+/**
+ * Gets the currently selected option(s) [containerId]=[options]
+ *
+ * @this {List}
+ * @returns {String} [container]=[options], options being a dot delimited string
+ * representing the key(s) of the currently selected option(s)
+ *
+*/
+SOTE.widget.List.prototype.getValue = function(){
+
+ var value = this.serialize(this.values);
+ return value;
+};
+
+SOTE.widget.List.prototype.serialize = function(values){
+ var categories = [];
+
+ for(var category in values){
+ var catStr = [category];
+ for(var item in values[category]){
+ if(item in this.hidden)
+ catStr.push("!" + item);
+ else
+ catStr.push(item);
+ }
+ categories.push(catStr.join(","));
+ }
+
+ return categories.join("~");
+};
+
+SOTE.widget.List.prototype.unserialize = function(string, selector){
+ var unserialized = new Object;
+ var hideIndicator = /^!/;
+ var categories = string.split("~");
+ var values = new Object;
+ this.count = 0;
+
+
+ for(var i=0; i<categories.length; i++){
+ var items = categories[i].split(/[\.,]/);
+ unserialized[items[0]] = new Array;
+ for(var j=1; j<items.length; j++){
+ if(hideIndicator.test(items[j])){
+ items[j] = items[j].replace(/!/g, "");
+ this.hidden[items[j]] = 1;
+ }
+ else {
+ if(!selector){
+ delete this.hidden[items[j]];
+ }
+ }
+
+ unserialized[items[0]].push(items[j]);
+ values[items[j]] = 1;
+ this.count++;
+ }
+
+ }
+
+ for(var key in this.hidden){
+ if( !key in values ){
+ delete this.hidden[key];
+ }
+ }
+
+ return [unserialized,values];
+};
+
+SOTE.widget.List.prototype.setHeight = function(height){
+ $("#"+this.id).css("height",height+"px");
+ SOTE.widget.List.adjustCategoryHeights({self:this});
+};
+
+/**
+ * Sets the data accessor that provides state change instructions given dependencies
+ *
+ * @this {List}
+ * @param {String} datasourceurl is the relative location of the data accessor
+ *
+*/
+SOTE.widget.List.prototype.setDataSourceUrl = function(dataSourceUrl){
+ this.dataSourceUrl = dataSourceUrl;
+};
+
+/**
+ * Gets the data accessor
+ *
+ * @this {List}
+ * @returns {String} the relative location of the accessor
+ *
+*/
+SOTE.widget.List.prototype.getDataSourceUrl = function(){
+ return this.dataSourceUrl;
+};
+
+/**
+ * Sets the status of the component
+ *
+ * @this {List}
+ * @param {String} s the current status of the component (user prompts, error messages)
+ *
+*/
+SOTE.widget.List.prototype.setStatus = function(s){
+ this.statusStr = s;
+};
+
+/**
+ * Gets the status of the component
+ *
+ * @this {List}
+ * @returns {String} the current status of the component (user prompts, error messages)
+ *
+*/
+SOTE.widget.List.prototype.getStatus = function(){
+ return this.statusStr;
+};
+