ui.js 17.6 KB
Newer Older
M
r69  
Mr.doob 已提交
1 2 3 4
/**
 * @author mrdoob / http://mrdoob.com/
 */

M
Mr.doob 已提交
5 6
var UI = {};

M
r70  
Mr.doob 已提交
7 8 9 10 11
UI.Element = function ( dom ) {

	this.dom = dom;

};
M
Mr.doob 已提交
12 13 14

UI.Element.prototype = {

M
r73  
Mr.doob 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	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.Element:', argument, 'is not an instance of UI.Element.' )

			}

		}

		return this;

	},

	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.Element:', argument, 'is not an instance of UI.Element.' )

			}

		}

		return this;

	},

	clear: function () {

		while ( this.dom.children.length ) {

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

		}

	},

M
r61  
Mr.doob 已提交
69 70 71
	setId: function ( id ) {

		this.dom.id = id;
M
r71  
Mr.doob 已提交
72

M
r61  
Mr.doob 已提交
73 74 75 76
		return this;

	},

M
Mr.doob 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	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
r73  
Mr.doob 已提交
93 94
		return this;

M
Mr.doob 已提交
95 96
	},

M
r59  
Mr.doob 已提交
97 98 99 100 101 102 103 104
	setDisabled: function ( value ) {

		this.dom.disabled = value;

		return this;

	},

M
Mr.doob 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117
	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 已提交
118
'borderTop', 'borderRight', 'borderBottom', 'borderColor', 'display', 'overflow', 'margin', 'marginLeft', 'marginTop', 'marginRight', 'marginBottom', 'padding', 'paddingLeft', 'paddingTop', 'paddingRight', 'paddingBottom', 'color',
M
r70  
Mr.doob 已提交
119
'backgroundColor', 'opacity', 'fontSize', 'fontWeight', 'textAlign', 'textDecoration', 'textTransform', 'cursor', 'zIndex' ];
M
Mr.doob 已提交
120 121 122 123 124 125 126 127

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 );
M
r73  
Mr.doob 已提交
128

M
Mr.doob 已提交
129 130 131 132 133 134 135 136
		return this;

	};

} );

// events

M
r69  
Mr.doob 已提交
137
var events = [ 'KeyUp', 'KeyDown', 'MouseOver', 'MouseOut', 'Click', 'DblClick', 'Change' ];
M
Mr.doob 已提交
138 139 140 141 142 143 144

events.forEach( function ( event ) {

	var method = 'on' + event;

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

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

M
Mr.doob 已提交
147 148 149 150 151 152
		return this;

	};

} );

M
r73  
Mr.doob 已提交
153
// Span
M
Mr.doob 已提交
154

M
r73  
Mr.doob 已提交
155
UI.Span = function () {
M
Mr.doob 已提交
156 157 158

	UI.Element.call( this );

M
r73  
Mr.doob 已提交
159
	this.dom = document.createElement( 'span' );
M
Mr.doob 已提交
160 161 162 163 164

	return this;

};

M
r73  
Mr.doob 已提交
165 166
UI.Span.prototype = Object.create( UI.Element.prototype );
UI.Span.prototype.constructor = UI.Span;
M
Mr.doob 已提交
167

M
r69  
Mr.doob 已提交
168

M
r73  
Mr.doob 已提交
169
// Panel
M
r69  
Mr.doob 已提交
170

M
r73  
Mr.doob 已提交
171
UI.Panel = function () {
M
r58  
Mr.doob 已提交
172

M
r73  
Mr.doob 已提交
173
	UI.Element.call( this );
M
r69  
Mr.doob 已提交
174

M
r73  
Mr.doob 已提交
175 176
	var dom = document.createElement( 'div' );
	dom.className = 'Panel';
M
r58  
Mr.doob 已提交
177

M
r73  
Mr.doob 已提交
178
	this.dom = dom;
M
r58  
Mr.doob 已提交
179 180 181 182 183

	return this;

};

M
r73  
Mr.doob 已提交
184 185
UI.Panel.prototype = Object.create( UI.Element.prototype );
UI.Panel.prototype.constructor = UI.Panel;
M
r63  
Mr.doob 已提交
186

M
r67  
Mr.doob 已提交
187 188 189 190 191 192 193

// Collapsible Panel

