Browse Source

added filter search to documentation page

spite 11 năm trước cách đây
mục cha
commit
69937588d5
1 tập tin đã thay đổi với 121 bổ sung11 xóa
  1. 121 11
      docs/index.html

+ 121 - 11
docs/index.html

@@ -11,6 +11,8 @@
 				font-style: normal;
 				font-style: normal;
 			}
 			}
 
 
+			*{ box-sizing: border-box;}
+
 			html {
 			html {
 				height: 100%;
 				height: 100%;
 			}
 			}
@@ -80,10 +82,58 @@
 				overflow: auto;
 				overflow: auto;
 			}
 			}
 
 
+			.filterBlock{
+				margin: 20px;
+				position: relative;
+			}
+			.filterBlock p {
+				margin: 0;
+			}
+			#filterInput {
+				width: 100%;
+				padding: 5px;
+				font-family: 'inconsolata';
+				font-size: 15px;
+				outline: none;
+				border: 1px solid #dedede;
+			}
+			#filterInput:focus{
+				border: 1px solid #2194CE;
+			}
+			#clearFilterButton {
+				position: absolute; 
+				right: 6px;
+				top: 50%;
+				margin-top: -8px;
+				width: 16px;
+				height: 16px;
+				font-size: 14px;
+				background-color: #b70000;
+				border-radius: 16px;
+				color: white;
+				text-align: center;
+				line-height: 0;
+				padding-top: 7px;
+				opacity: .5;
+				transition: opacity 150ms ease-out, transform 150ms ease-out;
+			}
+			#clearFilterButton:hover{
+				opacity: 1;
+				transform: scale( 1.2 );
+			}
+			.filtered{
+				display: none;
+			}
 		</style>
 		</style>
 	</head>
 	</head>
 	<body>
 	<body>
-		<div id="panel"></div>
+		<div id="panel">
+			<h1><a href="http://threejs.org">three.js</a> / docs</h1>
+			<div class="filterBlock" >
+				<input type="text" id="filterInput" placeholder="Type to filter"/>
+				<a href="#" id="clearFilterButton" >x</a>
+			</div>
+		</div>
 		<iframe id="viewer"></iframe>
 		<iframe id="viewer"></iframe>
 
 
 		<script src="list.js"></script>
 		<script src="list.js"></script>
@@ -91,46 +141,106 @@
 			var panel = document.getElementById( 'panel' );
 			var panel = document.getElementById( 'panel' );
 			var viewer = document.getElementById( 'viewer' );
 			var viewer = document.getElementById( 'viewer' );
 
 
-			var html = '<h1><a href="http://threejs.org">three.js</a> / docs</h1>';
+			var filterInput = document.getElementById( 'filterInput' );
+			filterInput.focus();
 
 
 			var DELIMITER = '/';
 			var DELIMITER = '/';
 			var nameCategoryMap = {};
 			var nameCategoryMap = {};
+			var sections = [];
+
+			var content = document.createDocumentFragment();
 
 
 			for ( var section in list ) {
 			for ( var section in list ) {
 
 
-				html += '<h2>' + section + '</h2>';
+				var h2 = document.createElement( 'h2' );
+				h2.textContent = section;
 
 
-				html += '<ul>';
+				content.appendChild( h2 );
 
 
 				for ( var category in list[ section ] ) {
 				for ( var category in list[ section ] ) {
 
 
-					html += '<h3>' + category + '</h3>';
+					var div = document.createElement( 'div' );
+
+					var h3 = document.createElement( 'h3' );
+					h3.textContent = category;
 
 
-					html += '<ul>';
+					div.appendChild( h3 );
+
+					var ul = document.createElement( 'ul' );
+					div.appendChild( ul );
 
 
 					for ( var i = 0; i < list[ section ][ category ].length; i ++ ) {
 					for ( var i = 0; i < list[ section ][ category ].length; i ++ ) {
 
 
 						var page = list[ section ][ category ][ i ];
 						var page = list[ section ][ category ][ i ];
 
 
-						html += '<li><a href="javascript:goTo(\'' + section + '\', \'' + category + '\', \'' + page[ 0 ] + '\')">' + page[ 0 ] + '</a></li>';
+						var li = document.createElement( 'li' );
+						var a = document.createElement( 'a' );
+						a.setAttribute( 'href', 'javascript:goTo(\'' + section + '\', \'' + category + '\', \'' + page[ 0 ] + '\')');
+						a.textContent = page[ 0 ];
+						li.appendChild( a );
+						ul.appendChild( li );
 
 
 						nameCategoryMap[page[0]] = {
 						nameCategoryMap[page[0]] = {
 							section: section,
 							section: section,
 							category: category,
 							category: category,
-							name: page[0]
+							name: page[0],
+							element: li
 						};
 						};
 
 
 					}
 					}
 
 
-					html += '</ul>';
+					content.appendChild( div );
+					sections.push( ul );
 
 
 				}
 				}
 
 
-				html += '</ul>';
 
 
 			}
 			}
 
 
-			panel.innerHTML += html;
+			panel.appendChild( content )
+
+			function layoutList() {
+
+				sections.forEach( function( el ) {
+					var collapsed = true;
+					Array.prototype.slice.apply( el.children ).forEach( function( item ) {
+						if( !item.classList.contains( 'filtered' ) ) {
+							collapsed = false;
+							return;
+						}
+					} );
+					if( collapsed ) {
+						el.parentElement.classList.add( 'filtered' );
+					} else {
+						el.parentElement.classList.remove( 'filtered' );
+					}
+				} );
+			}
+
+			filterInput.addEventListener( 'input', function( e ) {
+				updateFilter();
+			} );
+
+			document.getElementById( 'clearFilterButton' ).addEventListener( 'click', function( e ) {
+				filterInput.value = '';
+				updateFilter();
+				e.preventDefault();
+			} );
+
+			function updateFilter() {
+
+				var exp = new RegExp( filterInput.value, 'gi' );
+				for( var j in nameCategoryMap ) {
+					var res = nameCategoryMap[ j ].name.match( exp );
+					if( res && res.length > 0 ) {
+						nameCategoryMap[ j ].element.classList.remove( 'filtered' );
+					} else {
+						nameCategoryMap[ j ].element.classList.add( 'filtered' );
+					}
+				}
+				layoutList();
+
+			}
 
 
 			function encodeUrl( path ) {
 			function encodeUrl( path ) {