12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154 |
- var UI = {};
- UI.Element = function () {};
- UI.Element.prototype = {
- setId: function ( id ) {
- this.dom.id = id;
-
- return this;
- },
- setClass: function ( name ) {
- this.dom.className = name;
- return this;
- },
- setStyle: function ( style, array ) {
- for ( var i = 0; i < array.length; i ++ ) {
- this.dom.style[ style ] = array[ i ];
- }
- },
- setDisabled: function ( value ) {
- this.dom.disabled = value;
- return this;
- },
- setTextContent: function ( value ) {
- this.dom.textContent = value;
- return this;
- }
- }
- // properties
- var properties = [ 'position', 'left', 'top', 'right', 'bottom', 'width', 'height', 'border', 'borderLeft',
- 'borderTop', 'borderRight', 'borderBottom', 'borderColor', 'display', 'overflow', 'margin', 'marginLeft', 'marginTop', 'marginRight', 'marginBottom', 'padding', 'paddingLeft', 'paddingTop', 'paddingRight', 'paddingBottom', 'color',
- 'backgroundColor', 'opacity', 'fontSize', 'fontWeight', 'textAlign', 'textTransform', 'cursor' ];
- properties.forEach( function ( property ) {
- var method = 'set' + property.substr( 0, 1 ).toUpperCase() + property.substr( 1, property.length );
- UI.Element.prototype[ method ] = function () {
- this.setStyle( property, arguments );
- return this;
- };
- } );
- // events
- var events = [ 'KeyUp', 'KeyDown', 'MouseOver', 'MouseOut', 'Click', 'Change' ];
- events.forEach( function ( event ) {
- var method = 'on' + event;
- UI.Element.prototype[ method ] = function ( callback ) {
- this.dom.addEventListener( event.toLowerCase(), callback.bind( this ), false );
- return this;
- };
- } );
- // Panel
- UI.Panel = function () {
- UI.Element.call( this );
- var dom = document.createElement( 'div' );
- dom.className = 'Panel';
- this.dom = dom;
- return this;
- };
- UI.Panel.prototype = Object.create( UI.Element.prototype );
- UI.Panel.prototype.add = function () {
- for ( var i = 0; i < arguments.length; i ++ ) {
- var argument = arguments[ i ];
- if ( argument instanceof UI.Element ) {
- this.dom.appendChild( argument.dom );
- } else {
- console.error( 'UI.Panel:', argument, 'is not an instance of UI.Element.' )
- }
- }
- return this;
- };
- UI.Panel.prototype.remove = function () {
- for ( var i = 0; i < arguments.length; i ++ ) {
-
- var argument = arguments[ i ];
- if ( argument instanceof UI.Element ) {
- this.dom.removeChild( argument.dom );
- } else {
- console.error( 'UI.Panel:', argument, 'is not an instance of UI.Element.' )
- }
- }
- return this;
- };
- UI.Panel.prototype.clear = function () {
- while ( this.dom.children.length ) {
- this.dom.removeChild( this.dom.lastChild );
- }
- };
- // Collapsible Panel
- UI.CollapsiblePanel = function () {
- UI.Panel.call( this );
- this.dom.className = 'Panel CollapsiblePanel';
- this.button = document.createElement( 'div' );
- this.button.className = 'CollapsiblePanelButton';
- this.dom.appendChild( this.button );
- var scope = this;
- this.button.addEventListener( 'click', function ( event ) {
- scope.toggle();
- }, false );
- this.content = document.createElement( 'div' );
- this.content.className = 'CollapsibleContent';
- this.dom.appendChild( this.content );
- this.isCollapsed = false;
- return this;
- };
- UI.CollapsiblePanel.prototype = Object.create( UI.Panel.prototype );
- UI.CollapsiblePanel.prototype.addStatic = function () {
- for ( var i = 0; i < arguments.length; i ++ ) {
- this.dom.insertBefore( arguments[ i ].dom, this.content );
- }
- return this;
- };
- UI.CollapsiblePanel.prototype.removeStatic = UI.Panel.prototype.remove;
- UI.CollapsiblePanel.prototype.clearStatic = function () {
- this.dom.childNodes.forEach( function ( child ) {
- if ( child !== this.content ) {
- this.dom.removeChild( child );
- }
- });
- };
- UI.CollapsiblePanel.prototype.add = function () {
- for ( var i = 0; i < arguments.length; i ++ ) {
- this.content.appendChild( arguments[ i ].dom );
- }
- return this;
- };
- UI.CollapsiblePanel.prototype.remove = function () {
- for ( var i = 0; i < arguments.length; i ++ ) {
- this.content.removeChild( arguments[ i ].dom );
- }
- return this;
- };
- UI.CollapsiblePanel.prototype.clear = function () {
- while ( this.content.children.length ) {
- this.content.removeChild( this.content.lastChild );
- }
- };
- UI.CollapsiblePanel.prototype.toggle = function() {
- this.setCollapsed( !this.isCollapsed );
- };
- UI.CollapsiblePanel.prototype.collapse = function() {
- this.setCollapsed( true );
- };
- UI.CollapsiblePanel.prototype.expand = function() {
- this.setCollapsed( false );
- };
- UI.CollapsiblePanel.prototype.setCollapsed = function( setCollapsed ) {
- if ( setCollapsed ) {
- this.dom.classList.add('collapsed');
- } else {
- this.dom.classList.remove('collapsed');
- }
- this.isCollapsed = setCollapsed;
- };
- // Text
- UI.Text = function ( text ) {
- UI.Element.call( this );
- var dom = document.createElement( 'span' );
- dom.className = 'Text';
- dom.style.cursor = 'default';
- dom.style.display = 'inline-block';
- dom.style.verticalAlign = 'middle';
- this.dom = dom;
- this.setValue( text );
- return this;
- };
- UI.Text.prototype = Object.create( UI.Element.prototype );
- UI.Text.prototype.setValue = function ( value ) {
- if ( value !== undefined ) {
- this.dom.textContent = value;
- }
- return this;
- };
- // Input
- UI.Input = function () {
- UI.Element.call( this );
- var scope = this;
- var dom = document.createElement( 'input' );
- dom.className = 'Input';
- dom.style.padding = '2px';
- dom.style.border = '1px solid #ccc';
- dom.addEventListener( 'keydown', function ( event ) {
- event.stopPropagation();
- }, false );
- this.dom = dom;
- return this;
- };
- UI.Input.prototype = Object.create( UI.Element.prototype );
- UI.Input.prototype.getValue = function () {
- return this.dom.value;
- };
- UI.Input.prototype.setValue = function ( value ) {
- this.dom.value = value;
- return this;
- };
- // TextArea
- UI.TextArea = function () {
- UI.Element.call( this );
- var scope = this;
- var dom = document.createElement( 'textarea' );
- dom.className = 'TextArea';
- dom.style.padding = '2px';
- dom.style.border = '1px solid #ccc';
- dom.spellcheck = false;
- dom.addEventListener( 'keydown', function ( event ) {
- event.stopPropagation();
- if ( event.keyCode === 9 ) {
- event.preventDefault();
- var cursor = dom.selectionStart;
- dom.value = dom.value.substring( 0, cursor ) + '\t' + dom.value.substring( cursor );
- dom.selectionStart = cursor + 1;
- dom.selectionEnd = dom.selectionStart;
- }
- }, false );
- this.dom = dom;
- return this;
- };
- UI.TextArea.prototype = Object.create( UI.Element.prototype );
- UI.TextArea.prototype.getValue = function () {
- return this.dom.value;
- };
- UI.TextArea.prototype.setValue = function ( value ) {
- this.dom.value = value;
- return this;
- };
- // Select
- UI.Select = function () {
- UI.Element.call( this );
- var scope = this;
- var dom = document.createElement( 'select' );
- dom.className = 'Select';
- dom.style.width = '64px';
- dom.style.height = '16px';
- dom.style.border = '0px';
- dom.style.padding = '0px';
- this.dom = dom;
- return this;
- };
- UI.Select.prototype = Object.create( UI.Element.prototype );
- UI.Select.prototype.setMultiple = function ( boolean ) {
- this.dom.multiple = boolean;
- return this;
- };
- UI.Select.prototype.setOptions = function ( options ) {
- var selected = this.dom.value;
- while ( this.dom.children.length > 0 ) {
- this.dom.removeChild( this.dom.firstChild );
- }
- for ( var key in options ) {
- var option = document.createElement( 'option' );
- option.value = key;
- option.innerHTML = options[ key ];
- this.dom.appendChild( option );
- }
- this.dom.value = selected;
- return this;
- };
- UI.Select.prototype.getValue = function () {
- return this.dom.value;
- };
- UI.Select.prototype.setValue = function ( value ) {
- this.dom.value = value;
- return this;
- };
- // FancySelect
- UI.FancySelect = function () {
- UI.Element.call( this );
- var scope = this;
- var dom = document.createElement( 'div' );
- dom.className = 'FancySelect';
- dom.tabIndex = 0; // keyup event is ignored without setting tabIndex
- // Broadcast for object selection after arrow navigation
- var changeEvent = document.createEvent('HTMLEvents');
- changeEvent.initEvent( 'change', true, true );
- // Prevent native scroll behavior
- dom.addEventListener( 'keydown', function (event) {
- switch ( event.keyCode ) {
- case 38: // up
- case 40: // down
- event.preventDefault();
- event.stopPropagation();
- break;
- }
- }, false);
- // Keybindings to support arrow navigation
- dom.addEventListener( 'keyup', function (event) {
- switch ( event.keyCode ) {
- case 38: // up
- case 40: // down
- scope.selectedIndex += ( event.keyCode == 38 ) ? -1 : 1;
- if ( scope.selectedIndex >= 0 && scope.selectedIndex < scope.options.length ) {
- // Highlight selected dom elem and scroll parent if needed
- scope.setValue( scope.options[ scope.selectedIndex ].value );
- scope.dom.dispatchEvent( changeEvent );
- }
- break;
- }
- }, false);
- this.dom = dom;
- this.options = [];
- this.selectedIndex = -1;
- this.selectedValue = null;
- return this;
- };
- UI.FancySelect.prototype = Object.create( UI.Element.prototype );
- UI.FancySelect.prototype.setOptions = function ( options ) {
- var scope = this;
- var changeEvent = document.createEvent( 'HTMLEvents' );
- changeEvent.initEvent( 'change', true, true );
- while ( scope.dom.children.length > 0 ) {
- scope.dom.removeChild( scope.dom.firstChild );
- }
- scope.options = [];
- for ( var i = 0; i < options.length; i ++ ) {
- var option = options[ i ];
- var div = document.createElement( 'div' );
- div.className = 'option';
- div.innerHTML = option.html;
- div.value = option.value;
- scope.dom.appendChild( div );
- scope.options.push( div );
- div.addEventListener( 'click', function ( event ) {
- scope.setValue( this.value );
- scope.dom.dispatchEvent( changeEvent );
- }, false );
- }
- return scope;
- };
- UI.FancySelect.prototype.getValue = function () {
- return this.selectedValue;
- };
- UI.FancySelect.prototype.setValue = function ( value ) {
- for ( var i = 0; i < this.options.length; i ++ ) {
- var element = this.options[ i ];
- if ( element.value === value ) {
- element.classList.add( 'active' );
- // scroll into view
- var y = element.offsetTop - this.dom.offsetTop;
- var bottomY = y + element.offsetHeight;
- var minScroll = bottomY - this.dom.offsetHeight;
- if ( this.dom.scrollTop > y ) {
- this.dom.scrollTop = y
- } else if ( this.dom.scrollTop < minScroll ) {
- this.dom.scrollTop = minScroll;
- }
- this.selectedIndex = i;
- } else {
- element.classList.remove( 'active' );
- }
- }
- this.selectedValue = value;
- return this;
- };
- // Checkbox
- UI.Checkbox = function ( boolean ) {
- UI.Element.call( this );
- var scope = this;
- var dom = document.createElement( 'input' );
- dom.className = 'Checkbox';
- dom.type = 'checkbox';
- this.dom = dom;
- this.setValue( boolean );
- return this;
- };
- UI.Checkbox.prototype = Object.create( UI.Element.prototype );
- UI.Checkbox.prototype.getValue = function () {
- return this.dom.checked;
- };
- UI.Checkbox.prototype.setValue = function ( value ) {
- if ( value !== undefined ) {
- this.dom.checked = value;
- }
- return this;
- };
- // Color
- UI.Color = function () {
- UI.Element.call( this );
- var scope = this;
- var dom = document.createElement( 'input' );
- dom.className = 'Color';
- dom.style.width = '64px';
- dom.style.height = '16px';
- dom.style.border = '0px';
- dom.style.padding = '0px';
- dom.style.backgroundColor = 'transparent';
- try {
- dom.type = 'color';
- dom.value = '#ffffff';
- } catch ( exception ) {}
- this.dom = dom;
- return this;
- };
- UI.Color.prototype = Object.create( UI.Element.prototype );
- UI.Color.prototype.getValue = function () {
- return this.dom.value;
- };
- UI.Color.prototype.getHexValue = function () {
- return parseInt( this.dom.value.substr( 1 ), 16 );
- };
- UI.Color.prototype.setValue = function ( value ) {
- this.dom.value = value;
- return this;
- };
- UI.Color.prototype.setHexValue = function ( hex ) {
- this.dom.value = "#" + ( '000000' + hex.toString( 16 ) ).slice( -6 );
- return this;
- };
- // Number
- UI.Number = function ( number ) {
- UI.Element.call( this );
- var scope = this;
- var dom = document.createElement( 'input' );
- dom.className = 'Number';
- dom.value = '0.00';
- dom.addEventListener( 'keydown', function ( event ) {
- event.stopPropagation();
- if ( event.keyCode === 13 ) dom.blur();
- }, false );
- this.min = - Infinity;
- this.max = Infinity;
- this.precision = 2;
- this.step = 1;
- this.dom = dom;
- this.setValue( number );
- var changeEvent = document.createEvent( 'HTMLEvents' );
- changeEvent.initEvent( 'change', true, true );
- var distance = 0;
- var onMouseDownValue = 0;
- var pointer = new THREE.Vector2();
- var prevPointer = new THREE.Vector2();
- var onMouseDown = function ( event ) {
- event.preventDefault();
- distance = 0;
- onMouseDownValue = parseFloat( dom.value );
- prevPointer.set( event.clientX, event.clientY );
- document.addEventListener( 'mousemove', onMouseMove, false );
- document.addEventListener( 'mouseup', onMouseUp, false );
- };
- var onMouseMove = function ( event ) {
- var currentValue = dom.value;
- pointer.set( event.clientX, event.clientY );
- distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
- var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
- dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );
- if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
- prevPointer.set( event.clientX, event.clientY );
- };
- var onMouseUp = function ( event ) {
- document.removeEventListener( 'mousemove', onMouseMove, false );
- document.removeEventListener( 'mouseup', onMouseUp, false );
- if ( Math.abs( distance ) < 2 ) {
- dom.focus();
- dom.select();
- }
- };
- var onChange = function ( event ) {
- var number = parseFloat( dom.value );
- dom.value = isNaN( number ) === false ? number : 0;
- };
- var onFocus = function ( event ) {
- dom.style.backgroundColor = '';
- dom.style.borderColor = '#ccc';
- dom.style.cursor = '';
- };
- var onBlur = function ( event ) {
- dom.style.backgroundColor = 'transparent';
- dom.style.borderColor = 'transparent';
- dom.style.cursor = 'col-resize';
- };
- dom.addEventListener( 'mousedown', onMouseDown, false );
- dom.addEventListener( 'change', onChange, false );
- dom.addEventListener( 'focus', onFocus, false );
- dom.addEventListener( 'blur', onBlur, false );
- return this;
- };
- UI.Number.prototype = Object.create( UI.Element.prototype );
- UI.Number.prototype.getValue = function () {
- return parseFloat( this.dom.value );
- };
- UI.Number.prototype.setValue = function ( value ) {
- if ( value !== undefined ) {
- this.dom.value = value.toFixed( this.precision );
- }
- return this;
- };
- UI.Number.prototype.setRange = function ( min, max ) {
- this.min = min;
- this.max = max;
- return this;
- };
- UI.Number.prototype.setPrecision = function ( precision ) {
- this.precision = precision;
- return this;
- };
- // Integer
- UI.Integer = function ( number ) {
- UI.Element.call( this );
- var scope = this;
- var dom = document.createElement( 'input' );
- dom.className = 'Number';
- dom.value = '0.00';
- dom.addEventListener( 'keydown', function ( event ) {
- event.stopPropagation();
- }, false );
- this.min = - Infinity;
- this.max = Infinity;
- this.step = 1;
- this.dom = dom;
- this.setValue( number );
- var changeEvent = document.createEvent( 'HTMLEvents' );
- changeEvent.initEvent( 'change', true, true );
- var distance = 0;
- var onMouseDownValue = 0;
- var pointer = new THREE.Vector2();
- var prevPointer = new THREE.Vector2();
- var onMouseDown = function ( event ) {
- event.preventDefault();
- distance = 0;
- onMouseDownValue = parseFloat( dom.value );
- prevPointer.set( event.clientX, event.clientY );
- document.addEventListener( 'mousemove', onMouseMove, false );
- document.addEventListener( 'mouseup', onMouseUp, false );
- };
- var onMouseMove = function ( event ) {
- var currentValue = dom.value;
- pointer.set( event.clientX, event.clientY );
- distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
- var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
- dom.value = Math.min( scope.max, Math.max( scope.min, number ) ) | 0;
- if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
- prevPointer.set( event.clientX, event.clientY );
- };
- var onMouseUp = function ( event ) {
- document.removeEventListener( 'mousemove', onMouseMove, false );
- document.removeEventListener( 'mouseup', onMouseUp, false );
- if ( Math.abs( distance ) < 2 ) {
- dom.focus();
- dom.select();
- }
- };
- var onChange = function ( event ) {
- var number = parseInt( dom.value );
- if ( isNaN( number ) === false ) {
- dom.value = number;
- }
- };
- var onFocus = function ( event ) {
- dom.style.backgroundColor = '';
- dom.style.borderColor = '#ccc';
- dom.style.cursor = '';
- };
- var onBlur = function ( event ) {
- dom.style.backgroundColor = 'transparent';
- dom.style.borderColor = 'transparent';
- dom.style.cursor = 'col-resize';
- };
- dom.addEventListener( 'mousedown', onMouseDown, false );
- dom.addEventListener( 'change', onChange, false );
- dom.addEventListener( 'focus', onFocus, false );
- dom.addEventListener( 'blur', onBlur, false );
- return this;
- };
- UI.Integer.prototype = Object.create( UI.Element.prototype );
- UI.Integer.prototype.getValue = function () {
- return parseInt( this.dom.value );
- };
- UI.Integer.prototype.setValue = function ( value ) {
- if ( value !== undefined ) {
- this.dom.value = value | 0;
- }
- return this;
- };
- UI.Integer.prototype.setRange = function ( min, max ) {
- this.min = min;
- this.max = max;
- return this;
- };
- // Break
- UI.Break = function () {
- UI.Element.call( this );
- var dom = document.createElement( 'br' );
- dom.className = 'Break';
- this.dom = dom;
- return this;
- };
- UI.Break.prototype = Object.create( UI.Element.prototype );
- // HorizontalRule
- UI.HorizontalRule = function () {
- UI.Element.call( this );
- var dom = document.createElement( 'hr' );
- dom.className = 'HorizontalRule';
- this.dom = dom;
- return this;
- };
- UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
- // Button
- UI.Button = function ( value ) {
- UI.Element.call( this );
- var scope = this;
- var dom = document.createElement( 'button' );
- dom.className = 'Button';
- this.dom = dom;
- this.dom.textContent = value;
- return this;
- };
- UI.Button.prototype = Object.create( UI.Element.prototype );
- UI.Button.prototype.setLabel = function ( value ) {
- this.dom.textContent = value;
- return this;
- };
- // Dialog
- UI.Dialog = function ( value ) {
- var scope = this;
-
- var dom = document.createElement( 'dialog' );
- if ( dom.showModal === undefined ) {
- // fallback
- dom = document.createElement( 'div' );
- dom.style.display = 'none';
- dom.showModal = function () {
- dom.style.position = 'absolute';
- dom.style.left = '100px';
- dom.style.top = '100px';
- dom.style.zIndex = 1;
- dom.style.display = '';
- };
- }
- dom.className = 'Dialog';
- this.dom = dom;
- return this;
- };
- UI.Dialog.prototype = Object.create( UI.Panel.prototype );
- UI.Dialog.prototype.showModal = function () {
- this.dom.showModal();
- return this;
- };
|