UI.CollapsiblePanel = function () {

	UI.Panel.call( this );

M
r69  
Mr.doob 已提交
194
	this.setClass( 'Panel Collapsible' );
M
r67  
Mr.doob 已提交
195 196 197

	var scope = this;

M
r69  
Mr.doob 已提交
198 199 200
	this.static = new UI.Panel();
	this.static.setClass( 'Static' );
	this.static.onClick( function () {
M
r67  
Mr.doob 已提交
201
		scope.toggle();
M
r69  
Mr.doob 已提交
202 203
	} );
	this.dom.appendChild( this.static.dom );
M
r67  
Mr.doob 已提交
204

M
r69  
Mr.doob 已提交
205 206 207
	this.contents = new UI.Panel();
	this.contents.setClass( 'Content' );
	this.dom.appendChild( this.contents.dom );
M
r67  
Mr.doob 已提交
208

M
r69  
Mr.doob 已提交
209 210 211
	var button = new UI.Panel();
	button.setClass( 'Button' );
	this.static.add( button );
M
r67  
Mr.doob 已提交
212 213 214 215 216 217 218 219

	this.isCollapsed = false;

	return this;

};

UI.CollapsiblePanel.prototype = Object.create( UI.Panel.prototype );
M
r70  
Mr.doob 已提交
220
UI.CollapsiblePanel.prototype.constructor = UI.CollapsiblePanel;
M
r67  
Mr.doob 已提交
221 222 223

UI.CollapsiblePanel.prototype.addStatic = function () {

M
r69  
Mr.doob 已提交
224 225
	this.static.add.apply( this.static, arguments );
	return this;
M
r67  
Mr.doob 已提交
226

M
r69  
Mr.doob 已提交
227
};
M
r67  
Mr.doob 已提交
228

M
r69  
Mr.doob 已提交
229
UI.CollapsiblePanel.prototype.removeStatic = function () {
M
r67  
Mr.doob 已提交
230

M
r69  
Mr.doob 已提交
231
	this.static.remove.apply( this.static, arguments );
M
r67  
Mr.doob 已提交
232 233 234 235 236 237
	return this;

};

UI.CollapsiblePanel.prototype.clearStatic = function () {

M
r69  
Mr.doob 已提交
238 239
	this.static.clear();
	return this;
M
r67  
Mr.doob 已提交
240 241 242 243 244

};

UI.CollapsiblePanel.prototype.add = function () {

M
r69  
Mr.doob 已提交
245
	this.contents.add.apply( this.contents, arguments );
M
r67  
Mr.doob 已提交
246 247 248 249 250 251
	return this;

};

UI.CollapsiblePanel.prototype.remove = function () {

M
r69  
Mr.doob 已提交
252
	this.contents.remove.apply( this.contents, arguments );
M
r67  
Mr.doob 已提交
253 254 255 256 257 258
	return this;

};

UI.CollapsiblePanel.prototype.clear = function () {

M
r69  
Mr.doob 已提交
259 260
	this.contents.clear();
	return this;
M
r67  
Mr.doob 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

};

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

};

M
r69  
Mr.doob 已提交
282
UI.CollapsiblePanel.prototype.setCollapsed = function( boolean ) {
M
r67  
Mr.doob 已提交
283

M
r69  
Mr.doob 已提交
284
	if ( boolean ) {
M
r67  
Mr.doob 已提交
285

M
r69  
Mr.doob 已提交
286
		this.dom.classList.add( 'collapsed' );
M
r67  
Mr.doob 已提交
287 288 289

	} else {

M
r69  
Mr.doob 已提交
290
		this.dom.classList.remove( 'collapsed' );
M
r67  
Mr.doob 已提交
291 292 293

	}

M
r69  
Mr.doob 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306
	this.isCollapsed = boolean;

	if ( this.onCollapsedChangeCallback !== undefined ) {

		this.onCollapsedChangeCallback( boolean );

	}

};

UI.CollapsiblePanel.prototype.onCollapsedChange = function ( callback ) {

	this.onCollapsedChangeCallback = callback;
M
r67  
Mr.doob 已提交
307 308 309

};

M
Mr.doob 已提交
310 311 312 313 314 315 316 317 318 319
// 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 已提交
320
	dom.style.verticalAlign = 'middle';
M
Mr.doob 已提交
321 322 323 324 325 326 327 328 329

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

	return this;

};

UI.Text.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
330 331 332 333 334 335 336
UI.Text.prototype.constructor = UI.Text;

UI.Text.prototype.getValue = function () {

	return this.dom.textContent;

};
M
Mr.doob 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

UI.Text.prototype.setValue = function ( value ) {

	if ( value !== undefined ) {

		this.dom.textContent = value;

	}

	return this;

};


// Input

