OSDN Git Service

Merge branch 'master' of https://github.com/mtsgi/kit into async-launch
[kit/kit.git] / system.js
1 "use strict";
2
3 //   _    _ _   
4 //  | | _(_) |_ 
5 //  | |/ / | __|
6 //  |   <| | |_ 
7 //  |_|\_\_|\__|
8 //
9 // THIS IS THE KIT KERNEL, KIT APPS FRAMEWORK AND KIT WINDOW SYSTEM
10 // http://web.kitit.ml/
11 // https://github.com/mtsgi/kit
12
13
14 $( document ).ready( kit );
15
16 function kit() {
17     S = System;
18
19     if( localStorage.getItem( "kit-pid" ) ) pid = localStorage.getItem( "kit-pid" );
20
21     if( !localStorage.getItem( "kit-username" ) ) localStorage.setItem( "kit-username", "ユーザー" );
22     $( "#kit-header-username" ).text( localStorage.getItem( "kit-username" ) );
23
24     if( localStorage.getItem( "kit-lock" ) == null ) localStorage.setItem( "kit-lock", "false" );
25
26     if( System.bootopt.get("safe") ) $( "#kit-wallpaper" ).css( "background","#404040" );
27     else if( localStorage.getItem( "kit-wallpaper" ) ) $( "#kit-wallpaper" ).css( "background", localStorage.getItem( "kit-wallpaper" ) ).css( "background-size", "cover" ).css( "background-position", "center" );
28
29     if( !localStorage.getItem( "kit-default-browser" ) ) localStorage.setItem( "kit-default-browser", "browser" );
30
31     if( localStorage.getItem("kit-fusen") ){
32         this.list = JSON.parse(localStorage.getItem("kit-fusen"));
33         for( let i in this.list ){
34             KWS.fusen.add(this.list[i]);
35         }
36     }
37     
38     if( localStorage.getItem("kit-darkmode") == "true" ){
39         KWS.darkmode = true;
40         $("#kit-darkmode").attr("href", "system/theme/kit-darkmode.css");
41         $(".winc-darkmode").addClass("kit-darkmode");
42     }
43
44     if( System.bootopt.get("safe") ){
45         $("#kit-theme-file").attr("href", "./system/theme/theme-light.css" );
46     }
47     else{
48         if( !localStorage.getItem( "kit-theme" ) ) localStorage.setItem( "kit-theme", "theme-default.css" );
49         $("#kit-theme-file").attr("href", "./system/theme/" + localStorage.getItem("kit-theme") );
50     }
51
52     if( !localStorage.getItem( "kit-appdir" ) ) localStorage.setItem( "kit-appdir", "./app/" );
53     S.appdir = localStorage.getItem( "kit-appdir" );
54
55     if( localStorage.getItem( "kit-installed" ) ) System.installed = JSON.parse( localStorage.getItem( "kit-installed" ) );
56
57     if( localStorage["kit-userarea"] ) System.userarea = JSON.parse(localStorage["kit-userarea"]);
58     if( localStorage["kit-recycle"] ) System.recycle = JSON.parse(localStorage["kit-recycle"]);
59
60     System.moveDesktop( "1" );
61
62     var clockmove;
63     if( System.bootopt.get("safe") ) clockmove = setInterval( System.clock, 1000 );
64     else  clockmove = setInterval( System.clock, 10 );
65
66     if ( localStorage.getItem("kit-shutted-down") == "false" ) {
67         Notification.push( "お知らせ", "kitは前回終了時、正しくシャットダウンされませんでした。", "system" );
68     }
69     localStorage.setItem("kit-shutted-down", false);
70
71     Notification.push( "kitへようこそ", localStorage["kit-username"] + "さん、こんにちは。", "system", null, null, 'documents/icon.png', [
72         {
73             label: 'kitについて',
74             func: () => System.launch( 'settings', {'view': 'about'} )
75         }
76     ]);
77
78     if( localStorage.getItem( "kit-startup" ) == undefined ) {
79         localStorage.setItem( "kit-startup", new Array( "welcome" ) );
80     }
81     System.startup = localStorage.getItem( "kit-startup" ).split( "," );
82     if( System.bootopt.get("safe") ){
83         Notification.push( "セーフブート", "現在、kitをセーフモードで起動しています。", "system" );
84         System.alert( "セーフブート", "現在、kitをセーフモードで起動しています。<br><a class='kit-hyperlink' onclick='System.reboot()'>通常モードで再起動</a>", "system" );
85     }
86     else for( let i of System.startup ) if( i != "" ) System.launch(i);
87     
88     $("#kit-header-fullscreen").hide();
89
90     //イベントハンドラ
91     $( "#desktops" ).click( function() {
92         $( "#desktop-" + currentDesktop ).toggleClass( "selected-section" );
93     } ).mousedown( function() {
94         $( ".window" ).css( "opacity", "0.6" );
95     } ).mouseup( function() {
96         $( ".window" ).css( "opacity", "1.0" );
97     } );
98     //タスク一覧
99     $( "#footer-tasks" ).click( function() {
100         if( $( "#kit-tasks" ).is( ":visible" ) ) {
101             $( "#kit-tasks" ).html( "" ).fadeOut( 300 );
102         }
103         else {
104             $( "#task-ctx" ).fadeOut( 200 );
105             $( "#kit-tasks" ).html( $( "#tasks" ).html() ).fadeIn( 300 ).css( "z-index", "9997" );
106         }
107     } );
108     $.getJSON("system/testload.json").fail( () => {
109         $('#body').append(`<div id='wcors' class="window windowactive" style="top: 70px; left: 50px; width: calc(100% - 100px)">
110             <div class="wt">問題が発生しました</div>
111             <div class="winc">JSONデータ読み込みに失敗しました。<br>
112             クロスオリジン制約によりkitアプリケーションの動作が制限されている場合があります。<br>
113             詳細は<a class="kit-hyperlink" onclick="location.href = 'https://mtsgi.github.io/kitdocs/#/cors'">こちらの記事</a>をご確認ください。</div>
114         </div>`);
115         $('<kit-button-alt class="kit-block kit-text-c m">閉じる</kit-button-alt>').appendTo('#wcors .winc').on('click', ()=>{
116             $('#wcors').remove();
117         })
118     });
119     
120     $.getJSON("config/desktop.json", (data) => {
121         for( let i in data ){
122             $(".desktop-icons").append("<div class='desktop-icon' data-launch='" + i + "'><img src='" + data[i].icon + "'>" + data[i].name + "</div>");
123         }
124         $(".desktop-icon").on("click", function(){
125             System.launch( $(this).attr("data-launch") );
126         });
127     }).fail( function() {
128         Notification.push( "読み込みに失敗", "デスクトップ(config/desktop.json)の読み込みに失敗しました。", "system" );
129     } );
130     
131     $.getJSON("config/apps.json", System.initLauncher).fail( function() {
132         Notification.push( "ランチャー初期化失敗", "アプリケーション一覧(config/apps.json)の読み込みに失敗しました。", "system" );
133     } );
134     $( "#kit-tasks" ).delegate( ".task", "click", function() {
135         System.close( this.id.slice( 1 ) );
136         $( this ).hide();
137     } );
138     
139     $( "#footer-noti" ).click( function() {
140         $( "#last-notification" ).hide( "drop", {direction: "right"}, 300 );
141         if( $( "#notifications" ).is( ":visible" ) ) {
142             $( "#notifications" ).hide( "drop", {direction: "right"}, 300 );
143         }
144         else {
145             $( "#notifications" ).show( "drop", {direction: "right"}, 300 );
146         }
147     } );
148     $( "#last-notification-close" ).click( function() {
149         $( "#last-notification" ).hide( "drop", {direction: "right"}, 300 );
150     } );
151     $("#notifications-dnp").prop("checked", false).on("change", ()=>{
152         if( $("#notifications-dnp").is(":checked") ){
153             Notification.goodnight = true;
154         }
155         else Notification.goodnight = false;
156     });
157     
158     $( ".power-button" ).click( function() {
159         $( "#notifications" ).hide( "drop", {direction: "right"}, 300 );
160         $( "#last-notification" ).hide( "drop", {direction: "right"}, 300 );
161         $( "#kit-wallpaper" ).css( "filter", "blur(5px)" );
162         $( "footer, header, #launcher, #task-ctx, #kit-sightre, .dropdown, #desktop-" + currentDesktop ).hide();
163         $( "#kit-power" ).show();
164     } );
165     $( "#kit-power-back" ).click( function() {
166         $( "section, header, footer, #kit-wallpaper, .dropdown" ).css( "filter", "none" );
167         $( "footer, header, #desktop-" + currentDesktop ).show();
168         $( "#kit-power" ).hide();
169     } );
170     $( "#kit-power-shutdown" ).click( function() {
171         System.shutdown();
172     } );
173     $( "#kit-power-reboot" ).click( function() {
174         System.reboot();
175     } );
176     $( "#kit-power-suspend" ).click( function() {
177         location.reload();
178         $( "section, header, footer, #kit-wallpaper" ).css( "filter", "none" );
179         $( "#kit-power" ).fadeOut( 300 );
180         System.alert("サスペンド機能", "サスペンド機能はこのバージョンのkitではサポートされていません。");
181     } );
182     $( "#kit-power-lock" ).click( function() {
183         System.lock();
184     } );
185     $( "#lock-password" ).on( 'keypress', function( e ) {
186         if( e.which == 13 ) $( "#lock-unl" ).click();
187     } );
188     $( "#lock-unl" ).on( 'click', function() {
189         if( !localStorage.getItem( "kit-password" ) || $( "#lock-password" ).val() == localStorage.getItem( "kit-password" ) ) {
190             $( "header, footer" ).show();
191             $( "section, header, footer, #kit-wallpaper" ).css( "filter", "none" );
192             $( "#lock-password" ).val( "" );
193             System.moveDesktop(1);
194         }
195         else $( "#lock-password" ).effect( "bounce", {distance: 12, times: 4}, 500 );
196     } ).hover( function() {
197         $( "#lock-unl span" ).removeClass( "fa-lock" ).addClass( "fa-lock-open" );
198     }, function() {
199         $( "#lock-unl span" ).removeClass( "fa-lock-open" ).addClass( "fa-lock" );
200     } );
201     
202     $( "#launch" ).click( function() {
203         $( "#notifications" ).hide( "drop", {direction: "right"}, 300 );
204         if( $( "#launcher" ).is( ":visible" ) ) {
205             $( "#kit-wallpaper" ).css( "filter", "none" );
206             $( "#desktop-" + currentDesktop ).show();
207             $( "#launcher" ).hide();
208         }
209         else {
210             $( "#kit-wallpaper" ).css( "filter", "blur(5px)" )
211             $( "section, #task-ctx" ).hide();
212             $( "#launcher" ).show();
213         }
214     } );
215
216     //Sightre
217     $('#kit-header-sightre').on('click', () => {
218         if($('#kit-sightre').is( ":visible" )) {
219             $('#kit-sightre').fadeOut(300);
220         }
221         else {
222             $('#kit-sightre-results').html('');
223             $('#kit-sightre').show();
224             $('#kit-sightre-form').val('').focus();
225         }
226     });
227     let sightrePrevWord = '';
228     $('#kit-sightre-form').on('keypress', (e) => {
229         let _word = $('#kit-sightre-form').val();
230         if( e.which == 13 && _word ) {
231             if( _word == "kit" ){
232                 S.alert("", "<div style='text-align:left;'> _    _ _ <br>| | _(_) |_ <br>| |/ / | __|<br>|   〈| | |_ <br>|_|\_ \ _\__|</div><hr>", S.version);
233                 return;
234             }
235             $('.kit-sightre-result.-first').click();
236             sightrePrevWord = '';
237             $('#kit-sightre-form').val('');
238             $('#kit-sightre-results').html('');
239             $('#kit-sightre').fadeOut(300);
240         }
241     }).on('keydown keyup change', (e) => {
242         let _word = $('#kit-sightre-form').val();
243         if( e.which == 27 ) $('#kit-sightre').fadeOut(300);
244         else {
245             if( _word == sightrePrevWord ) return;
246             $('#kit-sightre-results').html('');
247             if( !_word ) return;
248             sightrePrevWord = _word;
249             if( _word.indexOf('kish ') == 0 || _word.indexOf('🥧 ') == 0 ){
250                 let _cmd = _word.substring( _word.indexOf(" ") + 1 );
251                 if( _cmd ){
252                     $(`<div class='kit-sightre-result -first'>
253                             <img class='--icon' src='app/kish/icon.png'/>
254                             <div class='--info'>
255                                 <div class='--name'>${_cmd}</div>
256                                 <div class='--desc'>kishでコマンドを実行</div>
257                             </div>
258                             <div class='--open fa fa-arrow-right'></div>
259                         </div>`).appendTo('#kit-sightre-results').on('click', () => {
260                             System.launch('kish', { 'rc': [ _cmd ] });
261                     });
262                 }
263             }
264             else if( _word.indexOf('http://') == 0 || _word.indexOf('https://') == 0 || _word.indexOf('localhost') == 0 ){
265                 $(`<div class='kit-sightre-result -first'>
266                         <img class='--icon' src='app/browser/icon.png'/>
267                         <div class='--info'>
268                             <div class='--name'>${_word}</div>
269                             <div class='--desc'>ブラウザでURLを開く</div>
270                         </div>
271                         <div class='--open fa fa-arrow-right'></div>
272                     </div>`).appendTo('#kit-sightre-results').on('click', () => {
273                         System.launch( localStorage.getItem('kit-default-browser'), { "url" : _word } );
274                 });
275             }
276             else {
277                 $(`<div class='kit-sightre-result -first'>
278                         <img class='--icon' src='system/icons/q.png'/>
279                         <div class='--info'>
280                             <div class='--name'>${_word}</div>
281                             <div class='--desc'>アプリを起動する</div>
282                         </div>
283                         <div class='--open fa fa-arrow-right'></div>
284                     </div>`).appendTo('#kit-sightre-results').on('click', () => {
285                         let _args = null;
286                         try {
287                             if( _word.split(",")[1] ) _args = JSON.parse( _word.split(",").slice(1).join().trim() );
288                         }
289                         catch(error) {
290                             Notification.push("引数の解釈に失敗", error, "system");
291                         }
292                         System.launch( _word.split(",")[0], _args );
293                 });
294             }
295             for( let i in System.apps ){
296                 if( i.indexOf(_word) == 0 || S.apps[i].name.indexOf(_word) == 0 ){
297                     $(`<div class='kit-sightre-result -app'>
298                             <img class='--icon' src='${S.apps[i].icon}'/>
299                             <div class='--info'>
300                                 <div class='--name'>${S.apps[i].name}</div>
301                                 <div class='--desc'>kitアプリケーション - ${i}</div>
302                             </div>
303                             <div class='--open fa fa-arrow-right'></div>
304                         </div>`).appendTo('#kit-sightre-results').on('click', () => {
305                         System.launch(i);
306                         $('#kit-sightre-results').html('');
307                         $('#kit-sightre').fadeOut(300);
308                     });
309                 }
310             }
311             for( let i in System.userarea ){
312                 if( i.indexOf(_word) == 0 || i.indexOf(_word) == 0 ){
313                     $(`<div class='kit-sightre-result -file'>
314                             <i class="fa fa-file --icon"></i>
315                             <div class='--info'>
316                                 <div class='--name'>${i}</div>
317                                 <div class='--desc'>ファイル - 種類:${S.userarea[i].type} - ユーザー:${System.username}</div>
318                             </div>
319                             <div class='--open fa fa-arrow-right'></div>
320                         </div>`).appendTo('#kit-sightre-results').on('click', () => {
321                         System.open(i);
322                         $('#kit-sightre-results').html('');
323                         $('#kit-sightre').fadeOut(300);
324                     });
325                 }
326             }
327             $(`<div class='kit-sightre-result -link'>
328                     <i class="fa fa-search --icon"></i>
329                     <div class='--info'>
330                         <div class='--name'>${_word}</div>
331                         <div class='--desc'>をWebで検索</div>
332                     </div>
333                     <div class='--open fa fa-arrow-right'></div>
334                 </div>`).appendTo('#kit-sightre-results').on('click', () => {
335                 System.launch( 'browser', { 'url' : 'https://www.bing.com/search?q=' + _word } );
336                 $('#kit-sightre-results').html('');
337                 $('#kit-sightre').fadeOut(300);
338             });
339             $(`<div class='kit-sightre-result -link'>
340                     <i class="fab fa-wikipedia-w --icon"></i>
341                     <div class='--info'>
342                         <div class='--name'>${_word}</div>
343                         <div class='--desc'>wikipediaの記事を表示</div>
344                     </div>
345                     <div class='--open fa fa-arrow-right'></div>
346                 </div>`).appendTo('#kit-sightre-results').on('click', () => {
347                 System.launch( 'browser', { 'url' : 'https://ja.wikipedia.org/wiki/' + _word } );
348                 $('#kit-sightre-results').html('');
349                 $('#kit-sightre').fadeOut(300);
350             });
351         }
352     });
353
354     $("#dropdown-sound-slider").slider({
355         min: 0, max: 100, step: 1, value: 100,
356         change: (e, ui) => {
357             System.audio.level = ui.value;
358             $("#dropdown-sound-level").text(ui.value);
359             localStorage.setItem("kit-audio-level", ui.value);
360             for( let i in System.audio.list ){
361                 System.audio.list[i].volume = System.audio.level / 100;
362             }
363             if( ui.value == 0 ) $("#kit-header-sound-icon").removeClass("fa-volume-up").addClass("fa-volume-mute");
364             else $("#kit-header-sound-icon").removeClass("fa-volume-mute").addClass("fa-volume-up");
365         }
366     });
367     if( localStorage["kit-audio-level"] ) System.audio.volume( localStorage["kit-audio-level"] );
368
369     $("#dropdown-sound-silent").prop("checked", false).on("change", ()=>{
370         if( $("#dropdown-sound-silent").is(":checked") ){
371             System.audio.silent = true;
372             $("#kit-header-sound-icon").removeClass("fa-volume-up").addClass("fa-volume-mute");
373         }
374         else{
375             System.audio.silent = false;
376             $("#kit-header-sound-icon").removeClass("fa-volume-mute").addClass("fa-volume-up");
377         }
378     });
379
380     $('#kit-header-user').on('click', () => System.launch('user') );
381
382     $(":root section:not(#desktop-l)").on("contextmenu", function() {
383         let _ptelem = $( document.elementFromPoint(S.mouseX, S.mouseY) );
384         S.selectedElement = _ptelem;
385         S.selectedText = window.getSelection();
386         $( "#kit-context-input" ).val( S.selectedText );
387         if( $( "#kit-context-input" ).val() == "" ) $("#kit-contextgroup-text").hide();
388         else $("#kit-contextgroup-text").show();
389         if( _ptelem[0].id == "desktop-" + currentDesktop ){
390             $("#kit-contextgroup-desktop").show();
391             $("#kit-contextgroup-elem").hide();
392         }
393         else{
394             $("#kit-contextgroup-desktop").hide();
395             $("#kit-contextgroup-elem").show();
396         }
397         $( "#kit-context-elem" ).text( _ptelem.prop("tagName").toLowerCase() + "要素" );
398         $("#kit-contextgroup-custom").hide();
399
400         let  _ctxid = _ptelem.attr("data-kit-contextid") || _ptelem.attr("kit-context");
401         if( _ctxid ){
402             $("#kit-contextgroup-custom").show().html('<div id="kit-context-custom"></div>');
403             let  _ctxname = KWS.context[_ctxid].name || _ctxid; 
404             $("#kit-context-custom").text( _ctxname );
405             for( let i in KWS.context[_ctxid]){
406                 if( i == "name" ) continue;
407                 $("#kit-contextgroup-custom").append("<a id='kit-context-" + _ctxid + "-" + i + "'><span class='fa " + KWS.context[_ctxid][i].icon + "'></span> " + KWS.context[_ctxid][i].label +"</a>");
408                 $("#kit-context-" + _ctxid + "-" + i).on("click", () => {
409                     KWS.context[_ctxid][i].function();
410                     $("#kit-context").fadeOut(300);
411                 });
412             }
413         }
414         if( _ptelem[0].id ) $( "#kit-context-elem" ).append( "#" + _ptelem[0].id );
415         $( "#kit-context-size" ).text( _ptelem[0].clientWidth + "✕" + _ptelem[0].clientHeight );
416         $("#kit-context").toggle().css("left", S.mouseX).css("top", S.mouseY);
417         return false;
418     });
419     $("#kit-context-open").on("click", function(){
420         S.alert("要素", S.selectedElement.clone());
421     });
422     $("#kit-context-save").on("click", function(){
423         S.obj2img( S.selectedElement , true );
424     });
425     $( "#kit-context-search" ).on("click", function(){
426         $("#kit-context").fadeOut(300);
427         System.launch( 'browser', {'url': `https://www.bing.com/search?q=${$('#kit-context-input').val()}`} );
428     });
429     $( "#kit-context-input" ).keypress( function( e ) {
430         if( e.which == 13 ) $( "#kit-context-search" ).click();
431     } );
432     $("#kit-context a").on("click", function(){
433         $("#kit-context").fadeOut(300);
434     });
435     $("#kit-context-vacuum").on("click", function(){
436         for( let i in process ){
437             KWS.vacuum( S.mouseX, S.mouseY );    
438         }
439         setTimeout(() => {
440             $(".window").css("transition", "none");
441         }, 500);
442     });
443     $("#kit-context-fusen").on("click", function(){
444         KWS.fusen.add("");
445     });
446
447
448     $("section").on("click", function(){
449         $("#kit-context").fadeOut(300);
450     })
451
452     $( document ).delegate('a', 'click', function() {
453         if( this.href ) {
454             System.launch( localStorage.getItem( "kit-default-browser" ), { "url" : this.href } );
455             return false;
456         }
457     } ).on("mousemove", function(e){
458         System.mouseX = e.clientX;
459         System.mouseY = e.clientY;
460     }).delegate('.textbox', 'keypress', function(e) {
461         if( e.which == 13 && this.id ){
462             if( $("#" + this.id + " + .kit-button").length ){
463                 Notification.push("debug", this.id, "system");
464                 $("#" + this.id + " + .kit-button").click();
465             }
466             else if( $("#" + this.id + " + kit-button").length ){
467                 Notification.push("debug", this.id, "system");
468                 $("#" + this.id + " + kit-button").click();
469             }
470         }
471     } );
472
473     window.onresize = () => {
474         System.display.width = window.innerWidth;
475         System.display.height = window.innerWidth;
476
477         if( KWS.fullscreen.pid ){
478             KWS.resize( KWS.fullscreen.pid, System.display.width, System.display.height - 30 );
479         }
480     }
481
482     if( localStorage.getItem( "kit-lock" ) == "true" ){
483         $("section").hide();
484         setTimeout(() =>  System.lock(), 100);
485     }
486 }
487
488 async function launch( str, args, dir ) {
489     while(System.launchLock) await System.ajaxWait();
490     System.launchLock = true;
491
492     let _pid = pid;
493     System.args[_pid] = args;
494     let _path = dir || System.appdir + str;
495     System.launchpath[_pid] = _path;
496
497     if( System.appCache[_path] ) {
498         if( KWS.fullscreen.pid ) KWS.unmax(KWS.fullscreen.pid);
499         appData( System.appCache[_path] );
500     }
501     else {
502         try{
503             $.getJSON( S.launchpath[_pid] + '/define.json', appData ).fail( () => {
504                 Notification.push('kitアプリをロードできません。', `${str}を展開できませんでした。`, 'system');
505             } );
506         }
507         catch(error){
508             Notification.push( "System Error", error, "system" );
509         }
510     }
511 }
512
513 async function appData( data ) {
514     let _pid = pid;
515     process[String( _pid )] = {
516         id: data.id,
517         time: System.time.obj.toLocaleString(),
518         isactive: false,
519         preventclose: false,
520         title: data.name
521     };
522     System.appCache[System.launchpath[pid]] = data;
523     app = new App(_pid);
524     let _taskAppend = `<span id='t${_pid}'>`;
525     if( data.icon && data.icon != "none" ) _taskAppend += `<img src='${S.launchpath[_pid]}/${data.icon}'>`;
526     _taskAppend += `<span id='tname${_pid}'>${data.name}<span></span>`;
527     $( "#tasks" ).append( _taskAppend );
528     $( "#t" + _pid ).addClass( "task" ).on({
529         click: function() {
530             if( $(this).hasClass("t-active") || $(this).hasClass("task-min") ) KWS.min( _pid );
531             else{
532                 $("#w"+_pid).css("z-index", KWS.windowIndex + 1);
533                 KWS.refreshWindowIndex();
534             }
535         },
536         mouseenter: function() {
537             $( "#task-ctx-name" ).text( data.name );
538             if( data.icon && data.icon != "none" ) $( "#task-ctx-img" ).attr( "src", System.launchpath[_pid] + "/" + data.icon );
539             else $( "#task-ctx-img" ).hide();
540             $( "#task-ctx-ver" ).text( data.version + "/pid:" + _pid );
541             $( "#task-ctx-info" ).off().on( "click", function() { System.appInfo( _pid )} );
542             $( "#task-ctx-sshot" ).off().on( "click", function() { S.screenshot(_pid, true) } );
543             $( "#task-ctx-min" ).off().on( "click", function() { KWS.min( String(_pid) ) } );
544             if( $(this).hasClass("t-active") ) $( "#task-ctx-front" ).hide();
545             else $( "#task-ctx-front" ).show();
546             $( "#task-ctx-front" ).off().on( "click", function() {
547                 $("#w"+_pid).css("z-index", KWS.windowIndex + 1);
548                 KWS.refreshWindowIndex();
549             } );
550             $( "#task-ctx-close" ).off().on("click", () => System.close(_pid));
551             $( "#task-ctx-kill" ).off().on("click", () => System.kill( data.id));
552             const _ctxleft = $(this).offset().left, _ctxtop = window.innerHeight - $(this).offset().top;
553             if( _ctxleft != $( "#task-ctx" ).offset().left ) $( "#task-ctx" ).hide();
554             $( "#task-ctx" ).css( "left", _ctxleft ).css( "bottom", _ctxtop ).show();
555         }
556     } );
557     $( "section, #kit-tasks" ).on( "mouseenter", function() {
558         $( "#task-ctx" ).fadeOut( 200 );
559     } );
560     $( "#t" + _pid ).on({
561         mouseenter: () => {
562             prevWindowIndex = $( "#w" + _pid ).css( "z-index" );
563             $( "#w" + _pid ).addClass( "win-highlight" );
564         },
565         mouseleave: () => $( "#w" + _pid ).removeClass( "win-highlight" )
566     });
567
568     let _windowAppend = "<div id='w" + _pid + "'><div id='wt" + _pid + "' class='wt'><i class='wmzx'><span id='wm" + _pid + "'></span>";
569     if( data.support && data.support['fullscreen'] == true ) _windowAppend += "<span id='wz" + _pid + "'></span>";
570     _windowAppend += "<span id='wx" + _pid + "'></span></i>";
571     if( data.icon && data.icon != "none" ) _windowAppend += "<img src='" + S.launchpath[_pid] + "/" + data.icon + "'>";
572     _windowAppend += "<span id='wtname" + _pid + "'>" + data.name + "</span></div><div class='winc winc-" + data.id + "' id='winc" + _pid + "'></div></div>";
573     $( "#desktop-" + currentDesktop ).append( _windowAppend );
574
575     if( data.support && data.support['darkmode'] == true ) $("#winc"+_pid).addClass("winc-darkmode");
576     if( KWS.darkmode ) $("#winc"+_pid).addClass("kit-darkmode");
577
578     if( data.size ){
579         $("#winc"+_pid).css("width", data.size.width).css("height", data.size.height);
580     }
581     if( data.resize ){
582         let _minwidth = 200, _minheight = 40;
583         if( data.resize.minWidth ) _minwidth = data.resize.minWidth;
584         if( data.resize.minHeight ) _minheight = data.resize.minHeight;
585         $("#winc"+_pid).windowResizable({
586             minWidth: _minwidth,
587             minHeight: _minheight
588         });
589     }
590
591     let windowPos = 50 + ( _pid % 10 ) * 20;
592     KWS.windowIndex ++;
593     $( "#w"+_pid ).addClass( "window" ).pep({
594         elementsWithInteraction: ".winc, .ui-resizable-handle",
595         useCSSTranslation: false,
596         disableSelect: false,
597         shouldEase:     true,
598         initiate: function(){
599             $(this.el).addClass("ui-draggable-dragging");
600             KWS.windowIndex ++;
601             this.el.style.zIndex = KWS.windowIndex;
602             KWS.refreshWindowIndex();
603         },
604         stop: function(){
605             this.el.style.transition = "none";
606             $(this.el).removeClass("ui-draggable-dragging");
607         }
608     }).on( "mousedown", function(){
609         $(".window").css( "transition", "none" );
610         $(this).css("z-index", KWS.windowIndex + 1);
611         KWS.refreshWindowIndex();
612     } ).css( "left", windowPos + "px" ).css( "top", windowPos + "px" ).css( "z-index",  KWS.windowIndex );
613     KWS.refreshWindowIndex();
614     $( `#wm${_pid}` ).addClass( "wm fa fa-window-minimize" ).on("click", () => KWS.min( _pid ) );
615     $( `#wz${_pid}` ).addClass( "wz fas fa-square" ).on("click", () => KWS.max( _pid ) );
616     $( `#wx${_pid}` ).addClass( "wx fa fa-times" ).on("click", () => System.close( _pid )  );
617     $( "#winc" + _pid ).resizable( {
618         minWidth: "200"
619     } ).load( System.launchpath[_pid] + "/" + data.view, (r, s, x) => {
620         if( s == "error" ){
621             Notification.push("起動に失敗:" + x.status, x.statusText);
622             return false;
623         }
624         if( !data.script || data.script != "none" ) $.getScript( System.launchpath[_pid] + "/" + data.script, () => {
625             if( !data.support || data.support['kaf'] != false ) App.kaf(_pid);
626             pid++;
627         }).fail( () => {
628             App.kaf(_pid);
629             pid++;
630         });
631         else if( !data.support || data.support['kaf'] != false ){
632             App.kaf(_pid);
633             pid++;
634         }
635         else pid++;
636         if( data.css != "none" && $("#kit-style-"+data.id).length == 0 ){
637             $( "head" ).append( '<link href="' + System.launchpath[_pid] + '/' + data.css + '" rel="stylesheet" id="kit-style-' + data.id + '"></link>' );
638         }
639         localStorage.setItem( "kit-pid", pid );
640         System.launchLock = false;
641     } );
642 }
643
644 const System = new function() {
645     this.version = "0.2.1";
646     this.username = localStorage.getItem("kit-username");
647     this.appdir = localStorage.getItem("kit-appdir");
648     this.loc = { ...location };
649
650     this.bootopt = new URLSearchParams(location.search);
651
652     this.mouseX = 0;
653     this.mouseY = 0;
654
655     this.display = {
656         "width": window.innerWidth,
657         "height": window.innerHeight
658     }
659
660     this.selectedElement = null;
661     this.selectedText = null;
662
663     this.dom = function(_pid, ..._elems) {
664         let q = "";
665         if( !_elems.length ) q = ",#winc" + _pid;
666         else for( let i of _elems ){
667             q += ",#winc" + _pid + " " + i;
668         }
669         return $( q.substring(1) );
670     }
671
672     this.qs = ( _pid, ..._elems ) => {
673         let q = "";
674         if( !_elems.length ) q = ",#winc" + _pid;
675         else for( let i of _elems ) q += ",#winc" + _pid + " " + i;
676         return document.querySelectorAll( q.substring(1) )
677     }
678
679     this.userarea = new Object();
680     this.recycle = new Object();
681
682     this.appCache = {};
683     //アプリ引数
684     this.args = {};
685     //アプリ起動パス
686     this.launchpath = {};
687
688     this.support = $.support;
689     this.debugmode = false;
690
691     this.battery = null;
692
693     this.log = new Array();
694     this.noop = () => {}
695
696     this.launchLock = false;
697     
698     this.waitLaunchUnlock = (callback) => {
699         setTimeout(()=>{
700             if(this.ajaxLock){
701                 this.waitLaunchUnlock(callback);
702             }else{
703                 return callback();
704             }
705         }, 100)
706     }
707
708     this.ajaxWait = () =>{
709         return new Promise(resolve =>{
710              System.waitLaunchUnlock(resolve);
711         });
712     }
713
714     this.setBattery = function(){
715         if( navigator.getBattery ) navigator.getBattery().then((e)=>{
716             let _lv =  e.level * 100;
717             System.battery = _lv;
718             return _lv;
719         });
720     }
721
722     this.screenshot = function( _pid, _popup ){
723         let _elem = document.querySelector("body");
724         if( _pid ) _elem = document.querySelector("#w"+_pid);
725         html2canvas( _elem ).then(canvas => {
726             if( _popup ){
727                 canvas.style.border = "1px solid #909090";
728                 S.save( canvas.toDataURL("image/png"), "image" );
729             }
730             return canvas;
731         });
732     }
733
734     this.obj2img = function( _obj, _popup ){
735         let _elem = _obj[0];
736         html2canvas( _elem ).then(canvas => {
737             if( _popup ){
738                 canvas.style.border = "1px solid #909090";
739                 S.save( canvas.toDataURL("image/png"), "image" );
740             }
741             return canvas;
742         });
743     }
744
745     this.save = function(data, type){
746         System.launch("fivr", { "save" : data, "type" : type });
747     }
748
749     this.open = function(filename){
750         System.launch("fivr", { "open" : filename });
751     }
752
753     this.preventClose = function( _pid ){
754         if( !process[_pid] ) return false;
755         process[_pid].preventclose = true;
756         return true;
757     }
758     
759     this.shutdown = function(_opt) {
760         $( "#last-notification-close" ).click();
761         $( "#kit-power-back" ).click();
762         for( let i in process ) {
763             if( process[i].preventclose == true ){
764                 S.dialog( "シャットダウンの中断", "pid" + System.appCache[System.launchpath[i]].name + "がシャットダウンを妨げています。<br>強制終了してシャットダウンを続行する場合は[OK]を押下してください。", () => {
765                     process[i].preventclose = false;
766                     System.shutdown();
767                 } );
768                 return false;
769             }
770             else System.close( i );
771         }
772         $( "section" ).hide();
773         $( "body" ).css( "background-color", "black" );
774         $( "header, footer" ).fadeOut( 300 );
775         $( "#kit-wallpaper" ).fadeOut( 1500 );
776         if( _opt == "reboot" ) location.href = "autorun.html";
777         localStorage.setItem("kit-shutted-down", true);
778     }
779
780     this.reboot = function() {
781         System.shutdown("reboot");
782     }
783
784     this.lock = function(){
785         System.moveDesktop( "l" );
786
787         $( "#lock-user-icon" ).css( "background", localStorage.getItem( "kit-user-color" ) );
788         $( "section, header, footer" ).css( "filter", "none" );
789         $( "#kit-wallpaper" ).css( "filter", "blur(20px)" );
790         $( "header, footer, #kit-power" ).hide();
791
792         $( "#lock-username" ).text( localStorage.getItem( "kit-username" ) );
793         if( localStorage.getItem( "kit-password" ) ) $( "#lock-password" ).show();
794         else $( "#lock-password" ).hide();
795     }
796
797     this.alert = function( title, content, wtitle ) {
798         System.launch("alert", [title, content, wtitle]);
799     }
800
801     this.dialog = function( title, content, func ){
802         System.launch("dialog", {
803             "title": title,
804             "content": content,
805             "func": func
806         });
807     }
808
809     this.appInfo = function( _pid ){
810         let _title = "", _content = "";
811         let ac = System.appCache[S.launchpath[_pid]];
812         let _lp = System.launchpath[_pid];
813         if( ac ){
814             _title = ac.name + " " + ac.version;
815             if( ac.icon && ac.icon != "none" ) _content = "<img style='height: 96px' src='" + _lp + "/" + ac.icon + "'><br>";
816             for( let i in ac ){
817                 if( typeof ac[i] != "object" ) _content += "<div><span style='font-weight: 100'>" + i + " </span>" + ac[i] + "</div>";
818             }
819             _content += "<br><span style='font-weight: 100'>起動パス " + _lp + "</span><br><br>"
820         }
821         else _title = "取得に失敗しました";
822         System.alert( _title, _content );
823     }
824
825     this.apps = new Object();
826     this.installed = new Array();
827
828     this.close = function( _str ) {
829         let _pid = String( _str );
830         $( "#w" + _pid ).remove();
831         $( "#t" + _pid ).remove();
832         $( "#task-ctx" ).hide();
833         delete process[_pid];
834         KWS.refreshWindowIndex();
835     }
836
837     this.kill = function( _str ){
838         for( let _pid in process ) {
839             if( process[_pid] && process[_pid].id == _str ) System.close( _pid );
840         }
841     }
842     
843     this.vacuum = function( _left, _top ){
844         KWS.vacuum( _left, _top ); //非推奨です(削除予定)。
845     }
846
847     this.time = {
848         "obj" : new Date(),
849         "y" : "1970",
850         "m" : "1",
851         "d" : "1",
852         "h" : "00",
853         "i" : "00",
854         "s" : "00",
855         "ms" : "0"
856     }
857
858     this.clock = function() {
859         let DD = new Date();
860         S.time.obj = DD;
861         let Year = DD.getFullYear();
862         S.time.day = DD.getDay();
863         S.time.y = Year;
864         let Month = ( "00" + Number(DD.getMonth()+1) ).slice( -2 );
865         S.time.m = Month;
866         let DateN = ( "00" + DD.getDate() ).slice( -2 );
867         S.time.d = DateN;
868         let Hour = ( "00" + DD.getHours() ).slice( -2 );
869         S.time.h = Hour;
870         let Min = ( "00" + DD.getMinutes() ).slice( -2 );
871         S.time.i = Min;
872         let Sec = ( "00" + DD.getSeconds() ).slice( -2 );
873         S.time.s = Sec;
874         $( ".os-time" ).text( Hour + ":" + Min + ":" + Sec );
875         let MS = DD.getMilliseconds();
876         S.time.ms = MS;
877         let circle = {
878             outer: { radius: .9, color: "transparent" },
879             inner: { radius: .85, color: "transparent" }
880         }
881         let lines = {
882             long: { from: .8, to: .7, width: 2, color: "#303030" },
883             short: { from: .8, to: .75, width: 1, color: "#a0a0a0" }
884         }
885         let hands = {
886             hour: { length: .4, width: 3, cap: "butt", color: "#303030", ratio: .2 },
887             minute: { length: .67, width: 2, cap: "butt", color: "#303030", ratio: .2 },
888             second: { length: .67, width: 1, cap: "butt", color: "dodgerblue", ratio: .2 }
889         }
890         let canvas = $(".dropdown-clock-canvas")[0];
891         canvas.width = "200", canvas.height = "200";
892         let context = canvas.getContext("2d");
893         let center = { x: Math.floor(canvas.width / 2), y: Math.floor(canvas.height / 2) };
894         let radius = Math.min(center.x, center.y), angle, len;
895         context.beginPath();context.fillStyle = circle.outer.color;
896         context.arc(center.x, center.y, radius * circle.outer.radius, 0, Math.PI * 2, false);
897         context.fill();context.beginPath();context.fillStyle = circle.inner.color;
898         context.arc(center.x, center.y, radius * circle.inner.radius, 0, Math.PI * 2, false);
899         context.fill();
900         for( let i=0; i<60; i++ ){
901             angle = Math.PI * i / 30;
902             context.beginPath();
903             let line = ( i%5 == 0 ) ? lines.long : lines.short;
904             context.lineWidth = line.width, context.strokeStyle = line.color;
905             context.moveTo(center.x + Math.sin(angle) * radius * line.from, center.y - Math.cos(angle) * radius * line.from)
906             context.lineTo(center.x + Math.sin(angle) * radius * line.to, center.y - Math.cos(angle) * radius * line.to);
907             context.stroke();
908         }
909         angle = Math.PI * ( Number(Hour)+Number(Min)/60 ) / 6, len = radius * hands.hour.length;
910         context.beginPath(), context.lineWidth = hands.hour.width;
911         context.lineCap = hands.hour.cap, context.strokeStyle = hands.hour.color;
912         context.moveTo(center.x - Math.sin(angle) * len * hands.hour.ratio, center.y + Math.cos(angle) * len * hands.hour.ratio);
913         context.lineTo(center.x + Math.sin(angle) * len, center.y - Math.cos(angle) * len), context.stroke();
914         angle = Math.PI * ( Number(Min)+Number(Sec) / 60) / 30, len = radius * hands.minute.length;
915         context.beginPath(), context.lineWidth = hands.minute.width;
916         context.lineCap = hands.minute.cap, context.strokeStyle = hands.minute.color;
917         context.moveTo(center.x - Math.sin(angle) * len * hands.minute.ratio, center.y + Math.cos(angle) * len * hands.minute.ratio);
918         context.lineTo(center.x + Math.sin(angle) * len, center.y - Math.cos(angle) * len), context.stroke();
919         angle = Math.PI * Number(Sec) / 30, len = radius * hands.second.length;
920         context.beginPath(), context.lineWidth = hands.second.width;
921         context.lineCap = hands.second.cap, context.strokeStyle = hands.second.color;
922         context.moveTo(center.x - Math.sin(angle) * len * hands.second.ratio, center.y + Math.cos(angle) * len * hands.second.ratio);
923         context.lineTo(center.x + Math.sin(angle) * len, center.y - Math.cos(angle) * len), context.stroke();
924     }
925
926     this.changeWallpaper = function( str ) {
927         $( "#kit-wallpaper" ).css( "background", str ).css( "background-size", "cover" );
928         localStorage.setItem( "kit-wallpaper", str )
929     }
930
931     this.moveDesktop = function( str ) {
932         str = String( str );
933         $( "section" ).hide();
934         $( "#desktop-" + str ).show();
935         $( "#desktops" ).html( "<span class='far fa-clone'></span>Desktop" + str );
936         currentDesktop = str;
937     }
938
939     this.avoidMultiple = function( _pid, _alert ) {
940         let _id = process[_pid].id;
941         let _cnt = 0;
942         for( let i in process ) {
943             if( process[i].id == _id ) _cnt += 1;
944         }
945         Notification.push( "debug", _cnt );
946         if( _cnt > 1 ) {
947             System.close( _pid );
948             if( !_alert ){
949                 System.alert( "多重起動", "アプリケーション" + _id + "が既に起動しています。このアプリケーションの多重起動は許可されていません。" );
950             }
951         }
952         return _cnt;
953     }
954
955     this.resizable = function( _pid, _elem, _width, _height ){
956         let E = ".winc";
957         if( _elem ) E = String( _elem );
958         if( !_width ) _width = null;
959         if( !_height ) _height = "100";
960         $("#w" + _pid).resizable({
961             alsoResize: "#w" + _pid + " " + E,
962             minWidth: _width,
963             minHeight: _height
964         });
965     }
966
967     this.initLauncher = function(data){
968         $("#launcher-apps").html("");
969         System.apps = data;
970         for( let i in data ){
971             $('#launcher-apps').append(`<div class='launcher-app' data-launch='${i}'><img src='${data[i].icon}'>${data[i].name}</div>`);
972         }
973         if( !System.bootopt.get('safe') ){
974             for( let i of System.installed ){
975                 $('#launcher-apps').append(`<div class='launcher-app' data-launch='${i.path}' data-define-id='${i.id}'><img src='${i.icon}'>${i.name}</div>`);
976             }
977         }
978         $('.launcher-app').on('click', function(){
979             $('#launch').click();
980             System.launch( $(this).attr('data-launch') );
981         });
982     }
983
984     this.launch = function(path, args) {
985         let _pid = pid;
986         System.args[_pid] = args;
987         let _path = path;
988         if(_path.lastIndexOf('/') == _path.length - 1) {
989             _path = _path.substring(0, _path.length - 1);
990         }
991         else if(_path.indexOf('/') == -1) {
992             _path = System.appdir + _path;
993         }
994
995         System.launchpath[_pid] = _path;
996     
997         if( System.appCache[_path] ) {
998             if( KWS.fullscreen.pid ) KWS.unmax(KWS.fullscreen.pid);
999             appData( System.appCache[_path] );
1000         }
1001         else {
1002             try{
1003                 $.getJSON( _path + '/define.json', appData ).fail( () => {
1004                     Notification.push('kitアプリをロードできません。', `${_path}を展開できませんでした。`, 'system');
1005                 } );
1006             }
1007             catch(error){
1008                 Notification.push( "System Error", error, "system" );
1009             }
1010         }
1011     }
1012
1013     this.clip = new function(){
1014         this.content = null;
1015         this.history = new Array();
1016
1017         this.set = function( content ){
1018             this.content = content;
1019             this.history.push(content);
1020             return content;
1021         }
1022         this.get = ()=>{ return this.content }
1023     }
1024
1025     this.config = new function(){
1026         this.apps = new Object();
1027     }
1028
1029     this.audio = new function(){
1030         this.level = localStorage["kit-audio-level"] || 100;
1031         this.silent = false;
1032
1033         this.list = new Array();
1034
1035         this.volume = function( _level ){
1036             $("#dropdown-sound-slider").slider("value", _level);
1037         }
1038
1039         this.play = function( _audioid, _src ){
1040             if( !System.audio.list[_audioid] ){
1041                 System.audio.list[_audioid] = new Audio(_src);
1042                 System.audio.list[_audioid].volume = System.audio.level / 100;
1043             }
1044             System.audio.list[_audioid].play();
1045         }
1046
1047         this.get = function( _audioid ){
1048             return System.audio.list[_audioid];
1049         }
1050
1051         this.pause = function( _audioid ){
1052             System.audio.list[_audioid].pause();
1053         }
1054
1055         this.stop = function( _audioid ){
1056             System.audio.list[_audioid].pause();
1057             System.audio.list[_audioid] = null;
1058         }
1059
1060         this.seek = function( _audioid, _time ){
1061             System.audio.list[_audioid].fastSeek(_time);
1062         }
1063
1064         this.mute = function( _audioid, _bool ){
1065             System.audio.list[_audioid].muted = _bool;
1066         }
1067     }
1068 }
1069
1070 const KWS = new function(){
1071     this.version = "3.2.2";
1072     this.active = null;
1073
1074     this.darkmode = false;
1075
1076     this.changeWindowTitle = function( _pid, _str ){
1077         $("#tname"+_pid).text( _str );
1078         $("#wtname"+_pid).text( _str );
1079     }
1080
1081     this.min = function( _str ) {
1082         let _pid = String( _str );
1083         if( $( "#w" + _pid ).is( ":visible" ) ) {
1084             $( "#w" + _pid ).css("transition", "none").hide( "drop", {direction: "down"}, 300 );
1085             $( "#task-ctx" ).effect( "bounce", {distance: 12, times: 1}, 400 );
1086             $( "#t" + _pid ).addClass( "task-min" );
1087         }
1088         else {
1089             $( "#w" + _pid ).show( "drop", {direction: "down"}, 300 );
1090             $( "#task-ctx" ).effect( "bounce", {distance: 12, times: 1}, 400 );
1091             $( "#t" + _pid ).removeClass( "task-min" );
1092         }
1093     }
1094
1095     this.fullscreen = {
1096         "pid": null,
1097         "prevWidth": null,
1098         "prevHeight": null,
1099         "prevTop": 0,
1100         "prevLeft": 0
1101     }
1102
1103     this.max = function( _pid ){
1104         if( KWS.fullscreen.pid ){
1105             Notification.push("最大化に失敗", "最大化しているウィンドウがあります。");
1106             return;
1107         }
1108         $( "#wt"+_pid ).addClass("wtmaximize");
1109         $( "#w"+_pid ).css({
1110             "top": "0px",
1111             "left": "0px"
1112         })
1113         .addClass("windowmaximize")
1114         .css("z-index", KWS.windowIndex + 1);
1115         KWS.refreshWindowIndex();
1116
1117         KWS.fullscreen.prevWidth = $("#winc"+_pid).outerWidth();
1118         KWS.fullscreen.prevHeight = $("#winc"+_pid).outerHeight();
1119         KWS.fullscreen.prevTop = $("#w"+_pid).offset().top;
1120         KWS.fullscreen.prevLeft = $("#w"+_pid).offset().left;
1121
1122         KWS.resize( _pid, System.display.width, System.display.height - 30 );
1123         $("footer").hide();
1124         $("#kit-header-fullscreen").show().on("click", () => {
1125             KWS.unmax( _pid );
1126         });
1127         KWS.fullscreen.pid = _pid;
1128     }
1129
1130     this.unmax = function( _pid ){
1131         if( _pid != KWS.fullscreen.pid ){
1132             Notification.push("最大化解除に失敗", "対象がフルスクリーンウィンドウではありません。");
1133             return;
1134         }
1135         $( "#wt"+_pid ).removeClass("wtmaximize");
1136         $( "#w"+_pid ).css({
1137             "top": KWS.fullscreen.prevTop,
1138             "left": KWS.fullscreen.prevLeft
1139         })
1140         .removeClass("windowmaximize");
1141         $("footer").show();
1142         $("#kit-header-fullscreen").hide().off();
1143         KWS.resize( _pid, KWS.fullscreen.prevWidth, KWS.fullscreen.prevHeight );
1144         KWS.fullscreen.pid = null;
1145         KWS.fullscreen.prevWidth = null;
1146         KWS.fullscreen.prevHeight = null;
1147         KWS.fullscreen.prevTop = null;
1148         KWS.fullscreen.prevLeft = null;
1149     }
1150
1151     this.vacuum = function( _left, _top ){
1152         for( let i in process ){
1153             $("#w"+i).css("transition", ".5s all ease").css("left", _left ).css("top", _top );
1154         }
1155         setTimeout(() => {
1156             $(".window").css("transition", "none");
1157         }, 500);
1158     }
1159
1160     this.active = null;
1161     this.windowIndex = 1;
1162
1163     this.refreshWindowIndex = function(){
1164         let num = $(".window").length;
1165         let array = new Array();
1166         let obj = new Object();
1167         for( let i = 0; i < num; i++ ){
1168             obj = { id: $(".window")[i].id, zindex: $(".window")[i].style.zIndex };
1169             array.push( obj );
1170         };
1171         array.sort( (a,b) => {
1172             return Number(a.zindex - b.zindex);
1173         } );
1174         for( let i in array ){
1175             document.getElementById(array[i].id).style.zIndex = i;
1176             if( i == num-1 ){
1177                 $("#"+array[i].id).addClass("windowactive");
1178                 $("#t"+String(array[i].id).substring(1)).addClass("t-active");
1179                 KWS.active = String(array[i].id).substring(1);
1180                 process[array[i].id.substring(1)].isactive = true;
1181             }
1182             else{
1183                 $("#"+array[i].id).removeClass("windowactive");
1184                 $("#t"+String(array[i].id).substring(1)).removeClass("t-active");
1185                 process[array[i].id.substring(1)].isactive = false;
1186             }
1187         }
1188         KWS.windowIndex = num;
1189     }
1190
1191     this.front = function( _pid ) {
1192         $(`#w${_pid}`).css("z-index", KWS.windowIndex + 1);
1193         KWS.refreshWindowIndex();
1194     }
1195
1196     this.resize = function( _pid, _width, _height ){
1197         if( _width ) $("#winc"+_pid).css("width", _width)
1198         if( _height ) $("#winc"+_pid).css("height", _height);
1199     }
1200
1201     this.fusen = new function(){
1202         this.fid = 0;
1203         this.list = new Object();
1204
1205         this.add = function(_text){
1206             KWS.fusen.list[KWS.fusen.fid] = String(_text);
1207             $("#desktop-"+currentDesktop).append("<div class='kit-fusen' id='kit-f"+KWS.fusen.fid+"'><i class='fa fa-quote-left'></i><textarea class='kit-fusen-textarea kit-selectable' data-fid='"+KWS.fusen.fid+"' kit-context='fusen'>"+_text+"</textarea></div>");
1208             $("#kit-f"+KWS.fusen.fid).css({
1209                 "left": Number(KWS.fusen.fid)*40 + 20,
1210                 "top": Number(KWS.fusen.fid)*10 + 100,
1211             }).pep({
1212                 elementsWithInteraction: ".kit-fusen-textarea",
1213                 useCSSTranslation: false,
1214                 disableSelect: false,
1215                 shouldEase:     true,
1216                 initiate: function(){
1217                     $(this.el).css("ui-opacity", "0.7");
1218                 },
1219                 stop: function(){
1220                     this.el.style.transition = "none";
1221                     $(this.el).css("ui-opacity", "1.0");
1222                 }
1223             })
1224             $(".kit-fusen-textarea").off().on("change",function(){
1225                 Notification.push($(this).attr("data-fid"), $(this).val(), "debug");
1226                 KWS.fusen.list[$(this).attr("data-fid")] = $(this).val();
1227                 localStorage.setItem("kit-fusen", JSON.stringify( KWS.fusen.list ));
1228             });
1229             localStorage.setItem("kit-fusen", JSON.stringify( KWS.fusen.list ));
1230             KWS.fusen.fid++;
1231         }
1232
1233         this.remove = function(_fid){
1234             delete KWS.fusen.list[_fid];
1235             localStorage.setItem("kit-fusen", JSON.stringify( KWS.fusen.list ));
1236             $("#kit-f"+_fid).remove();
1237         }
1238     }
1239
1240     this.addCustomContext = function( _elem, _contextid, _obj ){
1241         KWS.context[_contextid] = _obj;
1242     }
1243
1244     this.context = {
1245         "fusen" : {
1246             "name" : "ふせん",
1247             "delete" : {
1248                 "label" : "ふせんを削除",
1249                 "icon" : "fa-trash-alt",
1250                 "function" : function(){
1251                     KWS.fusen.remove( S.selectedElement.attr("data-fid") );
1252                 }
1253             },
1254             "copy" : {
1255                 "label" : "ふせんを複製",
1256                 "icon" : "fa-copy",
1257                 "function" : function(){
1258                     KWS.fusen.add( KWS.fusen.list[S.selectedElement.attr("data-fid")] );
1259                 }
1260             }
1261         }
1262     }
1263 }
1264
1265 const Notification = new function() {
1266     this.nid = 0;
1267     this.list = new Object();
1268
1269     this.goodnight = false;
1270     this.sound = null;
1271
1272     this.push = function( _title, _content, _app, _pid, _action, _img, _buttons ) {
1273         let _nid = this.nid;
1274         if( !System.debugmode && ( _title == "debug" || _app == "debug" ) ){
1275             return false;
1276         }
1277         Notification.list[_nid] = {
1278             title: _title,
1279             content: _content,
1280             app: _app,
1281             time: System.time.obj.toLocaleString(),
1282             pid: _pid,
1283             action: () => {
1284                 if( typeof _action == 'function' ) _action();
1285                 else if( _pid ) KWS.front(_pid);
1286                 $('#notifications').hide('drop', {direction: 'right'}, 300);
1287             },
1288             img: _img
1289         };
1290         if( _pid && System.appCache[System.launchpath[_pid]] ){
1291             _app = `<img src='${System.launchpath[_pid]}/${System.appCache[System.launchpath[_pid]].icon}'>${System.appCache[System.launchpath[_pid]].name}`;
1292         }
1293         if( !this.goodnight ){
1294             if( this.sound ) System.audio.play( "n" + this.nid, this.sound );
1295             $('#last-notification-title').text('').text( _title );
1296             $('#last-notification-content').text('').text( _content );
1297             $('#last-notification-app').text('').html( _app );
1298             $('#last-notification').hide().show('drop', {direction: "right"}, 300).off().on('click', Notification.list[this.nid].action);
1299             if( _img ) $('#last-notification-img').attr('src', _img).show();
1300             else $('#last-notification-img').attr('src', '').hide();
1301         }
1302         let imgtag = '';
1303         if( _img ) imgtag = `<img src='${_img }' alt='' class='notis_img'>`;
1304         $(`<div class='notis' id='nt${_nid}'>
1305                 ${imgtag}
1306                 <span class='notis_close' id='nc${_nid}'></span>
1307                 <div class='notis_app'>${_app}</div>
1308                 <span>${_title}</span>
1309                 ${_content}
1310                 <div class='notis_buttons'></div>
1311                 <div class='notis_time'>${System.time.obj.toTimeString()}</div>
1312             </div>`).appendTo('#notifications').on('click', Notification.list[this.nid].action);
1313         $(`#nc${_nid}`).on('click', (e) => {
1314             e.stopPropagation();
1315             $(`#nt${_nid}`).fadeOut(300);
1316         });
1317         $('#last-notification-buttons').html('');
1318         if( _buttons ){
1319             for( let b of _buttons ){
1320                 $(`<a>${b.label}</a>`).appendTo('#last-notification-buttons').on('click', (e) => {
1321                     e.stopPropagation();
1322                     b.func();
1323                 });
1324                 $(`<a>${b.label}</a>`).appendTo(`#nt${_nid} .notis_buttons`).on('click', (e) => {
1325                     e.stopPropagation();
1326                     b.func();
1327                 });
1328             }
1329         }
1330         this.nid ++;
1331         return (this.nid - 1);
1332     }
1333 }
1334
1335 class App {
1336     constructor(_pid) {
1337         App.e[_pid] = new Object();
1338         App.d[_pid] = new Object();
1339         
1340         this.process = process[_pid];
1341         this.cache = System.appCache[System.launchpath[_pid]];
1342
1343         this.args = System.args[_pid];
1344         this.close = () => System.close(_pid);
1345         this.d = App.d[_pid];
1346         this.dom = (..._args) => System.dom(_pid, ..._args);
1347         this.e =  App.e[_pid];
1348         this.ntf = (_title, _content, _action, _img, _buttons) => Notification.push(_title, _content, this.info.id, _pid, _action, _img, _buttons);
1349         this.qs = (...args) => System.qs(_pid, ...args);
1350         this.front = () => KWS.front(_pid);
1351
1352         this.changeWindowTitle = _t => App.changeWindowTitle( _pid, _t );
1353         this.data = (_name, _value) => App.data(_pid, _name, _value);
1354         this.event = (_name, _event) => App.event(_pid, _name, _event);
1355         this.getPath = _path => App.getPath(_pid, _path);
1356         this.kaf = () => App.kaf(_pid);
1357         this.load = _path => App.load(_pid, _path);
1358         this.preventClose = _bool => App.preventClose(_pid, _bool);
1359     }
1360
1361     static changeWindowTitle( _pid, _t ) {
1362         $( "#tname"+_pid ).text( _t );
1363         $( "#wtname"+_pid ).text( _t );
1364         process[_pid].title = _t;
1365         return App;
1366     }
1367
1368     static context( _cid, _obj ) {
1369         KWS.context[ _cid ] = _obj;
1370         return App;
1371     }
1372
1373     static data( _pid, _name, _value ) {
1374         if( _value !== undefined ) {
1375             S.dom(_pid, `[kit\\:bind=${_name}]`).val( _value );
1376             S.dom(_pid, `[kit\\:observe=${_name}]`).text( _value );
1377             if( _value ) S.dom(_pid, `[kit\\:if=${_name}]`).show();
1378             else S.dom(_pid, `[kit\\:if=${_name}]`).hide();
1379             return App.d[_pid][_name] = _value;
1380         }
1381         else if( _name ) return App.d[_pid][_name];
1382         else return App.d[_pid];
1383     }
1384
1385     static event( _pid, _name, _event ) {
1386         if( !App.e[_pid] ) App.e[_pid] = new Object();
1387         App.e[_pid][_name] = _event;
1388         return App;
1389     }
1390
1391     static getPath( _pid, _path ) {
1392         if( String(_path)[0] != '/' ) _path = '/' + _path;
1393         return System.launchpath[_pid] + _path;
1394     }
1395
1396     static kaf( _pid ) {
1397         let attrs = [
1398             "[kit-ref]",
1399             "[kit-e]",
1400             "[kit-src]",
1401             "[kit-alert]",
1402             "[kit-launch]",
1403             "[kit-close]",
1404             "[kit-text]",
1405             "[kit-html]",
1406             "[kit\\:bind]",
1407             "[kit\\:observe]",
1408             "[kit\\:value]",
1409             "[kit-value]",
1410             "[kit-color]",
1411             "[kit\\:if]",
1412             "[kit-if]"
1413         ]
1414         const PID = _pid;
1415         const DATA = App.data(_pid);
1416         const ARGS = System.args[_pid];
1417         for( let i of S.qs(_pid, ...attrs) ){
1418             if( i.hasAttribute("kit-ref") ){
1419                 $(i).on("click", () => App.load(_pid, i.getAttribute("kit-ref")) );
1420             }
1421             if( i.hasAttribute("kit-e") ){
1422                 let _eqs = i.getAttribute("kit-e").split(",");
1423                 for( let k of _eqs ){
1424                     let _eq = k.split(" ");
1425                     $(i).on( _eq[1]||"click", App.e[_pid][_eq[0]] );
1426                 }
1427             }
1428             if( i.hasAttribute("kit-src") ){
1429                 $(i).attr("src", `${System.launchpath[_pid]}/${i.getAttribute("kit-src")}` );
1430             }
1431             if( i.hasAttribute("kit-alert") ){
1432                 $(i).on("click", ()=> System.alert( System.appCache[System.launchpath[_pid]].name, i.getAttribute("kit-alert") ) );
1433             }
1434             if( i.hasAttribute("kit-launch") ){
1435                 $(i).on("click", ()=> System.launch( i.getAttribute("kit-launch") ) );
1436             }
1437             if( i.hasAttribute("kit-close") ){
1438                 $(i).on("click", ()=> System.close( i.getAttribute("kit-close") || _pid ) );
1439             }
1440             if( i.hasAttribute("kit-text") ){
1441                 $(i).text( eval(i.getAttribute("kit-text")) );
1442             }
1443             if( i.hasAttribute("kit-html") ){
1444                 $(i).html( eval(i.getAttribute("kit-html")) );
1445             }
1446             if( i.hasAttribute("kit:bind") ){
1447                 if( App.d[_pid] == undefined ) App.d[_pid] = new Object();
1448                 $(i).on('keydown keyup keypress change', () => {
1449                     let _name = i.getAttribute("kit:bind");
1450                     App.d[_pid][_name] = i.value;
1451                     S.dom(_pid, `[kit\\:observe=${_name}]`).text( i.value );
1452                     if( i.value ) S.dom(_pid, `[kit\\:if=${_name}]`).show();
1453                     else S.dom(_pid, `[kit\\:if=${_name}]`).hide();
1454                 } );
1455             }
1456             if( i.hasAttribute("kit:observe") ){
1457                 $(i).text( App.d[_pid][i.getAttribute("kit:observe")] );
1458             }
1459             if( i.hasAttribute("kit:value") ){
1460                 $(i).val( App.d[_pid][i.getAttribute("kit:value")] );
1461             }
1462             if( i.hasAttribute("kit-value") ){
1463                 $(i).val( eval(i.getAttribute("kit-value")) );
1464             }
1465             if( i.hasAttribute("kit-color") ){
1466                 $(i).css('color', i.getAttribute("kit-color"));
1467             }
1468             if( i.hasAttribute("kit:if") ){
1469                 if( App.d[_pid][i.getAttribute("kit:if")] ){
1470                     $(i).show();
1471                 }
1472                 else $(i).hide();
1473             }
1474             if( i.hasAttribute("kit-if") ){
1475                 if( eval( i.getAttribute("kit-if")) ){
1476                     $(i).show();
1477                 }
1478                 else $(i).hide();
1479             }
1480         }
1481     }
1482
1483     static load( _pid, _path ) {
1484         if( String(_path)[0] != '/' ) _path = '/' + _path;
1485         _path = System.launchpath[_pid] + _path;
1486         S.dom(_pid).load( _path, () => {
1487             App.kaf(_pid);
1488         } );
1489         return App;
1490     }
1491
1492     static preventClose( _pid, _bool = true ) {
1493         process[_pid].preventclose = _bool || true;
1494         return App;
1495     }
1496 }
1497
1498 App.d = new Object();
1499 App.e = new Object();
1500 App.version = "2.1.1";
1501
1502 var process = {}, pid = 0, app, currentDesktop = 1, currentCTX = "", prevWindowIndex, S;