Commits

Philippe Luickx committed 66aa674

Implemented a sorting on the input. One prerequisite less.
Fixed some wrong ref doing this.

Comments (0)

Files changed (2)

 // Sorted by start date
 // Make sure that every type has an array in box_color with color schemes (e.g. shades of blue)
 
-timeline_source =  [{
-		"type": "type1",
-		"title": "Title 1",
-		"subtitle": "Location 1",
-		"start_date": "2003-01-01",
-		"end_date": "2003-01-01",
-		"summary": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
-	}, {
+timeline_source_json =  [{
 		"type": "type1",
 		"title": "Title 2",
 		"subtitle": "Location 2",
 		"end_date": "2011-09-01",
 		"summary": "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.",
 	}, {
+		"type": "type1",
+		"title": "Title 1",
+		"subtitle": "Location 1",
+		"start_date": "2003-01-01",
+		"end_date": "2003-01-01",
+		"summary": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
+	}, {
 		"type": "type3",
 		"title": "Title 6",
 		"subtitle": "Location 6",
 
 window.onload = function() {
+	var timeline_source = timeline_source_json.sort(sort_by('start_date', false));
+	
 	draw_timeline(timeline_source);
 };
 
     // Keep an array of all used types, to be combined with color schemes.
     var types = [];
     
-	for (var i = 0; i < timeline_source.length; ++i) {
+	for (var i = 0; i < source.length; ++i) {
 		
-	    console.log(timeline_source[i]);
+	    console.log(source[i]);
 	    
 	    // Check if the type exists already
-	    if (!(contains(types, timeline_source[i].type))) {
+	    if (!(contains(types, source[i].type))) {
 	    	// Push it to the array so we can find it next time
-	    	types.push(timeline_source[i].type);
+	    	types.push(source[i].type);
 	    	// Deifne height
 	    	var height = timeline_height + 40 + types.length * 12;
 	    	// Draw an indicative block with the correct color
 	    		stroke: 'none',
 	    	});
 	    	// Draw the text fitting for the box
-	    	paper.text((timeline_padding / 2) + 30, height + 5, timeline_source[i].type);
+	    	paper.text((timeline_padding / 2) + 30, height + 5, source[i].type);
 	    }
 	    color_index = types.length - 1;
 	    
 	    // Define start and end date.
 	    // Dates can not be in the future.
-	    var start_date = new Date(Math.min(new Date(timeline_source[i].start_date), current_date));
-	    var end_date = new Date(Math.min(new Date(timeline_source[i].end_date), current_date));
+	    var start_date = new Date(Math.min(new Date(source[i].start_date), current_date));
+	    var end_date = new Date(Math.min(new Date(source[i].end_date), current_date));
 	    
 	    // Get the coordinates of the start date
 	    // Calculate the difference between min_date and start_date in days
 		});
 	    
 	    // Define the label
-	    var label = paper.text((box.getBBox().x + (box.getBBox().width / 2)), 50, cutLine(timeline_source[i].title,30));
+	    var label = paper.text((box.getBBox().x + (box.getBBox().width / 2)), 50, cutLine(source[i].title,30));
 	    label.attr({
 	    	fill: text_color,
 	    });
 	 // Create the tooltip
 	    var tip_config = {
 			content: {
-				text:  '<em>' + timeline_source[i].start_date + ' to ' + timeline_source[i].end_date + '</em><br/>' + timeline_source[i].summary,
+				text:  '<em>' + source[i].start_date + ' to ' + source[i].end_date + '</em><br/>' + source[i].summary,
 				title: {
-					text: timeline_source[i].title
+					text: source[i].title
 				}
 			},
 			style: {
         }
     }
     return false;
+}
+
+// Function to sort on start date
+// http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects
+var sort_by = function(field, reverse, primer){
+   var key = function (x) {return primer ? primer(x[field]) : x[field]};
+
+   return function (a,b) {
+       var A = key(a), B = key(b);
+       return (A < B ? -1 : (A > B ? 1 : 0)) * [1,-1][+!!reverse];
+   }
 }