1  
  2 /**
  3  * @fileOverview The file for detail-call handling. Details are displayd differently based on settings for particular UI block.
  4  * The file consists from Detail model and Detailset of all currently created detail-windows.
  5  * @author 
  6  * @version 
  7  */
  8 
  9 /**
 10  * 
 11  * @constructor
 12  */
 13 
 14 function Detail(detailwidget, name, url) {
 15 	this.detailwidget = detailwidget;
 16 	
 17 	this.did = '';
 18 	this.content_parent = undefined;
 19 	this.content_wrapper = '';
 20 	
 21 	this.url = url;
 22 	this.type = undefined;
 23 	this.name = name;
 24 	this.content = undefined;
 25 	this.temp_id = "";
 26 	
 27 	this.setData();
 28 	 
 29 };
 30 Detail.prototype.update = function(detailwidget, url){
 31 	this.detailwidget = detailwidget;
 32 	this.temp_id = "";
 33 	this.url = url;
 34 	this.setData();
 35 	
 36 };
 37 Detail.prototype.setData = function(){
 38 	
 39 	var widget = this.detailwidget;
 40 	
 41 	this.type = blocks_settings[this.name].detail.type;
 42 	switch (this.name){
 43 	case 'query':
 44 		//TODO class standard rename
 45 		this.content = $(widget).closest('.query_wrapper').find('.columns-wrapper');
 46 		this.url  = undefined;
 47 		this.temp_id  = $(widget).closest('.query_wrapper').attr('id');
 48 		break;
 49 	case 'collections':
 50 		this.content = $('#collections-block');
 51 		this.url  = undefined;
 52 		break;
 53 	case 'record':
 54 		this.content = undefined;
 55 		if (widget != undefined){
 56 			this.url = $(widget).parent().find("a").attr('href');
 57 		}
 58 		break;
 59 	case 'values':
 60 		this.content = undefined;
 61 		var index = $(widget).closest('.sc-wrapper').find('.index-input').val();
 62 		if (index != ""){
 63 			this.url = "values/htmllist/?q=" + index + "&sort=size&repository=" + getSelectedRepository();//$(widget).parent().find("a").attr('href');
 64 		} 
 65 		break;
 66 	case 'index':
 67 		this.content = undefined;
 68 		this.url = 'terms/htmldetail/?q=all';
 69 		break;
 70 	case 'info':
 71 		this.content = undefined;
 72 		if (this.detailwidget == undefined){
 73 			if (this.url == undefined) {
 74 				this.url = '/MDService2/docs/xml/welcome #welcome';
 75 			}
 76 			if (this.url == null) {
 77 				this.url = '/MDService2/docs/xml/welcome #welcome';
 78 			}
 79 
 80 		} else {
 81 			//var a = $(this).parent().next().attr('id');
 82 			this.url = window.location.pathname + "static/info.xml";
 83 		}
 84 		break;
 85 	default:
 86 	}
 87 };
 88 Detail.prototype.getTitle = function(){
 89 	var title = "";
 90 	title = "DETAIL: " + this.name;
 91 	return title;
 92 };
 93 Detail.prototype.create = function(){
 94 	var cw = "<div id='" + this.did  + "' class='detail-wrapper ui-widget' >" + 
 95 	"<div class='detail-header ui-widget-header ui-state-default'>" +// ui-corner-top'>" +
 96 	"<span class='cmd cmd_get'></span>" + 
 97 	"<span class='inner-title'>" + this.getTitle() +"</span>" +
 98 	"<span class='cmd cmd_del'></span></div>" +
 99 	"<div class='detail-content content'></div>" +
100 	"</div>";
101 	this.content_wrapper = $(cw);
102 	
103 };
104 Detail.prototype.enrichContent = function(){
105 	if (this.name == 'query'){
106 		if ($(this.content).find('.columns-selected').children('.widget-wrapper').length == 0){
107 			var query = queryset.getQuery(this.temp_id);
108 			//enrich link,xml
109 			var links = "<a class='cmd cmd_xml' target='_blank' href='" + query.link("xml") + "'>sru</a> " +
110 				"<a class='cmd cmd_link' target='_blank' href='" + query.link("fullpage") + "' >link</a>";
111 			$(this.content).find('.cmds').append($(links));
112 			//enrich with selectionlist
113 			var selectionlist  = new SelectionList($(this.content).find('.columns-selected'));
114 			selectionlistset.add(selectionlist);
115 			//selectionlist.listwidget.query = query;
116 			selectionlist.load(jsonw["WorkspaceProfile"]["CustomTermsets"]);
117 			$(this.content).find('.cmd_reload').data('selectionlist',selectionlist);
118 			if (query.columns == ""){
119 				var autolist = "";
120 				$('#'+ query.listid).find('.result').find('thead').find('th').each(function(i,item){
121 					if (i > 0){
122 						if (i> 1) {
123 							autolist = autolist + ",";
124 						}
125 						autolist = autolist + $(item).text();
126 					}
127 				});
128 				selectionlist.autolist = autolist;
129 				selectionlist.listwidget.load(autolist.split(','));
130 				selectionlist.select(1);
131 			}
132 			else {
133 				selectionlist.listwidget.load(query.columns.split(','));
134 				selectionlist.select(0);
135 			}
136 		}
137 	}
138 };
139 Detail.prototype.postprocessSelection = function(){
140 	if (this.name == "record"){
141 		queryset.recorddetailselection($(this.detailwidget).parent().parent());
142 	}
143 };
144 Detail.prototype.openContent = function() {
145 	if (this.content != undefined){
146 		this.content_parent = $(this.content).parent();
147 		this.enrichContent();
148 		$(this.content_wrapper).find('.detail-content').append($(this.content));
149 		$(this.content).show();
150 	}
151 };
152 
153 Detail.prototype.openUrl = function() {
154 	if (this.url != undefined){
155 		$(this.content_wrapper).show();
156 		$(this.content_wrapper).find('.detail-content').data('id', $(this.detailwidget).parent().next().attr('id'));
157 		$(this.content_wrapper).find('.detail-content').load(this.url,function(responseText, textStatus, XMLHttpRequest){
158 			//find info data
159 			if ($(this).data('id') != null){
160 				if ($(this).data('id').length > 0){
161 					$(this).html($(this).find('#' + $(this).data('id') + '-info'));
162 				}
163 			}
164 			var get = $(this).parent().find('.cmd_get');				
165 			get.removeClass('cmd_get');
166 			get.addClass('cmd_up');
167 			handleUIBlock($(this).children('.cmds-ui-block'));
168 			addPaging($(this).children('.cmds-ui-block'));
169 			//$(this).show();
170 			var name = $(this).closest('.detail-wrapper').attr("id");
171 			if (name == "detail_values"){
172 				//$(this).find(".cmd_columns").click(function(event) {
173 				$(this).find(".cmd_query").click(function(event) {
174 					event.preventDefault();
175 					handleValueSelection($(this));
176 				});	
177 			}
178 			if (name == "detail_index"){
179 				$(this).find(".terms-tree").treeTable({initialState:"collapsed"});
180 				$(this).find("a.value-caller").click(function(event) {
181 						event.preventDefault();
182 						handleValueCaller($(this));
183 					});		
184 				$(this).find(".terms-tree").find('.treecol').find(".cmd_query").click(function(event) {
185 						event.preventDefault();
186 						handleIndexSelection($(this));
187 				});	
188 			}
189 		});
190 	}
191 };
192 
193 Detail.prototype.open = function(){
194 	
195 	switch (blocks_settings[this.name].detail.type) {
196 	case "inline":
197 		//TODO 
198 		if (this.content != undefined){
199 			$(this.content).toggle();
200 		}
201 		if (this.url != undefined){
202 			$(this.detailwidget).after($(this.content_wrapper));
203 			this.openUrl();
204 		}
205 		break;
206 	case "floating":
207 		this.openContent();
208 		showDetail(this.content_wrapper, "html");
209 		//this.showFloatingDetail();
210 		this.openUrl();
211 		break;
212 	case "pane":
213 		this.openContent();
214 		addToDetailList(this.content_wrapper , "detail_" + this.name);
215 		this.openUrl();
216 		if (this.url == undefined ) {
217 			var get = $(this.content_wrapper).find('.cmd_get');				
218 			get.removeClass('cmd_get');
219 			get.addClass('cmd_up');
220 		}
221 		//$('#detailblock').children('.content').scrollTop($(this.content_wrapper).position().top);
222 		$('#detailblock').scrollTop($(this.content_wrapper).position().top + $(this.content_wrapper).find('.detail-header').height());
223 	default://pane ?
224 	};
225 		
226 	this.postprocessSelection();
227 };
228 Detail.prototype.close = function(){
229 	if (this.content_parent != undefined){
230 		$(this.content).hide();
231 		this.content_parent.append($(this.content));
232 	}
233 };
234 
235 Detail.prototype.showFloatingDetail = function(){
236 	var pos = $(this.detailwidget ).offset();
237 	//var iWidth = (options.width > 0) ? options.width : $input.width();
238 	// reposition
239 	$('#detail-floating').css({
240 		//width: parseInt(iWidth) + "px",
241 		top: (pos.y + $(this.detailwidget).height()) + "px",
242 		left: pos.x + "px",
243 		"z-index": "1000"
244 	});
245 	$('#detail-floating').append($(this.content_wrapper));
246 	$('#detail-floating').show();	
247 };
248 
249 /**
250  * Reference to UI-element
251  * @field
252  */ 
253 var detail_container = $("#detaillist"); 
254 
255 /**
256  * Maintains/Manages the detail-list
257  * @constructor
258  */
259 var detailcaller = { details: {},
260 	container: '#detaillist',
261 		
262 	calldetail: function(widget,name, url){
263 		//var name;
264 		var detail;
265 		var content;//, url;
266 		
267 		if (name == undefined){
268 			name = this.getdetailname(widget) ;
269 			if (name == undefined) {
270 				alert("detail name -  undefined");
271 				return;
272 			}
273 		}
274 		// test for values
275 		if (name == 'values'){
276 			var index = $(widget).closest('.sc-wrapper').find('.index-input').val();
277 			if (index == ""){
278 				return;
279 			}
280 		}
281 		detail = this.details[name];
282 		//url = this.getdetailurl(name, widget);
283 		if (detail == undefined) {
284 			//content = this.getdetailcontent(name, widget);
285 			detail = new Detail( widget, name, url);
286 			this.adddetail(detail);
287 		} else {
288 			detail.close();
289 			//content = this.getdetailcontent(name, widget);
290 			detail.update( widget , url);
291 		}
292 		
293 		detail.open();
294 	},
295 	
296 	
297 	adddetail: function (detail){		
298 		this.details[detail.name] = detail;
299 		detail.did = "detail_" + detail.name;
300 		detail.create();
301 	},
302 
303 	removedetail: function (did) {
304 		notifyUser("removing detail:"  + did);
305 		
306 		var detail = this.getdetail(did);
307 		//detail.clear();
308 		this.details[detail.name] = undefined;
309 		
310 		$('#' + did).remove();
311 		
312 	},
313 	
314 	getdetailname: function(widget) {
315 		var name;
316 		if ($(widget).parents('.result').length > 0){
317 			name = "record";
318 		} else {
319 			if ($(widget).parents('.result-summary').length > 0) {
320 				name = "values";
321 			} else {
322 				if ($(widget).closest('.ui-widget').attr('name') == 'query'){
323 					name = 'query';
324 				} else {
325 					if  ($(widget).closest('.c-widget').attr('id') == 'collections-widget'){
326 						name = 'collections';
327 					} else {
328 						if ($(widget).attr("name") == "detail_index"){
329 							name = 'index';
330 						} else {
331 							if ($(widget).prev().hasClass("value-input")){
332 								name = 'values';
333 							} else {
334 								if ($(widget).hasClass("cmd_info")){
335 									name = 'info';
336 								}
337 							}
338 						}
339 					}
340 				}
341 			}
342 		}
343 		
344 		return name;
345 	},
346 /*	
347 	getdetailcontent: function(name, widget) {
348 		var content;
349 		switch (name){
350 		case 'query':
351 			//TODO class standard rename
352 			content = $(widget).closest('.query_wrapper').find('.columns-wrapper');
353 			break;
354 		case 'record':
355 			content = undefined;
356 			break;
357 		case 'values':
358 			content = undefined;
359 			break;
360 		default:
361 		}
362 		return content;
363 	}, 
364 	
365 	getdetailurl: function(name, widget) {
366 		var url;
367 		switch (name){
368 		case 'query':
369 			url = undefined;
370 			break;
371 		case 'record':
372 			url = $(widget).parent().find("a").attr('href');
373 			break;
374 		case 'values':
375 			url = $(widget).parent().find("a").attr('href');
376 			break;
377 		default:
378 		}
379 
380 		return url;
381 	}, 
382 	*/
383 	getdetail: function(did) {
384 		var detail;
385 		$.each(this.details,function(){
386 			if (this.did == did) {
387 				detail = this;
388 			}
389 		});
390 		return detail;
391 	}
392 };
393 
394 
395 function showDetail(_data, textStatus, xmlhttpreq) {	
396 	try {
397 		var data = _data;
398 		var blankdata = "<html><\html>";
399 		
400 		if(typeof xmlhttpreq!='undefined')
401 		{
402 			if (xmlhttpreq.responseXML != null){
403 				var isinfo = xmlhttpreq.responseXML.baseURI.split("/static/info.xml?id="); 
404 				if (isinfo != "undefined"){
405 					if ( isinfo.length = 2){
406 						var infoid = new RegExp('[\\?]id=([^&#]*)').exec(xmlhttpreq.responseXML.baseURI);
407 						infoid = infoid[1] || 0;
408 						infoid = infoid + "-info";
409 						data = $(_data).find('#' + infoid);
410 					}
411 				}		
412 			}
413 		}
414 
415 		if (!$('#detail-float').is( ":ui-dialog" )) {
416 			initDetailFloat();
417 		}
418 		
419 		if (data ==  null){
420 			$('#detail-float').html(blankdata);
421 			
422 			openDialog("", true);
423 		} else
424 		{		
425 			// detail data
426 			var did = $(data).attr("id");
427 			if (did != undefined){
428 				if ((did.indexOf("-info") == -1) && (did.length > 0)) {
429 					var detail = detailcaller.getdetail(did);
430 					var pos = $(detail.detailwidget).offset();
431 					pos.top = pos.top + $(detail.detailwidget).height();
432 					$("#detail-float").dialog('option', 'position', [pos.left,pos.top]);	
433 				}
434 			}
435 			
436 			$('#detail-float').html(data);
437 			var title =  $('#detail-float').find('.inner-title').attr('value');
438 			if (title == undefined) title = $('#detail-float').find('.title, .inner-title').first().text();
439 			$('#detail-float').dialog( "option", "title", title );
440 			
441 			
442 
443 			$('#detail-float').find('.detail-header').hide();
444 			$('#detail-float').find('.detail-content').show(); 
445 			
446 			openDialog(title,false);
447 		}
448 		
449 	} catch (e) {
450 			notifyUser(e,'debug');
451 	}	
452 }
453 
454 function openDialog(title, is_get){
455 	if ( $('#detail-float').dialog("isOpen")) {
456 		if (! is_get) {
457 			var get = $('#detail-float').siblings('div.ui-dialog-titlebar').find('.cmd_get');
458 			get.removeClass('cmd_get').addClass('cmd_up');
459 			get.removeClass('ui-dialog-titlebar-get').addClass('ui-dialog-titlebar-up');
460 		} else {
461 			var up = $('#detail-float').siblings('div.ui-dialog-titlebar').find('.ui-dialog-titlebar-up');
462 			up.removeClass('.cmd_up .ui-dialog-titlebar-up').addClass('.cmd_get .ui-dialog-titlebar-get');
463 			var down = $('#detail-float').siblings('div.ui-dialog-titlebar').find('.ui-dialog-titlebar-down');
464 			down.removeClass('.cmd_down .ui-dialog-titlebar-down').addClass('.cmd_get .ui-dialog-titlebar-get');
465 		}		
466 		$('#detail-float').siblings('div.ui-dialog-titlebar').find('.ui-dialog-title').text(title);
467 		//$('#detail-float').closest('.ui-dialog').show();
468 		
469 	} else {
470 		$('#detail-float').dialog('open');
471 		$('#detail-float').siblings('div.ui-dialog-titlebar').addClass('ui-state-default');
472 		$('#detail-float').siblings('div.ui-dialog-titlebar').children().remove();
473 		var x = '<span class="cmd cmd_del ui-dialog-titlebar-del "/>' ;
474 		if (is_get){
475 			x = x + '<span class="cmd cmd_get ui-dialog-titlebar-get"/>';
476 		} else {
477 			x = x + '<span class="cmd cmd_up ui-dialog-titlebar-up"/>';
478 		}
479 		x = x + '<span class="ui-dialog-title">' + title +'</span>';
480 		$('#detail-float').siblings('div.ui-dialog-titlebar').append(x);				
481 	
482 		
483 		$("#detail-float").dialog({
484 			beforeclose: function(event, ui) {
485 			
486 			var did;
487 			if ($(this).length > 0){
488 				did = $(this).find('.detail-wrapper').attr('id');
489 			}
490 			if (did != undefined){
491 				detail = detailcaller.getdetail(did);
492 				detail.close();
493 				//if (detail.name == "collections"){
494 					detailcaller.removedetail(did);
495 				//}
496 			}
497 		
498 		}
499 		});
500 	}
501 
502 }
503 
504 function updateQueryDetailPane(query){
505 	var detail = detailcaller.getdetail("detail_query");
506 	if (detail != undefined){
507 		if (query.listid == detail.temp_id){
508 			$(detail.content_wrapper).find('.cmd_xml').attr('href',query.link("xml"));
509 			$(detail.content_wrapper).find('.cmd_link').attr('href',query.link("fullpage"));
510 		}
511 	}
512 }
513 /*
514 function updateColumnsFromDetail(query){
515 	var detail = detailcaller.getdetail("detail_query");
516 	if (detail != undefined){
517 		if (query.listid == detail.temp_id){
518 			
519 			if (selectionlistset.getselectionlist(slid).autoSelected()){
520 				this.columns = "";
521 			} else
522 			{
523 				this.columns = selectionlistset.getselectionlist(slid).listwidget.getListText();
524 			}
525 		}
526 	}
527 }
528 */