File size: 1,871 Bytes
1742de5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

// Compiled handlebars templates will go here, indexed by their ID
g_Handlebars = {}

$(document).ready(function() {
	// Register some helper functions for handlebars:
	
	// If a == b, output the text
	Handlebars.registerHelper('ifEq', function(a, b, options) {
		if (a === b) {
			return options.fn(this);
		}
		return options.inverse(this);
	});
	
	// If the current context has a key named 'item', output the text
	Handlebars.registerHelper('ifDefined', function(conditional, options) {
		if (conditional !== undefined) {
			return options.fn(this);
		}
		return options.inverse(this);
	});

	// Output a number as hex
	Handlebars.registerHelper('toHex', function(item) {
		return item ? "0x" + item.toString("16") : "";
	});

	// As soon as the page loads, we will look for all the handlebars templates and compile them. They should all have class="hbtemplate" 
	$(".hbtemplate").each(function(index) {
		g_Handlebars[this.id] = Handlebars.compile($(this).html());
	});
});

// Helper function for the array sort method, for selecting a key to sort by.
// Usage: spans.sort(sortWithKey(function(x) { return x.start; }));

function sortWithKey(sortKeyFn) {
	return function(a, b) {
		var aval = sortKeyFn(a),
			bval = sortKeyFn(b);
		if (aval < bval) { return -1; }
		if (aval > bval) { return 1; }
		return 0;
	};
}

function findElementInCollectionWithKey(collection, keyName, keyValue) {
	if (collection instanceof Array) {
		var len = collection.length;
		for(var i = 0; i < len; i++) {
			if (collection[i][keyName] == keyValue) {
				return collection[i];
			}
		}
	}
	else if (collection instanceof Object) {
		for(var key in collection) {
			if (collection.hasOwnProperty(key)) {
				if (collection[key][keyName] == keyValue) {
					return collection[key];
				}
			}
		}
	}
	return null;
}