ui.js 18.0 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4 5 6
var UI = {};

UI.Element = function () {};

UI.Element.prototype = {

M
r61  
Mr.doob 已提交
7 8 9 10 11 12 13 14
	setId: function ( id ) {

		this.dom.id = id;
		
		return this;

	},

M
Mr.doob 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	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 ];

		}

	},

M
r59  
Mr.doob 已提交
33 34 35 36 37 38 39 40
	setDisabled: function ( value ) {

		this.dom.disabled = value;

		return this;

	},

M
Mr.doob 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
	setTextContent: function ( value ) {

		this.dom.textContent = value;

		return this;

	}

}

// properties

var properties = [ 'position', 'left', 'top', 'right', 'bottom', 'width', 'height', 'border', 'borderLeft',
M
r58  
Mr.doob 已提交
54 55
'borderTop', 'borderRight', 'borderBottom', 'borderColor', 'display', 'overflow', 'margin', 'marginLeft', 'marginTop', 'marginRight', 'marginBottom', 'padding', 'paddingLeft', 'paddingTop', 'paddingRight', 'paddingBottom', 'color',
'backgroundColor', 'opacity', 'fontSize', 'fontWeight', 'textTransform', 'cursor' ];
M
Mr.doob 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

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

M
r59  
Mr.doob 已提交
72
var events = [ 'KeyUp', 'KeyDown', 'MouseOver', 'MouseOut', 'Click', 'Change' ];
M
Mr.doob 已提交
73 74 75 76 77 78 79

events.forEach( function ( event ) {

	var method = 'on' + event;

	UI.Element.prototype[ method ] = function ( callback ) {

M
r63  
Mr.doob 已提交
80 81
		this.dom.addEventListener( event.toLowerCase(), callback.bind( this ), false );

M
Mr.doob 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
		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 ++ ) {

		this.dom.appendChild( arguments[ i ].dom );

	}

	return this;

};


M
r58  
Mr.doob 已提交
118 119 120 121 122 123 124 125 126 127 128 129
UI.Panel.prototype.remove = function () {

	for ( var i = 0; i < arguments.length; i ++ ) {

		this.dom.removeChild( arguments[ i ].dom );

	}

	return this;

};

M
r63  
Mr.doob 已提交
130 131 132 133 134 135 136 137 138 139
UI.Panel.prototype.clear = function () {

	while ( this.dom.children.length ) {

		this.dom.removeChild( this.dom.lastChild );

	}

};

M
r67  
Mr.doob 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

// 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;

};

M
Mr.doob 已提交
268 269 270 271 272 273 274 275 276 277
// 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';
M
r60  
Mr.doob 已提交
278
	dom.style.verticalAlign = 'middle';
M
Mr.doob 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

	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';

M
r59  
Mr.doob 已提交
315
	dom.addEventListener( 'keydown', function ( event ) {
M
Mr.doob 已提交
316

M
r59  
Mr.doob 已提交
317
		event.stopPropagation();
M
Mr.doob 已提交
318 319 320

	}, false );

M
r59  
Mr.doob 已提交
321 322
	this.dom = dom;

M
Mr.doob 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	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';
M
r68  
Mr.doob 已提交
356
	dom.spellcheck = false;
M
Mr.doob 已提交
357

M
r59  
Mr.doob 已提交
358
	dom.addEventListener( 'keydown', function ( event ) {
M
r58  
Mr.doob 已提交
359

M
r59  
Mr.doob 已提交
360
		event.stopPropagation();
M
r58  
Mr.doob 已提交
361

M
r68  
Mr.doob 已提交
362 363 364 365 366 367 368 369 370 371 372 373
		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;

		}

M
r58  
Mr.doob 已提交
374 375
	}, false );

M
r59  
Mr.doob 已提交
376
	this.dom = dom;
M
Mr.doob 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

	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 ) {

M
r59  
Mr.doob 已提交
432 433
	var selected = this.dom.value;

M
Mr.doob 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	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 );

	}

M
r59  
Mr.doob 已提交
449 450
	this.dom.value = selected;

M
Mr.doob 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	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';
M
r62  
Mr.doob 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	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;
		}
M
r67  
Mr.doob 已提交
517

M
r62  
Mr.doob 已提交
518
	}, false);
M
Mr.doob 已提交
519 520 521 522

	this.dom = dom;

	this.options = [];
M
r62  
Mr.doob 已提交
523
	this.selectedIndex = -1;
