view gis_gui/lib/blocks.js @ 267:260ec888fe15

Multilayer for Europeana4D
author fknauft
date Wed, 25 Jan 2012 20:45:13 +0100
parents c37e8c173136
children d1b43624cc63
line wrap: on
line source

/* 
 * fallback for console.log calls
 */
if (typeof(console) == 'undefined') {
    console = {
	log : function(){},
	debug : function(){},
	error : function(){}
    };
}

function defined(x) {
    // returns if x is defined
    return (typeof arguments[0] !== "undefined");
}

function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    return randomstring;
}



/* 
 *  guiBlocks base
 */
function guiBlocks(container) {
    // list of blocks
    this.blocks = [];
    this.containerElement = container;
    return this;
}
//var guiBlocks = new Object();

/*
 * Block base class
 */
guiBlocks.Block = function(id, element) {
    this.id = id; // the blocks html id
    this.element = element; // the html dom element
    this.storeId = null; // the id in online storage
    this.params = {}; // parameters to store
    return this;
};

// create a new block and add it to the current workspaces stack
guiBlocks.prototype.addBlock = function(url, baseId, params, whenDone) {
	// add title to url
    var newblock = $("<div>Block loading...</div>");
	var newid = baseId+"_"+randomString();
	var container = this.containerElement;
	console.debug("addblock newid="+newid);
    newblock.hide();
    newblock.load(url, function() {
    	// after load function
    	console.debug("addblock after load block");
    	console.debug(container);
    	$(this).find(".block").attr("id",newid);
    	$(this).appendTo(container);
 
    	// chain done function
        if (typeof(whenDone) == "function") {
        	// chain whenDone (should we add parameters?)
        	whenDone.apply(this);
        } else {
        	// default after load function
        	$(this).fadeIn();
        }
    	
    });
    // create new Block object
    var block = new guiBlocks.Block(newid, newblock);
    // add parameters
    if (params == null) {
        // create new params
        params = {"id": newid};
    }
    block.params = params;
    // add to list of blocks
    this.blocks.push(block);
    return block;
};

guiBlocks.prototype.getBlock = function(id) {
    for (b in this.blocks) {
	if (b.id == id) {
	    return b;
	}
    }
    return null;
};

/*
guiBlocks.prototype.getStoreItem = function(tag,type,item) {
    // get item from online storage
    jQuery.get();
}
*/

guiBlocks.prototype.loadListOfItems = function(storeTag,storeType,callback) {
    // loads list of id and type objects and executes callback function
    var url = "../db/RESTjson/db/public/gui_objects/"+escape(storeTag)+"/"+storeType+"?recursive=true";
    jQuery.getJSON(url,callback);
};

guiBlocks.prototype.e4DOpen = function(titel,tables){
	var e4D_URL="http://mappit.mpiwg-berlin.mpg.de:8080/e4D?";
	var paramstr = "format=KML";
//	var layer_URL = "http://chinagis-develop.mpiwg-berlin.mpg.de:9080/db/RESTdb/db/public/" + escape(table)  + "?" + paramstr;
	var layer_URL = "";
	
	for (var i=0;i<tables.length;i++){
		if (i!=0) {e4D_URL=e4D_URL+"&";}
		layer_URL= "http://mappit.mpiwg-berlin.mpg.de/db/RESTdb/db/public/" + escape(tables[i])  + "?" + paramstr;
        e4D_URL=e4D_URL+"kml"+[i+1]+"="+layer_URL+"&source"+[i+1]+"=1";
	}
    console.debug("                e4D_URL=", e4D_URL );
    var newwindow=window.open(e4D_URL,"Mappit@Europeana4D","height=800,width=900,resizable=1,scrollbars=1");
    if (window.focus) {newwindow.focus();}
			return false;

  /*  });*/
};

guiBlocks.Block.prototype.storeBlock = function(storeTag) {
    var storeType = this.params.type;
    var storeItem = this.params.id;
    var url = "../db/RESTjson/db/public/gui_objects/"+escape(storeTag)+"/"+storeType+"/"+storeItem;
    var ds = JSON.stringify(this.params);
    jQuery.ajax({
        type: "PUT",
        url: url,
        data: ds,
        success: function(msg){
       		console.debug("PUT success msg=",msg);
            this.storeId = storeItem;
        }
    });
};



// collapse the block so that only its titlebar is visible
function foldBlock(segment){
    if (segment == null) {
        return;
    }
	if(segment.hasClass("folded")){
	  //if the segment is collapsed
	  segment.removeClass("folded");
	  segment.find(".body").slideDown();
	} else{
	  //if the segment is expanded
	  segment.addClass("folded");
	  segment.find(".body").slideUp();
	}
}

function switchSubscreens(container, id1, id2) {
  var oldScreen = container.find("#"+id1);
  var newScreen = container.find("#"+id2);

  oldScreen.fadeOut();
  newScreen.fadeIn();
}

function guiBlocks_init() {
    // initialize collapsed windows
    $("div.block.folded").each(function(e){
	$(this).find(".body").slideUp();
      });

    // TitleBar Click
    $("div.block div.titlebar h1").live("click", function(e) {
	var segment = $(this).parents().filter("div.block");
	foldBlock(segment);
      });

    // TitleBar Close
    $("div.block div.titlebar div.close_button").live("click", function(e){
	segment = $(this).parents().filter("div.block");
	segment.fadeOut(function(){ segment.remove();});
      });

    // TitleBar Reload
    $("div.block div.titlebar div.reload_button").live("click", function(e){
	segment = $(this).parents().filter("div.block");
	console.debug("guiBlocks_init().live(reload) segment[0].id="+ segment[0].id);
	//FJK: funktioniert leider noch nicht!
//	var blockid=$(this).parents().firstElementChild.id;
//	var mapblock=getBlock(blockid);
//	var params=mapblock.readMapParams();
//	var be = mapblock.element;
	//var mapblock = $(be).find(".map_box");
//	console.debug("guiBlocks_init().live(reload) params="+ new String(params));
//	mapblock.updateMap(be,params);
      });

}

function getBlock(id){
    for (b in this.blocks) {
		if (b.id == id) {
		    return b;
		}
	    }
	    return null;	
}