OSDN Git Service

Merge pull request #4 from mtsgi/feature/appInstaller
[kit/kit.git] / system.js
1 //   _    _ _   
2 //  | | _(_) |_ 
3 //  | |/ / | __|
4 //  |   <| | |_ 
5 //  |_|\_\_|\__|
6 //
7 // THIS IS THE KIT KERNEL AND KIT WINDOW SYSTEM
8 // http://web.kitit.ml/
9 // https://github.com/mtsgi/kit
10 "use strict";
11
12
13 $( document ).ready( kit );
14
15 function kit() {
16     S = System;
17
18     if( !localStorage.getItem( "kit-pid" ) ) processID = 0;
19     else processID = 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" );
28
29     if( !localStorage.getItem( "kit-default-browser" ) ) localStorage.setItem( "kit-default-browser", "browser" );
30
31     if( localStorage.getItem("kit-fusen") ){
32         this.list = JSON.parse(localStorage.getItem("kit-fusen"));
33         for( let i in this.list ){
34             KWS.fusen.add(this.list[i]);
35         }
36     }
37     
38     if( localStorage.getItem("kit-darkmode") == "true" ){
39         KWS.darkmode = true;
40         $("#kit-darkmode").attr("href", "system/theme/kit-darkmode.css");
41         $(".winc-darkmode").addClass("kit-darkmode");
42     }
43
44     if( System.bootopt.get("safe") ){
45         $("#kit-theme-file").attr("href", "./system/theme/theme-light.css" );
46     }
47     else{
48         if( !localStorage.getItem( "kit-theme" ) ) localStorage.setItem( "kit-theme", "theme-default.css" );
49         $("#kit-theme-file").attr("href", "./system/theme/" + localStorage.getItem("kit-theme") );
50     }
51
52     if( !localStorage.getItem( "kit-appdir" ) ) localStorage.setItem( "kit-appdir", "./app/" );
53     S.appdir = localStorage.getItem( "kit-appdir" );
54
55     if( localStorage.getItem( "kit-installed" ) ) System.installed = JSON.parse( localStorage.getItem( "kit-installed" ) );
56
57     if( localStorage["kit-userarea"] ) System.userarea = JSON.parse(localStorage["kit-userarea"]);
58     if( localStorage["kit-recycle"] ) System.recycle = JSON.parse(localStorage["kit-recycle"]);
59
60     System.moveDesktop( "1" );
61
62     var clockmove;
63     if( System.bootopt.get("safe") ) clockmove = setInterval( System.clock, 1000 );
64     else  clockmove = setInterval( System.clock, 10 );
65
66     Notification.push( "kitへようこそ", localStorage["kit-username"] + "さん、こんにちは。", "system" );
67     //スタートアップ
68     if( localStorage.getItem( "kit-startup" ) == undefined ) {
69         localStorage.setItem( "kit-startup", new Array( "welcome" ) );
70     }
71     System.startup = localStorage.getItem( "kit-startup" ).split( "," );
72     if( System.bootopt.get("safe") ){
73         Notification.push( "セーフブート", "現在、kitをセーフモードで起動しています。", "system" );
74         System.alert( "セーフブート", "現在、kitをセーフモードで起動しています。<br><a class='kit-hyperlink' onclick='location.href=\"index.html\";'>通常モードで再起動</a>", "system" );
75     }
76     else for( let i of System.startup ) if( i != "" ) launch( i );
77     
78     $("#kit-header-fullscreen").hide();
79
80     //イベントハンドラ
81     $( "#desktops" ).click( function() {
82         $( "#desktop-" + currentDesktop ).toggleClass( "selected-section" );
83     } ).mousedown( function() {
84         $( ".window" ).css( "opacity", "0.6" );
85     } ).mouseup( function() {
86         $( ".window" ).css( "opacity", "1.0" );
87     } );
88     //タスク一覧
89     $( "#footer-tasks" ).click( function() {
90         if( $( "#kit-tasks" ).is( ":visible" ) ) {
91             $( "#kit-tasks" ).html( "" ).fadeOut( 300 );
92         }
93         else {
94             $( "#task-ctx" ).fadeOut( 200 );
95             $( "#kit-tasks" ).html( $( "#tasks" ).html() ).fadeIn( 300 ).css( "z-index", "9997" );
96         }
97     } );
98     //デスクトップアイコン
99     $.getJSON("config/desktop.json", (data) => {
100         for( let i in data ){
101             $(".desktop-icons").append("<div class='desktop-icon' data-launch='" + i + "'><img src='" + data[i].icon + "'>" + data[i].name + "</div>");
102         }
103         $(".desktop-icon").on("click", function(){
104             launch( $(this).attr("data-launch") );
105         });
106     }).fail( function() {
107         Notification.push( "読み込みに失敗", "デスクトップ(config/desktop.json)の読み込みに失敗しました。", system );
108     } );
109     //ランチャー
110     $.getJSON("config/apps.json", System.initLauncher).fail( function() {
111         Notification.push( "ランチャー初期化失敗", "アプリケーション一覧(config/apps.json)の読み込みに失敗しました。", system );
112     } );
113     $( "#kit-tasks" ).delegate( ".task", "click", function() {
114         System.close( this.id.slice( 1 ) );
115         $( this ).hide();
116     } );
117     //通知バー
118     $( "#footer-noti" ).click( function() {
119         $( "#last-notification" ).hide( "drop", {direction: "right"}, 300 );
120         if( $( "#notifications" ).is( ":visible" ) ) {
121             $( "#notifications" ).hide( "drop", {direction: "right"}, 300 );
122         }
123         else {
124             $( "#notifications" ).show( "drop", {direction: "right"}, 300 );
125         }
126     } );
127     $( "#last-notification-close" ).click( function() {
128         $( "#last-notification" ).hide( "drop", {direction: "right"}, 300 );
129     } );
130     $("#notifications-dnp").prop("checked", false).on("change", ()=>{
131         if( $("#notifications-dnp").is(":checked") ){
132             Notification.goodnight = true;
133         }
134         else Notification.goodnight = false;
135     });
136     //電源管理
137     $( ".power-button" ).click( function() {
138         $( "#notifications" ).hide( "drop", {direction: "right"}, 300 );
139         $( "#last-notification" ).hide( "drop", {direction: "right"}, 300 );
140         $( "section, header, footer, #kit-wallpaper, .dropdown" ).css( "filter", "blur(5px)" );
141         $( "#kit-power" ).fadeIn( 300 );
142     } );
143     $( "#kit-power-back" ).click( function() {
144         $( "section, header, footer, #kit-wallpaper, .dropdown" ).css( "filter", "none" );
145         $( "#kit-power" ).fadeOut( 300 );
146     } );
147     $( "#kit-power-shutdown" ).click( function() {
148         System.shutdown();
149     } );
150     $( "#kit-power-reboot" ).click( function() {
151         System.reboot();
152     } );
153     $( "#kit-power-suspend" ).click( function() {
154         $( "section, header, footer, #kit-wallpaper" ).css( "filter", "none" );
155         $( "#kit-power" ).fadeOut( 300 );
156         System.alert("サスペンド機能", "サスペンド機能はこのバージョンのkitではサポートされていません。");
157     } );
158     $( "#kit-power-lock" ).click( function() {
159         System.lock();
160     } );
161     $( "#lock-password" ).keypress( function( e ) {
162         if( e.which == 13 ) $( "#lock-unl" ).click();
163     } );
164     $( "#lock-unl" ).click( function() {
165         if( !localStorage.getItem( "kit-password" ) || $( "#lock-password" ).val() == localStorage.getItem( "kit-password" ) ) {
166             $( "header, footer" ).show();
167             $( "section, header, footer, #kit-wallpaper" ).css( "filter", "none" );
168             $( "#lock-password" ).val( "" );
169             System.moveDesktop(1);
170         }
171         else $( "#lock-password" ).effect( "bounce", {distance: 12, times: 4}, 500 );
172     } ).hover( function() {
173         $( "#lock-unl span" ).removeClass( "fa-lock" ).addClass( "fa-lock-open" );
174     }, function() {
175         $( "#lock-unl span" ).removeClass( "fa-lock-open" ).addClass( "fa-lock" );
176     } );
177     //ランチャー起動
178     $( "#launch" ).click( function() {
179         $( "#notifications" ).hide( "drop", {direction: "right"}, 300 );
180         if( $( "#launcher" ).is( ":visible" ) ) {
181             $( "#kit-wallpaper" ).css( "filter", "none" );
182             $( "#desktop-" + currentDesktop ).show();
183             $( "#launcher" ).hide();
184         }
185         else {
186             $( "#kit-wallpaper" ).css( "filter", "blur(5px)" )
187             $( "section" ).hide();
188             $( "#launcher" ).show();
189         }
190     } );
191
192     //検索バー
193     $( "#milp" ).val( "" ).on( "focus", function() {
194         $( "#kit-milp" ).show();
195     } ).on( "blur", function() {
196         $( "#kit-milp" ).fadeOut( 200 );
197     } ).on( 'keydown keyup keypress change', function() {
198         $( "#kit-milp-text" ).text( $( this ).val() );
199     } ).keypress( function( e ) {
200         if( e.which == 13 ) $( "#kit-milp-launch" ).click();
201     } );
202     $( "#kit-milp-launch" ).click( function() {
203         if( $("#milp").val() == "kit" ){
204             System.alert("", "<div style='text-align:left;'> _    _ _ <br>| | _(_) |_ <br>| |/ / | __|<br>|   〈| | |_ <br>|_|\_ \ _\__|</div><hr>", S.version);
205             return;
206         }
207         let _app = $( "#milp" ).val().split(",")[0];
208         let _args = null;
209         try {
210             if( $( "#milp" ).val().split(",")[1] ){
211                 _args = JSON.parse( $( "#milp" ).val().split(",").slice(1).join() );
212             }
213         }
214         catch(error) {
215             Notification.push("引数の解釈に失敗", error, "system");
216         }
217         launch( _app, _args );
218     } );
219     $( "#kit-milp-search" ).click( function() {
220         launch( "browser", { "url" : "https://www.bing.com/search?q=" + $( "#milp" ).val() } );
221     } );
222     $( "#kit-milp-wikipedia" ).click( function() {
223         launch( "browser", { "url" : "https://ja.wikipedia.org/wiki/" + $( "#milp" ).val() } );
224     } );
225
226     //サウンドドロップダウン
227     $("#dropdown-sound-slider").slider({
228         min: 0, max: 100, step: 1, value: 100,
229         change: (e, ui) => {
230             System.audio.level = ui.value;
231             $("#dropdown-sound-level").text(ui.value);
232             localStorage.setItem("kit-audio-level", ui.value);
233             for( let i in System.audio.list ){
234                 System.audio.list[i].volume = System.audio.level / 100;
235             }
236             if( ui.value == 0 ) $("#kit-header-sound-icon").removeClass("fa-volume-up").addClass("fa-volume-mute");
237             else $("#kit-header-sound-icon").removeClass("fa-volume-mute").addClass("fa-volume-up");
238         }
239     });
240     if( localStorage["kit-audio-level"] ) System.audio.volume( localStorage["kit-audio-level"] );
241
242     $("#dropdown-sound-silent").prop("checked", false).on("change", ()=>{
243         if( $("#dropdown-sound-silent").is(":checked") ){
244             System.audio.silent = true;
245             $("#kit-header-sound-icon").removeClass("fa-volume-up").addClass("fa-volume-mute");
246         }
247         else{
248             System.audio.silent = false;
249             $("#kit-header-sound-icon").removeClass("fa-volume-mute").addClass("fa-volume-up");
250         }
251     });
252
253     $("#kit-header-user").on("click", ()=>{
254         launch("user");
255     });
256
257     //コンテキストメニュー
258     $(":root section:not(#desktop-l)").on("contextmenu", function() {
259         let _ptelem = $( document.elementFromPoint(S.mouseX, S.mouseY) );
260         S.selectedElement = _ptelem;
261         S.selectedText = window.getSelection();
262         $( "#kit-context-input" ).val( S.selectedText );
263         if( $( "#kit-context-input" ).val() == "" ) $("#kit-contextgroup-text").hide();
264         else $("#kit-contextgroup-text").show();
265         if( _ptelem[0].id == "desktop-" + currentDesktop ){
266             $("#kit-contextgroup-desktop").show();
267             $("#kit-contextgroup-elem").hide();
268         }
269         else{
270             $("#kit-contextgroup-desktop").hide();
271             $("#kit-contextgroup-elem").show();
272         }
273         $( "#kit-context-elem" ).text( _ptelem.prop("tagName").toLowerCase() + "要素" );
274         $("#kit-contextgroup-custom").hide();
275
276         let  _ctxid = _ptelem.attr("data-kit-contextid");
277         if( _ctxid ){
278             $("#kit-contextgroup-custom").show().html('<div id="kit-context-custom"></div>');
279             let  _ctxname = KWS.context[_ctxid].name || _ctxid; 
280             $("#kit-context-custom").text( _ctxname );
281             for( let i in KWS.context[_ctxid]){
282                 if( i == "name" ) continue;
283                 $("#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>");
284                 $("#kit-context-" + _ctxid + "-" + i).on("click", () => {
285                     KWS.context[_ctxid][i].function();
286                     $("#kit-context").fadeOut(300);
287                 });
288             }
289         }
290         if( _ptelem[0].id ) $( "#kit-context-elem" ).append( "#" + _ptelem[0].id );
291         $( "#kit-context-size" ).text( _ptelem[0].clientWidth + "✕" + _ptelem[0].clientHeight );
292         $("#kit-context").toggle().css("left", S.mouseX).css("top", S.mouseY);
293         return false;
294     });
295     $("#kit-context-open").on("click", function(){
296         S.alert("要素", S.selectedElement.clone());
297     });
298     $("#kit-context-save").on("click", function(){
299         S.obj2img( S.selectedElement , true );
300     });
301     $( "#kit-context-search" ).on("click", function(){
302         $("#kit-context").fadeOut(300);
303         launch( "browser", { "url" : "https://www.bing.com/search?q=" + $( "#kit-context-input" ).val() } );
304     });
305     $( "#kit-context-input" ).keypress( function( e ) {
306         if( e.which == 13 ) $( "#kit-context-search" ).click();
307     } );
308     $("#kit-context a").on("click", function(){
309         $("#kit-context").fadeOut(300);
310     });
311     $("#kit-context-vacuum").on("click", function(){
312         for( let i in process ){
313             KWS.vacuum( S.mouseX, S.mouseY );    
314         }
315         setTimeout(() => {
316             $(".window").css("transition", "none");
317         }, 500);
318     });
319     $("#kit-context-fusen").on("click", function(){
320         KWS.fusen.add("");
321     });
322
323
324     $("section").on("click", function(){
325         $("#kit-context").fadeOut(300);
326     })
327
328     $( document ).delegate( "a", "click", function() {
329         if( this.href ) {
330             launch( localStorage.getItem( "kit-default-browser" ), { "url" : this.href } );
331             return false;
332         }
333     } ).on("mousemove", function(event){
334         System.mouseX = event.clientX;
335         System.mouseY = event.clientY;
336     }).delegate( ".textbox", "keypress", function( e ) {
337         if( e.which == 13 && this.id && $("#" + this.id + " + .kit-button") ){
338             Notification.push("debug", this.id, "system");
339             $("#" + this.id + " + .kit-button").click();
340         }
341     } );
342
343     window.onresize = () => {
344         System.display.width = window.innerWidth;
345         System.display.height = window.innerWidth;
346
347         if( KWS.fullscreen.pid ){
348             KWS.resize( KWS.fullscreen.pid, System.display.width, System.display.height - 30 );
349         }
350     }
351
352     if( localStorage.getItem( "kit-lock" ) == "true" ){
353         $("section").hide();
354         setTimeout(() =>  System.lock(), 100);
355     }
356 }
357
358 function launch( str, args, dir ) {
359     pid = processID;
360     System.args[pid] = args;
361     System.launchpath[pid] = dir || System.appdir + str;
362
363     if( System.appCache[str] ) {
364         if( KWS.fullscreen.pid ) KWS.unmax(KWS.fullscreen.pid);
365         //app[str].open();
366         appData( System.appCache[str] );
367     }
368     else {
369         try{
370             $.getJSON( S.launchpath[pid] + "/define.json", appData ).fail( function() {
371                 System.alert( "起動エラー", "アプリケーションの起動に失敗しました<br>アプリケーション" + str + "は存在しないかアクセス権がありません(pid:" + processID + ")。ヘルプは<a class='kit-hyperlink' href='https://kitdev.home.blog/'>こちら</a>" );
372             } );
373         }
374         catch(error){
375             Notification.push( "System Error", error, "system" );
376         }
377     }
378 }
379
380 function appData( data ) {
381     var pid = processID;
382     process[String( pid )] = {
383         id: data.id,
384         time: System.time.obj.toLocaleString(),
385         isactive: false,
386         preventclose: false
387     };
388     System.appCache[data.id] = data;
389     $( "#tasks" ).append( "<span id='t" + pid + "'><img src='" + S.launchpath[pid] + "/" + data.icon + "'><span id='tname" + pid + "'>" + data.name + "<span></span>" );
390     //タスクバーのクリック挙動
391     $( "#t" + pid ).addClass( "task" ).click( function() {
392         if( $(this).hasClass("t-active") || $(this).hasClass("task-min") ) KWS.min( pid );
393         else{
394             $("#w"+pid).css("z-index", KWS.windowIndex + 1);
395             KWS.refreshWindowIndex();
396         }
397     } );
398     $( "#t" + pid ).addClass( "task" ).on( "mouseenter", function() {
399         $( "#task-ctx-name" ).text( data.name );
400         $( "#task-ctx-img" ).attr( "src", "./app/" + data.id + "/" + data.icon );
401         $( "#task-ctx-ver" ).text( data.version + "/pid:" + pid );
402         $( "#task-ctx-info" ).off().on( "click", function() { System.appInfo( data.id )} );
403         $( "#task-ctx-sshot" ).off().on( "click", function() { S.screenshot(pid, true) } );
404         $( "#task-ctx-min" ).off().on( "click", function() { KWS.min( String(pid) ) } );
405         if( $(this).hasClass("t-active") ) $( "#task-ctx-front" ).hide();
406         else $( "#task-ctx-front" ).show();
407         $( "#task-ctx-front" ).off().on( "click", function() {
408             $("#w"+pid).css("z-index", KWS.windowIndex + 1);
409             KWS.refreshWindowIndex();
410         } );
411         $( "#task-ctx-close" ).off().on( "click", () => { System.close( String(pid) ) } );
412         $( "#task-ctx-kill" ).off().on( "click", () => { System.kill( String(data.id) ) } );
413         const _ctxleft = $( "#t" + pid ).offset().left;
414         const _ctxtop = window.innerHeight - $( "#t" + pid ).offset().top;
415         if( _ctxleft != $( "#task-ctx" ).offset().left ) {
416             $( "#task-ctx" ).hide();
417         }
418         $( "#task-ctx" ).css( "left", _ctxleft ).css( "bottom", _ctxtop ).show();
419     } );
420     $( "section, #kit-tasks" ).on( "mouseenter", function() {
421         $( "#task-ctx" ).fadeOut( 200 );
422     } );
423     $( "#t" + pid ).hover( function() {
424         prevWindowIndex = $( "#w" + pid ).css( "z-index" );
425         $( "#w" + pid ).addClass( "win-highlight" );
426     }, function() {
427         $( "#w" + pid ).removeClass( "win-highlight" );
428     } );
429     let _windowAppend = "<div id='w" + pid + "'><div id='wt" + pid + "' class='wt'><i class='wmzx'><span id='wm" + pid + "'></span>";
430     if( data.support && data.support.fullscreen == true ) _windowAppend += "<span id='wz" + pid + "'></span>";
431     _windowAppend += "<span id='wx" + pid + "'></span></i><img src='" + S.launchpath[pid] + "/" + data.icon + "'><span id='wtname" + pid + "'>" + data.name + "</span></div><div class='winc winc-" + data.id + "' id='winc" + pid + "'></div></div>";
432     $( "#desktop-" + currentDesktop ).append( _windowAppend );
433
434     if( data.support && data.support.darkmode == true ) $("#winc"+pid).addClass("winc-darkmode");
435     if( KWS.darkmode ) $("#winc"+pid).addClass("kit-darkmode");
436
437     if( data.size ){
438         $("#winc"+pid).css("width", data.size.width).css("height", data.size.height);
439     }
440     if( data.resize ){
441         let _minwidth = 200, _minheight = 40;
442         if( data.resize.minWidth ) _minwidth = data.resize.minWidth;
443         if( data.resize.minHeight ) _minheight = data.resize.minHeight;
444         $("#winc"+pid).windowResizable({
445             minWidth: _minwidth,
446             minHeight: _minheight
447         });
448     }
449
450     var windowPos = 50 + ( pid % 10 ) * 20;
451     //$( "#w" + pid ).addClass( "window" ).draggable( {cancel: ".winc", stack: ".window"} ).css( "left", windowPos + "px" ).css( "top", windowPos + "px" ).css( "z-index", $( ".window" ).length + 1 );
452     KWS.windowIndex ++;
453     $( "#w"+pid ).addClass( "window" ).pep({
454         elementsWithInteraction: ".winc, .ui-resizable-handle",
455         useCSSTranslation: false,
456         disableSelect: false,
457         shouldEase:     true,
458         initiate: function(){
459             $(this.el).addClass("ui-draggable-dragging");
460             KWS.windowIndex ++;
461             this.el.style.zIndex = KWS.windowIndex;
462             KWS.refreshWindowIndex();
463         },
464         stop: function(){
465             this.el.style.transition = "none";
466             $(this.el).removeClass("ui-draggable-dragging");
467         }
468     }).on( "mousedown", function(){
469         $(".window").css( "transition", "none" );
470         $(this).css("z-index", KWS.windowIndex + 1);
471         KWS.refreshWindowIndex();
472     } ).css( "left", windowPos + "px" ).css( "top", windowPos + "px" ).css( "z-index",  KWS.windowIndex );
473     KWS.refreshWindowIndex();
474     $( "#wm" + pid ).addClass( "wm fa fa-window-minimize" ).click( () => KWS.min( String(pid) ) );
475     $( "#wz" + pid ).addClass( "wz fas fa-square" ).click( () => KWS.max( String(pid) ) );
476     $( "#wx" + pid ).addClass( "wx fa fa-times" ).click( () => System.close( String(pid) ) );
477     $( "#winc" + pid ).resizable( {
478         minWidth: "200"
479     } ).load( System.launchpath[pid] + "/" + data.view );
480
481     //スクリプト読み込み
482     if( data.script != "none" ) $.getScript( System.launchpath[pid] + "/" + data.script );
483     if( data.css != "none" && $("#kit-style-"+data.id).length == 0 ){
484         $( "head" ).append( '<link href="' + System.launchpath[pid] + '/' + data.css + '" rel="stylesheet" id="kit-style-' + data.id + '"></link>' );
485         Notification.push("debug", "新規スタイルシートの読み込み", data.id);
486     }
487
488     processID++;
489     localStorage.setItem( "kit-pid", processID );
490 }
491
492 //非推奨メソッド
493 function appInfo( str ){
494     System.appInfo(str)
495 }
496
497 //非推奨メソッド
498 function close( str ) {
499     System.close( str )
500 }
501
502 //非推奨メソッド
503 function kill( str ) {
504     System.kill(str)
505 }
506
507 const System = new function() {
508     this.version = "0.2.0";
509     this.username = localStorage.getItem("kit-username");
510     this.appdir = localStorage.getItem("kit-appdir");
511
512     this.bootopt = new URLSearchParams(location.search);
513
514     this.mouseX = 0;
515     this.mouseY = 0;
516
517     this.display = {
518         "width": window.innerWidth,
519         "height": window.innerHeight
520     }
521
522     this.selectedElement = null;
523     this.selectedText = null;
524
525     this.dom = function(_pid, _elements) {
526         _elements = _elements || "";
527         return $("#winc" + _pid + " " + _elements);
528     }
529
530     this.userarea = new Object();
531     this.recycle = new Object();
532
533     this.appCache = {};
534     //アプリ引数
535     this.args = {};
536     //アプリ起動パス
537     this.launchpath = {};
538
539     this.support = $.support;
540     this.debugmode = false;
541
542     this.battery = null;
543
544     this.log = new Array();
545     this.noop = () => {}
546
547     this.setBattery = function(){
548         if( navigator.getBattery ) navigator.getBattery().then((e)=>{
549             let _lv =  e.level * 100;
550             System.battery = _lv;
551             return _lv;
552         });
553     }
554
555     this.screenshot = function( _pid, _popup ){
556         let _elem = document.querySelector("body");
557         if( _pid ) _elem = document.querySelector("#w"+_pid);
558         html2canvas( _elem ).then(canvas => {
559             if( _popup ){
560                 canvas.style.border = "1px solid #909090";
561                 S.save( canvas.toDataURL("image/png"), "image" );
562             }
563             return canvas;
564         });
565     }
566
567     this.obj2img = function( _obj, _popup ){
568         let _elem = _obj[0];
569         html2canvas( _elem ).then(canvas => {
570             if( _popup ){
571                 canvas.style.border = "1px solid #909090";
572                 S.save( canvas.toDataURL("image/png"), "image" );
573             }
574             return canvas;
575         });
576     }
577
578     this.save = function(data, type){
579         launch("fivr", { "save" : data, "type" : type });
580     }
581
582     this.open = function(filename){
583         launch("fivr", { "open" : filename });
584     }
585
586     this.preventClose = function( _pid ){
587         if( !process[_pid] ) return false;
588         process[_pid].preventclose = true;
589         return true;
590     }
591     
592     this.shutdown = function(_opt) {
593         $( "#last-notification-close" ).click();
594         $( "#kit-power-back" ).click();
595         for( let i in process ) {
596             if( process[i].preventclose == true ){
597                 S.dialog( "シャットダウンの中断", "pid" + System.appCache[process[i].id].name + "がシャットダウンを妨げています。<br>強制終了してシャットダウンを続行する場合は[OK]を押下してください。", () => {
598                     process[i].preventclose = false;
599                     System.shutdown();
600                 } );
601                 return false;
602             }
603             else System.close( i );
604         }
605         $( "section" ).hide();
606         $( "body" ).css( "background-color", "black" );
607         $( "header, footer" ).fadeOut( 300 );
608         $( "#kit-wallpaper" ).fadeOut( 1500 );
609         if( _opt == "reboot" ) location.reload();
610     }
611
612     this.reboot = function() {
613         System.shutdown("reboot");
614     }
615
616     this.lock = function(){
617         System.moveDesktop( "l" );
618
619         $( "#lock-user-icon" ).css( "background", localStorage.getItem( "kit-user-color" ) );
620         $( "section, header, footer" ).css( "filter", "none" );
621         $( "#kit-wallpaper" ).css( "filter", "blur(20px)" );
622         $( "header, footer, #kit-power" ).hide();
623
624         $( "#lock-username" ).text( localStorage.getItem( "kit-username" ) );
625         if( localStorage.getItem( "kit-password" ) ) $( "#lock-password" ).show();
626         else $( "#lock-password" ).hide();
627     }
628
629     this.alert = function( title, content, winname ) {
630         launch( "alert", [title, content, winname] );
631     }
632
633     this.dialog = function( title, content, func ){
634         launch("dialog", {
635             "title": title,
636             "content": content,
637             "func": func
638         })
639     }
640
641     this.appInfo = function( str ){
642         let _title = "", _content = "";
643         let ac = System.appCache[str];
644         if( ac ){
645             _title = ac.name + " (" + ac.version + ")";
646             _content = "<img style='height: 96px' src='./app/" + ac.id + "/" + ac.icon + "'><br>";
647             for( let i in ac ){
648                 _content += "<div><span style='font-weight: 100'>" + i + " </span>" + ac[i] + "</div>";
649             }
650         }
651         else _title = "取得に失敗しました";
652         System.alert( _title, _content );
653     }
654
655     this.installed = new Array();
656  
657     //非推奨です(削除予定)。
658     this.min = function( _str ) {
659         KWS.min( _str );
660     }
661
662     this.close = function( _str ) {
663         let _pid = String( _str );
664         $( "#w" + _pid ).remove();
665         $( "#t" + _pid ).remove();
666         $( "#task-ctx" ).hide();
667         delete process[_pid];
668         KWS.refreshWindowIndex();
669     }
670
671     this.kill = function( _str ){
672         for( let pid in process ) {
673             if( process[pid] && process[pid].id == _str ) System.close( pid );
674         }
675     }
676     
677     this.vacuum = function( _left, _top ){
678         KWS.vacuum( _left, _top ); //非推奨です(削除予定)。
679     }
680
681     this.time = {
682         "obj" : new Date(),
683         "y" : "1970",
684         "m" : "1",
685         "d" : "1",
686         "h" : "00",
687         "i" : "00",
688         "s" : "00",
689         "ms" : "0"
690     }
691
692     this.clock = function() {
693         let DD = new Date();
694         S.time.obj = DD;
695         let Year = DD.getFullYear();
696         S.time.day = DD.getDay();
697         S.time.y = Year;
698         let Month = ( "00" + Number(DD.getMonth()+1) ).slice( -2 );
699         S.time.m = Month;
700         let DateN = ( "00" + DD.getDate() ).slice( -2 );
701         S.time.d = DateN;
702         let Hour = ( "00" + DD.getHours() ).slice( -2 );
703         S.time.h = Hour;
704         let Min = ( "00" + DD.getMinutes() ).slice( -2 );
705         S.time.i = Min;
706         let Sec = ( "00" + DD.getSeconds() ).slice( -2 );
707         S.time.s = Sec;
708         $( ".os-time" ).text( Hour + ":" + Min + ":" + Sec );
709         let MS = DD.getMilliseconds();
710         S.time.ms = MS;
711         let circle = {
712             outer: { radius: .9, color: "transparent" },
713             inner: { radius: .85, color: "transparent" }
714         }
715         let lines = {
716             long: { from: .8, to: .7, width: 2, color: "#303030" },
717             short: { from: .8, to: .75, width: 1, color: "#a0a0a0" }
718         }
719         let hands = {
720             hour: { length: .4, width: 3, cap: "butt", color: "#303030", ratio: .2 },
721             minute: { length: .67, width: 2, cap: "butt", color: "#303030", ratio: .2 },
722             second: { length: .67, width: 1, cap: "butt", color: "dodgerblue", ratio: .2 }
723         }
724         let canvas = $(".dropdown-clock-canvas")[0];
725         canvas.width = "200", canvas.height = "200";
726         let context = canvas.getContext("2d");
727         let center = { x: Math.floor(canvas.width / 2), y: Math.floor(canvas.height / 2) };
728         let radius = Math.min(center.x, center.y), angle, len;
729         context.beginPath();context.fillStyle = circle.outer.color;
730         context.arc(center.x, center.y, radius * circle.outer.radius, 0, Math.PI * 2, false);
731         context.fill();context.beginPath();context.fillStyle = circle.inner.color;
732         context.arc(center.x, center.y, radius * circle.inner.radius, 0, Math.PI * 2, false);
733         context.fill();
734         for( let i=0; i<60; i++ ){
735             angle = Math.PI * i / 30;
736             context.beginPath();
737             let line = ( i%5 == 0 ) ? lines.long : lines.short;
738             context.lineWidth = line.width, context.strokeStyle = line.color;
739             context.moveTo(center.x + Math.sin(angle) * radius * line.from, center.y - Math.cos(angle) * radius * line.from)
740             context.lineTo(center.x + Math.sin(angle) * radius * line.to, center.y - Math.cos(angle) * radius * line.to);
741             context.stroke();
742         }
743         angle = Math.PI * ( Hour+Min/60 ) / 6, len = radius * hands.hour.length;
744         context.beginPath(), context.lineWidth = hands.hour.width;
745         context.lineCap = hands.hour.cap, context.strokeStyle = hands.hour.color;
746         context.moveTo(center.x - Math.sin(angle) * len * hands.hour.ratio, center.y + Math.cos(angle) * len * hands.hour.ratio);
747         context.lineTo(center.x + Math.sin(angle) * len, center.y - Math.cos(angle) * len), context.stroke();
748         angle = Math.PI * (Min + Sec / 60) / 30, len = radius * hands.minute.length;
749         context.beginPath(), context.lineWidth = hands.minute.width;
750         context.lineCap = hands.minute.cap, context.strokeStyle = hands.minute.color;
751         context.moveTo(center.x - Math.sin(angle) * len * hands.minute.ratio, center.y + Math.cos(angle) * len * hands.minute.ratio);
752         context.lineTo(center.x + Math.sin(angle) * len, center.y - Math.cos(angle) * len), context.stroke();
753         angle = Math.PI * Sec / 30, len = radius * hands.second.length;
754         context.beginPath(), context.lineWidth = hands.second.width;
755         context.lineCap = hands.second.cap, context.strokeStyle = hands.second.color;
756         context.moveTo(center.x - Math.sin(angle) * len * hands.second.ratio, center.y + Math.cos(angle) * len * hands.second.ratio);
757         context.lineTo(center.x + Math.sin(angle) * len, center.y - Math.cos(angle) * len), context.stroke();
758     }
759
760     this.changeWallpaper = function( str ) {
761         $( "#kit-wallpaper" ).css( "background", str ).css( "background-size", "cover" );
762         localStorage.setItem( "kit-wallpaper", str )
763     }
764
765     this.moveDesktop = function( str ) {
766         str = String( str );
767         $( "section" ).hide();
768         $( "#desktop-" + str ).show();
769         $( "#desktops" ).html( "<span class='far fa-clone'></span>Desktop" + str );
770         currentDesktop = str;
771     }
772
773     this.avoidMultiple = function( _pid, _alert ) {
774         let _id = process[_pid].id;
775         let _cnt = 0;
776         for( let i in process ) {
777             if( process[i].id == _id ) _cnt += 1;
778         }
779         console.log( _cnt );
780         if( _cnt > 1 ) {
781             System.close( _pid );
782             if( !_alert ){
783                 System.alert( "多重起動", "アプリケーション" + _id + "が既に起動しています。このアプリケーションの多重起動は許可されていません。" );
784             }
785         }
786         return _cnt;
787     }
788
789     this.resizable = function( _pid, _elem, _width, _height ){
790         let E = ".winc";
791         if( _elem ) E = String( _elem );
792         if( !_width ) _width = null;
793         if( !_height ) _height = "100";
794         $("#w" + _pid).resizable({
795             alsoResize: "#w" + _pid + " " + E,
796             minWidth: _width,
797             minHeight: _height
798         });
799     }
800
801     this.initLauncher = function(data){
802         $("#launcher-apps").html("");
803         for( let i in data ){
804             $("#launcher-apps").append("<div class='launcher-app' data-launch='" + i + "'><img src='" + data[i].icon + "'>" + data[i].name + "</div>");
805         }
806         if( !System.bootopt.get("safe") ){
807             for( let i of System.installed ){
808                 $("#launcher-apps").append("<div class='launcher-app' data-define-path='" + i.path + "' data-define-id='" + i.id + "'><img src='" + i.icon + "'>" + i.name + "</div>");
809             }
810         }
811         $(".launcher-app").on("click", function(){
812             $("#launch").click();
813             if( $(this).attr("data-launch") ) launch( $(this).attr("data-launch") );
814             else if( $(this).attr("data-define-path") ){
815                 launch( $(this).attr("data-define-id"), null, $(this).attr("data-define-path") );
816             };
817         });
818     }
819
820     this.clip = new function(){
821         this.content = null;
822         this.history = new Array();
823
824         this.set = function( content ){
825             this.content = content;
826             this.history.push(content);
827             return content;
828         }
829         this.get = ()=>{ return this.content }
830     }
831
832     this.config = new function(){
833         this.apps = new Object();
834     }
835
836     this.audio = new function(){
837         this.level = localStorage["kit-audio-level"] || 100;
838         this.silent = false;
839
840         this.list = new Array();
841
842         this.volume = function( _level ){
843             $("#dropdown-sound-slider").slider("value", _level);
844         }
845
846         this.play = function( _audioid, _src ){
847             if( !System.audio.list[_audioid] ){
848                 System.audio.list[_audioid] = new Audio(_src);
849                 System.audio.list[_audioid].volume = System.audio.level / 100;
850             }
851             System.audio.list[_audioid].play();
852         }
853
854         this.get = function( _audioid ){
855             return System.audio.list[_audioid];
856         }
857
858         this.pause = function( _audioid ){
859             System.audio.list[_audioid].pause();
860         }
861
862         this.stop = function( _audioid ){
863             System.audio.list[_audioid].pause();
864             System.audio.list[_audioid] = null;
865         }
866
867         this.seek = function( _audioid, _time ){
868             System.audio.list[_audioid].fastSeek(_time);
869         }
870
871         this.mute = function( _audioid, _bool ){
872             System.audio.list[_audioid].muted = _bool;
873         }
874     }
875 }
876
877 const KWS = new function(){
878     this.version = "3.2.2";
879     this.active = null;
880
881     this.darkmode = false;
882
883     this.changeWindowTitle = function( _pid, _str ){
884         $("#tname"+_pid).text( _str );
885         $("#wtname"+_pid).text( _str );
886     }
887
888     this.min = function( _str ) {
889         let _pid = String( _str );
890         if( $( "#w" + _pid ).is( ":visible" ) ) {
891             $( "#w" + _pid ).css("transition", "none").hide( "drop", {direction: "down"}, 300 );
892             $( "#task-ctx" ).effect( "bounce", {distance: 12, times: 1}, 400 );
893             $( "#t" + _pid ).addClass( "task-min" );
894         }
895         else {
896             $( "#w" + _pid ).show( "drop", {direction: "down"}, 300 );
897             $( "#task-ctx" ).effect( "bounce", {distance: 12, times: 1}, 400 );
898             $( "#t" + _pid ).removeClass( "task-min" );
899         }
900     }
901
902     this.fullscreen = {
903         "pid": null,
904         "prevWidth": null,
905         "prevHeight": null,
906         "prevTop": 0,
907         "prevLeft": 0
908     }
909
910     this.max = function( _pid ){
911         if( KWS.fullscreen.pid ){
912             Notification.push("最大化に失敗", "最大化しているウィンドウがあります。");
913             return;
914         }
915         $( "#wt"+_pid ).addClass("wtmaximize");
916         $( "#w"+_pid ).css({
917             "top": "0px",
918             "left": "0px"
919         })
920         .addClass("windowmaximize")
921         .css("z-index", KWS.windowIndex + 1);
922         KWS.refreshWindowIndex();
923
924         KWS.fullscreen.prevWidth = $("#winc"+_pid).outerWidth();
925         KWS.fullscreen.prevHeight = $("#winc"+_pid).outerHeight();
926         KWS.fullscreen.prevTop = $("#w"+_pid).offset().top;
927         KWS.fullscreen.prevLeft = $("#w"+_pid).offset().left;
928
929         KWS.resize( _pid, System.display.width, System.display.height - 30 );
930         $("footer").hide();
931         $("#kit-header-fullscreen").show().on("click", () => {
932             KWS.unmax( _pid );
933         });
934         KWS.fullscreen.pid = _pid;
935     }
936
937     this.unmax = function( _pid ){
938         if( _pid != KWS.fullscreen.pid ){
939             Notification.push("最大化解除に失敗", "対象がフルスクリーンウィンドウではありません。");
940             return;
941         }
942         $( "#wt"+_pid ).removeClass("wtmaximize");
943         $( "#w"+_pid ).css({
944             "top": KWS.fullscreen.prevTop,
945             "left": KWS.fullscreen.prevLeft
946         })
947         .removeClass("windowmaximize");
948         $("footer").show();
949         $("#kit-header-fullscreen").hide().off();
950         KWS.resize( _pid, KWS.fullscreen.prevWidth, KWS.fullscreen.prevHeight );
951         KWS.fullscreen.pid = null;
952         KWS.fullscreen.prevWidth = null;
953         KWS.fullscreen.prevHeight = null;
954         KWS.fullscreen.prevTop = null;
955         KWS.fullscreen.prevLeft = null;
956     }
957
958     this.vacuum = function( _left, _top ){
959         for( let i in process ){
960             $("#w"+i).css("transition", ".5s all ease").css("left", _left ).css("top", _top );
961         }
962         setTimeout(() => {
963             $(".window").css("transition", "none");
964         }, 500);
965     }
966
967     this.active = null;
968     this.windowIndex = 1;
969
970     this.refreshWindowIndex = function(){
971         let num = $(".window").length;
972         let array = new Array();
973         let obj = new Object();
974         for( let i = 0; i < num; i++ ){
975             obj = { id: $(".window")[i].id, zindex: $(".window")[i].style.zIndex };
976             array.push( obj );
977         };
978         array.sort( (a,b) => {
979             return Number(a.zindex - b.zindex);
980         } );
981         for( let i in array ){
982             document.getElementById(array[i].id).style.zIndex = i;
983             if( i == num-1 ){
984                 $("#"+array[i].id).addClass("windowactive");
985                 $("#t"+String(array[i].id).substring(1)).addClass("t-active");
986                 KWS.active = String(array[i].id).substring(1);
987                 process[array[i].id.substring(1)].isactive = true;
988             }
989             else{
990                 $("#"+array[i].id).removeClass("windowactive");
991                 $("#t"+String(array[i].id).substring(1)).removeClass("t-active");
992                 process[array[i].id.substring(1)].isactive = false;
993             }
994         }
995         KWS.windowIndex = num;
996     }
997
998     this.resize = function( _pid, _width, _height ){
999         if( _width ) $("#winc"+_pid).css("width", _width)
1000         if( _height ) $("#winc"+_pid).css("height", _height);
1001     }
1002
1003     this.fusen = new function(){
1004         this.fid = 0;
1005         this.list = new Object();
1006
1007         this.add = function(_text){
1008             KWS.fusen.list[KWS.fusen.fid] = String(_text);
1009             $("#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+"' data-kit-contextid='fusen'>"+_text+"</textarea></div>");
1010             $("#kit-f"+KWS.fusen.fid).css({
1011                 "left": Number(KWS.fusen.fid)*40 + 20,
1012                 "top": Number(KWS.fusen.fid)*10 + 100,
1013             }).pep({
1014                 elementsWithInteraction: ".kit-fusen-textarea",
1015                 useCSSTranslation: false,
1016                 disableSelect: false,
1017                 shouldEase:     true,
1018                 initiate: function(){
1019                     $(this.el).css("ui-opacity", "0.7");
1020                 },
1021                 stop: function(){
1022                     this.el.style.transition = "none";
1023                     $(this.el).css("ui-opacity", "1.0");
1024                 }
1025             })
1026             $(".kit-fusen-textarea").off().on("change",function(){
1027                 Notification.push($(this).attr("data-fid"), $(this).val(), "debug");
1028                 KWS.fusen.list[$(this).attr("data-fid")] = $(this).val();
1029                 localStorage.setItem("kit-fusen", JSON.stringify( KWS.fusen.list ));
1030             });
1031             localStorage.setItem("kit-fusen", JSON.stringify( KWS.fusen.list ));
1032             KWS.fusen.fid++;
1033         }
1034
1035         this.remove = function(_fid){
1036             delete KWS.fusen.list[_fid];
1037             localStorage.setItem("kit-fusen", JSON.stringify( KWS.fusen.list ));
1038             $("#kit-f"+_fid).remove();
1039         }
1040     }
1041
1042     this.addCustomContext = function( _elem, _contextid, _obj ){
1043         KWS.context[_contextid] = _obj;
1044     }
1045
1046     this.context = {
1047         "fusen" : {
1048             "name" : "ふせん",
1049             "delete" : {
1050                 "label" : "ふせんを削除",
1051                 "icon" : "fa-trash-alt",
1052                 "function" : function(){
1053                     KWS.fusen.remove( S.selectedElement.attr("data-fid") );
1054                 }
1055             },
1056             "copy" : {
1057                 "label" : "ふせんを複製",
1058                 "icon" : "fa-copy",
1059                 "function" : function(){
1060                     KWS.fusen.add( KWS.fusen.list[S.selectedElement.attr("data-fid")] );
1061                 }
1062             }
1063         }
1064     }
1065 }
1066
1067 const Notification = new function() {
1068     this.nid = 0;
1069     this.list = new Object();
1070
1071     this.goodnight = false;
1072     this.sound = null;
1073
1074     this.push = function( _title, _content, _app ) {
1075         if( !System.debugmode && ( _title == "debug" || _app == "debug" ) ){
1076             return false;
1077         }
1078         this.list[this.nid] = {
1079             "title" : _title,
1080             "content" : _content,
1081             "app" : _app,
1082             "time" : System.time.obj.toLocaleString()
1083         };
1084         if( !this.goodnight ){
1085             if( this.sound ) System.audio.play( "n" + this.nid, this.sound );
1086             $( "#last-notification-title" ).text("").text( _title );
1087             $( "#last-notification-content" ).text("").text( _content );
1088             $( "#last-notification-app" ).text("").text( _app );
1089             $( "#last-notification" ).hide().show( "drop", {direction: "right"}, 300 );
1090         }
1091         $( "#notifications" ).append( "<div class='notis' id='nt" + this.nid + "'><span class='notis_close' id='nc" + this.nid + "'></span><span><span class='fas fa-comment-alt'></span>" + _title + "</span>" + _content + "<div class='notis_time'>" + System.time.obj.toLocaleString() + "</div></div>" );
1092         $("#nc" + this.nid).on("click", function(){
1093             let _nid = this.id.slice(2);
1094             $("#nt" + _nid).fadeOut(300);
1095             return false;
1096         } );
1097         $("#nt" + this.nid).on("click", function(){
1098             let _nid = this.id.slice(2);
1099             if( Notification.list[ _nid ].app != "system" ){
1100                 launch(Notification.list[ _nid ].app);
1101             }
1102         } );
1103         this.nid ++;
1104         return (this.nid - 1);
1105     }
1106 }
1107
1108 var process = {}, processID = 0, pid, currentDesktop = 1, currentCTX = "", prevWindowIndex, S;