8
OptionInspector
Objective
- Responder
- View
- Inspector
- OptionInspector
- Inspector
- View
Choose how the text is aligned.
- function OptionInspector(value, options = false) {
- options = options || {};
- let tags = options.tags;
- if (typeof value !== 'string')
- throw new TypeError();
- if (! (Array.isArray(tags) && tags.length > 0 && tags.every((e) => typeof e === 'string')))
- throw new TypeError();
- if (tags.indexOf(value) == -1)
- throw new RangeError();
- Inspector.call(this);
- this._tags = tags;
- this._value = value;
- this._inputWidgets = null;
- }
- OptionInspector.prototype = Object.create(Inspector.prototype);
- Object.defineProperty(OptionInspector.prototype, 'constructor', { value: OptionInspector, enumerable: false, writable: true });
- OptionInspector.prototype.validate = function(val) {
- return typeof val === 'string' && this._tags.indexOf(val) != -1;
- };
- OptionInspector.prototype.reset = function() {
- if (!this._inputWidgets)
- return false;
- for (let tag in this._inputWidgets) {
- if (this._inputWidgets[tag].checked) {
- this._value = tag;
- break;
- }
- }
- return true;
- };
- OptionInspector.prototype.resetWidget = function() {
- if (this._inputWidgets) {
- let i = this._inputWidgets[this._value];
- if (i)
- i.checked = true;
- }
- return this;
- };
- OptionInspector.prototype.setWidget = function(w) {
- Inspector.prototype.setWidget.call(this, w);
- this._inputWidgets = {};
- for (let i of w.querySelectorAll('input[type="radio"]')) {
- let value = i.attributes.value;
- if (value !== undefined) {
- let tag = value.value;
- if (this._tags.indexOf(tag) != -1)
- this._inputWidgets[tag] = i;
- }
- }
- return this;
- };
Test
- <?php $text='Objective.js'; ?>
- <?php $font='Roboto'; ?>
- <?php $fontSize=32; ?>
- <?php $color='#333'; ?>
- <?php $bg='#fd1'; ?>
- <?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 class="options_panel">
- <span><input type="radio" id="text_alignment_left" name="text_alignment" value="left" checked><label for="text_alignment_left"><i class="fas fa-align-left small"></i></label></span>
- <span><input type="radio" id="text_alignment_center" name="text_alignment" value="center"><label for="text_alignment_center"><i class="fas fa-align-center small"></i></label></span>
- <span><input type="radio" id="text_alignment_right" name="text_alignment" value="right"><label for="text_alignment_right"><i class="fas fa-align-right small"></i></label></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/OptionInspector.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) {
- switch (sender.get()) {
- case 'right':
- this._display.style.justifyContent = 'flex-end';
- break;
- case 'center':
- this._display.style.justifyContent = 'center';
- break;
- case 'left':
- default:
- this._display.style.justifyContent = 'flex-start';
- break;
- }
- }
- return true;
- }
- const inspector = new OptionInspector('center', {tags: ['left', 'center', 'right']});
- const container = document.querySelector('#<?php echo $id; ?>');
- const display = container.querySelector('.test_display');
- inspector.setManagedWidget(container.querySelector('.options_panel')).resetWidget();
- const tester = new Tester(display, inspector);
SEE ALSO
Inspector, NumberInspector, StringInspector, SelectInspector, SelectionInspector, ImageInspector, ColorInspector
Comments