M
r70  
Mr.doob 已提交
353
UI.Input = function ( text ) {
M
Mr.doob 已提交
354 355 356 357 358 359 360 361

	UI.Element.call( this );

	var scope = this;

	var dom = document.createElement( 'input' );
	dom.className = 'Input';
	dom.style.padding = '2px';
M
r71  
Mr.doob 已提交
362
	dom.style.border = '1px solid transparent';
M
Mr.doob 已提交
363

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

M
r59  
Mr.doob 已提交
366
		event.stopPropagation();
M
Mr.doob 已提交
367 368 369

	}, false );

M
r59  
Mr.doob 已提交
370
	this.dom = dom;
M
r70  
Mr.doob 已提交
371
	this.setValue( text );
M
r59  
Mr.doob 已提交
372

M
Mr.doob 已提交
373 374 375 376 377
	return this;

};

UI.Input.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
378
UI.Input.prototype.constructor = UI.Input;
M
Mr.doob 已提交
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

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';
M
r68  
Mr.doob 已提交
406
	dom.spellcheck = false;
M
Mr.doob 已提交
407

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

M
r59  
Mr.doob 已提交
410
		event.stopPropagation();
M
r58  
Mr.doob 已提交
411

M
r68  
Mr.doob 已提交
412 413 414 415 416 417 418 419 420 421 422 423
		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 已提交
424 425
	}, false );

M
r59  
Mr.doob 已提交
426
	this.dom = dom;
M
Mr.doob 已提交
427 428 429 430 431 432

	return this;

};

UI.TextArea.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
433
UI.TextArea.prototype.constructor = UI.TextArea;
M
Mr.doob 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

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';
M
r71  
Mr.doob 已提交
460
	dom.style.padding = '2px';
M
Mr.doob 已提交
461 462 463 464 465 466 467 468

	this.dom = dom;

	return this;

};

UI.Select.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
469
UI.Select.prototype.constructor = UI.Select;
M
Mr.doob 已提交
470 471 472 473 474 475 476 477 478 479 480

UI.Select.prototype.setMultiple = function ( boolean ) {

	this.dom.multiple = boolean;

	return this;

};

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

M
r59  
Mr.doob 已提交
481 482
	var selected = this.dom.value;

M
Mr.doob 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
	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 已提交
498 499
	this.dom.value = selected;

M
Mr.doob 已提交
500 501 502 503 504 505 506 507 508 509 510 511
	return this;

};

UI.Select.prototype.getValue = function () {

	return this.dom.value;

};

UI.Select.prototype.setValue = function ( value ) {

M
r69  
Mr.doob 已提交
512 513 514 515 516 517 518
	value = String( value );

	if ( this.dom.value !== value ) {

		this.dom.value = value;

	}
M
Mr.doob 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

	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 );
M
r70  
Mr.doob 已提交
544
UI.Checkbox.prototype.constructor = UI.Checkbox;
M
Mr.doob 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

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

M
r63  
Mr.doob 已提交
581 582 583 584 585 586
	try {

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

	} catch ( exception ) {}
M
Mr.doob 已提交
587 588 589 590 591 592 593 594

	this.dom = dom;

	return this;

};

UI.Color.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
595
UI.Color.prototype.constructor = UI.Color;
M
Mr.doob 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618

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

M
r69  
Mr.doob 已提交
619
	this.dom.value = '#' + ( '000000' + hex.toString( 16 ) ).slice( -6 );
M
Mr.doob 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637

	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 已提交
638 639 640 641 642 643 644 645
	dom.addEventListener( 'keydown', function ( event ) {

		event.stopPropagation();

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

	}, false );

M
Mr.doob 已提交
646 647 648 649 650 651
	this.min = - Infinity;
	this.max = Infinity;

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

M
r58  
Mr.doob 已提交
652 653 654
	this.dom = dom;
	this.setValue( number );

M
r59  
Mr.doob 已提交
655 656
	var changeEvent = document.createEvent( 'HTMLEvents' );
	changeEvent.initEvent( 'change', true, true );
M
Mr.doob 已提交
657 658 659 660

	var distance = 0;
	var onMouseDownValue = 0;

M
r71  
Mr.doob 已提交
661 662
	var pointer = [ 0, 0 ];
	var prevPointer = [ 0, 0 ];
M
r63  
Mr.doob 已提交
663

