OSDN Git Service

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