6
RangeInspector
Objective
- Responder
- View
- Inspector
- NumberInspector
- RangeInspector
- NumberInspector
- Inspector
- View
Move the ball of the slider to the left or to the right to change the font size of the text.
- function RangeInspector(value = 0, options = false) {
- NumberInspector.call(this, value, options);
- options = options || {};
- let fixed = options.fixed;
- if (typeof fixed === 'undefined')
- fixed = false;
- else {
- if (!Number.isInteger(fixed))
- throw new TypeError();
- if (fixed < 0)
- throw new RangeError();
- }
- this._fixed = fixed;
- this._outputWidget = null;
- }
- RangeInspector.prototype = Object.create(NumberInspector.prototype);
- Object.defineProperty(RangeInspector.prototype, 'constructor', { value: RangeInspector, enumerable: false, writable: true });
- RangeInspector.prototype.resetWidget = function() {
- NumberInspector.prototype.resetWidget.call(this);
- if (this._outputWidget)
- this._outputWidget.value = this._fixed !== false ? this._value.toFixed(this._fixed) : this._value;
- return this;
- };
- RangeInspector.prototype.setWidget = function(w) {
- NumberInspector.prototype.setWidget.call(this, w);
- if (this._widget.id) {
- this._outputWidget = document.querySelector(`output[for="${this._widget.id}"]`);
- if (this._outputWidget)
- this._widget.addEventListener('change', () => this._outputWidget.value = this._fixed !== false ? this._value.toFixed(this._fixed) : this._value);
- }
- return this;
- };
Test
- <?php $text='Objective.js'; ?>
- <?php $font='Roboto'; ?>
- <?php $fontSize=32; ?>
- <?php $color='#333'; ?>
- <?php $bg='#fd1'; ?>
- <?php $min=10; ?>
- <?php $max=40; ?>
- <?php head('font', $font); ?>
- <?php $id=uniqid('id'); ?>
- .test_display {
- width: 240px;
- height: 135px;
- display: flex;
- align-items: center;
- justify-content: center;
- background: <?php echo $bg; ?>;
- color: <?php echo $color; ?>;
- font-family: "<?php echo $font; ?>", sans-serif;
- font-size: <?php echo $fontSize; ?>px;
- margin-bottom: 10px;
- border-radius: 3px;
- }
- <div id="<?php echo $id; ?>" class="noprint">
- <div class="test_display"><?php echo $text; ?></div>
- <div class="ojs">
- <div>
- <span>
- <i class="fas fa-fw fa-text-height small"></i>
- <input id="text_fontsize" type="range" min="<?php echo $min; ?>" max="<?php echo $max; ?>" step="1" value="<?php echo $fontSize; ?>"/>
- <output for="text_fontsize"><?php echo $fontSize; ?></output>
- </span>
- </div>
- </div>
- </div>
- <?php head('javascript', '/objectivejs/Objective.js'); ?>
- <?php head('javascript', '/objectivejs/Responder.js'); ?>
- <?php head('javascript', '/objectivejs/View.js'); ?>
- <?php head('javascript', '/objectivejs/Inspector.js'); ?>
- <?php head('javascript', '/objectivejs/NumberInspector.js'); ?>
- <?php head('javascript', '/objectivejs/RangeInspector.js'); ?>
- function Tester(display, inspector) {
- Responder.call(this);
- this._display = display;
- inspector.addNextResponder(this);
- this._inspector = inspector;
- }
- Tester.prototype = Object.create(Responder.prototype);
- Object.defineProperty(Tester.prototype, 'constructor', { value: Tester, enumerable: false, writable: true });
- Tester.prototype.inspectorValueChanged = function(sender) {
- if (sender === this._inspector)
- this._display.style.fontSize = `${sender.get()}px`;
- return true;
- }
- const inspector = new RangeInspector(<?php echo $fontSize; ?>, { min: <?php echo $min; ?>, max: <?php echo $max; ?> });
- const container = document.querySelector('#<?php echo $id; ?>');
- const display = container.querySelector('.test_display');
- inspector.setManagedWidget(container.querySelector('#text_fontsize')).resetWidget();
- const tester = new Tester(display, inspector);
Comments