ui.js 18.0 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
	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 {

M
r74  
Mr.doob 已提交
27
				console.error( 'UI.Element:', argument, 'is not an instance of UI.Element.' );
M
r73  
Mr.doob 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

			}

		}

		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 {

M
r74  
Mr.doob 已提交
49
				console.error( 'UI.Element:', argument, 'is not an instance of UI.Element.' );
M
r73  
Mr.doob 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

			}

		}

		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
	setTextContent: function ( value ) {

		this.dom.textContent = value;

		return this;

	}

M
r74  
Mr.doob 已提交
113
};
M
Mr.doob 已提交
114 115 116 117

// 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
r77  
Mr.doob 已提交
119
'background', '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
r74  
Mr.doob 已提交
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
// Div

UI.Div = function () {

	UI.Element.call( this );

	this.dom = document.createElement( 'div' );

	return this;

};

UI.Div.prototype = Object.create( UI.Element.prototype );
UI.Div.prototype.constructor = UI.Div;

// Row

UI.Row = function () {

	UI.Element.call( this );

	var dom = document.createElement( 'div' );
	dom.className = 'Row';

	this.dom = dom;

	return this;

};

UI.Row.prototype = Object.create( UI.Element.prototype );
UI.Row.prototype.constructor = UI.Row;
M
r69  
Mr.doob 已提交
200

M
r73  
Mr.doob 已提交
201
// Panel
M
r69  
Mr.doob 已提交
202

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

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

M
r73  
Mr.doob 已提交
207 208
	var dom = document.createElement( 'div' );
	dom.className = 'Panel';
M
r58  
Mr.doob 已提交
209

M
r73  
Mr.doob 已提交
210
	this.dom = dom;
M
r58  
Mr.doob 已提交
211 212 213 214 215

	return this;

};

M
r73  
Mr.doob 已提交
216 217
UI.Panel.prototype = Object.create( UI.Element.prototype );
UI.Panel.prototype.constructor = UI.Panel;
M
r63  
Mr.doob 已提交
218

M
r67  
Mr.doob 已提交
219 220 221 222 223 224 225

// Collapsible Panel

UI.CollapsiblePanel = function () {

	UI.Panel.call( this );

M
r69  
Mr.doob 已提交
226
	this.setClass( 'Panel Collapsible' );
M
r67  
Mr.doob 已提交
227 228 229

	var scope = this;

M
r69  
Mr.doob 已提交
230 231 232
	this.static = new UI.Panel();
	this.static.setClass( 'Static' );
	this.static.onClick( function () {
M
r74  
Mr.doob 已提交
233

M
r67  
Mr.doob 已提交
234
		scope.toggle();
M
r74  
Mr.doob 已提交
235

M
r69  
Mr.doob 已提交
236 237
	} );
	this.dom.appendChild( this.static.dom );
M
r67  
Mr.doob 已提交
238

M
r69  
Mr.doob 已提交
239 240 241
	this.contents = new UI.Panel();
	this.contents.setClass( 'Content' );
	this.dom.appendChild( this.contents.dom );
M
r67  
Mr.doob 已提交
242

M
r69  
Mr.doob 已提交
243 244 245
	var button = new UI.Panel();
	button.setClass( 'Button' );
	this.static.add( button );
M
r67  
Mr.doob 已提交
246 247 248 249 250 251 252 253

	this.isCollapsed = false;

	return this;

};

UI.CollapsiblePanel.prototype = Object.create( UI.Panel.prototype );
M
r70  
Mr.doob 已提交
254
UI.CollapsiblePanel.prototype.constructor = UI.CollapsiblePanel;
M
r67  
Mr.doob 已提交
255 256 257

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

M
r69  
Mr.doob 已提交
258 259
	this.static.add.apply( this.static, arguments );
	return this;
M
r67  
Mr.doob 已提交
260

M
r69  
Mr.doob 已提交
261
};
M
r67  
Mr.doob 已提交
262

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

M
r69  
Mr.doob 已提交
265
	this.static.remove.apply( this.static, arguments );
M
r67  
Mr.doob 已提交
266 267 268 269 270 271
	return this;

};

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

M
r69  
Mr.doob 已提交
272 273
	this.static.clear();
	return this;
M
r67  
Mr.doob 已提交
274 275 276 277 278

};

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

M
r69  
Mr.doob 已提交
279
	this.contents.add.apply( this.contents, arguments );
M
r67  
Mr.doob 已提交
280 281 282 283 284 285
	return this;

};

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

