OSDN Git Service

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