OSDN Git Service

Add Methods to Push Rich Notifications [0.2.1]
[kit/kit.git] / app / kish / kish.js
1 ((_pid) => {
2
3     $.getJSON( System.launchpath[_pid] + "/kish_config.json", (data) => {
4         let props = ["background","font-family","font-size","font-weight","color","text-align","text-shadow","background-size","background-attachment","backdrop-filter"];
5         KWS.resize(_pid, data.width || "", data.height || "");
6         if( data.styles ) for( let i in data.styles ){
7             if( props.includes(i) ){
8                 S.dom( _pid, "#kish-wrapper" ).css( i, data.styles[i] );
9                 if( i == "backdrop-filter" ) S.dom( _pid ).css( i, data.styles[i] );
10             }
11         }
12     });
13
14     let kishHistory = [], kishCur = -1;
15
16     const Kish = new function(){
17         this.dir = "~";
18
19         this.cat = function(arg){
20             let path = arg.split(" ", 1), _r = "";
21             if( S.userarea[path] ) _r = "<pre><code>" + JSON.stringify( System.userarea[path].data + "</code></pre>", null, 4 );
22             else _r = "File not found: " + path;
23             return _r;
24         }
25
26         this.clear = function(arg){
27             let args = arg.split(" ");
28             let num = Number(args[0]);
29             if( !num ) S.dom(_pid, "#kish-out").html("");
30             return false;
31         }
32     
33         this.echo = function(arg){
34             _r = arg;
35             if( typeof arg == "object" ) _r = JSON.stringify(arg, null, 4);
36             return _r;
37         }
38
39         this.eval = (arg) => { return eval(arg) }
40     
41         this.exec = function(arg){
42             let cmd = arg.split(" ", 1);
43             let args = "";
44             kishHistory.unshift( arg );
45             kishCur = -1;
46             if( arg.indexOf(" ") != -1 ) args = arg.substring( arg.indexOf(" ") + 1 );
47             S.dom(_pid, "#kish-out").append( "<div class='kish-item'><i class='fa fa-dollar-sign'></i><span class='kish-highlight'>" + cmd + "</span>" + args + "</div>" );
48             if( !Kish[cmd] ){
49                 S.dom(_pid, "#kish-out").append( "<div class='kish-item'><i class='fa fa-angle-double-right'></i> kishコマンドは存在しません: " + cmd + "</div>" );
50                 return false;  
51             }
52             let exec = "Kish." + cmd + "('" + args + "')";
53             try {
54                 let rtn = eval(exec)
55                 if( typeof rtn == "object" ) rtn = JSON.stringify(rtn, null, 4);
56                 if( rtn ) S.dom(_pid, "#kish-out").append( "<div class='kish-item'><span class='kish-from'>" +cmd+ "</span><i class='fa fa-angle-double-right'></i> " + rtn + "</div>" );            
57             }
58             catch (error) {
59                 S.dom(_pid, "#kish-out").append( "<div class='kish-item'><span class='kish-from'>" +cmd+ "</span><i class='fa fa-exclamation-triangle'></i> " + error + "</div>" );
60             }
61     
62         }
63
64         this.exit = function(){
65             System.close(_pid);
66         }
67
68         this.install = function(arg){
69             let args = arg.split(" ");
70             let iobj = new Object();
71             $.getJSON( args[0] + "define.json" , function( data ){
72                 iobj.path = args[0];
73                 iobj.id = data.id;
74                 iobj.name = data.name;
75                 iobj.icon = args[0] + data.icon;
76                 System.installed.push(iobj);
77                 localStorage.setItem("kit-installed", JSON.stringify(System.installed));
78                 Kish.print("An app was installed from " + args[0], "install");
79                 $.getJSON("config/apps.json", System.initLauncher);
80
81             }).fail( function() {
82                 Kish.print("Faild to install an app from " + args[0], "install");
83             } );
84             return "Start installing...";
85         }
86     
87         this.kish = function(){
88             return "kish v0.3.0";
89         }
90
91         this.launch = function(arg){
92             args = arg.split(" ");
93             System.launchpath[processID] = System.appdir + args[0];
94             $.getJSON( "./app/" + args[0] + "/define.json", appData ).fail( function() {
95                 System.launchpath[processID] = args[0];
96                 $.getJSON( args[0] + "/define.json", appData ).fail( function() {
97                     Kish.print("Faild to launch an App: " + args[0], "launch");
98                 } );
99             } );
100         }
101
102         this.load = function(arg){
103         }
104
105         this.ls = function(){
106             let _r = "<i>path ~</i><br>";
107             for( let i in System.userarea ){
108                 _r += "- " + i + "<br>";
109             }
110             return _r;
111         }
112
113         this.open = function(arg){
114             if( !arg ) return "ファイル名を指定してください";
115             else System.open(arg);
116         }
117
118         this.print = function(arg, from){
119             let _from = "";
120             if( from ){
121                 _from = "<span class='kish-from'>" +from+ "</span><i class='fa fa-angle-double-right'></i> "
122             }
123             S.dom(_pid, "#kish-out").append( "<div class='kish-item'>" + _from + arg + "</div>" );          
124         }
125
126         this.uninstall = function(arg){
127             let count = 0;
128             for( let i in System.installed ){
129                 if( System.installed[i].path == arg ){
130                     System.installed.splice(i, 1);
131                     count ++;
132                 }
133             }
134             localStorage.setItem("kit-installed", JSON.stringify(System.installed));
135             $.getJSON("config/apps.json", System.initLauncher);
136             return count + "app(s) was uninstalled from kit.";
137         }
138
139         this.ver = function(){
140             return System.version;
141         }
142     }
143
144     $.getJSON( System.launchpath[_pid] + "/kishrc.json", (data) => {
145         for( let i of data.rc ) Kish.exec(i);
146     });
147
148     S.dom(_pid, "#kish-input").on( "keypress keyup keydown", (e) => {
149         let input = S.dom(_pid, "#kish-input").val().split(" ");
150
151         if( typeof Kish[ input[0] ] == "function" ){
152             S.dom(_pid, "#kish-curcmd").show().text( input[0] );
153         }
154         else S.dom(_pid, "#kish-curcmd").hide();
155
156         if( e.keyCode == 13 && S.dom(_pid, "#kish-input").val() ){
157             Kish.exec( S.dom(_pid, "#kish-input").val() );
158             S.dom(_pid, "#kish-input").val("");
159         }
160     } );
161
162     S.dom(_pid, "#kish-input").on( "keydown", (e) => {
163         if( e.keyCode == 38 ){
164             if( kishCur < kishHistory.length - 1 ){
165                 kishCur ++;
166                 S.dom(_pid, "#kish-input").val( kishHistory[kishCur] );
167             }
168         }
169         else if( e.keyCode == 40 ){
170             if( kishCur > 0 ){
171                 kishCur --;
172                 S.dom(_pid, "#kish-input").val( kishHistory[kishCur] );
173             }
174             else if( kishCur == 0 ){
175                 kishCur = -1;
176                 S.dom(_pid, "#kish-input").val( "" );
177             }
178         }
179     });
180
181     KWS.changeWindowTitle(_pid, "(kish)"+ System.username);
182     App.changeWindowTitle(_pid, "(kish)"+ System.username);
183 })(pid);