2017年4月15日土曜日

miniライブラリにグループ機能追加

旧バージョンを使って情報を追加していくうちに、HTA・WSH・HTA&WSH・DOSコマンドのようなキーワードで項目を分類したいと感じました。
それに対応するものを作成しました。

データ構造が変わるため、旧バージョンで作成したファイルを新バージョンで読み込むことはできません。

ソース
<hta:application icon="icon.ico" />
<html>
<head>
<title>miniライブラリ</title>
<style type="text/css">
.max{width:100%;height:100%}
.w-max{width:100%}
.nowrap{white-space:nowrap}
.ta-c{text-align:center}
.bc{border-collapse:collapse}
.of-auto{overflow:auto}
.of-hidden{overflow:hidden}
.border1{border:solid 1px #000}
*{font-size:16px}
</style>
</head>
<body class=of-hidden>
<table class="bc max">
<td style="width:200px">
<table class="max bc">
<tr height=1>
<td>
<table class="w-max bc">
<td><select id=selグループ class=w-max></select></td>
<td width=1><button id=btnグループ追加>+</button></td>
<td width=1><button id=btnグループ削除>-</button></td>
</table>
</td>
</tr>
<tr height=1><td><input id=inp検索 class=w-max></td></tr>
<tr><td><div id=div候補 class="of-auto border1 max"></div></td></tr>
</table>
</td>
<td>
<table class="bc max">
<tr height=1>
<td>
<table class="w-max bc">
<td><input id=inp名前 class=w-max></td>
<td width=1><button id=btnサンプル追加>サンプル追加</button></td>
</table>
</td>
</tr>
<tr><td><textarea id=taメイン class=max wrap=off></textarea></td></tr>
<tr><td><div id=divSample class="of-auto border1 max"></div></td></tr>
</table>
</td>
</table>
</body>
<script>
resizeTo(1200,800)
window.attachEvent('onload',function(){
基準フォルダ = decodeURI(location.href.replace(/^file:|[\\\/][^\\\/]+$/g,'').replace(/^\/+([A-Z]:)/i,'$1'))
// 初期化
objライブラリ()
objDOM()
})
objライブラリ = function(){
var path=基準フォルダ+'/objデータ.js'
objライブラリ={
データ:{
wsh:{
0:{
str:'WScript.Echo(1)',
sample:{
'sample.js':'WScript.Echo(1)'
}
}
}
},
ファイルから読み込み:function(){
if(!fso.AXO.FileExists(path)){return}
var this_=this
eval('this_.データ = '+fso.Read(path))
},
ファイルに出力:function(){ fso.Write(path, toJSON(this.データ).replace(/\n/g,'\r\n')) },
getG:function(){return this.データ[selグループ.options[selグループ.selectedIndex].text]},
getN:function(sw作成, name){var o=this.getG(), n=name || inp名前.value; return o[n] || (sw作成 ? (o[n]={str:'', sample:{}}) : 0)}
}
objライブラリ.ファイルから読み込み(path)
}
objDOM=function(){
グループ表示更新()
selグループ.onchange()
}
グループ表示更新=function(name){
var sel=selグループ, op=sel.options, i=0, o
while(op[0]){op[0]=null}
forIn(objライブラリ.データ, function(n){op[i++]=o=new Option(n); n==name && (o.selected=true)})
}
selグループ.onchange=function(){
inp検索.value = ''
inp検索.onkeyup()
inp名前.value = ''
inp名前.onkeyup()
}
inp検索.onkeyup=function(){候補表示更新(this.value)}
inp名前.onkeyup=inp名前.onchange=function(){
var s=this.value, obj=(s?objライブラリ.getG():{})[s] || {}
taメイン.disabled = !s
btnサンプル追加.style.display = s ? 'block' : 'none'
taメイン.value = s ? (obj.str || '') : ''
サンプル表示更新(s)
}
候補表示更新=function(str){
div候補[iH] = '<div style="padding:0px;width:100%"><input class=w-max></div>'
var div=div候補[FI].removeNode(true), newDIV, arr=[]
forIn(objライブラリ.getG(), function(name){ name.indexOf(str)!=-1 && arr.push(name) })
for0L(arr.sort(),function(i,name){
div候補[iB](newDIV=div.cloneNode(true))
var inp=newDIV[FI]
setTimeout(function(){inp.value=name},0)
inp.onchange = function(){ 項目_名前変更(name, this) }
inp.onclick = function(){ 項目_表示 (name, this) }
})
}
サンプル表示更新=function(selectサンプル名){
var obj=objライブラリ.getN() || {}
divSample[iH] = (''+function(){/*
<div style="padding:10px;width:100%">
<table class="w-max bc">
<td><input class=w-max></td>
<td width=1><button>Edit</button></td>
<td width=1><button>Run</button></td>
</table>
</div>
*/}).split('/*')[1].split('*/')[0].replace(/\t/g,'')
var tbl=divSample[FI].removeNode(true), newTBL, getsETN=function(elem,name){return elem.getElementsByTagName(name)}
forIn(obj.sample, function(サンプル名){
divSample[iB](newTBL=tbl.cloneNode(true))
var arrBtn=getsETN(newTBL,'button'), btnE=arrBtn[0], btnR=arrBtn[1], obj={
inp :getsETN(newTBL,'input')[0],
サンプル名:サンプル名,
tbl:newTBL,
nameChange:function(str){
var サンプル名=this.サンプル名, obj=objライブラリ.getN().sample
if(!str){ return 削除(サンプル名, obj) ? (this.inp.value = サンプル名) : this.tbl.removeNode(true) }
if(obj[str]){
alert('['+str+']というサンプルは定義済みです')
return this.inp.value = this.サンプル名
}
obj[this.サンプル名=str] = obj[サンプル名]
delete obj[サンプル名]
},
R:function(){
var サンプル名=this.サンプル名, objサンプル=createTMP(サンプル名, objライブラリ.getN().sample[サンプル名])
var str=['<'+'job>','<'+'script>','('+function(path){
AXO=function(name){return new ActiveXObject(name)}
pathDQ=function(path){return 0<path.indexOf(' ') ? ('"'+path+'"') : path}
try{AXO('WScript.Shell').run(pathDQ(path), 1, true)}
catch(e){if(e.message=='処理を待つことができません。'){}}
WScript.Sleep(1000 * 5)
fs=AXO('Scripting.FileSystemObject')
d=function(path){
var parentDir=fs.GetParentFolderName(path)
fs.FolderExists(parentDir) && fs.DeleteFolder(parentDir)
}
d(path)
d(WScript.ScriptFullName)
}+')("'+objサンプル.path.replace(/\\/g,'\\\\')+'")','<'+'/script>','<'+'/job>'].join('\r\n')
shell.run(pathDQ(createTMP(サンプル名+'.wsf',str).path))
},
E:function(){
var サンプル名=this.サンプル名, obj=objライブラリ.getN().sample, str0=obj[サンプル名], str1, objTMP=createTMP(サンプル名, str0)
shell.Run('notepad '+pathDQ(objTMP.path), 1, true)
obj[サンプル名] = str1 = fso.Read(objTMP.path)
objTMP.close()
str0!=str1 && objライブラリ.ファイルに出力()
}
}
obj.inp.onchange = function(){ obj.nameChange(this.value) }
btnR.onclick = function(){ obj.R() }
btnE.onclick = function(){ obj.E() }
setTimeout(function(){obj.inp.value = サンプル名},0)
selectサンプル名 == サンプル名 && setTimeout(function(){ obj.inp.select() },0)
})
}
btnサンプル追加.onclick=function(){
var サンプル名=(new Date()).getTime()+'.wsf', obj=objライブラリ.getN(true).sample
obj[サンプル名] = obj[サンプル名] || '' // 可能性は低いがゼロではないので一応、定義済みの名前なら書き換えないようにする。
サンプル表示更新(サンプル名)
}
taメイン.onchange = function(){ objライブラリ.getN(true).str=this.value; objライブラリ.ファイルに出力() }
項目_名前変更=function(before, inp候補){
var after=inp候補.value, objG=objライブラリ.getG()
var 中断=function(msg){inp候補.value=before; msg && alert(msg)}
if(!after){ return 削除(before, objG) ? 中断() : objライブラリ.ファイルに出力(), inp候補[PA].removeNode(true), サンプル表示更新() }
if(objG[after]){return 中断('['+after+']は定義済みです')}
objG[after] = objG[before]
delete objG[before]
inp名前.value = after
inp名前.onkeyup()
objライブラリ.ファイルに出力()
}
項目_表示=function(name){ setTimeout(function(){taメイン.value = objライブラリ.getG()[inp名前.value=name].str; inp名前.onkeyup()},0) }
createTMP=function(name, str){
var fs=fso.AXO, path=fso.gsf2+'\\'+(new Date()).getTime(), path0=path, i=0, path1
while(fs.FolderExists(path)){ path=path0+(i++) }
fs.CreateFolder(path)
path1 = path+'\\'+name
return fso.Write(path1, str), {path:path1, dir:path, close:function(){ fs.FolderExists(this.dir) && fs.DeleteFolder(this.dir) }}
}
削除=function(name, obj){
if(!confirm('['+name+']を削除しますか?')){ return true }
delete obj[name]
objライブラリ.ファイルに出力()
}
btnグループ追加.onclick=function(){
var n=prompt('追加するグループ名を入力してください','新規グループ'), obj=objライブラリ.データ
if(!n){return}
if(obj[n]){return for0L(selグループ名.options,function(i,o){return o.text==n ? (selグループ名.selectedIndex=i)+1 : 0})}
obj[n] = {}
objライブラリ.ファイルに出力()
objDOM(n)
}
btnグループ削除.onclick=function(){
var ops=selグループ.options, n=ops[selグループ.selectedIndex].text
if(ops.length==1){return alert('グループ名を0個にすることはできません')}
if(!confirm('['+n+']を削除しますか?')){return}
delete objライブラリ.データ[n]
objライブラリ.ファイルに出力()
objDOM(n)
}
iT='innerText', iH='innerHTML', FI='firstChild', CN='childNodes', PA='parentNode', iB='insertBefore', LEN='length'
for0L = function(arr,fun){for(var i=0,L=arr[LEN],res;i<L;i++){if(res=fun(i,arr[i])){return res}}}
forIn = function(obj,fun){var name,res; for(name in obj){if(res=fun(name, obj[name])){return res}}}
AXO=function(name){return new ActiveXObject(name)}
shell=AXO('WScript.Shell')
pathDQ=function(path){return 0<path.indexOf(' ') ? ('"'+path+'"') : path}
fso=function(){
var fs=AXO('Scripting.FileSystemObject')
return {
AXO:fs,
gsf2:fs.GetSpecialFolder(2), // Z:\TEMP
Read:function(path, swCreate, swUnicode){
if(!fs.FileExists(path)){return}
with(fs.OpenTextFile(path, 1, swCreate, swUnicode)){ var str=AtEndOfStream ? '' : ReadAll(); Close() }
return str
},
Write:function(path,str,swUnicode,sw上書きしない){ with(fs.CreateTextFile(path, !sw上書きしない, swUnicode)){ Write(str); Close() } }
}
}()
toJSON=function(){
var gt=function(o){return typeof(o)}, gc=function(o){return o.constructor}, objT={}, objC={}, fStr, rNg=/\n/g, nt='\n\t', main
objT[gt('')] = fStr = function(s){return '"'+s.replace(/\\/g,'\\\\').replace(/\r/g,'\\r').replace(/\n/g,'\\n').replace(/"/g,'\\"')+'"'}
objT[gt(0)] = objT[gt(true)] = objT[gt(gt.und)] = objC[gc(/1/)] = function(v){return v}
objC[gc(new Date())] = function(o){return 'new Date("'+o+'")'}
objC[gc(gc)] = function(o){return インデント調整(o)}
objC[gc({})] = function(o){
var a=[], i=0
forIn(o,function(n,v){ a[i++] = fStr(n)+':'+(main(v)+'').replace(rNg,nt) })
return '{'+nt+a.join(','+nt)+'\n}'
}
objC[gc([])] = function(o){
var a0=[], s, a1=[], i=0
for0L(o,function(i,v){a0[i]=main(v)}), s='['+nt+a0.join(','+nt)+'\n]'
forIn(o,function(n,v){ !a0[n] && (a1[i++] = 'a['+fStr(n)+']='+main(v)) })
return a1[LEN] ? (インデント調整(function(){
var a=sss
aaa111
return a
})+'()').replace(/sss/,s.replace(rNg,nt)).replace(/aaa111/,a1.join(nt)) : s
}
return main=function(o){ var t=gt(o), r=o===null ? 'null' : objT[t] ? objT[t](o) : objC[gc(o)](o); return r }
}()
</script>
</html>

0 件のコメント:

コメントを投稿