M
r69  
Mr.doob 已提交
286
	this.contents.remove.apply( this.contents, arguments );
M
r67  
Mr.doob 已提交
287 288 289 290 291 292
	return this;

};

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

M
r69  
Mr.doob 已提交
293 294
	this.contents.clear();
	return this;
M
r67  
Mr.doob 已提交
295 296 297 298 299

};

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

M
r74  
Mr.doob 已提交
300
	this.setCollapsed( ! this.isCollapsed );
M
r67  
Mr.doob 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

};

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

	this.setCollapsed( true );

};

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

	this.setCollapsed( false );

};

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

M
r69  
Mr.doob 已提交
318
	if ( boolean ) {
M
r67  
Mr.doob 已提交
319

M
r69  
Mr.doob 已提交
320
		this.dom.classList.add( 'collapsed' );
M
r67  
Mr.doob 已提交
321 322 323

	} else {

M
r69  
Mr.doob 已提交
324
		this.dom.classList.remove( 'collapsed' );
M
r67  
Mr.doob 已提交
325 326 327

	}

M
r69  
Mr.doob 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340
	this.isCollapsed = boolean;

	if ( this.onCollapsedChangeCallback !== undefined ) {

		this.onCollapsedChangeCallback( boolean );

	}

};

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

	this.onCollapsedChangeCallback = callback;
M
r67  
Mr.doob 已提交
341 342 343

};

M
Mr.doob 已提交
344 345 346 347 348 349 350 351 352 353
// 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 已提交
354
	dom.style.verticalAlign = 'middle';
M
Mr.doob 已提交
355 356 357 358 359 360 361 362 363

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

	return this;

};

UI.Text.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
364 365 366 367 368 369 370
UI.Text.prototype.constructor = UI.Text;

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

	return this.dom.textContent;

};
M
Mr.doob 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

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

	if ( value !== undefined ) {

		this.dom.textContent = value;

	}

	return this;

};


// Input

M
r70  
Mr.doob 已提交
387
UI.Input = function ( text ) {
M
Mr.doob 已提交
388 389 390 391 392 393 394 395

	UI.Element.call( this );

	var scope = this;

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

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

M
r59  
Mr.doob 已提交
400
		event.stopPropagation();
M
Mr.doob 已提交
401 402 403

	}, false );

M
r59  
Mr.doob 已提交
404
	this.dom = dom;
M
r70  
Mr.doob 已提交
405
	this.setValue( text );
M
r59  
Mr.doob 已提交
406

M
Mr.doob 已提交
407 408 409 410 411
	return this;

};

UI.Input.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
412
UI.Input.prototype.constructor = UI.Input;
M
Mr.doob 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

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 已提交
440
	dom.spellcheck = false;
M
Mr.doob 已提交
441

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

M
r59  
Mr.doob 已提交
444
		event.stopPropagation();
M
r58  
Mr.doob 已提交
445

M
r68  
Mr.doob 已提交
446 447 448 449 450 451 452 453 454 455 456 457
		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 已提交
458 459
	}, false );

M
r59  
Mr.doob 已提交
460
	this.dom = dom;
M
Mr.doob 已提交
461 462 463 464 465 466

	return this;

};

UI.TextArea.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
467
UI.TextArea.prototype.constructor = UI.TextArea;
M
Mr.doob 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

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 已提交
494
	dom.style.padding = '2px';
M
Mr.doob 已提交
495 496 497 498 499 500 501 502

	this.dom = dom;

	return this;

};

UI.Select.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
503
UI.Select.prototype.constructor = UI.Select;
M
Mr.doob 已提交
504 505 506 507 508 509 510 511 512 513 514

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

	this.dom.multiple = boolean;

	return this;

};

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

M
r59  
Mr.doob 已提交
515 516
	var selected = this.dom.value;

M
Mr.doob 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	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 已提交
532 533
	this.dom.value = selected;

M
Mr.doob 已提交
534 535 536 537 538 539 540 541 542 543 544 545
	return this;

};

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

	return this.dom.value;

};

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

M
r69  
Mr.doob 已提交
546 547 548 549 550 551 552
	value = String( value );

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

		this.dom.value = value;

	}
M
Mr.doob 已提交
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

	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 已提交
578
UI.Checkbox.prototype.constructor = UI.Checkbox;
M
Mr.doob 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

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';
M
r74  
Mr.doob 已提交
610
	dom.style.height = '17px';
M
Mr.doob 已提交
611
	dom.style.border = '0px';