M
Mr.doob 已提交
524 525 526 527 528 529 530 531 532 533 534 535
	this.selectedValue = null;

	return this;

};

UI.FancySelect.prototype = Object.create( UI.Element.prototype );

UI.FancySelect.prototype.setOptions = function ( options ) {

	var scope = this;

M
r59  
Mr.doob 已提交
536 537 538
	var changeEvent = document.createEvent( 'HTMLEvents' );
	changeEvent.initEvent( 'change', true, true );

M
Mr.doob 已提交
539 540 541 542 543 544 545 546
	while ( scope.dom.children.length > 0 ) {

		scope.dom.removeChild( scope.dom.firstChild );

	}

	scope.options = [];

M
r67  
Mr.doob 已提交
547
	for ( var i = 0; i < options.length; i ++ ) {
M
Mr.doob 已提交
548

M
r67  
Mr.doob 已提交
549 550 551 552 553 554 555
		var option = options[ i ];

		var div = document.createElement( 'div' );
		div.className = 'option';
		div.innerHTML = option.html;
		div.value = option.value;
		scope.dom.appendChild( div );
M
Mr.doob 已提交
556

M
r67  
Mr.doob 已提交
557
		scope.options.push( div );
M
r59  
Mr.doob 已提交
558

M
r67  
Mr.doob 已提交
559
		div.addEventListener( 'click', function ( event ) {
M
r59  
Mr.doob 已提交
560 561 562 563 564

			scope.setValue( this.value );
			scope.dom.dispatchEvent( changeEvent );

		}, false );
M
Mr.doob 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

	}

	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 ];

M
r59  
Mr.doob 已提交
584
		if ( element.value === value ) {
M
Mr.doob 已提交
585

M
r62  
Mr.doob 已提交
586
			element.classList.add( 'active' );
M
Mr.doob 已提交
587

M
r59  
Mr.doob 已提交
588 589
			// scroll into view

M
r62  
Mr.doob 已提交
590 591 592 593 594 595 596
			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
M
r59  
Mr.doob 已提交
597

M
r62  
Mr.doob 已提交
598
			} else if ( this.dom.scrollTop < minScroll ) {
M
r59  
Mr.doob 已提交
599

M
r62  
Mr.doob 已提交
600
				this.dom.scrollTop = minScroll;
M
r59  
Mr.doob 已提交
601 602 603

			}

M
r62  
Mr.doob 已提交
604 605
			this.selectedIndex = i;

M
Mr.doob 已提交
606 607
		} else {

M
r62  
Mr.doob 已提交
608
			element.classList.remove( 'active' );
M
Mr.doob 已提交
609 610 611 612 613 614 615 616 617 618 619

		}

	}

	this.selectedValue = value;

	return this;

};

M
r62  
Mr.doob 已提交
620

M
Mr.doob 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
// 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';
676

M
r63  
Mr.doob 已提交
677 678 679 680 681 682
	try {

		dom.type = 'color';
		dom.value = '#ffffff';

	} catch ( exception ) {}
M
Mr.doob 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

	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';

M
r59  
Mr.doob 已提交
733 734 735 736 737 738 739 740
	dom.addEventListener( 'keydown', function ( event ) {

		event.stopPropagation();

		if ( event.keyCode === 13 ) dom.blur();

	}, false );

M
Mr.doob 已提交
741 742 743 744 745 746
	this.min = - Infinity;
	this.max = Infinity;

	this.precision = 2;
	this.step = 1;

M
r58  
Mr.doob 已提交
747 748 749
	this.dom = dom;
	this.setValue( number );

M
r59  
Mr.doob 已提交
750 751
	var changeEvent = document.createEvent( 'HTMLEvents' );
	changeEvent.initEvent( 'change', true, true );
M
Mr.doob 已提交
752 753 754 755

	var distance = 0;
	var onMouseDownValue = 0;

M
r63  
Mr.doob 已提交
756 757 758
	var pointer = new THREE.Vector2();
	var prevPointer = new THREE.Vector2();

