/* * Copyright 2011-2012 Univention GmbH * * http://www.univention.de/ * * All rights reserved. * * The source code of this program is made available * under the terms of the GNU Affero General Public License version 3 * (GNU AGPL V3) as published by the Free Software Foundation. * * Binary versions of this program provided by Univention to you as * well as other copyrighted, protected or trademarked materials like * Logos, graphics, fonts, specific documentations and configurations, * cryptographic keys etc. are subject to a license agreement between * you and Univention and not subject to the GNU AGPL V3. * * In the case you use this program under the terms of the GNU AGPL V3, * the program is provided in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public * License with the Debian GNU/Linux or Univention distribution in file * /usr/share/common-licenses/AGPL-3; if not, see * . */ /*global console MyError dojo dojox dijit umc */ // TODO: Add support min/max constraint: "If min=1 MiB, don't show KiB", but "If max=1 GiB, allow 0.5 GiB"? // TODO: Update value when unit changes: 1024 KiB → 1 MiB // TODO: Think more about handling fractional values: 0.5 MiB = 512 KiB // TODO: Check why static widget does not work, but dynamic dojo.place(new umc.widgets.ByteBox({id: 'bb', value: 0}).domNode, dojo.body()); works dojo.provide("umc.widgets.ByteBox"); dojo.require("umc.widgets.ContainerWidget"); dojo.require("umc.widgets.ComboBox"); dojo.require("umc.widgets.NumberSpinner"); dojo.require("umc.widgets._FormWidgetMixin"); dojo.require("umc.widgets._WidgetsInWidgetsMixin"); dojo.declare("umc.widgets.ByteBox", [ umc.widgets.ContainerWidget, umc.widgets._FormWidgetMixin, umc.widgets._WidgetsInWidgetsMixin ], { // summary: // Display human readable size in bytes name: '', label: '', required: '', disabled: false, i18nClass: 'umc.app', // list of available units units: [{ unit: 'B', factor: 1 }, { unit: 'KiB', factor: 1024 }, { unit: 'MiB', factor: 1024*1024 }, { unit: 'GiB', factor: 1024*1024*1024 }, { unit: 'TiB', factor: 1024*1024*1024*1024 }, { unit: 'PiB', factor: 1024*1024*1024*1024*1024 }], constraints: { min: 0, max: 1024*1024*1024*1024*1024*1024 - 1, }, // the widget's class name as CSS class 'class': 'umcByteBox', // internal reference to the NumberSpinner _spinBox: null, // internal reference to the ComboBox containing the units _unitBox: null, _setDisabledAttr: function(newVal) { this.disabled = newVal; if (this._spinBox && this._unitBox) { this._spinBox.set('disabled', this.disabled); this._unitBox.set('disabled', this.disabled); } }, _setRequiredAttr: function(newVal) { this.required = newVal; if (this._spinBox && this._unitBox) { this._spinBox.set('required', this.required); this._unitBox.set('required', this.required); } }, _setValueAttr: function(_val) { var unit = this.units[0]; dojo.forEach(this.units, function(iunit) { if (iunit.factor > _val) return false; unit = iunit; }); this._spinBox.set('value', _val / unit.factor); this._unitBox.set('value', unit.unit); }, _getValueAttr: function() { var factor = 1; var unit = this._unitBox.get('value'); dojo.forEach(this.units, function(iunit) { if (iunit.unit == unit) { factor = iunit.factor; return false; } }); return this._spinBox.get('value') * factor; }, postMixInProperties: function() { this.inherited(arguments); // ignore specified size classes this.sizeClass = null; }, buildRendering: function() { this.inherited(arguments); // create widgets this._spinBox = this.adopt(umc.widgets.NumberSpinner, { required: this.required, disabled: this.disabled, constraints: { min: 1, max: 1024, }, sizeClass: 'TwoThirds' }); this._unitBox = this.adopt(umc.widgets.ComboBox, { required: this.required, disabled: this.disabled, sizeClass: 'OneThird', staticValues: dojo.map(this.units, function(iunit) { return iunit.unit; }) }); // register to 'onChange' events this.connect(this._unitBox, 'onChange', function() { this.onChange(this.get('value')); }); this.connect(this._spinBox, 'onChange', function() { this.onChange(this.get('value')); }); // create layout this.addChild(this._spinBox); this.addChild(this._unitBox); this.startup(); }, isValid: function() { return this._spinBox.isValid() && this._unitBox.isValid(); }, // provide 'onChange' method stub in case it does not exist yet onChange: function(newValue, widgetName) { // event stub } });