File: components/webfan/cli/js/console.js

Recommend this page to a friend!
  Classes of Till Wehowski   µ.Flow   components/webfan/cli/js/console.js   Download  
File: components/webfan/cli/js/console.js
Role: Class source
Content type: text/plain
Description: Class source
Class: µ.Flow
General purpose library of objects
Author: By
Last change: Update console.js
Date: 7 years ago
Size: 22,125 bytes
 

Contents

Class file image Download
/* webfan (C) Till Wehowski, Webfan.de - All rights reserved. */ var frdlBounce = function(json){ var o = ('string'===typeof json.out) ? json.out : JSON.stringify(json); console.log('You SHOULD set a callback function!' + o); $('<div></div>').appendTo('.cmd_terminal_content').html(o); }; var frdlBounce_Default = frdlBounce; $(document).ready(function() { $.fn['PttyLoad'] = function(options) { this.user_token = ('undefined'!==typeof $.WebfanDesktop.Registry && 'undefined'!==typeof $.WebfanDesktop.Registry.User && null!==$.WebfanDesktop.Registry.User) ? $.WebfanDesktop.Registry.User.token : ''; if('undefined' !== typeof options){ $.extend(this,options); } $(this).Ptty({ theme : 'default', welcome: 'frdl:\/\/>', autocomplete : true, history : true, history_max : 800, charset : 'UTF-8', enctype : 'multipart/form-data', autofocus : true }); var shebang = '#!'; var by = 'Good by!'; /* var URI_RPC = 'jsonrpc/remote.json'; */ var banner = [ '************************************************************', 'FRDL Terminal v3.0.2 ', 'Copyright (c) 2015 Till Wehowski [frdl.de] ', 'based on Ptty [code.patxipierce.com] ', 'DESKTOP powered by [github.com/nathansmith/jQuery-Desktop] ', '************************************************************'].join('\n') + '\n'; var THIS = this; var clisetup = require('http://'+frdl.route('HOST_CDN_PUBLIC_FRDL')+'/cdn/frdl/flow/components/webfan/cli/clisetup/clisetup'); $(THIS).data('url1', frdl.route('API_JSONP') ); $(THIS).data('url2', frdl.route('API_CLIENT_URL') ); var getUrl = function(wich){ wich=wich.toString(); var m='url'; switch (wich){ case '1' : /* m = 'url1'; */ return frdl.route('API_JSONP'); break; case '2': /* m = 'url2'; */ return frdl.route('API_CLIENT_URL'); break; default: return frdl.route('API_JSONP'); break; } return $(THIS).data(m) }; $.register_command( 'about', 'About frdl Terminal.', 'about [no options]', function(){ var about = banner; return { type : 'print', out : about }; } ); $.register_command( shebang + 'test', 'Testsuite', shebang + 'test [mixed]', { ps : shebang + 'test', start_hook : function(){ return { type : 'print', callback : 'typewriter', out : 'Mock interface, for developers only!', write : 'Do your tests...' }; }, exit_hook : function(){ var BY = by; return { type : 'print', out : BY }; }, dispatch_method : function(args){ return { type : 'print', callback : 'test', out : '', write : args }; } } ); var stripQuotes = function(a) { if ((a.charAt(0) === '"' && a.charAt(a.length-1) === '"') || a.charAt(0) === "'" && a.charAt(a.length-1) === "'") { return a.substr(1, a.length-2); } return a.trim(); }; var http_status_string = function(code){ code = parseInt(code); switch(code){ case 200 : return 'Ok'; break; case 201 : return 'Created'; break; case 304 : return 'Not Modified'; break; case 400 : return 'Bad Request'; break; case 401 : return 'Unauthorized'; break; case 403 : return 'Forbidden'; break; case 404 : return 'Not Found'; break; case 409 : return 'Conflict'; break; case 455 : return 'Blocked Due To Misbehavior'; break; case 500 : return 'Internal Server Error'; break; case 501 : return 'Not Implemented'; break; default: return'undefined error'; break; } }; $.register_command( 'search', 'Do a Webfan.de search.', 'search [ searchterm ]', function(tokens) { var t = []; if('search' !== tokens.shift()){ var msg = 'Unexpected error in console.search!'; console.error(msg); alert(msg); return ; } var cmd = tokens.join(' ').trim(); var q = ('string' === typeof tokens[1]) ? tokens[1] : null; frdl.wd().Registry.Programs['search'].cb_open({ query : urlencode(cmd) }); $('<div></div>').appendTo('.cmd_terminal_content').html('Search on ' + q + ' opened...'); return; } ); $.register_command( 'webfan', 'webfan command', 'webfan [ arguments ]', function(tokens) { var t = [], cmd = tokens.join(' ').trim(); frdl.c(cmd, function(err, data){ if(err){ $('.cmd_terminal_content').append('<div style="color:red;">'+err+'</div>'); return; } var util = require('util'); $('.cmd_terminal_content').append('<div><pre>'+util.inspect(data)+'</pre></div>'); }, true, frdl); return; } ); frdl.each(clisetup.sqlstarts, function(i, c){ function reg(command){ $.register_command( command, 'sql command -' +command + '-', command + ' [ ...rest of sql query ]', function(tokens) { var sql_query = tokens.join(' ').trim(); var promise = frdl.sql.query(sql_query, function(err, resultset) { if (err) { $('<div></div>').appendTo('.cmd_terminal_content').html('Error: #' + err ); if(0<frdl.debug.mode())console.error(err); } else { $('.cmd_terminal_content').append('<div>!#sql:> &quot;'+sql_query+'&quot;</div>'); $('.cmd_terminal_content').append('<div>' + require('util').inspect(resultset, { showHidden : true, depth : 8 }) +'</div>' ); } }); return; } ); } reg(c); reg(c.toUpperCase()); }); $.register_command( 'frdl', 'Send frdl command to an selected endpoint.', 'frdl [ mixed ]' + "\n" + "\tResponse types e.g.:\n" +"\t\t--expect=json\n" +"\t\t--expect=jsonp\n" + "\tFlags:\n" +"\t\t-b send to client API\n" +"\t\t-d debug messages on\n" +"\t\t-t testrequest\n" + "\tExample:\n" +"\t\tfrdl help -b\n" +"\t\tfrdl help --expect=json\n" , function(tokens) { var cache = false; var url = getUrl(1); var i = 0, j, u, expect = 'jsonp', method = 'GET'; var out = '', debug = false, quiet = false, typeOut = 'print', preparse = false; var k,toks,cmd = tokens.join(' ').trim(), kvp, _data = {}, _jsonpCallback = 'frdlBounce'; var tokens = [], test = false; var toks = clisetup.parseArgs(cmd, true); for(i in toks){ tokens.push(toks[i]); } for(i = 0; i < tokens.length; i++){ if( '-' === tokens[i].substr(0 , 1) && '--' !== tokens[i].substr(0 , 2) && tokens[i].length > 2 ){ for(j = 1; j < tokens[i].length; j++){ tokens.push('-' + tokens[i][j]); } tokens[i] = ''; } } for(i = 0; i < tokens.length; i++){ if('' === tokens[i])continue; tokens[i] = stripQuotes(tokens[i]); if('preparse' === tokens[i] || '-p' === tokens[i]){ preparse = true; } if('bounce' === tokens[i] || '-b' === tokens[i]){ url = getUrl(2); } if('debug' === tokens[i] || '-d' === tokens[i] || ('undefined'!==typeof frdl.wd().o && true === frdl.wd().o.debug)){ debug = true; } if('quiet' === tokens[i] || '-q' === tokens[i]){ quiet = true; } if('test' === tokens[i] || '-t' === tokens[i]){ test = true; } if(true === new RegExp("\=").test(tokens[i])){ kvp = explode('=', tokens[i], 2); kvp[1] = stripQuotes(kvp[1]); if('--' === kvp[0].substr(0 , 2) ){ kvp[0]=kvp[0].substr(2 , kvp[0].length); } _data[kvp[0]] = kvp[1]; if( 'POST' === kvp[0] || 'GET' === kvp[0] || 'PUT' === kvp[0] || 'DELETE' === kvp[0] || 'HEAD' === kvp[0]){ method = kvp[0]; url = kvp[1]; } if('callback' === kvp[0]){ _jsonpCallback = kvp[1]; } if('expect' === kvp[0]){ expect = kvp[1]; } if('method' === kvp[0]){ method = kvp[1]; } }else{ if('-' === tokens[i].substr(0 , 1) ){ tokens[i]=tokens[i].substr(1 , tokens[i].length); } _data[tokens[i]] = true; } } if(expect === 'jsonp'){ method = 'jsonp'; } var result = { type : typeOut, out : out, quiet : quiet }; $.set_command_option({cmd_quiet: quiet}); var _DATA = _data; if(true !== preparse)_data = {}; if(expect === 'jsonp')_data.callback = _jsonpCallback; $.extend(_data, { cmd: ('GET' === method) ? urlencode(cmd) : cmd, token : THIS.user_token, /* cmd_tokens: tokens, */ frdlcmd : tokens[1], target_url : getUrl(2) }); if('jsonp'===method){ u = frdl.Url(url); frdl.each(_data, function(k,v){ u.setParam(k,urlencode(v)); }); frdl.getScript(u.urlMakeNew(), function(){},false,false); return; } $.ajax({ url: url, crossDomain: true, type: method, /* jsonpCallback: _jsonpCallback, jsonp: _jsonpCallback, */ /* jsonp: false,*/ jsonp: "callback", jsonpCallback: _jsonpCallback, dataType: expect, data: _data, beforeSend : function( jqXHR, settings){ if(true === test || true === debug){ var _out = 'Sending: '+ JSON.stringify(_data) +' :> ' + method + ' ' + url +' :> ' + '--PARSE [CMD] :> ' + JSON.stringify(_DATA) + ' :> ' + ' --ABORTED [TEST]' + ' :> --DEBUG [REQUEST] :>' + JSON.stringify(jqXHR) +' --DEBUG [SETTINGS] :> ' + JSON.stringify(settings) ; $('<div></div>').appendTo('.cmd_terminal_content').html(_out); if('undefined' !== typeof _DATA.test && 'response' !== _DATA.test ){ return false; }else{ /* return true; */ } }else{ /* return true; */ } }, cache: cache }).done(function( response ) { if('undefined' !== typeof $.WebfanDesktop.o){ $.WebfanDesktop.o.$ = response; } var _out = ''; result = $.extend(result, response); if('string' === typeof response.cmd){ try{ $.cliExec(response.cmd); }catch(err){ console.error(err); } } try{ if('function' === typeof response.callback){ response.callback(response); }else if('string' === typeof response.callback && 'function' === window.document[response.callback] ){ window.document[response.callback](response); } }catch(err){ console.error(err); } if('string' === typeof response.js){ try{ window.eval(response.js); }catch(err){ console.error(err); } } if(true === debug && 'array' === typeof response.log){ for(var i = 0; i < response.log; i++){ $('<div class="webfan-red"></div>').appendTo('.cmd_terminal_content').html( 'Error #' + response.log[i].code + ': ' + response.log[i].message ); } } if(true === debug)_out += ((true===debug) ? 'Sending: '+ JSON.stringify(_data) +' :> ' : ' :> ') + method + url +' :> ' + JSON.stringify(response); /* alert(JSON.stringify(response)); */ if(true !== quiet){ if('string' === typeof response.out){ _out += '<p>' + response.out + '</p>'; }else{ _out+= JSON.stringify(response); } }S//else if(true === debug){ // _out += ' - Debuginfo: Output none verbose option enabled -'; // } // if(true === debug || true !== quiet){ $('<div></div>').appendTo('.cmd_terminal_content').html(_out); // } $('.cmd_terminal_prompt').show(); }).fail(function(jqXHR, textStatus ) { $('<div></div>').appendTo('.cmd_terminal_content').html(frdl.debug.last()); if('undefined' !== typeof $.WebfanDesktop.o && 'undefined' !== typeof $.WebfanDesktop.o.$)$.WebfanDesktop.o.$ = null; $('<div></div>').appendTo('.cmd_terminal_content').html('Error: #' + jqXHR.status + ' ('+http_status_string(jqXHR.status)+')' + ' - ' + textStatus ); if(true === debug){ $('<div></div>').appendTo('.cmd_terminal_content').html(JSON.stringify(jqXHR)); } $('.cmd_terminal_prompt').show(); }).always(function( ) { }); return result; } ); /* $.register_callback('test', function(data){ try{ $('<div></div>').appendTo('.cmd_terminal_content').html('Input: ' + JSON.stringify(data.write)); }catch(err){ throw( settings.error_prefix + ' ' + err ); } $('<div></div>').appendTo('.cmd_terminal_content').html('Run test...'); }); */ /** $.register_command( 'date', 'returns the current date', 'date [no options]', function(){ var now = new Date(); return { type : 'print', out : now + ' ' }; } ); */ $.register_command( shebang + 'js', 'Execute command line as javascript.', shebang + 'js [javascript]', { ps : shebang + 'js', start_hook : function(){ return { type : 'print', callback : 'typewriter', out : 'Execute command line as javascript.', write : 'Type in some javascript contents to execute in your browser...' }; }, exit_hook : function(){ var BY = by; return { type : 'print', out : BY }; }, dispatch_method : function(args){ return { type : 'print', callback : 'javascript', out : '', write : args.join(' ') }; } } ); $.register_command( shebang + 'write', 'Type into terminal screen.', shebang + 'write [html]', { ps : shebang + 'write', start_hook : function(){ return { type : 'print', callback : 'typewriter', out : 'Type into terminal screen.', write : 'Type in some (HTML) contents to display in terminal screen...' }; }, exit_hook : function(){ var BY = by; return { type : 'print', out : BY }; }, dispatch_method : function(args){ return { type : 'print', callback : 'typewriter', out : '', write : args.join(' ') }; } } ); $.register_callback('jexe', function(data){ try{ if(data.write && '' !== data.write)window.eval(data.write); }catch(err){ throw( settings.error_prefix + ' ' + err ); } }); $.register_callback('javascript', function(data){ try{ $('<div></div>').appendTo('.cmd_terminal_content').html(window.eval(data.write)); }catch(err){ $('<div></div>').appendTo('.cmd_terminal_content').html(err); /* throw( settings.error_prefix + ' ' + err ); */ } }); $.register_callback('typewriter', function(data){ var text_input = $('.cmd_terminal_prompt'); text_input.hide(); if(typeof data.write === 'string'){ var str = $('<div/>').html(data.write + ' ').text(), typebox = $('<div></div>').appendTo('.cmd_terminal_content'), i = 0, isTag, text; (function typewriter() { text = str.slice(0, ++i); if (text === str) return text_input.show(); typebox.html(text); var char = text.slice(-1); if( char === '<' ) isTag = true; if( char === '>' ) isTag = false; if (isTag) return typewriter(); setTimeout(typewriter, 10); }()); } }); /* $(this).Ptty(); */ return this; }; });