M
Mr.doob 已提交
759 760 761 762 763 764 765 766
	var onMouseDown = function ( event ) {

		event.preventDefault();

		distance = 0;

		onMouseDownValue = parseFloat( dom.value );

M
r63  
Mr.doob 已提交
767 768
		prevPointer.set( event.clientX, event.clientY );

M
Mr.doob 已提交
769 770 771 772 773 774 775
		document.addEventListener( 'mousemove', onMouseMove, false );
		document.addEventListener( 'mouseup', onMouseUp, false );

	};

	var onMouseMove = function ( event ) {

M
r58  
Mr.doob 已提交
776 777
		var currentValue = dom.value;

M
r63  
Mr.doob 已提交
778
		pointer.set( event.clientX, event.clientY );
M
Mr.doob 已提交
779

M
r63  
Mr.doob 已提交
780
		distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
M
Mr.doob 已提交
781

M
r58  
Mr.doob 已提交
782
		var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
M
Mr.doob 已提交
783 784 785

		dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );

M
r59  
Mr.doob 已提交
786
		if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
M
Mr.doob 已提交
787

M
r63  
Mr.doob 已提交
788 789
		prevPointer.set( event.clientX, event.clientY );

M
Mr.doob 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
	};

	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 );

M
r62  
Mr.doob 已提交
810
		dom.value = isNaN( number ) === false ? number : 0;
M
Mr.doob 已提交
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

	};

	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;

M
r58  
Mr.doob 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
	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';

M
r59  
Mr.doob 已提交
889 890 891 892 893 894
	dom.addEventListener( 'keydown', function ( event ) {

		event.stopPropagation();

	}, false );

M
r58  
Mr.doob 已提交
895 896 897 898 899 900 901 902
	this.min = - Infinity;
	this.max = Infinity;

	this.step = 1;

	this.dom = dom;
	this.setValue( number );

M
r59  
Mr.doob 已提交
903 904
	var changeEvent = document.createEvent( 'HTMLEvents' );
	changeEvent.initEvent( 'change', true, true );
M
r58  
Mr.doob 已提交
905 906 907 908

	var distance = 0;
	var onMouseDownValue = 0;

M
r63  
Mr.doob 已提交
909 910 911
	var pointer = new THREE.Vector2();
	var prevPointer = new THREE.Vector2();

M
r58  
Mr.doob 已提交
912 913 914 915 916 917 918 919
	var onMouseDown = function ( event ) {

		event.preventDefault();

		distance = 0;

		onMouseDownValue = parseFloat( dom.value );

M
r63  
Mr.doob 已提交
920 921
		prevPointer.set( event.clientX, event.clientY );

M
r58  
Mr.doob 已提交
922 923 924 925 926 927 928 929 930
		document.addEventListener( 'mousemove', onMouseMove, false );
		document.addEventListener( 'mouseup', onMouseUp, false );

	};

	var onMouseMove = function ( event ) {

		var currentValue = dom.value;

M
r63  
Mr.doob 已提交
931
		pointer.set( event.clientX, event.clientY );
M
r58  
Mr.doob 已提交
932

M
r63  
Mr.doob 已提交
933
		distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
M
r58  
Mr.doob 已提交
934 935 936 937 938

		var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;

		dom.value = Math.min( scope.max, Math.max( scope.min, number ) ) | 0;

M
r59  
Mr.doob 已提交
939
		if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
M
r58  
Mr.doob 已提交
940

M
r63  
Mr.doob 已提交
941 942
		prevPointer.set( event.clientX, event.clientY );

M
r58  
Mr.doob 已提交
943 944 945 946 947 948 949 950
	};

	var onMouseUp = function ( event ) {

		document.removeEventListener( 'mousemove', onMouseMove, false );
		document.removeEventListener( 'mouseup', onMouseUp, false );

		if ( Math.abs( distance ) < 2 ) {
M
Mr.doob 已提交
951

M
r58  
Mr.doob 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
			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;
M
Mr.doob 已提交
1009 1010 1011 1012 1013 1014 1015

	}

	return this;

};

M
r58  
Mr.doob 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024
UI.Integer.prototype.setRange = function ( min, max ) {

	this.min = min;
	this.max = max;

	return this;

};

M
Mr.doob 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063

// 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

M
r59  
Mr.doob 已提交
1064
UI.Button = function ( value ) {
M
Mr.doob 已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073

	UI.Element.call( this );

	var scope = this;

	var dom = document.createElement( 'button' );
	dom.className = 'Button';

	this.dom = dom;
M
r59  
Mr.doob 已提交
1074
	this.dom.textContent = value;
M
Mr.doob 已提交
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087

	return this;

};

UI.Button.prototype = Object.create( UI.Element.prototype );

UI.Button.prototype.setLabel = function ( value ) {

	this.dom.textContent = value;

	return this;

M
r67  
Mr.doob 已提交
1088
};