OSDN Git Service

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