M
r74  
Mr.doob 已提交
612
	dom.style.padding = '2px';
M
Mr.doob 已提交
613
	dom.style.backgroundColor = 'transparent';
614

M
r63  
Mr.doob 已提交
615 616 617 618 619 620
	try {

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

	} catch ( exception ) {}
M
Mr.doob 已提交
621 622 623 624 625 626 627 628

	this.dom = dom;

	return this;

};

UI.Color.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
629
UI.Color.prototype.constructor = UI.Color;
M
Mr.doob 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652

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
r74  
Mr.doob 已提交
653
	this.dom.value = '#' + ( '000000' + hex.toString( 16 ) ).slice( - 6 );
M
Mr.doob 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671

	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 已提交
672 673 674 675 676 677 678 679
	dom.addEventListener( 'keydown', function ( event ) {

		event.stopPropagation();

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

	}, false );

M
r74  
Mr.doob 已提交
680 681
	this.value = 0;

M
Mr.doob 已提交
682 683 684 685 686 687
	this.min = - Infinity;
	this.max = Infinity;

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

M
r58  
Mr.doob 已提交
688
	this.dom = dom;
M
r74  
Mr.doob 已提交
689

M
r58  
Mr.doob 已提交
690 691
	this.setValue( number );

M
r59  
Mr.doob 已提交
692 693
	var changeEvent = document.createEvent( 'HTMLEvents' );
	changeEvent.initEvent( 'change', true, true );
M
Mr.doob 已提交
694 695 696 697

	var distance = 0;
	var onMouseDownValue = 0;

M
r71  
Mr.doob 已提交
698 699
	var pointer = [ 0, 0 ];
	var prevPointer = [ 0, 0 ];
M
r63  
Mr.doob 已提交
700

M
r74  
Mr.doob 已提交
701
	function onMouseDown( event ) {
M
Mr.doob 已提交
702 703 704 705 706

		event.preventDefault();

		distance = 0;

M
r74  
Mr.doob 已提交
707
		onMouseDownValue = scope.value;
M
Mr.doob 已提交
708

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

M
Mr.doob 已提交
711 712 713
		document.addEventListener( 'mousemove', onMouseMove, false );
		document.addEventListener( 'mouseup', onMouseUp, false );

M
r74  
Mr.doob 已提交
714
	}
M
Mr.doob 已提交
715

M
r74  
Mr.doob 已提交
716
	function onMouseMove( event ) {
M
Mr.doob 已提交
717

M
r74  
Mr.doob 已提交
718
		var currentValue = scope.value;
M
r58  
Mr.doob 已提交
719

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

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

M
r74  
Mr.doob 已提交
724 725
		var value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
		value = Math.min( scope.max, Math.max( scope.min, value ) );
M
Mr.doob 已提交
726

M
r74  
Mr.doob 已提交
727
		if ( currentValue !== value ) {
M
Mr.doob 已提交
728

M
r74  
Mr.doob 已提交
729 730 731 732
			scope.setValue( value );
			dom.dispatchEvent( changeEvent );

		}
M
Mr.doob 已提交
733

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

M
r74  
Mr.doob 已提交
736
	}
M
Mr.doob 已提交
737

M
r74  
Mr.doob 已提交
738
	function onMouseUp( event ) {
M
Mr.doob 已提交
739 740 741 742 743 744 745 746 747 748 749

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

		if ( Math.abs( distance ) < 2 ) {

			dom.focus();
			dom.select();

		}

M
r74  
Mr.doob 已提交
750
	}
M
Mr.doob 已提交
751

M
r74  
Mr.doob 已提交
752
	function onChange( event ) {
M
Mr.doob 已提交
753

M
r69  
Mr.doob 已提交
754 755 756
		var value = 0;

		try {
M
Mr.doob 已提交
757

M
r69  
Mr.doob 已提交
758 759 760 761 762 763 764 765
			value = eval( dom.value );

		} catch ( error ) {

			console.error( error.message );

		}

M
r74  
Mr.doob 已提交
766
		scope.setValue( value );
M
Mr.doob 已提交
767

M
r74  
Mr.doob 已提交
768
	}
M
Mr.doob 已提交
769

M
r74  
Mr.doob 已提交
770
	function onFocus( event ) {
M
Mr.doob 已提交
771 772 773 774

		dom.style.backgroundColor = '';
		dom.style.cursor = '';

M
r74  
Mr.doob 已提交
775
	}
M
Mr.doob 已提交
776

