www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

timelineControls.js (12404B)


      1 var localeBundle = initLocaleBundle();
      2 var jumpToYearTimer;
      3 var lastJumpToYearValue;
      4 
      5 /*
      6  * Set up the localization string bundle from timeline.properties
      7  */
      8 function initLocaleBundle() {
      9 	var src = 'chrome://zotero/locale/timeline.properties';
     10 	var stringBundleService = Components.classes["@mozilla.org/intl/stringbundle;1"]
     11 		.getService(Components.interfaces.nsIStringBundleService);
     12 	return stringBundleService.createBundle(src);
     13 }
     14 
     15 /*
     16  * Get a localized string from the string bundle
     17  *
     18  * This is copied from zotero.js
     19  */
     20 function getString(name, params){
     21 	try {
     22 		if (params != undefined){
     23 			if (typeof params != 'object'){
     24 				params = [params];
     25 			}
     26 			var l10n = localeBundle.formatStringFromName(name, params, params.length);
     27 		}
     28 		else {
     29 			var l10n = localeBundle.GetStringFromName(name);
     30 		}
     31 	}
     32 	catch (e){
     33 		throw ('Localized string not available for ' + name);
     34 	}
     35 	return l10n;
     36 }
     37 
     38 
     39 function getTimeline() {
     40 	var tt = getHeight();
     41 	tt -= 165;
     42 	if (tt < 100) {
     43 		tt = 100;
     44 	}
     45 	return '<div class="timeline" id="my-timeline" style="height: ' + tt + 'px; border: 1px solid #aaa"></div>';
     46 }
     47 
     48 function getHeight() {
     49 	var temp = document.documentElement.clientHeight;
     50 	if(temp && temp > 0) {
     51 		return temp;
     52 	}
     53 	else {
     54 		temp=document.body.clientHeight;
     55 		if(temp && temp > 0) {
     56 			return temp;
     57 		}
     58 	}
     59 	return 0;
     60 }
     61 
     62 function wasChanged(current) {
     63 	if (current != lastJumpToYearValue) {
     64 		lastJumpToYearValue = current;
     65 		var theYear = document.getElementById("jumpYear").value;
     66 		if(theYear.length == 0) {
     67 			centerTimeline(new Date());
     68 		}
     69 		else {
     70 			checkDate(theYear);
     71 		}
     72 	}
     73 }
     74 
     75 function doKeyPress(e)
     76 {
     77 	clearTimeout(jumpToYearTimer);
     78 	lastJumpToYearValue = document.getElementById('jumpYear').value;
     79 	if((e.which == '8' || e.which == '0') && lastJumpToYearValue.length == 0) {
     80 		centerTimeline(new Date());
     81 	}
     82 	else if(e.which == '13'){
     83 		checkDate(lastJumpToYearValue);
     84 	}
     85 	else {
     86 		jumpToYearTimer = setTimeout(function () {
     87 			wasChanged(document.getElementById("jumpYear").value);
     88 		}, 1000)
     89 	}
     90 }
     91 
     92 function getMonthNum(month) {
     93 	var months = new Array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
     94 	var num = months.indexOf(month.toLowerCase());
     95 	if(num<0){
     96 		num = 0;
     97 	}
     98 	return num;
     99 }
    100 
    101 function createDate(date){
    102 	date = date.split('.');
    103 	var theDate = new Date();
    104 	theDate.setMonth(getMonthNum(date[0]));
    105 	theDate.setDate(date[1]);
    106 	theDate.setFullYear(date[2]);
    107 	return theDate;
    108 }
    109 
    110 function getTimelineDate(timeline){
    111 	var timelineDate = timeline.getBand(0).getCenterVisibleDate().toString();
    112 	var dateParts = timelineDate.split(' ');
    113 	timelineDate = dateParts[1] + '.' + dateParts[2] + '.' + dateParts[3];
    114 	return timelineDate;
    115 }
    116 
    117 function checkDate(date) {
    118 	var arr = new Array();
    119 	var i = 0;
    120 	date = date.replace(/[\s|.]/g,'');
    121 
    122 	//check to see if the year is B.C.
    123 	var bc = false;
    124 	if (date.substr(date.length - 2).toLowerCase() == 'bc') {
    125 		bc = true;
    126 		date = date.substr(0, date.length - 2);
    127 	}
    128 	
    129 	if(/\D/.test(date)) {
    130 		return;
    131 	}
    132 	
    133 	if (bc) {
    134 		if(date < 10000) {
    135 			centerTimeline(date + ' BC');
    136 		}
    137 	}
    138 	else {
    139 		if(date < 275000) {
    140 			centerTimeline(date);
    141 		}
    142 	}
    143 }
    144 function changeBand(queryString, band, intervals, selectedIndex) {
    145 	var values = new Array('d', 'm', 'y', 'e', 'c', 'i');
    146 	
    147 	var newIntervals = '';
    148 	for (var i = 0; i < intervals.length; i++) {
    149 		if (i == band) {
    150 			newIntervals += values[selectedIndex];
    151 		}
    152 		else {
    153 			newIntervals += intervals[i];
    154 		}
    155 	}
    156 	
    157 	window.location.search = queryString + 'i=' + newIntervals;
    158 }
    159 
    160 function createOption(t, selected) {
    161 	var option = document.createElement("option");
    162 	if (selected) {
    163 		option.selected = "true";
    164 	}
    165 	var text = document.createTextNode(t);
    166 	option.setAttribute("value", t);
    167 	option.appendChild(text);
    168 	return option;
    169 }
    170 
    171 function getFull(a) {
    172 	switch (a) {
    173 		case 'd':
    174 			return getString("interval.day");
    175 		case 'm':
    176 			return getString("interval.month");
    177 		case 'y':
    178 			return getString("interval.year");
    179 		case 'e':
    180 			return getString("interval.decade");
    181 		case 'c':
    182 			return getString("interval.century");
    183 		case 'i':
    184 			return getString("interval.millennium");
    185 		default:
    186 			return false;
    187 	}
    188 }
    189 
    190 function createQueryString(theQueryValue, except, timeline) {
    191 	var temp = '';
    192 	for(var i in theQueryValue) {
    193 		if(except != i) {
    194 			temp += i + '=' + theQueryValue[i] + '&';
    195 		}
    196 	}
    197 	if(except != 'd') {
    198 		temp += 'd=' + getTimelineDate(timeline) + '&';
    199 	}
    200 	//remove last & if no exceptions
    201 	if(!except) {
    202 		temp = temp.substr(0, temp.length -1)
    203 	}
    204 	return temp;
    205 }
    206 
    207 function setupOtherControls(div, timeline, queryString) {
    208 	var table = document.createElement("table");
    209 	
    210 	var defaultQueryValue = new Object();
    211 		defaultQueryValue['i'] = 'mye';
    212 		defaultQueryValue['t'] = 'd';
    213 		
    214 	var theQueryValue = new Object;
    215 	
    216 	if (queryString) {
    217 		var queryVars = queryString.split('&');
    218 		for (var i in queryVars) {
    219 			var [key, val] = queryVars[i].split('=');
    220 			if(val) {
    221 				switch (key) {
    222 					case 'i':
    223 						theQueryValue['i'] = val;
    224 						break;
    225 					case 't':
    226 						theQueryValue['t'] = val;
    227 						break;
    228 				}
    229 			}
    230 		}
    231 	}
    232 
    233 	var intervals = (theQueryValue['i']) ? theQueryValue['i'] : defaultQueryValue['i'];
    234 	if (intervals.length < 3) {
    235 		intervals += defaultQueryValue['i'].substr(intervals.length);
    236 	}
    237 	var dateType = (theQueryValue['t']) ? theQueryValue['t'] : defaultQueryValue['t'];
    238 	if(dateType != 'da' && dateType != 'dm') {
    239 		dateType = defaultQueryValue['t'];
    240 	}
    241 	
    242 	var tr = table.insertRow(0);
    243 	
    244 	var td = tr.insertCell(0);
    245 	td.innerHTML = getString("general.jumpToYear");
    246 	td = tr.insertCell(tr.cells.length);
    247 	td.innerHTML = getString("general.firstBand");
    248 	td = tr.insertCell(tr.cells.length);
    249 	td.innerHTML = getString("general.secondBand");;
    250 	td = tr.insertCell(tr.cells.length);
    251 	td.innerHTML = getString("general.thirdBand");;
    252 	td = tr.insertCell(tr.cells.length);
    253 	td.innerHTML = getString("general.dateType");;
    254 	td = tr.insertCell(tr.cells.length);
    255 	td.innerHTML = getString("general.timelineHeight");;
    256 
    257 	tr = table.insertRow(1);
    258 	tr.style.verticalAlign = "top";
    259 	
    260 	td = tr.insertCell(0);
    261 	var input = document.createElement("input");
    262 	input.type = "text";
    263 	input.size = "15";
    264 	input.id = "jumpYear";
    265 	input.onkeypress=doKeyPress;
    266 	td.appendChild(input);
    267 	
    268 	var options = new Array(getString("interval.day"), getString("interval.month"), getString("interval.year"),
    269 		getString("interval.decade"), getString("interval.century"), getString("interval.millennium"));
    270 	var selected = '';
    271 	var theSelects = new Array();
    272 	
    273 	td = tr.insertCell(tr.cells.length);
    274 	
    275 	var select1 = document.createElement("select");
    276 	selected = getFull(intervals[0]);
    277 	for (var i = 0; i < options.length; i++) {
    278 		select1.appendChild(createOption(options[i],(options[i] == selected)));
    279 	}
    280 	select1.onchange = function () {
    281 		changeBand(createQueryString(theQueryValue, 'i', timeline), 0, intervals, table.rows[1].cells[1].firstChild.selectedIndex);
    282 	};
    283 	td.appendChild(select1);
    284 	
    285 	td = tr.insertCell(tr.cells.length);
    286 	
    287 	var select2 = document.createElement("select");
    288 	selected = getFull(intervals[1]);
    289 	for (var i = 0; i < options.length; i++) {
    290 		select2.appendChild(createOption(options[i],(options[i] == selected)));
    291 	}
    292 	select2.onchange = function () {
    293 		changeBand(createQueryString(theQueryValue, 'i', timeline), 1, intervals, table.rows[1].cells[2].firstChild.selectedIndex);
    294 	};
    295 	td.appendChild(select2);
    296 	
    297 	td = tr.insertCell(tr.cells.length);
    298 	
    299 	var select3 = document.createElement("select");
    300 	selected = getFull(intervals[2]);
    301 	for (var i = 0; i < options.length; i++) {
    302 		select3.appendChild(createOption(options[i],(options[i] == selected)));
    303 	}
    304 	select3.onchange = function () {
    305 		changeBand(createQueryString(theQueryValue, 'i', timeline), 2, intervals, table.rows[1].cells[3].firstChild.selectedIndex);
    306 	};
    307 	td.appendChild(select3);
    308 	
    309 	
    310 	td = tr.insertCell(tr.cells.length);
    311 	options = new Array(getString("dateType.published"), Zotero.getString("itemFields.dateAdded"), getString("dateType.modified"));
    312 	var values = new Array('d', 'da', 'dm');
    313 	var select4 = document.createElement("select");
    314 		
    315 	for (var i = 0; i < options.length; i++) {
    316 		select4.appendChild(createOption(options[i],(values[i] == dateType)));
    317 	}
    318 	select4.onchange = function () {
    319 		window.location.search = createQueryString(theQueryValue, 't', timeline) + 't=' + values[table.rows[1].cells[4].firstChild.selectedIndex];
    320 	};
    321 	td.appendChild(select4);
    322 	
    323 	td = tr.insertCell(tr.cells.length);
    324 	var fitToScreen = document.createElement("button");
    325 	fitToScreen.innerHTML = getString("general.fitToScreen");
    326 	Timeline.DOM.registerEvent(fitToScreen, "click", function () {
    327 		window.location.search = createQueryString(theQueryValue, false, timeline);
    328 	});
    329 	td.appendChild(fitToScreen);
    330 	
    331 	div.appendChild(table);
    332 }
    333 
    334 /*
    335 	Everything below is from http://simile.mit.edu/timeline/examples/examples.js unless noted otherwise
    336 */
    337 
    338 function centerTimeline(date) {
    339 	tl.getBand(0).setCenterVisibleDate(Timeline.DateTime.parseGregorianDateTime(date));
    340 }
    341 
    342 function setupFilterHighlightControls(div, timeline, bandIndices, theme) {
    343 	var table = document.createElement("table");
    344 	var tr = table.insertRow(0);
    345 	
    346 	var td = tr.insertCell(0);
    347 	td.innerHTML = getString("general.filter");
    348 	
    349 	td = tr.insertCell(1);
    350 	td.innerHTML = getString("general.highlight");
    351 	
    352 	var handler = function(elmt, evt, target) {
    353 		onKeyPress(timeline, bandIndices, table);
    354 	};
    355 	
    356 	tr = table.insertRow(1);
    357 	tr.style.verticalAlign = "top";
    358 	
    359 	td = tr.insertCell(0);
    360 	
    361 	var input = document.createElement("input");
    362 	input.type = "text";
    363 	input.size = "18";//Added by Ben for Zotero
    364 	Timeline.DOM.registerEvent(input, "keypress", handler);
    365 	td.appendChild(input);
    366 	
    367 	for (var i = 0; i < theme.event.highlightColors.length; i++) {
    368 		td = tr.insertCell(i + 1);
    369 		
    370 		input = document.createElement("input");
    371 		input.type = "text";
    372 		Timeline.DOM.registerEvent(input, "keypress", handler);
    373 		td.appendChild(input);
    374 		input.size = "15";//Added by Ben for Zotero
    375 		var divColor = document.createElement("div");
    376 		divColor.style.height = "0.5em";
    377 		divColor.style.background = theme.event.highlightColors[i];
    378 		td.appendChild(divColor);
    379 	}
    380 	
    381 	td = tr.insertCell(tr.cells.length);
    382 	var button = document.createElement("button");
    383 	button.innerHTML = getString("general.clearAll");
    384 	Timeline.DOM.registerEvent(button, "click", function() {
    385 		clearAll(timeline, bandIndices, table);
    386 	});
    387 	td.appendChild(button);
    388 	
    389 	div.appendChild(table);
    390 }
    391 
    392 var timerID = null;
    393 function onKeyPress(timeline, bandIndices, table) {
    394 	if (timerID != null) {
    395 		window.clearTimeout(timerID);
    396 	}
    397 	timerID = window.setTimeout(function() {
    398 		performFiltering(timeline, bandIndices, table);
    399 	}, 300);
    400 }
    401 function cleanString(s) {
    402 	return s.replace(/^\s+/, '').replace(/\s + $/, '');
    403 }
    404 function performFiltering(timeline, bandIndices, table) {
    405 	timerID = null;
    406 	
    407 	var tr = table.rows[1];
    408 	var text = cleanString(tr.cells[0].firstChild.value);
    409 	
    410 	var filterMatcher = null;
    411 	if (text.length > 0) {
    412 		var regex = new RegExp(text, "i");
    413 		filterMatcher = function(evt) {
    414 			return regex.test(evt.getText()) || regex.test(evt.getDescription());
    415 		};
    416 	}
    417 	
    418 	var regexes = [];
    419 	var hasHighlights = false;
    420 	for (var x = 1; x < tr.cells.length - 1; x++) {
    421 		var input = tr.cells[x].firstChild;
    422 		var text2 = cleanString(input.value);
    423 		if (text2.length > 0) {
    424 			hasHighlights = true;
    425 			regexes.push(new RegExp(text2, "i"));
    426 		} else {
    427 			regexes.push(null);
    428 		}
    429 	}
    430 	var highlightMatcher = hasHighlights ? function(evt) {
    431 		var text = evt.getText();
    432 		var description = evt.getDescription();
    433 		for (var x = 0; x < regexes.length; x++) {
    434 			var regex = regexes[x];
    435 			if (regex != null && (regex.test(text) || regex.test(description))) {
    436 				return x;
    437 			}
    438 		}
    439 		return -1;
    440 	} : null;
    441 	
    442 	for (var i = 0; i < bandIndices.length; i++) {
    443 		var bandIndex = bandIndices[i];
    444 		timeline.getBand(bandIndex).getEventPainter().setFilterMatcher(filterMatcher);
    445 		timeline.getBand(bandIndex).getEventPainter().setHighlightMatcher(highlightMatcher);
    446 	}
    447 	timeline.paint();
    448 }
    449 function clearAll(timeline, bandIndices, table) {
    450 	var tr = table.rows[1];
    451 	for (var x = 0; x < tr.cells.length - 1; x++) {
    452 		tr.cells[x].firstChild.value = "";
    453 	}
    454 	
    455 	for (var i = 0; i < bandIndices.length; i++) {
    456 		var bandIndex = bandIndices[i];
    457 		timeline.getBand(bandIndex).getEventPainter().setFilterMatcher(null);
    458 		timeline.getBand(bandIndex).getEventPainter().setHighlightMatcher(null);
    459 	}
    460 	timeline.paint();
    461 }