-
-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathChoiceSwitchDialogue.ts
More file actions
87 lines (67 loc) Β· 2.72 KB
/
Copy pathChoiceSwitchDialogue.ts
File metadata and controls
87 lines (67 loc) Β· 2.72 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { ChoiceSwitchDialogue as BaseChoiceSwitchDialogue } from "../../modules/uv-dialogues-module/ChoiceSwitchDialogue";
import { IIIFEvents } from "../../IIIFEvents";
import OpenSeadragonExtension from "../../extensions/uv-openseadragon-extension/Extension";
export class ChoiceSwitchDialogue extends BaseChoiceSwitchDialogue {
open(): void {
this.$choiceList.empty();
const extension = <OpenSeadragonExtension>this.extension;
const indices = extension.getPagedIndices();
const isTwoUp = indices.length > 1;
// we can use the OSD world as the source of the current view state
const world = extension.centerPanel.viewer.world;
indices.forEach((canvasIndex) => {
const canvas = extension.helper.getCanvasByIndex(canvasIndex);
const choices = canvas.getChoices();
if (!choices.length) return;
const isFirstCanvas = indices.indexOf(canvasIndex) === 0;
// this is to update the radio buttons to reflect the current state of the OSD world
// a canvas with "zero" choices has to be counted as one
const getWorldItemCount = (canvas) => {
const numChoices = canvas.getChoices().length;
return numChoices === 0 ? 1 : numChoices;
};
const worldOffset = isFirstCanvas
? 0
: getWorldItemCount(extension.helper.getCanvasByIndex(indices[0]));
let currentChoiceIndex = 0;
for (let c = 0; c < choices.length; c++) {
const item = world.getItemAt(worldOffset + c);
if (item && item.getOpacity() === 1) {
currentChoiceIndex = c;
break;
}
}
const canvasLabel =
canvas.getLabel().getValue() || `Canvas ${canvasIndex + 1}`;
if (isTwoUp) {
const $heading = $(`<div class="choiceHeading">${canvasLabel}</div>`);
this.$choiceList.append($heading);
}
const $group = $(
`<div role="radiogroup" aria-label="${canvasLabel}"></div>`
);
choices.forEach((choice, index) => {
const label = choice.getLabel().getValue() || `Choice ${index + 1}`;
const isActive = index === currentChoiceIndex;
const $item = $(`
<label class="choiceItem">
<input type="radio" name="choice-${canvas.id}" value="${index}" ${isActive ? "checked" : ""} />
${label}
</label>
`);
$item.find("input").on("change", () => {
this.extensionHost.publish(IIIFEvents.CHOICE_CHANGE, {
canvasId: canvas.id,
choiceIndex: index,
});
});
$group.append($item);
});
this.$choiceList.append($group);
});
this.$anchor = (<OpenSeadragonExtension>(
this.extension
)).centerPanel.$choiceSwitchButton;
super.open();
}
}