forked from RaspberryPiFoundation/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbox-modal.component.js
More file actions
188 lines (171 loc) · 6.73 KB
/
Copy pathtoolbox-modal.component.js
File metadata and controls
188 lines (171 loc) · 6.73 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* AccessibleBlockly
*
* Copyright 2016 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Angular2 Component representing the toolbox modal.
*
* @author sll@google.com (Sean Lip)
*/
goog.provide('blocklyApp.ToolboxModalComponent');
goog.require('Blockly.CommonModal');
goog.require('blocklyApp.AudioService');
goog.require('blocklyApp.KeyboardInputService');
goog.require('blocklyApp.ToolboxModalService');
goog.require('blocklyApp.TranslatePipe');
goog.require('blocklyApp.TreeService');
goog.require('blocklyApp.UtilsService');
blocklyApp.ToolboxModalComponent = ng.core.Component({
selector: 'blockly-toolbox-modal',
template: `
<div *ngIf="modalIsVisible" class="blocklyModalCurtain"
(click)="dismissModal()">
<!-- $event.stopPropagation() prevents the modal from closing when its
interior is clicked. -->
<div id="toolboxModal" class="blocklyModal" role="alertdialog"
(click)="$event.stopPropagation()" tabindex="-1"
aria-labelledby="toolboxModalHeading">
<h3 id="toolboxModalHeading">{{'SELECT_A_BLOCK'|translate}}</h3>
<div *ngFor="#toolboxCategory of toolboxCategories; #categoryIndex=index">
<h4 *ngIf="toolboxCategory.categoryName">{{toolboxCategory.categoryName}}</h4>
<div class="blocklyModalButtonContainer"
*ngFor="#block of toolboxCategory.blocks; #blockIndex=index">
<button [id]="getOptionId(getOverallIndex(categoryIndex, blockIndex))"
(click)="selectBlock(getBlock(categoryIndex, blockIndex))"
[ngClass]="{activeButton: activeButtonIndex == getOverallIndex(categoryIndex, blockIndex)}">
{{getBlockDescription(block)}}
</button>
</div>
</div>
<hr>
<div class="blocklyModalButtonContainer">
<button [id]="getCancelOptionId()" (click)="dismissModal()"
[ngClass]="{activeButton: activeButtonIndex == totalNumBlocks}">
{{'CANCEL'|translate}}
</button>
</div>
</div>
</div>
`,
pipes: [blocklyApp.TranslatePipe]
})
.Class({
constructor: [
blocklyApp.ToolboxModalService, blocklyApp.KeyboardInputService,
blocklyApp.AudioService, blocklyApp.UtilsService, blocklyApp.TreeService,
function(
toolboxModalService_, keyboardInputService_, audioService_,
utilsService_, treeService_) {
this.toolboxModalService = toolboxModalService_;
this.keyboardInputService = keyboardInputService_;
this.audioService = audioService_;
this.utilsService = utilsService_;
this.treeService = treeService_;
this.modalIsVisible = false;
this.toolboxCategories = [];
this.onSelectBlockCallback = null;
this.onDismissCallback = null;
this.firstBlockIndexes = [];
this.activeButtonIndex = -1;
this.totalNumBlocks = 0;
var that = this;
this.toolboxModalService.registerPreShowHook(
function(
toolboxCategories, onSelectBlockCallback, onDismissCallback) {
that.modalIsVisible = true;
that.toolboxCategories = toolboxCategories;
that.onSelectBlockCallback = onSelectBlockCallback;
that.onDismissCallback = onDismissCallback;
// The indexes of the buttons corresponding to the first block in
// each category, as well as the 'cancel' button at the end.
that.firstBlockIndexes = [];
that.activeButtonIndex = -1;
that.totalNumBlocks = 0;
var cumulativeIndex = 0;
that.toolboxCategories.forEach(function(category) {
that.firstBlockIndexes.push(cumulativeIndex);
cumulativeIndex += category.blocks.length;
});
that.firstBlockIndexes.push(cumulativeIndex);
that.totalNumBlocks = cumulativeIndex;
Blockly.CommonModal.setupKeyboardOverrides(that);
that.keyboardInputService.addOverride('13', function(evt) {
evt.preventDefault();
evt.stopPropagation();
if (that.activeButtonIndex == -1) {
return;
}
var button = document.getElementById(
that.getOptionId(that.activeButtonIndex));
for (var i = 0; i < that.toolboxCategories.length; i++) {
if (that.firstBlockIndexes[i + 1] > that.activeButtonIndex) {
var categoryIndex = i;
var blockIndex =
that.activeButtonIndex - that.firstBlockIndexes[i];
var block = that.getBlock(categoryIndex, blockIndex);
that.selectBlock(block);
return;
}
}
// The 'Cancel' button has been pressed.
that.dismissModal();
});
setTimeout(function() {
document.getElementById('toolboxModal').focus();
}, 150);
}
);
}
],
// Closes the modal (on both success and failure).
hideModal_: Blockly.CommonModal.hideModal,
// Focuses on the button represented by the given index.
focusOnOption: function(index) {
var button = document.getElementById(this.getOptionId(index));
button.focus();
},
// Counts the number of interactive elements for the modal.
numInteractiveElements: function() {
return this.totalNumBlocks + 1;
},
getOverallIndex: function(categoryIndex, blockIndex) {
return this.firstBlockIndexes[categoryIndex] + blockIndex;
},
getBlock: function(categoryIndex, blockIndex) {
return this.toolboxCategories[categoryIndex].blocks[blockIndex];
},
getBlockDescription: function(block) {
return this.utilsService.getBlockDescription(block);
},
// Returns the ID for the corresponding option button.
getOptionId: function(index) {
return 'toolbox-modal-option-' + index;
},
// Returns the ID for the "cancel" option button.
getCancelOptionId: function() {
return 'toolbox-modal-option-' + this.totalNumBlocks;
},
selectBlock: function(block) {
this.onSelectBlockCallback(block);
this.hideModal_();
},
// Dismisses and closes the modal.
dismissModal: function() {
this.hideModal_();
this.onDismissCallback();
}
});