M
r74  
Mr.doob 已提交
777
	function onBlur( event ) {
M
Mr.doob 已提交
778 779 780 781

		dom.style.backgroundColor = 'transparent';
		dom.style.cursor = 'col-resize';

M
r74  
Mr.doob 已提交
782 783 784
	}

	onBlur();
M
Mr.doob 已提交
785 786 787 788 789 790 791 792 793 794 795

	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 已提交
796
UI.Number.prototype.constructor = UI.Number;
M
Mr.doob 已提交
797 798 799

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

M
r74  
Mr.doob 已提交
800
	return this.value;
M
Mr.doob 已提交
801 802 803 804 805 806 807

};

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

	if ( value !== undefined ) {

M
r74  
Mr.doob 已提交
808 809 810 811 812 813
		value = parseFloat( value );

		if ( value < this.min ) value = this.min;
		if ( value > this.max ) value = this.max;

		this.value = value;
M
Mr.doob 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
		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 已提交
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
	return this;

};


// Integer

UI.Integer = function ( number ) {

	UI.Element.call( this );

	var scope = this;

	var dom = document.createElement( 'input' );
	dom.className = 'Number';
M
r74  
Mr.doob 已提交
850
	dom.value = '0';
M
r58  
Mr.doob 已提交
851

M
r59  
Mr.doob 已提交
852 853 854 855 856 857
	dom.addEventListener( 'keydown', function ( event ) {

		event.stopPropagation();

	}, false );

M
r74  
Mr.doob 已提交
858 859
	this.value = 0;

M
r58  
Mr.doob 已提交
860 861 862 863 864 865
	this.min = - Infinity;
	this.max = Infinity;

	this.step = 1;

	this.dom = dom;
M
r74  
Mr.doob 已提交
866

M
r58  
Mr.doob 已提交
867 868
	this.setValue( number );

M
r59  
Mr.doob 已提交
869 870
	var changeEvent = document.createEvent( 'HTMLEvents' );
	changeEvent.initEvent( 'change', true, true );
M
r58  
Mr.doob 已提交
871 872 873 874

	var distance = 0;
	var onMouseDownValue = 0;

M
r71  
Mr.doob 已提交
875 876
	var pointer = [ 0, 0 ];
	var prevPointer = [ 0, 0 ];
M
r63  
Mr.doob 已提交
877

M
r74  
Mr.doob 已提交
878
	function onMouseDown( event ) {
M
r58  
Mr.doob 已提交
879 880 881 882 883

		event.preventDefault();

		distance = 0;

M
r74  
Mr.doob 已提交
884
		onMouseDownValue = scope.value;
M
r58  
Mr.doob 已提交
885

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

M
r58  
Mr.doob 已提交
888 889 890
		document.addEventListener( 'mousemove', onMouseMove, false );
		document.addEventListener( 'mouseup', onMouseUp, false );

M
r74  
Mr.doob 已提交
891
	}
M
r58  
Mr.doob 已提交
892

M
r74  
Mr.doob 已提交
893
	function onMouseMove( event ) {
M
r58  
Mr.doob 已提交
894

M
r74  
Mr.doob 已提交
895
		var currentValue = scope.value;
M
r58  
Mr.doob 已提交
896

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

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

M
r74  
Mr.doob 已提交
901 902
		var value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
		value = Math.min( scope.max, Math.max( scope.min, value ) ) | 0;
M
r58  
Mr.doob 已提交
903

M
r74  
Mr.doob 已提交
904
		if ( currentValue !== value ) {
M
r58  
Mr.doob 已提交
905

M
r74  
Mr.doob 已提交
906 907 908 909
			scope.setValue( value );
			dom.dispatchEvent( changeEvent );

		}
M
r58  
Mr.doob 已提交
910

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

M
r74  
Mr.doob 已提交
913
	}
M
r58  
Mr.doob 已提交
914

M
r74  
Mr.doob 已提交
915
	function onMouseUp( event ) {
M
r58  
Mr.doob 已提交
916 917 918 919 920

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

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

M
r58  
Mr.doob 已提交
922 923 924 925 926
			dom.focus();
			dom.select();

		}

M
r74  
Mr.doob 已提交
927
	}
M
r58  
Mr.doob 已提交
928

