OSDN Git Service

Add kitstrap for /setup.html and "Skip Setup" [0.2.1]
[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 function launch( str, args, dir ) {
489     let _pid = pid;
490     System.args[_pid] = args;
491     let _path = dir || System.appdir + str;
492     System.launchpath[_pid] = _path;
493
494     if( System.appCache[_path] ) {
495         if( KWS.fullscreen.pid ) KWS.unmax(KWS.fullscreen.pid);
496         appData( System.appCache[_path] );
497     }
498     else {
499         try{
500             $.getJSON( S.launchpath[_pid] + '/define.json', appData ).fail( () => {
501                 Notification.push('kitアプリをロードできません。', `${str}を展開できませんでした。`, 'system');
502             } );
503         }
504         catch(error){
505             Notification.push( "System Error", error, "system" );
506         }
507     }
508 }
509
510 function appData( data ) {
511     let _pid = pid;
512     process[String( _pid )] = {
513         id: data.id,
514         time: System.time.obj.toLocaleString(),
515         isactive: false,
516         preventclose: false,
517         title: data.name
518     };
519     System.appCache[System.launchpath[pid]] = data;
520     app = new App(_pid);
521     let _taskAppend = `<span id='t${_pid}'>`;
522     if( data.icon && data.icon != "none" ) _taskAppend += `<img src='${S.launchpath[_pid]}/${data.icon}'>`;
523     _taskAppend += `<span id='tname${_pid}'>${data.name}<span></span>`;
524     $( "#tasks" ).append( _taskAppend );
525     $( "#t" + _pid ).addClass( "task" ).on({
526         click: function() {
527             if( $(this).hasClass("t-active") || $(this).hasClass("task-min") ) KWS.min( _pid );
528             else{
529                 $("#w"+_pid).css("z-index", KWS.windowIndex + 1);
530                 KWS.refreshWindowIndex();
531             }
532         },
533         mouseenter: function() {
534             $( "#task-ctx-name" ).text( data.name );
535             if( data.icon && data.icon != "none" ) $( "#task-ctx-img" ).attr( "src", System.launchpath[_pid] + "/" + data.icon );
536             else $( "#task-ctx-img" ).hide();
537             $( "#task-ctx-ver" ).text( data.version + "/pid:" + _pid );
538             $( "#task-ctx-info" ).off().on( "click", function() { System.appInfo( _pid )} );
539             $( "#task-ctx-sshot" ).off().on( "click", function() { S.screenshot(_pid, true) } );
540             $( "#task-ctx-min" ).off().on( "click", function() { KWS.min( String(_pid) ) } );
541             if( $(this).hasClass("t-active") ) $( "#task-ctx-front" ).hide();
542             else $( "#task-ctx-front" ).show();
543             $( "#task-ctx-front" ).off().on( "click", function() {
544                 $("#w"+_pid).css("z-index", KWS.windowIndex + 1);
545                 KWS.refreshWindowIndex();
546             } );
547             $( "#task-ctx-close" ).off().on("click", () => System.close(_pid));
548             $( "#task-ctx-kill" ).off().on("click", () => System.kill( data.id));
549             const _ctxleft = $(this).offset().left, _ctxtop = window.innerHeight - $(this).offset().top;
550             if( _ctxleft != $( "#task-ctx" ).offset().left ) $( "#task-ctx" ).hide();
551             $( "#task-ctx" ).css( "left", _ctxleft ).css( "bottom", _ctxtop ).show();
552         }
553     } );
554     $( "section, #kit-tasks" ).on( "mouseenter", function() {
555         $( "#task-ctx" ).fadeOut( 200 );
556     } );
557     $( "#t" + _pid ).on({
558         mouseenter: () => {
559             prevWindowIndex = $( "#w" + _pid ).css( "z-index" );
560             $( "#w" + _pid ).addClass( "win-highlight" );
561         },
562         mouseleave: () => $( "#w" + _pid ).removeClass( "win-highlight" )
563     });
564
565     let _windowAppend = "<div id='w" + _pid + "'><div id='wt" + _pid + "' class='wt'><i class='wmzx'><span id='wm" + _pid + "'></span>";
566     if( data.support && data.support['fullscreen'] == true ) _windowAppend += "<span id='wz" + _pid + "'></span>";
567     _windowAppend += "<span id='wx" + _pid + "'></span></i>";
568     if( data.icon && data.icon != "none" ) _windowAppend += "<img src='" + S.launchpath[_pid] + "/" + data.icon + "'>";
569     _windowAppend += "<span id='wtname" + _pid + "'>" + data.name + "</span></div><div class='winc winc-" + data.id + "' id='winc" + _pid + "'></div></div>";
570     $( "#desktop-" + currentDesktop ).append( _windowAppend );
571
572     if( data.support && data.support['darkmode'] == true ) $("#winc"+_pid).addClass("winc-darkmode");
573     if( KWS.darkmode ) $("#winc"+_pid).addClass("kit-darkmode");
574
575     if( data.size ){
576         $("#winc"+_pid).css("width", data.size.width).css("height", data.size.height);
577     }
578     if( data.resize ){
579         let _minwidth = 200, _minheight = 40;
580         if( data.resize.minWidth ) _minwidth = data.resize.minWidth;
581         if( data.resize.minHeight ) _minheight = data.resize.minHeight;
582         $("#winc"+_pid).windowResizable({
583             minWidth: _minwidth,
584             minHeight: _minheight
585         });
586     }
587
588     let windowPos = 50 + ( _pid % 10 ) * 20;
589     KWS.windowIndex ++;
590     $( "#w"+_pid ).addClass( "window" ).pep({
591         elementsWithInteraction: ".winc, .ui-resizable-handle",
592         useCSSTranslation: false,
593         disableSelect: false,
594         shouldEase:     true,
595         initiate: function(){
596             $(this.el).addClass("ui-draggable-dragging");
597             KWS.windowIndex ++;
598             this.el.style.zIndex = KWS.windowIndex;
599             KWS.refreshWindowIndex();
600         },
601         stop: function(){
602             this.el.style.transition = "none";
603             $(this.el).removeClass("ui-draggable-dragging");
604         }
605     }).on( "mousedown", function(){
606         $(".window").css( "transition", "none" );
607         $(this).css("z-index", KWS.windowIndex + 1);
608         KWS.refreshWindowIndex();
609     } ).css( "left", windowPos + "px" ).css( "top", windowPos + "px" ).css( "z-index",  KWS.windowIndex );
610     KWS.refreshWindowIndex();
611     $( `#wm${_pid}` ).addClass( "wm fa fa-window-minimize" ).on("click", () => KWS.min( _pid ) );
612     $( `#wz${_pid}` ).addClass( "wz fas fa-square" ).on("click", () => KWS.max( _pid ) );
613     $( `#wx${_pid}` ).addClass( "wx fa fa-times" ).on("click", () => System.close( _pid )  );
614     $( "#winc" + _pid ).resizable( {
615         minWidth: "200"
616     } ).load( System.launchpath[_pid] + "/" + data.view, (r, s, x) => {
617         if( s == "error" ){
618             Notification.push("起動に失敗:" + x.status, x.statusText);
619             return false;
620         }
621         if( !data.script || data.script != "none" ) $.getScript( System.launchpath[_pid] + "/" + data.script, () => {
622             if( !data.support || data.support['kaf'] != false ) App.kaf(_pid);
623             pid++;
624         }).fail( () => {
625             App.kaf(_pid);
626             pid++;
627         });
628         else if( !data.support || data.support['kaf'] != false ){
629             App.kaf(_pid);
630             pid++;
631         }
632         else pid++;
633         if( data.css != "none" && $("#kit-style-"+data.id).length == 0 ){
634             $( "head" ).append( '<link href="' + System.launchpath[_pid] + '/' + data.css + '" rel="stylesheet" id="kit-style-' + data.id + '"></link>' );
635         }
636         localStorage.setItem( "kit-pid", pid );
637     } );
638 }
639
640 const System = new function() {
641     this.version = "0.2.1";
642     this.username = localStorage.getItem("kit-username");
643     this.appdir = localStorage.getItem("kit-appdir");
644     this.loc = { ...location };
645
646     this.bootopt = new URLSearchParams(location.search);
647
648     this.mouseX = 0;
649     this.mouseY = 0;
650
651     this.display = {
652         "width": window.innerWidth,
653         "height": window.innerHeight
654     }
655
656     this.selectedElement = null;
657     this.selectedText = null;
658
659     this.dom = function(_pid, ..._elems) {
660         let q = "";
661         if( !_elems.length ) q = ",#winc" + _pid;
662         else for( let i of _elems ){
663             q += ",#winc" + _pid + " " + i;
664         }
665         return $( q.substring(1) );
666     }
667
668     this.qs = ( _pid, ..._elems ) => {
669         let q = "";
670         if( !_elems.length ) q = ",#winc" + _pid;
671         else for( let i of _elems ) q += ",#winc" + _pid + " " + i;
672         return document.querySelectorAll( q.substring(1) )
673     }
674
675     this.userarea = new Object();
676     this.recycle = new Object();
677
678     this.appCache = {};
679     //アプリ引数
680     this.args = {};
681     //アプリ起動パス
682     this.launchpath = {};
683
684     this.support = $.support;
685     this.debugmode = false;
686
687     this.battery = null;
688
689     this.log = new Array();
690     this.noop = () => {}
691
692     this.setBattery = function(){
693         if( navigator.getBattery ) navigator.getBattery().then((e)=>{
694             let _lv =  e.level * 100;
695             System.battery = _lv;
696             return _lv;
697         });
698     }
699
700     this.screenshot = function( _pid, _popup ){
701         let _elem = document.querySelector("body");
702         if( _pid ) _elem = document.querySelector("#w"+_pid);
703         html2canvas( _elem ).then(canvas => {
704             if( _popup ){
705                 canvas.style.border = "1px solid #909090";
706                 S.save( canvas.toDataURL("image/png"), "image" );
707             }
708             return canvas;
709         });
710     }
711
712     this.obj2img = function( _obj, _popup ){
713         let _elem = _obj[0];
714         html2canvas( _elem ).then(canvas => {
715             if( _popup ){
716                 canvas.style.border = "1px solid #909090";
717                 S.save( canvas.toDataURL("image/png"), "image" );
718             }
719             return canvas;
720         });
721     }
722
723     this.save = function(data, type){
724         System.launch("fivr", { "save" : data, "type" : type });
725     }
726
727     this.open = function(filename){
728         System.launch("fivr", { "open" : filename });
729     }
730
731     this.preventClose = function( _pid ){
732         if( !process[_pid] ) return false;
733         process[_pid].preventclose = true;
734         return true;
735     }
736     
737     this.shutdown = function(_opt) {
738         $( "#last-notification-close" ).click();
739         $( "#kit-power-back" ).click();
740         for( let i in process ) {
741             if( process[i].preventclose == true ){
742                 S.dialog( "シャットダウンの中断", "pid" + System.appCache[System.launchpath[i]].name + "がシャットダウンを妨げています。<br>強制終了してシャットダウンを続行する場合は[OK]を押下してください。", () => {
743                     process[i].preventclose = false;
744                     System.shutdown();
745                 } );
746                 return false;
747             }
748             else System.close( i );
749         }
750         $( "section" ).hide();
751         $( "body" ).css( "background-color", "black" );
752         $( "header, footer" ).fadeOut( 300 );
753         $( "#kit-wallpaper" ).fadeOut( 1500 );
754         if( _opt == "reboot" ) location.href = "autorun.html";
755         localStorage.setItem("kit-shutted-down", true);
756     }
757
758     this.reboot = function() {
759         System.shutdown("reboot");
760     }
761
762     this.lock = function(){
763         System.moveDesktop( "l" );
764
765         $( "#lock-user-icon" ).css( "background", localStorage.getItem( "kit-user-color" ) );
766         $( "section, header, footer" ).css( "filter", "none" );
767         $( "#kit-wallpaper" ).css( "filter", "blur(20px)" );
768         $( "header, footer, #kit-power" ).hide();
769
770         $( "#lock-username" ).text( localStorage.getItem( "kit-username" ) );
771         if( localStorage.getItem( "kit-password" ) ) $( "#lock-password" ).show();
772         else $( "#lock-password" ).hide();
773     }
774
775     this.alert = function( title, content, wtitle ) {
776         System.launch("alert", [title, content, wtitle]);
777     }
778
779     this.dialog = function( title, content, func ){
780         System.launch("dialog", {
781             "title": title,
782             "content": content,
783             "func": func
784         });
785     }
786
787     this.appInfo = function( _pid ){
788         let _title = "", _content = "";
789         let ac = System.appCache[S.launchpath[_pid]];
790         let _lp = System.launchpath[_pid];
791         if( ac ){
792             _title = ac.name + " " + ac.version;
793             if( ac.icon && ac.icon != "none" ) _content = "<img style='height: 96px' src='" + _lp + "/" + ac.icon + "'><br>";
794             for( let i in ac ){
795                 if( typeof ac[i] != "object" ) _content += "<div><span style='font-weight: 100'>" + i + " </span>" + ac[i] + "</div>";
796             }
797             _content += "<br><span style='font-weight: 100'>起動パス " + _lp + "</span><br><br>"
798         }
799         else _title = "取得に失敗しました";
800         System.alert( _title, _content );
801     }
802
803     this.apps = new Object();
804     this.installed = new Array();
805
806     this.close = function( _str ) {
807         let _pid = String( _str );
808         $( "#w" + _pid ).remove();
809         $( "#t" + _pid ).remove();
810         $( "#task-ctx" ).hide();
811         delete process[_pid];
812         KWS.refreshWindowIndex();
813     }
814
815     this.kill = function( _str ){
816         for( let _pid in process ) {
817             if( process[_pid] && process[_pid].id == _str ) System.close( _pid );
818         }
819     }
820     
821     this.vacuum = function( _left, _top ){
822         KWS.vacuum( _left, _top ); //非推奨です(削除予定)。
823     }
824
825     this.time = {
826         "obj" : new Date(),
827         "y" : "1970",
828         "m" : "1",
829         "d" : "1",
830         "h" : "00",
831         "i" : "00",
832         "s" : "00",
833         "ms" : "0"
834     }
835
836     this.clock = function() {
837         let DD = new Date();
838         S.time.obj = DD;
839         let Year = DD.getFullYear();
840         S.time.day = DD.getDay();
841         S.time.y = Year;
842         let Month = ( "00" + Number(DD.getMonth()+1) ).slice( -2 );
843         S.time.m = Month;
844         let DateN = ( "00" + DD.getDate() ).slice( -2 );
845         S.time.d = DateN;
846         let Hour = ( "00" + DD.getHours() ).slice( -2 );
847         S.time.h = Hour;
848         let Min = ( "00" + DD.getMinutes() ).slice( -2 );
849         S.time.i = Min;
850         let Sec = ( "00" + DD.getSeconds() ).slice( -2 );
851         S.time.s = Sec;
852         $( ".os-time" ).text( Hour + ":" + Min + ":" + Sec );
853         let MS = DD.getMilliseconds();
854         S.time.ms = MS;
855         let circle = {
856             outer: { radius: .9, color: "transparent" },
857             inner: { radius: .85, color: "transparent" }
858         }
859         let lines = {
860             long: { from: .8, to: .7, width: 2, color: "#303030" },
861             short: { from: .8, to: .75, width: 1, color: "#a0a0a0" }
862         }
863         let hands = {
864             hour: { length: .4, width: 3, cap: "butt", color: "#303030", ratio: .2 },
865             minute: { length: .67, width: 2, cap: "butt", color: "#303030", ratio: .2 },
866             second: { length: .67, width: 1, cap: "butt", color: "dodgerblue", ratio: .2 }
867         }
868         let canvas = $(".dropdown-clock-canvas")[0];
869         canvas.width = "200", canvas.height = "200";
870         let context = canvas.getContext("2d");
871         let center = { x: Math.floor(canvas.width / 2), y: Math.floor(canvas.height / 2) };
872         let radius = Math.min(center.x, center.y), angle, len;
873         context.beginPath();context.fillStyle = circle.outer.color;
874         context.arc(center.x, center.y, radius * circle.outer.radius, 0, Math.PI * 2, false);
875         context.fill();context.beginPath();context.fillStyle = circle.inner.color;
876         context.arc(center.x, center.y, radius * circle.inner.radius, 0, Math.PI * 2, false);
877         context.fill();
878         for( let i=0; i<60; i++ ){
879             angle = Math.PI * i / 30;
880             context.beginPath();
881             let line = ( i%5 == 0 ) ? lines.long : lines.short;
882             context.lineWidth = line.width, context.strokeStyle = line.color;
883             context.moveTo(center.x + Math.sin(angle) * radius * line.from, center.y - Math.cos(angle) * radius * line.from)
884             context.lineTo(center.x + Math.sin(angle) * radius * line.to, center.y - Math.cos(angle) * radius * line.to);
885             context.stroke();
886         }
887         angle = Math.PI * ( Number(Hour)+Number(Min)/60 ) / 6, len = radius * hands.hour.length;
888         context.beginPath(), context.lineWidth = hands.hour.width;
889         context.lineCap = hands.hour.cap, context.strokeStyle = hands.hour.color;
890         context.moveTo(center.x - Math.sin(angle) * len * hands.hour.ratio, center.y + Math.cos(angle) * len * hands.hour.ratio);
891         context.lineTo(center.x + Math.sin(angle) * len, center.y - Math.cos(angle) * len), context.stroke();
892         angle = Math.PI * ( Number(Min)+Number(Sec) / 60) / 30, len = radius * hands.minute.length;
893         context.beginPath(), context.lineWidth = hands.minute.width;
894         context.lineCap = hands.minute.cap, context.strokeStyle = hands.minute.color;
895         context.moveTo(center.x - Math.sin(angle) * len * hands.minute.ratio, center.y + Math.cos(angle) * len * hands.minute.ratio);
896         context.lineTo(center.x + Math.sin(angle) * len, center.y - Math.cos(angle) * len), context.stroke();
897         angle = Math.PI * Number(Sec) / 30, len = radius * hands.second.length;
898         context.beginPath(), context.lineWidth = hands.second.width;
899         context.lineCap = hands.second.cap, context.strokeStyle = hands.second.color;
900         context.moveTo(center.x - Math.sin(angle) * len * hands.second.ratio, center.y + Math.cos(angle) * len * hands.second.ratio);
901         context.lineTo(center.x + Math.sin(angle) * len, center.y - Math.cos(angle) * len), context.stroke();
902     }
903
904     this.changeWallpaper = function( str ) {
905         $( "#kit-wallpaper" ).css( "background", str ).css( "background-size", "cover" );
906         localStorage.setItem( "kit-wallpaper", str )
907     }
908
909     this.moveDesktop = function( str ) {
910         str = String( str );
911         $( "section" ).hide();
912         $( "#desktop-" + str ).show();
913         $( "#desktops" ).html( "<span class='far fa-clone'></span>Desktop" + str );
914         currentDesktop = str;
915     }
916
917     this.avoidMultiple = function( _pid, _alert ) {
918         let _id = process[_pid].id;
919         let _cnt = 0;
920         for( let i in process ) {
921             if( process[i].id == _id ) _cnt += 1;
922         }
923         Notification.push( "debug", _cnt );
924         if( _cnt > 1 ) {
925             System.close( _pid );
926             if( !_alert ){
927                 System.alert( "多重起動", "アプリケーション" + _id + "が既に起動しています。このアプリケーションの多重起動は許可されていません。" );
928             }
929         }
930         return _cnt;
931     }
932
933     this.resizable = function( _pid, _elem, _width, _height ){
934         let E = ".winc";
935         if( _elem ) E = String( _elem );
936         if( !_width ) _width = null;
937         if( !_height ) _height = "100";
938         $("#w" + _pid).resizable({
939             alsoResize: "#w" + _pid + " " + E,
940             minWidth: _width,
941             minHeight: _height
942         });
943     }
944
945     this.initLauncher = function(data){
946         $("#launcher-apps").html("");
947         System.apps = data;
948         for( let i in data ){
949             $('#launcher-apps').append(`<div class='launcher-app' data-launch='${i}'><img src='${data[i].icon}'>${data[i].name}</div>`);
950         }
951         if( !System.bootopt.get('safe') ){
952             for( let i of System.installed ){
953                 $('#launcher-apps').append(`<div class='launcher-app' data-launch='${i.path}' data-define-id='${i.id}'><img src='${i.icon}'>${i.name}</div>`);
954             }
955         }
956         $('.launcher-app').on('click', function(){
957             $('#launch').click();
958             System.launch( $(this).attr('data-launch') );
959         });
960     }
961
962     this.launch = function(path, args) {
963         let _pid = pid;
964         System.args[_pid] = args;
965         let _path = path;
966         if(_path.lastIndexOf('/') == _path.length - 1) {
967             _path = _path.substring(0, _path.length - 1);
968         }
969         else if(_path.indexOf('/') == -1) {
970             _path = System.appdir + _path;
971         }
972
973         System.launchpath[_pid] = _path;
974     
975         if( System.appCache[_path] ) {
976             if( KWS.fullscreen.pid ) KWS.unmax(KWS.fullscreen.pid);
977             appData( System.appCache[_path] );
978         }
979         else {
980             try{
981                 $.getJSON( _path + '/define.json', appData ).fail( () => {
982                     Notification.push('kitアプリをロードできません。', `${_path}を展開できませんでした。`, 'system');
983                 } );
984             }
985             catch(error){
986                 Notification.push( "System Error", error, "system" );
987             }
988         }
989     }
990
991     this.clip = new function(){
992         this.content = null;
993         this.history = new Array();
994
995         this.set = function( content ){
996             this.content = content;
997             this.history.push(content);
998             return content;
999         }
1000         this.get = ()=>{ return this.content }
1001     }
1002
1003     this.config = new function(){
1004         this.apps = new Object();
1005     }
1006
1007     this.audio = new function(){
1008         this.level = localStorage["kit-audio-level"] || 100;
1009         this.silent = false;
1010
1011         this.list = new Array();
1012
1013         this.volume = function( _level ){
1014             $("#dropdown-sound-slider").slider("value", _level);
1015         }
1016
1017         this.play = function( _audioid, _src ){
1018             if( !System.audio.list[_audioid] ){
1019                 System.audio.list[_audioid] = new Audio(_src);
1020                 System.audio.list[_audioid].volume = System.audio.level / 100;
1021             }
1022             System.audio.list[_audioid].play();
1023         }
1024
1025         this.get = function( _audioid ){
1026             return System.audio.list[_audioid];
1027         }
1028
1029         this.pause = function( _audioid ){
1030             System.audio.list[_audioid].pause();
1031         }
1032
1033         this.stop = function( _audioid ){
1034             System.audio.list[_audioid].pause();
1035             System.audio.list[_audioid] = null;
1036         }
1037
1038         this.seek = function( _audioid, _time ){
1039             System.audio.list[_audioid].fastSeek(_time);
1040         }
1041
1042         this.mute = function( _audioid, _bool ){
1043             System.audio.list[_audioid].muted = _bool;
1044         }
1045     }
1046 }
1047
1048 const KWS = new function(){
1049     this.version = "3.2.3";
1050     this.active = null;
1051
1052     this.darkmode = false;
1053
1054     this.changeWindowTitle = function( _pid, _str ){
1055         $("#tname"+_pid).text( _str );
1056         $("#wtname"+_pid).text( _str );
1057     }
1058
1059     this.min = function( _str ) {
1060         let _pid = String( _str );
1061         if( $( "#w" + _pid ).is( ":visible" ) ) {
1062             $( "#w" + _pid ).css("transition", "none").hide( "drop", {direction: "down"}, 300 );
1063             $( "#task-ctx" ).effect( "bounce", {distance: 12, times: 1}, 400 );
1064             $( "#t" + _pid ).addClass( "task-min" );
1065         }
1066         else {
1067             $( "#w" + _pid ).show( "drop", {direction: "down"}, 300 );
1068             $( "#task-ctx" ).effect( "bounce", {distance: 12, times: 1}, 400 );
1069             $( "#t" + _pid ).removeClass( "task-min" );
1070         }
1071     }
1072
1073     this.fullscreen = {
1074         "pid": null,
1075         "prevWidth": null,
1076         "prevHeight": null,
1077         "prevTop": 0,
1078         "prevLeft": 0
1079     }
1080
1081     this.max = function( _pid ){
1082         let _appcache = System.appCache[System.launchpath[_pid]];
1083         if( KWS.fullscreen.pid || _appcache.support.fullscreen != true ){
1084             Notification.push('最大化に失敗', 'ウィンドウの最大化に失敗しました。', 'system');
1085             return;
1086         }
1087         KWS.fullscreen.prevWidth = $("#winc"+_pid).outerWidth();
1088         KWS.fullscreen.prevHeight = $("#winc"+_pid).outerHeight();
1089         KWS.fullscreen.prevTop = $("#w"+_pid).offset().top;
1090         KWS.fullscreen.prevLeft = $("#w"+_pid).offset().left;
1091         KWS.fullscreen.pid = _pid;
1092         $( "#wt"+_pid ).addClass("wtmaximize");
1093         $( "#w"+_pid ).css({
1094             "top": "0px",
1095             "left": "0px"
1096         })
1097         .addClass("windowmaximize")
1098         .css("z-index", KWS.windowIndex + 1);
1099         KWS.refreshWindowIndex();
1100         KWS.resize( _pid, System.display.width, System.display.height - 30 );
1101         $("footer").hide();
1102         $("#kit-header-fullscreen").show().on('click', () => KWS.unmax( _pid ));
1103     }
1104
1105     this.unmax = function( _pid ){
1106         if( _pid != KWS.fullscreen.pid ){
1107             Notification.push("最大化解除に失敗", "対象がフルスクリーンウィンドウではありません。");
1108             return;
1109         }
1110         $('#wt'+_pid).removeClass("wtmaximize");
1111         $('#w'+_pid).css({
1112             "top": KWS.fullscreen.prevTop,
1113             "left": KWS.fullscreen.prevLeft
1114         })
1115         .removeClass("windowmaximize");
1116         $("footer").show();
1117         $("#kit-header-fullscreen").hide().off();
1118         KWS.resize( _pid, KWS.fullscreen.prevWidth, KWS.fullscreen.prevHeight );
1119         KWS.fullscreen.pid = null;
1120         KWS.fullscreen.prevWidth = null;
1121         KWS.fullscreen.prevHeight = null;
1122         KWS.fullscreen.prevTop = null;
1123         KWS.fullscreen.prevLeft = null;
1124
1125         if( !System.appCache[System.launchpath[_pid]].size.height ) {
1126             System.qs(_pid)[0].style.height = "auto";
1127         }
1128     }
1129
1130     this.vacuum = function( _left, _top ){
1131         for( let i in process ){
1132             $("#w"+i).css("transition", ".5s all ease").css("left", _left ).css("top", _top );
1133         }
1134         setTimeout(() => {
1135             $(".window").css("transition", "none");
1136         }, 500);
1137     }
1138
1139     this.active = null;
1140     this.windowIndex = 1;
1141
1142     this.refreshWindowIndex = function(){
1143         let num = $(".window").length;
1144         let array = new Array();
1145         let obj = new Object();
1146         for( let i = 0; i < num; i++ ){
1147             obj = { id: $(".window")[i].id, zindex: $(".window")[i].style.zIndex };
1148             array.push( obj );
1149         };
1150         array.sort( (a,b) => {
1151             return Number(a.zindex - b.zindex);
1152         } );
1153         for( let i in array ){
1154             document.getElementById(array[i].id).style.zIndex = i;
1155             if( i == num-1 ){
1156                 $("#"+array[i].id).addClass("windowactive");
1157                 $("#t"+String(array[i].id).substring(1)).addClass("t-active");
1158                 KWS.active = String(array[i].id).substring(1);
1159                 process[array[i].id.substring(1)].isactive = true;
1160             }
1161             else{
1162                 $("#"+array[i].id).removeClass("windowactive");
1163                 $("#t"+String(array[i].id).substring(1)).removeClass("t-active");
1164                 process[array[i].id.substring(1)].isactive = false;
1165             }
1166         }
1167         KWS.windowIndex = num;
1168     }
1169
1170     this.front = function( _pid ) {
1171         $(`#w${_pid}`).css("z-index", KWS.windowIndex + 1);
1172         KWS.refreshWindowIndex();
1173     }
1174
1175     this.resize = function( _pid, _width, _height ){
1176         if( _width ) $("#winc"+_pid).css("width", _width)
1177         if( _height ) $("#winc"+_pid).css("height", _height);
1178     }
1179
1180     this.setTheme = function(_name){
1181         localStorage.setItem('kit-theme', _name);
1182         if(_name != 'none') $('#kit-theme-file').attr('href', `./system/theme/${localStorage.getItem('kit-theme')}`);
1183         else $('#kit-theme-file').attr('href', '');
1184         System.moveDesktop(currentDesktop);
1185     }
1186
1187     this.fusen = new function(){
1188         this.fid = 0;
1189         this.list = new Object();
1190
1191         this.add = function(_text){
1192             KWS.fusen.list[KWS.fusen.fid] = String(_text);
1193             $("#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>");
1194             $("#kit-f"+KWS.fusen.fid).css({
1195                 "left": Number(KWS.fusen.fid)*40 + 20,
1196                 "top": Number(KWS.fusen.fid)*10 + 100,
1197             }).pep({
1198                 elementsWithInteraction: ".kit-fusen-textarea",
1199                 useCSSTranslation: false,
1200                 disableSelect: false,
1201                 shouldEase:     true,
1202                 initiate: function(){
1203                     $(this.el).css("ui-opacity", "0.7");
1204                 },
1205                 stop: function(){
1206                     this.el.style.transition = "none";
1207                     $(this.el).css("ui-opacity", "1.0");
1208                 }
1209             })
1210             $(".kit-fusen-textarea").off().on("change",function(){
1211                 KWS.fusen.list[$(this).attr("data-fid")] = $(this).val();
1212                 localStorage.setItem("kit-fusen", JSON.stringify( KWS.fusen.list ));
1213             });
1214             localStorage.setItem("kit-fusen", JSON.stringify( KWS.fusen.list ));
1215             KWS.fusen.fid++;
1216         }
1217
1218         this.remove = function(_fid){
1219             delete KWS.fusen.list[_fid];
1220             localStorage.setItem("kit-fusen", JSON.stringify( KWS.fusen.list ));
1221             $("#kit-f"+_fid).remove();
1222         }
1223     }
1224
1225     this.addCustomContext = function( _elem, _contextid, _obj ){
1226         KWS.context[_contextid] = _obj;
1227     }
1228
1229     this.context = {
1230         "fusen" : {
1231             "name" : "ふせん",
1232             "delete" : {
1233                 "label" : "ふせんを削除",
1234                 "icon" : "fa-trash-alt",
1235                 "function" : function(){
1236                     KWS.fusen.remove( S.selectedElement.attr("data-fid") );
1237                 }
1238             },
1239             "copy" : {
1240                 "label" : "ふせんを複製",
1241                 "icon" : "fa-copy",
1242                 "function" : function(){
1243                     KWS.fusen.add( KWS.fusen.list[S.selectedElement.attr("data-fid")] );
1244                 }
1245             }
1246         }
1247     }
1248 }
1249
1250 const Notification = new function() {
1251     this.nid = 0;
1252     this.list = new Object();
1253
1254     this.goodnight = false;
1255     this.sound = null;
1256
1257     this.push = function( _title, _content, _app, _pid, _action, _img, _buttons ) {
1258         let _nid = this.nid;
1259         if( !System.debugmode && ( _title == "debug" || _app == "debug" ) ){
1260             return false;
1261         }
1262         Notification.list[_nid] = {
1263             title: _title,
1264             content: _content,
1265             app: _app,
1266             time: System.time.obj.toLocaleString(),
1267             pid: _pid,
1268             action: () => {
1269                 if( typeof _action == 'function' ) _action();
1270                 else if( _pid ) KWS.front(_pid);
1271                 $('#notifications').hide('drop', {direction: 'right'}, 300);
1272             },
1273             img: _img
1274         };
1275         if( _pid && System.appCache[System.launchpath[_pid]] ){
1276             _app = `<img src='${System.launchpath[_pid]}/${System.appCache[System.launchpath[_pid]].icon}'>${System.appCache[System.launchpath[_pid]].name}`;
1277         }
1278         if( !this.goodnight ){
1279             if( this.sound ) System.audio.play( "n" + this.nid, this.sound );
1280             $('#last-notification-title').text('').text( _title );
1281             $('#last-notification-content').text('').text( _content );
1282             $('#last-notification-app').text('').html( _app );
1283             $('#last-notification').hide().show('drop', {direction: "right"}, 300).off().on('click', Notification.list[this.nid].action);
1284             if( _img ) $('#last-notification-img').attr('src', _img).show();
1285             else $('#last-notification-img').attr('src', '').hide();
1286         }
1287         let imgtag = '';
1288         if( _img ) imgtag = `<img src='${_img }' alt='' class='notis_img'>`;
1289         $(`<div class='notis' id='nt${_nid}'>
1290                 ${imgtag}
1291                 <span class='notis_close' id='nc${_nid}'></span>
1292                 <div class='notis_app'>${_app}</div>
1293                 <span>${_title}</span>
1294                 ${_content}
1295                 <div class='notis_buttons'></div>
1296                 <div class='notis_time'>${System.time.obj.toTimeString()}</div>
1297             </div>`).appendTo('#notifications').on('click', Notification.list[this.nid].action);
1298         $(`#nc${_nid}`).on('click', (e) => {
1299             e.stopPropagation();
1300             $(`#nt${_nid}`).fadeOut(300);
1301         });
1302         $('#last-notification-buttons').html('');
1303         if( _buttons ){
1304             for( let b of _buttons ){
1305                 $(`<a>${b.label}</a>`).appendTo('#last-notification-buttons').on('click', (e) => {
1306                     e.stopPropagation();
1307                     b.func();
1308                 });
1309                 $(`<a>${b.label}</a>`).appendTo(`#nt${_nid} .notis_buttons`).on('click', (e) => {
1310                     e.stopPropagation();
1311                     b.func();
1312                 });
1313             }
1314         }
1315         this.nid ++;
1316         return (this.nid - 1);
1317     }
1318 }
1319
1320 class App {
1321     constructor(_pid) {
1322         App.e[_pid] = new Object();
1323         App.d[_pid] = new Object();
1324         
1325         this.process = process[_pid];
1326         this.cache = System.appCache[System.launchpath[_pid]];
1327
1328         this.args = System.args[_pid];
1329         this.close = () => System.close(_pid);
1330         this.d = App.d[_pid];
1331         this.dom = (..._args) => System.dom(_pid, ..._args);
1332         this.e =  App.e[_pid];
1333         this.ntf = (_title, _content, _action, _img, _buttons) => Notification.push(_title, _content, this.info.id, _pid, _action, _img, _buttons);
1334         this.qs = (...args) => System.qs(_pid, ...args);
1335         this.front = () => KWS.front(_pid);
1336
1337         this.changeWindowTitle = _t => App.changeWindowTitle( _pid, _t );
1338         this.data = (_name, _value) => App.data(_pid, _name, _value);
1339         this.event = (_name, _event) => App.event(_pid, _name, _event);
1340         this.getPath = _path => App.getPath(_pid, _path);
1341         this.kaf = () => App.kaf(_pid);
1342         this.load = _path => App.load(_pid, _path);
1343         this.preventClose = _bool => App.preventClose(_pid, _bool);
1344     }
1345
1346     static changeWindowTitle( _pid, _t ) {
1347         $( "#tname"+_pid ).text( _t );
1348         $( "#wtname"+_pid ).text( _t );
1349         process[_pid].title = _t;
1350         return App;
1351     }
1352
1353     static context( _cid, _obj ) {
1354         KWS.context[ _cid ] = _obj;
1355         return App;
1356     }
1357
1358     static data( _pid, _name, _value ) {
1359         if( _value !== undefined ) {
1360             S.dom(_pid, `[kit\\:bind=${_name}]`).val( _value );
1361             S.dom(_pid, `[kit\\:observe=${_name}]`).text( _value );
1362             if( _value ) S.dom(_pid, `[kit\\:if=${_name}]`).show();
1363             else S.dom(_pid, `[kit\\:if=${_name}]`).hide();
1364             return App.d[_pid][_name] = _value;
1365         }
1366         else if( _name ) return App.d[_pid][_name];
1367         else return App.d[_pid];
1368     }
1369
1370     static event( _pid, _name, _event ) {
1371         if( !App.e[_pid] ) App.e[_pid] = new Object();
1372         if( !_event && App.e[_pid][_name] ) App.e[_pid][_name].call();
1373         else App.e[_pid][_name] = _event;
1374         return App;
1375     }
1376
1377     static getPath( _pid, _path ) {
1378         if( String(_path)[0] != '/' ) _path = '/' + _path;
1379         return System.launchpath[_pid] + _path;
1380     }
1381
1382     static kaf( _pid ) {
1383         let attrs = [
1384             "[kit-ref]",
1385             "[kit-e]",
1386             "[kit-src]",
1387             "[kit-alert]",
1388             "[kit-launch]",
1389             "[kit-close]",
1390             "[kit-text]",
1391             "[kit-html]",
1392             "[kit\\:bind]",
1393             "[kit\\:observe]",
1394             "[kit\\:value]",
1395             "[kit-value]",
1396             "[kit-color]",
1397             "[kit\\:if]",
1398             "[kit-if]"
1399         ]
1400         const PID = _pid;
1401         const DATA = App.data(_pid);
1402         const ARGS = System.args[_pid];
1403         for( let i of S.qs(_pid, ...attrs) ){
1404             if( i.hasAttribute("kit-ref") ){
1405                 $(i).on("click", () => App.load(_pid, i.getAttribute("kit-ref")) );
1406             }
1407             if( i.hasAttribute("kit-e") ){
1408                 let _eqs = i.getAttribute("kit-e").split(",");
1409                 for( let k of _eqs ){
1410                     let _eq = k.split(" ");
1411                     $(i).on( _eq[1]||"click", App.e[_pid][_eq[0]] );
1412                 }
1413             }
1414             if( i.hasAttribute("kit-src") ){
1415                 $(i).attr("src", `${System.launchpath[_pid]}/${i.getAttribute("kit-src")}` );
1416             }
1417             if( i.hasAttribute("kit-alert") ){
1418                 $(i).on("click", ()=> System.alert( System.appCache[System.launchpath[_pid]].name, i.getAttribute("kit-alert") ) );
1419             }
1420             if( i.hasAttribute("kit-launch") ){
1421                 $(i).on("click", ()=> System.launch( i.getAttribute("kit-launch") ) );
1422             }
1423             if( i.hasAttribute("kit-close") ){
1424                 $(i).on("click", ()=> System.close( i.getAttribute("kit-close") || _pid ) );
1425             }
1426             if( i.hasAttribute("kit-text") ){
1427                 $(i).text( eval(i.getAttribute("kit-text")) );
1428             }
1429             if( i.hasAttribute("kit-html") ){
1430                 $(i).html( eval(i.getAttribute("kit-html")) );
1431             }
1432             if( i.hasAttribute("kit:bind") ){
1433                 if( App.d[_pid] == undefined ) App.d[_pid] = new Object();
1434                 $(i).on('keydown keyup keypress change', () => {
1435                     let _name = i.getAttribute("kit:bind");
1436                     App.d[_pid][_name] = i.value;
1437                     S.dom(_pid, `[kit\\:observe=${_name}]`).text( i.value );
1438                     if( i.value ) S.dom(_pid, `[kit\\:if=${_name}]`).show();
1439                     else S.dom(_pid, `[kit\\:if=${_name}]`).hide();
1440                 } );
1441             }
1442             if( i.hasAttribute("kit:observe") ){
1443                 $(i).text( App.d[_pid][i.getAttribute("kit:observe")] );
1444             }
1445             if( i.hasAttribute("kit:value") ){
1446                 $(i).val( App.d[_pid][i.getAttribute("kit:value")] );
1447             }
1448             if( i.hasAttribute("kit-value") ){
1449                 $(i).val( eval(i.getAttribute("kit-value")) );
1450             }
1451             if( i.hasAttribute("kit-color") ){
1452                 $(i).css('color', i.getAttribute("kit-color"));
1453             }
1454             if( i.hasAttribute("kit:if") ){
1455                 if( App.d[_pid][i.getAttribute("kit:if")] ){
1456                     $(i).show();
1457                 }
1458                 else $(i).hide();
1459             }
1460             if( i.hasAttribute("kit-if") ){
1461                 if( eval( i.getAttribute("kit-if")) ){
1462                     $(i).show();
1463                 }
1464                 else $(i).hide();
1465             }
1466         }
1467     }
1468
1469     static load( _pid, _path ) {
1470         if( String(_path)[0] != '/' ) _path = '/' + _path;
1471         _path = System.launchpath[_pid] + _path;
1472         S.dom(_pid).load( _path, () => {
1473             App.kaf(_pid);
1474             let _appcache = System.appCache[System.launchpath[_pid]];
1475             if( !KWS.fullscreen.pid && !_appcache.size.height ) {
1476                 System.qs(_pid)[0].style.height = "auto";
1477             }
1478         } );
1479         return App;
1480     }
1481
1482     static preventClose( _pid, _bool = true ) {
1483         process[_pid].preventclose = _bool || true;
1484         return App;
1485     }
1486 }
1487
1488 App.d = new Object();
1489 App.e = new Object();
1490 App.version = "2.1.1";
1491
1492 var process = {}, pid = 0, app, currentDesktop = 1, currentCTX = "", prevWindowIndex, S;