M
Mr.doob 已提交
664 665 666 667 668 669 670 671
	var onMouseDown = function ( event ) {

		event.preventDefault();

		distance = 0;

		onMouseDownValue = parseFloat( dom.value );

M
r71  
Mr.doob 已提交
672
		prevPointer = [ event.clientX, event.clientY ];
M
r63  
Mr.doob 已提交
673

M
Mr.doob 已提交
674 675 676 677 678 679 680
		document.addEventListener( 'mousemove', onMouseMove, false );
		document.addEventListener( 'mouseup', onMouseUp, false );

	};

	var onMouseMove = function ( event ) {

M
r58  
Mr.doob 已提交
681 682
		var currentValue = dom.value;

M
r71  
Mr.doob 已提交
683
		pointer = [ event.clientX, event.clientY ];
M
Mr.doob 已提交
684

M
r71  
Mr.doob 已提交
685
		distance += ( pointer[ 0 ] - prevPointer[ 0 ] ) - ( pointer[ 1 ] - prevPointer[ 1 ] );
M
Mr.doob 已提交
686

M
r58  
Mr.doob 已提交
687
		var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
M
Mr.doob 已提交
688 689 690

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

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

M
r71  
Mr.doob 已提交
693
		prevPointer = [ event.clientX, event.clientY ];
M
r63  
Mr.doob 已提交
694

M
Mr.doob 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	};

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

M
r69  
Mr.doob 已提交
713 714 715
		var value = 0;

		try {
M
Mr.doob 已提交
716

M
r69  
Mr.doob 已提交
717 718 719 720 721 722 723 724 725
			value = eval( dom.value );

		} catch ( error ) {

			console.error( error.message );

		}

		dom.value = parseFloat( value );
M
Mr.doob 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

	};

	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 );
M
r70  
Mr.doob 已提交
755
UI.Number.prototype.constructor = UI.Number;
M
Mr.doob 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

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 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
	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 已提交
805 806 807 808 809 810
	dom.addEventListener( 'keydown', function ( event ) {

		event.stopPropagation();

	}, false );

M
r58  
Mr.doob 已提交
811 812 813 814 815 816 817 818
	this.min = - Infinity;
	this.max = Infinity;

	this.step = 1;

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

M
r59  
Mr.doob 已提交
819 820
	var changeEvent = document.createEvent( 'HTMLEvents' );
	changeEvent.initEvent( 'change', true, true );
M
r58  
Mr.doob 已提交
821 822 823 824

	var distance = 0;
	var onMouseDownValue = 0;

M
r71  
Mr.doob 已提交
825 826
	var pointer = [ 0, 0 ];
	var prevPointer = [ 0, 0 ];
M
r63  
Mr.doob 已提交
827

M
r58  
Mr.doob 已提交
828 829 830 831 832 833 834 835
	var onMouseDown = function ( event ) {

		event.preventDefault();

		distance = 0;

		onMouseDownValue = parseFloat( dom.value );

M
r71  
Mr.doob 已提交
836
		prevPointer = [ event.clientX, event.clientY ];
M
r63  
Mr.doob 已提交
837

M
r58  
Mr.doob 已提交
838 839 840 841 842 843 844 845 846
		document.addEventListener( 'mousemove', onMouseMove, false );
		document.addEventListener( 'mouseup', onMouseUp, false );

	};

	var onMouseMove = function ( event ) {

		var currentValue = dom.value;

M
r71  
Mr.doob 已提交
847
		pointer = [ event.clientX, event.clientY ];
M
r58  
Mr.doob 已提交
848

M
r71  
Mr.doob 已提交
849
		distance += ( pointer[ 0 ] - prevPointer[ 0 ] ) - ( pointer[ 1 ] - prevPointer[ 1 ] );
M
r58  
Mr.doob 已提交
850 851 852 853 854

		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 已提交
855
		if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
M
r58  
Mr.doob 已提交
856

M
r71  
Mr.doob 已提交
857
		prevPointer = [ event.clientX, event.clientY ];
M
r63  
Mr.doob 已提交
858

M
r58  
Mr.doob 已提交
859 860 861 862 863 864 865 866
	};

	var onMouseUp = function ( event ) {

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

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

M
r58  
Mr.doob 已提交
868 869 870 871 872 873 874 875 876
			dom.focus();
			dom.select();

		}

	};

	var onChange = function ( event ) {

M
r69  
Mr.doob 已提交
877 878 879
		var value = 0;

		try {
M
r58  
Mr.doob 已提交
880

M
r69  
Mr.doob 已提交
881
			value = eval( dom.value );
M
r58  
Mr.doob 已提交
882

M
r69  
Mr.doob 已提交
883 884 885
		} catch ( error ) {

			console.error( error.message );
M
r58  
Mr.doob 已提交
886 887 888

		}

M
r69  
Mr.doob 已提交
889 890
		dom.value = parseInt( value );

M
r58  
Mr.doob 已提交
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
	};

	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 );
