9
StringInspector
Objective
- Responder
- View
- Inspector
- StringInspector
- Inspector
- View
Tapez Salut !
et appuyez sur Entrée. La longueur du texte est limitée à 12 caractères. Essayez d'entrer <b>Bold</b>
. Les balises HTML sont échappées.
- function StringInspector(value = '', options = false) {
- options = options || {};
- let min = options.min;
- let max = options.max;
- let trim = options.trim === undefined || options.trim ? true : false;
- let escapeHTML = options.escapeHTML === undefined || options.escapeHTML ? true : false;
- let required = options.required ? true : false;
- if (typeof value !== 'string')
- throw new TypeError();
- if (! (typeof min === 'undefined' || typeof min === 'number'))
- throw new TypeError();
- if (! (typeof max === 'undefined' || typeof max === 'number'))
- throw new TypeError();
- if (typeof min === 'number' && typeof max === 'number' && min > max)
- throw new RangeError();
- if (typeof min === 'number' && value < min)
- throw new RangeError();
- if (typeof max === 'number' && value > max)
- throw new RangeError();
- if (trim)
- value = value.trim();
- if (escapeHTML)
- value = StringInspector._escapeHTML(value);
- if (this._min !== undefined && value.length < this._min)
- throw new RangeError();
- if (this._max !== undefined && value.length > this._max)
- throw new RangeError();
- Inspector.call(this);
- this._min = min;
- this._max = max;
- this._trim = trim;
- this._escapeHTML = escapeHTML;
- this._required = required;
- this._value = value;
- }
- StringInspector.prototype = Object.create(Inspector.prototype);
- Object.defineProperty(StringInspector.prototype, 'constructor', { value: StringInspector, enumerable: false, writable: true });
- StringInspector.prototype.validate = function(val) {
- if (typeof val !== "string")
- return false;
- if (this._trim)
- val = val.trim();
- if (this._min !== undefined && val.length < this._min)
- return false;
- if (this._max !== undefined && val.length > this._max)
- return false;
- return true;
- };
- StringInspector.prototype.normalize = function(val) {
- if (this._trim)
- val = val.trim();
- if (this._escapeHTML)
- val = StringInspector._escapeHTML(val);
- return val;
- };
- StringInspector.prototype.setWidget = function(w) {
- Inspector.prototype.setWidget.call(this, w);
- if (this._min !== undefined)
- this._widget.minLength = this._min;
- if (this._max !== undefined)
- this._widget.maxLength = this._max;
- this._widget.required = this._required === true;
- return this;
- };
- StringInspector._escapeHTML = function(s) {
- const map = {
- '&': '&',
- '<': '<',
- '>': '>'
- };
- return s.replace(/[&<>]/g, function(c) { return map[c]; });
- };
Test
- <?php $text='Objective.js'; ?>
- <?php $font='Roboto'; ?>
- <?php $fontSize=32; ?>
- <?php $color='#333'; ?>
- <?php $bg='#fd1'; ?>
- <?php $minlen=2; ?>
- <?php $maxlen=12; ?>
- <?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>
- <input id="text_input" type="text" size="10" maxlength="<?php echo $maxlen; ?>" spellcheck="false"/>
- </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/StringInspector.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.innerHTML = `<span>${sender.get()}</span>`;
- return true;
- }
- const inspector = new StringInspector('<?php echo $text; ?>', { min: <?php echo $minlen; ?>, max: <?php echo $maxlen; ?>, trim: true, required: true, escapeHTML: true });
- const container = document.querySelector('#<?php echo $id; ?>');
- const display = container.querySelector('.test_display');
- inspector.setManagedWidget(container.querySelector('#text_input')).resetWidget();
- const tester = new Tester(display, inspector);
VOIR AUSSI
Inspector, BooleanInspector, NumberInspector, SelectInspector, OptionInspector, SelectionInspector, ImageInspector, ColorInspector
Commentaires