-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathNetSimBitLogPanel.js
More file actions
245 lines (216 loc) · 6.62 KB
/
Copy pathNetSimBitLogPanel.js
File metadata and controls
245 lines (216 loc) · 6.62 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* @overview UI component, a log panel (used as "Sent Bits" and "Received Bits")
* that is used in the single-bit-sending configurations of the simulator.
*
* @see INetSimLogPanel for the interface implemented here.
* @see NetSimLogPanel for the component used in packet-sending mode.
*/
require('../utils'); // For Function.prototype.inherits()
import $ from 'jquery';
var i18n = require('@cdo/netsim/locale');
var markup = require('./NetSimBitLogPanel.html.ejs');
var NetSimEncodingControl = require('./NetSimEncodingControl');
var NetSimGlobals = require('./NetSimGlobals');
var logger = require('./NetSimLogger').getSingleton();
var NetSimPanel = require('./NetSimPanel');
/**
* Generator and controller for bit-log, which receives bits one at a time.
* @param {jQuery} rootDiv
* @param {Object} options
* @param {string} options.logTitle
* @param {boolean} [options.isMinimized] defaults to FALSE
* @param {boolean} [options.showReadWireButton] defaults to FALSE
* @param {NetSim} options.netsim
* @constructor
* @augments NetSimPanel
* @implements INetSimLogPanel
*/
var NetSimBitLogPanel = (module.exports = function (rootDiv, options) {
/**
* The current binary contents of the log panel
* @type {string}
* @private
*/
this.binary_ = '';
/**
* A message encoding (display) setting.
* @type {string}
* @private
*/
this.encodings_ = [];
/**
* Current chunk size (bytesize) for interpreting binary in the log.
* @type {number}
* @private
*/
this.chunkSize_ = 8;
/**
* Localized panel title
* @type {string}
* @private
*/
this.logTitle_ = options.logTitle;
/**
* Reference to the top-level NetSim controller for reading bits and
* triggering animations.
* @type {NetSim}
* @private
*/
this.netsim_ = options.netsim;
/**
* Whether this log should have a "Read Wire" button.
* @type {boolean}
* @private
*/
this.showReadWireButton_ = options.showReadWireButton;
/**
* How tall the overall panel should be when it's open (in pixels).
* Set by a dynamic resize system.
* @type {number}
* @private
*/
this.openHeight_ = 0;
// Initial render
NetSimPanel.call(this, rootDiv, {
className: 'netsim-log-panel',
panelTitle: options.logTitle,
beginMinimized: options.isMinimized,
});
});
NetSimBitLogPanel.inherits(NetSimPanel);
NetSimBitLogPanel.prototype.render = function () {
// Create boilerplate panel markup
NetSimBitLogPanel.superPrototype.render.call(this);
// Add our own content markup
var newMarkup = $(
markup({
binary: this.binary_,
enabledEncodings: this.encodings_,
chunkSize: this.chunkSize_,
showReadWireButton: this.showReadWireButton_,
})
);
this.getBody().html(newMarkup);
NetSimEncodingControl.hideRowsByEncoding(this.getBody(), this.encodings_);
this.getBody()
.find('#read-wire-button')
.click(this.onReceiveButtonPress_.bind(this));
// Add a clear button to the panel header
this.addButton(i18n.clear(), this.onClearButtonPress_.bind(this));
// Snap back to the dynamic size we've been given.
this.sizeToOpenHeight_();
};
/**
* Remove all packets from the log, resetting its state.
* @private
*/
NetSimBitLogPanel.prototype.onClearButtonPress_ = function () {
this.binary_ = '';
this.render();
};
/**
* Asynchronously fetch the wire state from remote storage, and log it.
* @param {Event} jQueryEvent
* @private
*/
NetSimBitLogPanel.prototype.onReceiveButtonPress_ = function (jQueryEvent) {
var thisButton = $(jQueryEvent.target);
if (thisButton.is('[disabled]')) {
return;
}
thisButton.attr('disabled', 'disabled');
this.netsim_.receiveBit(
function (err, message) {
if (err) {
logger.warn('Error reading wire state: ' + err.message);
thisButton.removeAttr('disabled');
return;
}
// A successful fetch with a null message means there's nothing
// on the wire. We should log its default state: off/zero
var receivedBit = '0';
if (message) {
receivedBit = message.payload;
}
this.log(receivedBit);
this.netsim_.animateReadWireState(receivedBit);
thisButton.removeAttr('disabled');
}.bind(this)
);
};
/**
* Put a message into the log.
* @param {string} binaryBit
*/
NetSimBitLogPanel.prototype.log = function (binaryBit) {
this.binary_ += binaryBit.toString();
this.render();
};
/**
* Show or hide parts of the send UI based on the currently selected encoding
* mode.
* @param {EncodingType[]} newEncodings
*/
NetSimBitLogPanel.prototype.setEncodings = function (newEncodings) {
this.encodings_ = newEncodings;
this.render();
};
/**
* Change how binary input in interpreted and formatted in the log.
* @param {number} newChunkSize
*/
NetSimBitLogPanel.prototype.setChunkSize = function (newChunkSize) {
this.chunkSize_ = newChunkSize;
this.render();
};
/**
* Sets the vertical space that this log panel should consume (including margins)
* @param {number} heightPixels
*/
NetSimBitLogPanel.prototype.setHeight = function (heightPixels) {
this.openHeight_ = heightPixels;
this.sizeToOpenHeight_();
};
/**
* Scale the scroll area inside the panel so that the whole panel
* is the desired height.
* @private
*/
NetSimBitLogPanel.prototype.sizeToOpenHeight_ = function () {
var root = this.getRoot().find('.netsim-panel');
var panelHeader = root.find('h1');
var panelBody = root.find('.panel-body');
var scrollArea = root.find('.scroll-area');
var panelMargins =
parseFloat(root.css('margin-top')) + parseFloat(root.css('margin-bottom'));
var headerHeight = panelHeader.outerHeight(true);
var panelBorders =
parseFloat(panelBody.css('border-top-width')) +
parseFloat(panelBody.css('border-bottom-width'));
var scrollMargins =
parseFloat(scrollArea.css('margin-top')) +
parseFloat(scrollArea.css('margin-bottom'));
// We set the panel height by fixing the size of its inner scrollable
// area.
var newScrollViewportHeight =
this.openHeight_ -
(panelMargins + headerHeight + panelBorders + scrollMargins);
scrollArea.height(Math.floor(newScrollViewportHeight));
};
/**
* @returns {number} vertical space that panel currently consumes (including
* margins) in pixels.
*/
NetSimBitLogPanel.prototype.getHeight = function () {
return this.getRoot().find('.netsim-panel').outerHeight(true);
};
/**
* After toggling panel visibility, trigger a layout update so send/log panel
* space is shared correctly.
* @private
* @override
*/
NetSimBitLogPanel.prototype.onMinimizerClick_ = function () {
NetSimBitLogPanel.superPrototype.onMinimizerClick_.call(this);
NetSimGlobals.updateLayout();
};