Skip to content

Commit 05e9d79

Browse files
authored
Make the download page work on IE7
- List higher-order functions (map, filter, forEach) are not available on IE, so implement them ourselves. - Fix a bug in attachEvent. The event name must have the prefix 'on'. - Use e.srcElement instead of e.target for IE. - IE hates trailing commas, so remove them.
1 parent 689cdc7 commit 05e9d79

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

download/download-pages.rkt

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,33 @@
131131
before installing other packages.}
132132
@script/inline[type: 'text/javascript]{
133133

134+
// higher-order functions
135+
136+
function map(xs, f) {
137+
var result = [];
138+
for (var i = 0; i < xs.length; i++) {
139+
result.push(f(xs[i], i));
140+
}
141+
return result;
142+
}
143+
144+
function forEach(xs, f) {
145+
for (var i = 0; i < xs.length; i++) {
146+
f(xs[i], i);
147+
}
148+
}
149+
150+
function filter(xs, f) {
151+
var result = [];
152+
for (var i = 0; i < xs.length; i++) {
153+
if (f(xs[i], i)) {
154+
result.push(xs[i]);
155+
}
156+
}
157+
return result;
158+
}
159+
160+
134161
// big-bang for HTML
135162

136163
// bigbang :: (HTMLElement, 'state, ('state -> Elem)) -> void
@@ -172,7 +199,7 @@ var elem = null;
172199
if (tree.node.addEventListener) {
173200
tree.node.addEventListener(key, closure, false);
174201
} else {
175-
tree.node.attachEvent(key, closure);
202+
tree.node.attachEvent('on' + key, closure);
176203
}
177204
} else {
178205
tree.node.setAttribute(key, value);
@@ -182,6 +209,7 @@ var elem = null;
182209

183210
function makeClosure(value) {
184211
return function (e) {
212+
e.target = e.target || e.srcElement; // IE uses srcElement
185213
globalState = value(globalState, e);
186214
rerender();
187215
};
@@ -196,7 +224,8 @@ var elem = null;
196224
var e = document.createElement(tree.type);
197225
tree.node = e;
198226
setAttribute(tree);
199-
tree.children.forEach(function (child) {
227+
228+
forEach(tree.children, function (child) {
200229
e.appendChild(render(child));
201230
});
202231
return e;
@@ -216,7 +245,7 @@ var elem = null;
216245
if (newTree.node.removeEventListener) {
217246
newTree.node.removeEventListener(key, closure, false);
218247
} else {
219-
newTree.node.detachEvent(key, closure);
248+
newTree.node.detachEvent('on' + key, closure);
220249
}
221250
} else {
222251
newTree.node.removeAttribute(key);
@@ -260,7 +289,7 @@ var elem = null;
260289
};
261290

262291
elem = function (type, attrs, children) {
263-
return new Elem(type, attrs, children.map(function (child) {
292+
return new Elem(type, attrs, map(children, function (child) {
264293
return (typeof child === 'string') ?
265294
new Text(child) :
266295
child;
@@ -484,37 +513,37 @@ var elem = null;
484513
else return 0;
485514
}
486515
// sort the options, need to use a temporary array
487-
var tmps = platforms.map(function (platform, i) {
516+
var tmps = map(platforms, function (platform, i) {
488517
return {
489518
name: platform.platformName,
490519
index: i,
491520
platform: platform
492521
};
493522
});
494523
tmps.sort(isBetter);
495-
tmps.forEach(function (platformData, i) {
524+
forEach(tmps, function (platformData, i) {
496525
platforms[i] = platformData.platform;
497526
});
498527
}
499528

500-
allInstallers.forEach(function (dist) {
529+
forEach(allInstallers, function (dist) {
501530
orderPlatform(dist.installers);
502531
});
503532

504533
function getAllPlatforms(allInstallers, currentDist) {
505-
return allInstallers.filter(function (group) {
534+
return filter(allInstallers, function (group) {
506535
return group.dist === currentDist;
507536
})[0].installers;
508537
}
509538

510539
function getAllVariants(allPlatforms, currentPlatform) {
511-
return allPlatforms.filter(function (group) {
540+
return filter(allPlatforms, function (group) {
512541
return group.platform === currentPlatform;
513542
})[0].installers;
514543
}
515544

516545
function getPackage(allVariants, currentVariant) {
517-
return allVariants.filter(function (group) {
546+
return filter(allVariants, function (group) {
518547
return computeVariant(group) === currentVariant;
519548
})[0];
520549
}
@@ -541,7 +570,7 @@ var elem = null;
541570
return {
542571
dist: currentDist,
543572
platform: currentPlatform,
544-
variant: currentVariant,
573+
variant: currentVariant
545574
};
546575
}
547576

@@ -554,15 +583,15 @@ var elem = null;
554583
return {
555584
dist: currentDist,
556585
platform: currentPlatform,
557-
variant: currentVariant,
586+
variant: currentVariant
558587
};
559588
}
560589

561590
function handleVariantChange(state, e) {
562591
return {
563592
dist: state.dist,
564593
platform: state.platform,
565-
variant: e.target.value,
594+
variant: e.target.value
566595
};
567596
}
568597

@@ -572,7 +601,7 @@ var elem = null;
572601
elem('div', {}, [
573602
'Distribution: ',
574603
elem('select', {onchange: handleDistChange},
575-
allInstallers.map(function (group) {
604+
map(allInstallers, function (group) {
576605
return elem('option',
577606
group.dist === currentDist ?
578607
{selected: 'selected', value: group.dist} :
@@ -583,22 +612,22 @@ var elem = null;
583612
elem('div', {}, [
584613
'Platform: ',
585614
elem('select', {onchange: handlePlatformChange},
586-
allPlatforms.map(function (group) {
615+
map(allPlatforms, function (group) {
587616
return elem('option',
588617
group.platform === currentPlatform ?
589618
{selected: 'selected', value: group.platform} :
590619
{value: group.platform},
591620
[group.platformName]);
592621
}))
593-
]),
622+
])
594623
];
595624

596625
if (allVariants.length !== 1) {
597626
children.push(
598627
elem('div', {}, [
599628
'Variant: ',
600629
elem('select', {onchange: handleVariantChange},
601-
allVariants.map(function (group) {
630+
map(allVariants, function (group) {
602631
var theVariant = computeVariant(group);
603632
return elem('option',
604633
theVariant === currentVariant ?
@@ -620,7 +649,7 @@ var elem = null;
620649
bigbang(document.getElementById('control'), {
621650
dist: currentDist,
622651
platform: currentPlatform,
623-
variant: currentVariant,
652+
variant: currentVariant
624653
}, toDraw);
625654
}
626655

0 commit comments

Comments
 (0)