console.log( "words.js loading.." ); wapp = new Object(); wapp.shift = false; wapp.shown = false; addEventListener( "keydown", function( e ) { switch( e.which ) { case 16: //shift wapp.shift = true; break; } }, false ); addEventListener( "keyup", function( e ) { if( wapp.shift ) { switch( e.which ) { case 16: wapp.shift = false; break; case 72: if( ! wapp.shown ) { wapp.init(); } break; default: console.log( "words: ", e.which ); } } }, false ); wapp.init = function() { //トップウィンドウ wapp.windows = new Array(); wapp.topElement = document.createElement( "div" ); wapp.topElement.fullPath = ""; wapp.topElement.isTop = true; with( wapp.topElement ) { style.display = "block"; style.backgroundColor = "transparent"; style.border = "solid 0px black"; style.width = "100%"; style.height = "256px"; style.position = "fixed"; style.left = "0px"; style.top = "0px"; innerHTML = "wapp"; } wapp.onkeydownx = function( e ) { console.log( "down" ); var o = wapp.keyType( e.which, false ); //ブラウザ操作のキャンセルしたい if( o.propaga ) e.stopPropagation(); if( o.prevent ) e.preventDefault(); return o.return; }; wapp.onkeyupx = function( e ) { console.log( "up" ); var o = wapp.keyType( e.which, true ); if( o.propaga ) e.stopPropagation(); if( o.prevent ) e.preventDefault(); return o.return; }; addEventListener( "keydown", wapp.onkeydownx, false ); addEventListener( "keyup", wapp.onkeyupx, false ); document.body.appendChild( wapp.topElement ); wapp.shown = true; wapp.newWindow_willBeNotBack( wapp.topElement, "/cgi-bin/prj/_words/data" ); } wapp.end = function() { console.log( "wapp.end" ); removeEventListener( "keydown", wapp.onkeydownx, false ); removeEventListener( "keyup", wapp.onkeyupx, false ); document.body.removeChild( wapp.topElement ); wapp.shown = false; } wapp.itemEnter = function( itemIdx ) { var item = wapp.currentWindow.items[ itemIdx ]; var fullPath = wapp.currentWindow.fullPath + "/" + item.name; if( item.type == "dir" ) { if( item.name == ".." ) { wapp.closeWindow( wapp.currentWindow ); //~閉じる 呼び場所 if( wapp.currentWindow.isTop ) wapp.end(); } else { wapp.newWindow_willBeNotBack( wapp.currentWindow, fullPath ); } } else if( item.type == "file" ) { wapp.newWindow_willBeNotBack( wapp.currentWindow, fullPath ); } } wapp.draw = function() { wapp.clear(); console.log( "wapp.draw" ); wapp.select( wapp.currentWindow.selectedIdx ); } wapp.clear = function() { for( var i = 0; i < wapp.currentWindow.items.length; i++ ) { with( wapp.currentWindow.items[ i ] ) { if( i == 0 ) { style.backgroundColor = "gray"; style.color = "white"; style.fontWeight = "bolder"; style.width = "100%"; style.display = "inline-block"; style.borderBottom = "double 3px black"; style.paddingLeft = ".5em"; style.boxSizing = "border-box"; } else { style.backgroundColor = "transparent"; style.color = "black"; } } } } wapp.keyType = function( ewhich, ku ) { //ku is keyup console.log( "wapp.keyType" ); var ret = { "prevent" : false, "propaga" : false, "return" : true, } if( ku ) { //keyup時 var cw = wapp.currentWindow; switch( ewhich ) { case 37: //left case 38: if( cw.selectedIdx > 1 ) cw.selectedIdx --; //up wapp.draw(); ret.prevent = true; //ブラウザのスクロールはキャンセル break; case 39: //right case 40: if( cw.selectedIdx < wapp.currentWindow.items.length - 1 ) cw.selectedIdx ++; //down wapp.draw(); ret.prevent = true; //ブラウザのスクロールはキャンセル break; case 13: //enter wapp.itemEnter( cw.selectedIdx ); break; default: console.log( "wapp:", ewhich ); } } else { //keydown時 switch( ewhich ) { case 37: //left case 38: case 39: //right case 40: ret.prevent = true; //ブラウザのスクロールはキャンセル break; case 13: //enter break; } } return ret; } //~閉じる 定義 wapp.closeWindow = function( element ) { wapp.currentWindow = element.parentWindow; wapp.topElement.removeChild( element ); wapp.windows.splice( wapp.windows.indexOf( element ), 1 ); } wapp.newWindow_willBeNotBack = function( parent, fullPath ) { console.log( "wapp.newWindow_willBeNotBack" ); var subElement = document.createElement( "div" ); subElement.fullPath = fullPath; subElement.parentWindow = parent; with( subElement ) { style.position = "absolute"; style.display = "block"; style.left = wapp.windows.length + "em"; style.top = wapp.windows.length + "em"; className = "wapp_window"; innerHTML = "loading.."; } wapp.windows.push( subElement ); wapp.topElement.appendChild( subElement ); var css = window.getComputedStyle( subElement ); var rect = subElement.getBoundingClientRect(); wapp.load( subElement.fullPath, function() { console.log( "wapp.load callback" ); console.log( subElement.fullPath ); var html = ""; var hashes = new Array(); if( this.responseText.match( /^thisis=header;type=fileinfo;/ ) ) { //ファイルウィンドウ html += '' + parent.items[ parent.selectedIdx ].name + '
\n'; html += '閉じる
\n'; html += '
'; html += RegExp.rightContext; html += '
'; hashes.push( "dummy" ); hashes.push( line2hash( "thisis=entry;type=dir;name=..;" ) ); //閉じる用 with( subElement ) { style.position = "absolute"; style.width = ( window.innerWidth - rect.left - 48 ) + "px"; style.maxWidth = "60em"; style.height = ( window.innerHeight - rect.top - 48 ) + "px"; style.maxHeight = "60em"; style.overflowY = "scroll"; innerHTML = html; } subElement.onwheel = function( e ) { //スクロールがいっぱいになったとき、ほかのスクロールが動かないようにする if( e.deltaY < 0 && this.scrollTop == 0 || e.deltaY > 0 && this.scrollTop == this.scrollHeight - this.clientHeight ) { e.preventDefault(); } //※e.targetはイベントハンドラの要素とは限らない } subElement.onscroll = function( e ) { // this.limitFlg = this.scrollTop == this.scrollHeight - this.clientHeight || this.scrollTop == 0; // console.log( "scroll", this.limitFlg ); // if( ! this.limitFlg ) // e.preventDefault(); //ブラウザデフォルト動作をキャンセル すべて止まる } } else { //ディレクトリウィンドウ var lines = this.responseText.split( /\n/ ); console.log( this.responseText ); for( var i = 0; i < lines.length; i++ ) { var line = lines[ i ]; var hash = line2hash( line ); //check. if( hash.thisis == "alert" ) { alert( hash.data ); } else if( hash.thisis == "log" ) { console.log( hash.data ); } if( hash.thisis != "entry" ) continue; hash.line = line; if( hash.type == "dir" ) { if( hash.name == "." ) { if( parent.isTop ) { html += '' + "HELP" + ''; } else { html += '' + parent.items[ parent.selectedIdx ].name + ''; } } else if( hash.name == ".." ) { if( parent.isTop ) { html += '' + "終了" + ''; } else { html += '' + "戻る" + ''; } } else { html += '[' + hash.name + ']'; } } else if( hash.type == "file" ) { html += '' + hash.name + ''; } else { alert( "error!" ); } hashes.push( hash ); html += "
\n"; }//for with( subElement ) { style.minWidth = "10em"; style.whiteSpace = "nowrap"; innerHTML = html; } }//if wapp.currentWindow = subElement; wapp.currentWindow.items = subElement.getElementsByClassName( "wapp_items" ); for( var i = 0; i < wapp.currentWindow.items.length; i++ ) { var hash = hashes[ i ]; for( var name in hash ) { wapp.currentWindow.items[ i ][ name ] = hash[ name ]; } } wapp.currentWindow.selectedIdx = 1; wapp.draw(); } ); } wapp.select = function( itemIdx ) { with( wapp.currentWindow.items[ itemIdx ] ) { style.backgroundColor = "black"; style.color = "white"; } } wapp.load = function( dir, onloadx ) { console.log( "wapp.load" ); with( wapp.httpRequest = new XMLHttpRequest() ) { addEventListener( "load", onloadx ); open( "get", dir ); send(); } } function line2hash( line ) { var hash = new Object(); var pairs = line.split( /;/ ); for( var i = 0; i < pairs.length; i++ ) { var pair = pairs[ i ]; var tokens = pair.split( "=" ); var left = tokens[ 0 ]; var right = tokens.splice( 1 ).join(); var rightTryNum = Number( right ); if( isNaN( rightTryNum ) ) { hash[ left ] = right; } else { hash[ left ] = rightTryNum; } } return hash; }