13
DimensionInspector
Objective
- Responder
- View
- Inspector
- DimensionInspector
- Inspector
- View
Changez la largeur ou la hauteur de l'affichage. L'autre valeur est automatiquement calculée afin de préserver le rapport d'aspect 4:3 de l'affichage. La dimension est contrainte entre 120x90 et 480x360.
- function DimensionInspector(width = 0, height = 0, options = false) {
- if (!Number.isInteger(width))
- throw new TypeError();
- if (!Number.isInteger(height))
- throw new TypeError();
- options = options || {};
- let ratio = this.validateOptions(width, height, options);
- let minWidth = options.minWidth;
- let maxWidth = options.maxWidth;
- let minHeight = options.minHeight;
- let maxHeight = options.maxHeight;
- if (width < minWidth || (maxWidth !== 'undefined' && width > maxWidth))
- throw new RangeError();
- if (height < minHeight || (maxHeight !== 'undefined' && height > maxHeight))
- throw new RangeError();
- Inspector.call(this);
- const widthInspector = new NumberInspector(width, {min: minWidth, max: maxWidth});
- const heightInspector = new NumberInspector(height, {min: minHeight, max: maxHeight});
- this._widthInspector = widthInspector.addNextResponder(this);
- this._heightInspector = heightInspector.addNextResponder(this);
- this._ratio = ratio;
- }
- DimensionInspector.prototype = Object.create(Inspector.prototype);
- Object.defineProperty(DimensionInspector.prototype, 'constructor', { value: DimensionInspector, enumerable: false, writable: true });
- Object.defineProperty(DimensionInspector.prototype, 'ratio', {
- get: function() {
- return this._ratio;
- }
- });
- Object.defineProperty(DimensionInspector.prototype, 'minWidth', {
- get: function() {
- return this._widthInspector.min;
- }
- });
- Object.defineProperty(DimensionInspector.prototype, 'maxWidth', {
- get: function() {
- return this._widthInspector.max;
- }
- });
- Object.defineProperty(DimensionInspector.prototype, 'minHeight', {
- get: function() {
- return this._heightInspector.min;
- }
- });
- Object.defineProperty(DimensionInspector.prototype, 'maxHeight', {
- get: function() {
- return this._heightInspector.max;
- }
- });
- DimensionInspector.prototype.validate = function(val) {
- return Array.isArray(val) && val.length == 2 && this._widthInspector.validate(val[0]) && this._heightInspector.validate(val[1]);
- };
- DimensionInspector.prototype.get = function() {
- return [this._widthInspector.get(), this._heightInspector.get()];
- };
- DimensionInspector.prototype.set = function(val) {
- if (!this.validate(val))
- return false;
- const [width, height] = val;
- if (this._ratio != DimensionInspector._computeRatio(width, height))
- return false;
- if (this._minWidth !== 'undefined' && width < this._minWidth)
- return false;
- if (this._maxWidth !== 'undefined' && width > this._maxWidth)
- return false;
- if (this._minHeight !== 'undefined' && height < this._minHeight)
- return false;
- if (this._maxHeight !== 'undefined' && height > this._maxHeight)
- return false;
- this._widthInspector.set(width);
- this._heightInspector.set(height);
- if (this.interfaced())
- this.resetWidget();
- return true;
- };
- DimensionInspector.prototype.setOptions = function(width, height, options) {
- options = options || {};
- let ratio = this.validateOptions(width, height, options);
- this._widthInspector.min = options.minWidth;
- this._widthInspector.max = options.maxWidth;
- this._heightInspector.min = options.minHeight;
- this._heightInspector.max = options.maxHeight;
- if (this._ratio !== ratio) {
- this._ratio = ratio;
- if (ratio !== 0) {
- if (width >= height)
- this.adjustHeight();
- else
- this.adjustWidth();
- }
- }
- return this;
- };
- DimensionInspector.prototype.validateOptions = function(width, height, options) {
- let ratio = DimensionInspector._computeRatio(width, height);
- let minWidth = options.minWidth;
- let maxWidth = options.maxWidth;
- let minHeight = options.minHeight;
- let maxHeight = options.maxHeight;
- if (typeof minWidth === 'undefined')
- minWidth = 0;
- else if (!Number.isInteger(minWidth))
- throw new TypeError();
- if (typeof minHeight === 'undefined')
- minHeight = 0;
- else if (!Number.isInteger(minHeight))
- throw new TypeError();
- if (! (typeof maxWidth === 'undefined' || Number.isInteger(maxWidth)))
- throw new TypeError();
- if (! (typeof maxHeight === 'undefined' || Number.isInteger(maxHeight)))
- throw new TypeError();
- if (maxWidth !== 'undefined' && minWidth > maxWidth)
- throw new RangeError();
- if (maxHeight !== 'undefined' && minHeight > maxHeight)
- throw new RangeError();
- if (ratio) {
- if (maxWidth && maxHeight) {
- if (ratio != DimensionInspector._computeRatio(maxWidth, maxHeight))
- throw new RangeError();
- }
- else if (maxWidth)
- maxHeight = maxWidth / ratio;
- else if (maxHeight)
- maxWidth = maxHeight * ratio;
- if (minWidth && minHeight) {
- if (ratio != DimensionInspector._computeRatio(minWidth, minHeight))
- throw new RangeError();
- }
- else if (minWidth)
- minHeight = minWidth / ratio;
- else if (minHeight)
- minWidth = minHeight * ratio;
- }
- options.minWidth = minWidth;
- options.maxWidth = maxWidth;
- options.minHeight = minHeight;
- options.maxHeight = maxHeight;
- return ratio;
- };
- DimensionInspector.prototype.adjustWidth = function() {
- if (this._ratio)
- this._widthInspector.set(2 * Math.round(this._heightInspector.get() * this._ratio / 2));
- };
- DimensionInspector.prototype.adjustHeight = function() {
- if (this._ratio)
- this._heightInspector.set(2 * Math.round(this._widthInspector.get() / this._ratio / 2));
- };
- DimensionInspector.prototype.inspectorValueChanged = function(sender) {
- if (sender === this._widthInspector)
- this.adjustHeight();
- else if (sender === this._heightInspector)
- this.adjustWidth();
- this.nextRespondTo('inspectorValueChanged', this);
- return true;
- };
- DimensionInspector.prototype.resetWidget = function() {
- this._widthInspector.resetWidget();
- this._heightInspector.resetWidget();
- return true;
- };
- DimensionInspector.prototype.setWidget = function(w) {
- let wlist = w.querySelectorAll('input[type=number]');
- if (wlist.length != 2)
- throw new TypeError();
- this._widthInspector.setWidget(wlist[0]);
- this._heightInspector.setWidget(wlist[1]);
- View.prototype.setWidget.call(this, w);
- return this;
- };
- DimensionInspector._computeRatio = (w, h) => w !== 0 && h !== 0 ? Math.round(w / h * 100) / 100 : 0;
Test
- <?php $bg='#fd1'; ?>
- <?php $width=240; ?>
- <?php $height=180; ?>
- <?php $minWidth=120; ?>
- <?php $maxWidth=480; ?>
- <?php $minHeight=90; ?>
- <?php $maxHeight=360; ?>
- <?php $id=uniqid('id'); ?>
- .test_display {
- width: <?php echo $width; ?>px;
- height: <?php echo $height; ?>px;
- background: <?php echo $bg; ?>;
- margin-bottom: 10px;
- border-radius: 3px;
- }
- .size_panel > input {
- width: 4em;
- }
- <div id="<?php echo $id; ?>" class="noprint">
- <div class="test_display"></div>
- <div class="ojs">
- <div>
- <span class="size_panel">
- <input type="number" step="40"/> <i class="fas fa-arrows-alt-h small"></i>
- <input type="number" step="30"/> <i class="fas fa-arrows-alt-v small"></i>
- </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/DimensionInspector.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) {
- let [w, h] = sender.get();
- this._display.style.width = `${w}px`;
- this._display.style.height = `${h}px`;
- }
- return true;
- }
- const inspector = new DimensionInspector(<?php echo $width; ?>, <?php echo $height; ?>, {minWidth: <?php echo $minWidth; ?>, maxWidth: <?php echo $maxWidth; ?>, minHeight: <?php echo $minHeight; ?>, maxHeight: <?php echo $maxHeight; ?>});
- const container = document.querySelector('#<?php echo $id; ?>');
- const display = container.querySelector('.test_display');
- inspector.setManagedWidget(container.querySelector('.size_panel')).resetWidget();
- const tester = new Tester(display, inspector);
Commentaires