11

OptionInspector

Objective
  • Responder
    • View
      • Inspector
        • OptionInspector
Objective.js

Sélectionnez l'alignement du texte.

  1. function OptionInspector(value, options = false) {
  2.     options = options || {};
  3.  
  4.     let tags = options.tags;
  5.  
  6.     if (typeof value !== 'string')
  7.         throw new TypeError();
  8.  
  9.     if (! (Array.isArray(tags) && tags.length > 0 && tags.every((e) => typeof e === 'string')))
  10.         throw new TypeError();
  11.  
  12.     if (tags.indexOf(value) == -1)
  13.         throw new RangeError();
  14.  
  15.     Inspector.call(this);
  16.  
  17.     this._tags = tags;
  18.  
  19.     this._value = value;
  20.  
  21.     this._inputWidgets = null;
  22. }
  23.  
  24. OptionInspector.prototype = Object.create(Inspector.prototype);
  25.  
  26. Object.defineProperty(OptionInspector.prototype, 'constructor', { value: OptionInspector, enumerable: false, writable: true });
  27.  
  28. OptionInspector.prototype.validate = function(val) {
  29.     return typeof val === 'string' && this._tags.indexOf(val) != -1;
  30. };
  31.  
  32. OptionInspector.prototype.reset = function() {
  33.     if (!this._inputWidgets)
  34.         return false;
  35.  
  36.     for (let tag in this._inputWidgets) {
  37.         if (this._inputWidgets[tag].checked) {
  38.             this._value = tag;
  39.             break;
  40.         }
  41.     }
  42.  
  43.     return true;
  44. };
  45.  
  46. OptionInspector.prototype.resetWidget = function() {
  47.     if (this._inputWidgets) {
  48.         let i = this._inputWidgets[this._value];
  49.  
  50.         if (i)
  51.             i.checked = true;
  52.     }
  53.  
  54.     return this;
  55. };
  56.  
  57. OptionInspector.prototype.setWidget = function(w) {
  58.     Inspector.prototype.setWidget.call(this, w);
  59.  
  60.     this._inputWidgets = {};
  61.  
  62.     for (let i of w.querySelectorAll('input[type="radio"]')) {
  63.         let value = i.attributes.value;
  64.  
  65.         if (value !== undefined) {
  66.             let tag = value.value;
  67.  
  68.             if (this._tags.indexOf(tag) != -1)
  69.                 this._inputWidgets[tag] = i;
  70.         }
  71.     }
  72.  
  73.     return this;
  74. };
Test
  1. <?php $text='Objective.js'; ?>
  2. <?php $font='Roboto'; ?>
  3. <?php $fontSize=32; ?>
  4. <?php $color='#333'; ?>
  5. <?php $bg='#fd1'; ?>
  1. <?php head('font', $font); ?>
  1. <?php $id=uniqid('id'); ?>
  1. .test_display {
  2.     width: 240px;
  3.     height: 135px;
  4.     display: flex;
  5.     align-items: center;
  6.     justify-content: center;
  7.     background: <?php echo $bg; ?>;
  8.     color: <?php echo $color; ?>;
  9.     font-family: "<?php echo $font; ?>", sans-serif;
  10.     font-size: <?php echo $fontSize; ?>px;
  11.     margin-bottom: 10px;
  12.     border-radius: 3px;
  13. }
  1. <div id="<?php echo $id; ?>" class="noprint">
  2. <div class="test_display"><?php echo $text; ?></div>
  3. <div class="ojs">
  4. <div class="options_panel">
  5. <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>
  6. <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>
  7. <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>
  8. </div>
  9. </div>
  10. </div>
  1. <?php head('javascript', '/objectivejs/Objective.js'); ?>
  2. <?php head('javascript', '/objectivejs/Responder.js'); ?>
  3. <?php head('javascript', '/objectivejs/View.js'); ?>
  4. <?php head('javascript', '/objectivejs/Inspector.js'); ?>
  5. <?php head('javascript', '/objectivejs/OptionInspector.js'); ?>
  1. function Tester(display, inspector) {
  2.     Responder.call(this);
  3.  
  4.     this._display = display;
  5.  
  6.     inspector.addNextResponder(this);
  7.  
  8.     this._inspector = inspector;
  9. }
  10.  
  11. Tester.prototype = Object.create(Responder.prototype);
  12.  
  13. Object.defineProperty(Tester.prototype, 'constructor', { value: Tester, enumerable: false, writable: true });
  14.  
  15. Tester.prototype.inspectorValueChanged = function(sender) {
  16.     if (sender === this._inspector) {
  17.         switch (sender.get()) {
  18.         case 'right':
  19.             this._display.style.justifyContent = 'flex-end';
  20.             break;
  21.         case 'center':
  22.             this._display.style.justifyContent = 'center';
  23.             break;
  24.         case 'left':
  25.         default:
  26.             this._display.style.justifyContent = 'flex-start';
  27.             break;
  28.         }
  29.     }
  30.  
  31.     return true;
  32. }
  1. const inspector = new OptionInspector('center', {tags: ['left', 'center', 'right']});
  2.  
  3. const container = document.querySelector('#<?php echo $id; ?>');
  4.  
  5. const display = container.querySelector('.test_display');
  6.  
  7. inspector.setManagedWidget(container.querySelector('.options_panel')).resetWidget();
  8.  
  9. const tester = new Tester(display, inspector);
VOIR AUSSI

Inspector, NumberInspector, StringInspector, SelectInspector, SelectionInspector, ImageInspector, ColorInspector

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].