M
r70  
Mr.doob 已提交
919
UI.Integer.prototype.constructor = UI.Integer;
M
r58  
Mr.doob 已提交
920 921 922 923 924 925 926 927 928 929 930 931

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 已提交
932 933 934 935 936 937 938

	}

	return this;

};

M
r58  
Mr.doob 已提交
939 940 941 942 943 944 945 946 947
UI.Integer.prototype.setRange = function ( min, max ) {

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

	return this;

};

M
Mr.doob 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964

// 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 );
M
r70  
Mr.doob 已提交
965
UI.Break.prototype.constructor = UI.Break;
M
Mr.doob 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983


// 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 );
M
r70  
Mr.doob 已提交
984
UI.HorizontalRule.prototype.constructor = UI.HorizontalRule;
M
Mr.doob 已提交
985 986 987 988


// Button

M
r59  
Mr.doob 已提交
989
UI.Button = function ( value ) {
M
Mr.doob 已提交
990 991 992 993 994 995 996 997 998

	UI.Element.call( this );

	var scope = this;

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

	this.dom = dom;
M
r59  
Mr.doob 已提交
999
	this.dom.textContent = value;
M
Mr.doob 已提交
1000 1001 1002 1003 1004 1005

	return this;

};

UI.Button.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
1006
UI.Button.prototype.constructor = UI.Button;
M
Mr.doob 已提交
1007 1008 1009 1010 1011 1012 1013

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

	this.dom.textContent = value;

	return this;

M
r69  
Mr.doob 已提交
1014 1015 1016
};


M
r73  
Mr.doob 已提交
1017
// Modal
M
r69  
Mr.doob 已提交
1018

M
r73  
Mr.doob 已提交
1019
UI.Modal = function ( value ) {
M
r69  
Mr.doob 已提交
1020 1021

	var scope = this;
M
r71  
Mr.doob 已提交
1022

M
r73  
Mr.doob 已提交
1023
	var dom = document.createElement( 'div' );
M
r69  
Mr.doob 已提交
1024

M
r73  
Mr.doob 已提交
1025 1026 1027 1028 1029 1030 1031 1032
	dom.style.position = 'absolute';
	dom.style.width = '100%';
	dom.style.height = '100%';
	dom.style.backgroundColor = 'rgba(0,0,0,0.5)';
	dom.style.display = 'none';
	dom.style.alignItems = 'center';
	dom.style.justifyContent = 'center';
	dom.addEventListener( 'click', function ( event ) {
M
r69  
Mr.doob 已提交
1033

M
r73  
Mr.doob 已提交
1034
		scope.hide();
M
r69  
Mr.doob 已提交
1035

M
r73  
Mr.doob 已提交
1036
	} );
M
r69  
Mr.doob 已提交
1037

M
r73  
Mr.doob 已提交
1038
	this.dom = dom;
M
r69  
Mr.doob 已提交
1039

M
r73  
Mr.doob 已提交
1040 1041 1042 1043 1044
	this.container = new UI.Panel();
	this.container.dom.style.width = '200px';
	this.container.dom.style.padding = '20px';
	this.container.dom.style.backgroundColor = '#ffffff';
	this.container.dom.style.boxShadow = '0px 5px 10px rgba(0,0,0,0.5)';
M
r69  
Mr.doob 已提交
1045

M
r73  
Mr.doob 已提交
1046
	this.add( this.container );
M
r69  
Mr.doob 已提交
1047

M
r73  
Mr.doob 已提交
1048
	return this;
M
r69  
Mr.doob 已提交
1049

M
r73  
Mr.doob 已提交
1050
};
M
r69  
Mr.doob 已提交
1051

M
r73  
Mr.doob 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060
UI.Modal.prototype = Object.create( UI.Element.prototype );
UI.Modal.prototype.constructor = UI.Modal;

UI.Modal.prototype.show = function ( content ) {

	this.container.clear();
	this.container.add( content );

	this.dom.style.display = 'flex';
M
r69  
Mr.doob 已提交
1061 1062 1063 1064 1065

	return this;

};

M
r73  
Mr.doob 已提交
1066
UI.Modal.prototype.hide = function () {
M
r69  
Mr.doob 已提交
1067

M
r73  
Mr.doob 已提交
1068
	this.dom.style.display = 'none';
M
r69  
Mr.doob 已提交
1069 1070 1071 1072

	return this;

};