M
r74  
Mr.doob 已提交
929
	function onChange( event ) {
M
r58  
Mr.doob 已提交
930

M
r69  
Mr.doob 已提交
931 932 933
		var value = 0;

		try {
M
r58  
Mr.doob 已提交
934

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

M
r69  
Mr.doob 已提交
937 938 939
		} catch ( error ) {

			console.error( error.message );
M
r58  
Mr.doob 已提交
940 941 942

		}

M
r74  
Mr.doob 已提交
943
		scope.setValue( value );
M
r69  
Mr.doob 已提交
944

M
r74  
Mr.doob 已提交
945
	}
M
r58  
Mr.doob 已提交
946

M
r74  
Mr.doob 已提交
947
	function onFocus( event ) {
M
r58  
Mr.doob 已提交
948 949 950 951

		dom.style.backgroundColor = '';
		dom.style.cursor = '';

M
r74  
Mr.doob 已提交
952
	}
M
r58  
Mr.doob 已提交
953

M
r74  
Mr.doob 已提交
954
	function onBlur( event ) {
M
r58  
Mr.doob 已提交
955 956 957 958

		dom.style.backgroundColor = 'transparent';
		dom.style.cursor = 'col-resize';

M
r74  
Mr.doob 已提交
959 960 961
	}

	onBlur();
M
r58  
Mr.doob 已提交
962 963 964 965 966 967 968 969 970 971 972

	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 已提交
973
UI.Integer.prototype.constructor = UI.Integer;
M
r58  
Mr.doob 已提交
974 975 976

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

M
r74  
Mr.doob 已提交
977
	return this.value;
M
r58  
Mr.doob 已提交
978 979 980 981 982 983 984

};

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

	if ( value !== undefined ) {

M
r74  
Mr.doob 已提交
985
		this.value = value | 0;
M
r58  
Mr.doob 已提交
986
		this.dom.value = value | 0;
M
Mr.doob 已提交
987 988 989 990 991 992 993

	}

	return this;

};

M
r58  
Mr.doob 已提交
994 995 996 997 998 999 1000 1001 1002
UI.Integer.prototype.setRange = function ( min, max ) {

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

	return this;

};

M
Mr.doob 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

// 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 已提交
1020
UI.Break.prototype.constructor = UI.Break;
M
Mr.doob 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038


// 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 已提交
1039
UI.HorizontalRule.prototype.constructor = UI.HorizontalRule;
M
Mr.doob 已提交
1040 1041 1042 1043


// Button

M
r59  
Mr.doob 已提交
1044
UI.Button = function ( value ) {
M
Mr.doob 已提交
1045 1046 1047 1048 1049 1050 1051

	UI.Element.call( this );

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

	this.dom = dom;
M
r59  
Mr.doob 已提交
1052
	this.dom.textContent = value;
M
Mr.doob 已提交
1053 1054 1055 1056 1057 1058

	return this;

};

UI.Button.prototype = Object.create( UI.Element.prototype );
M
r70  
Mr.doob 已提交
1059
UI.Button.prototype.constructor = UI.Button;
M
Mr.doob 已提交
1060 1061 1062 1063 1064 1065 1066

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

	this.dom.textContent = value;

	return this;

M
r69  
Mr.doob 已提交
1067 1068 1069
};


M
r73  
Mr.doob 已提交
1070
// Modal
M
r69  
Mr.doob 已提交
1071

M
r73  
Mr.doob 已提交
1072
UI.Modal = function ( value ) {
M
r69  
Mr.doob 已提交
1073 1074

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

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

M
r73  
Mr.doob 已提交
1078 1079 1080 1081 1082 1083 1084 1085
	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 已提交
1086

M
r73  
Mr.doob 已提交
1087
		scope.hide();
M
r69  
Mr.doob 已提交
1088

M
r73  
Mr.doob 已提交
1089
	} );
M
r69  
Mr.doob 已提交
1090

M
r73  
Mr.doob 已提交
1091
	this.dom = dom;
M
r69  
Mr.doob 已提交
1092

M
r73  
Mr.doob 已提交
1093 1094 1095 1096 1097
	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 已提交
1098

M
r73  
Mr.doob 已提交
1099
	this.add( this.container );
M
r69  
Mr.doob 已提交
1100

M
r73  
Mr.doob 已提交
1101
	return this;
M
r69  
Mr.doob 已提交
1102

M
r73  
Mr.doob 已提交
1103
};
M
r69  
Mr.doob 已提交
1104

M
r73  
Mr.doob 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113
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 已提交
1114 1115 1116 1117 1118

	return this;

};

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

M
r73  
Mr.doob 已提交
1121
	this.dom.style.display = 'none';
M
r69  
Mr.doob 已提交
1122 1123 1124 1125

	return this;

};