Skip to content
This repository
  • 2 commits
  • 10 files changed
  • 0 comments
  • 1 contributor
131 engines/console/app/controllers/erp_app/desktop/console/base_controller.rb
... ... @@ -0,0 +1,131 @@
  1 +class ErpApp::Desktop::Console::BaseController < ErpApp::Desktop::BaseController
  2 + def command
  3 + logger.debug("command received:#{params}")
  4 + begin
  5 + result=""
  6 +
  7 + # NOTE- the console uses a shared binding. this is due to the fact
  8 + # that binding instances are not serializable and cant be stored
  9 + # in a user session. Until this is resolved we use a global var.
  10 + # this should not pose a problem as the console should only be used
  11 + # in development or by a sysadmin.
  12 +
  13 + # the shared binding is needed to allow for variable scope visibility
  14 + # across multiple requests
  15 + if($session_binding==nil)
  16 + $session_binding=binding
  17 + end
  18 +
  19 + command_message=params[:command_message]
  20 + logger.debug("console session context:#{$session_binding}")
  21 + logger.debug("command:#{command_message}")
  22 +
  23 + # here we handle any desktop console-specific command
  24 + # these can include non-eval related funtions
  25 + # or provide shortcuts to common eval expressions
  26 + if(command_message.match(/^-help/))
  27 + result = help_message
  28 + elsif(command_message.match(/^-clear/))
  29 + # this is actually handled in the console desktop application
  30 + #result = evaluate_command("")
  31 + elsif(command_message.match(/^-time/))
  32 + result = evaluate_command("Time.now")
  33 + elsif(command_message.match(/^-whoami/))
  34 + result = evaluate_command("current_user.login")
  35 + else
  36 + result=evaluate_command(command_message)
  37 + end
  38 +
  39 + logger.debug("result#{result}")
  40 +
  41 + result_message =result.to_s.gsub("\n", "<br />\n")
  42 + render :json=> {:success=>"#{result_message}<hr><br>"}
  43 + end
  44 + end
  45 + private
  46 + #****************************************************************************
  47 + def help_message()
  48 + message = "<font color='lightgray'><b>Compass Desktop Console Help<b><hr>"
  49 + message<< "<ul>"
  50 + message<< "<li>-clear : <font color='yellow'>Clear screen contents.</font></li>"
  51 + message<< "<li>-help : <font color='yellow'>This help list.</font></li>"
  52 + message<< "<li>-time : <font color='yellow'>Current time.</font></li>"
  53 + message<< "<li>-whoami : <font color='yellow'>Logged in as.</font></li>"
  54 + message<< "</ul> </font>"
  55 + end
  56 + #****************************************************************************
  57 + def highlight(model)
  58 + buffer=""
  59 + model.attributes.keys.sort.each do |model_attribute_key|
  60 + CompassLogger.debug("key:#{model_attribute_key}")
  61 +
  62 + buffer<<"<font color='yellow'>#{model_attribute_key}</font> <font color='lightgray'>=</font><font color='gold'>#{model.attributes[model_attribute_key]}</font> <font color='lightgray'>, </font>"
  63 + end
  64 + return buffer
  65 + end
  66 + #****************************************************************************
  67 +
  68 + def evaluate_command(command_message)
  69 + CompassLogger.debug("evaluate_command(#{command_message}")
  70 + begin
  71 +
  72 + result_eval = $session_binding.eval( command_message)
  73 +
  74 + if(result_eval.is_a? ActiveRecord::Base)
  75 + result= render_active_record_model(result_eval)
  76 + elsif (result_eval.is_a? Array)
  77 + result= render_array(result_eval)
  78 + elsif (result_eval.is_a? Hash)
  79 + result= render_hash(result_eval)
  80 + else
  81 + #result="#{result_eval.class.to_s} #{result_eval.to_s}<br>"
  82 + result="#{result_eval.to_s}<br>"
  83 + end
  84 +
  85 + rescue Exception => e
  86 + result = "<font color='red'>#{e.to_s}</font>"
  87 + end
  88 +
  89 + return result
  90 + end
  91 + #****************************************************************************
  92 + def render_active_record_model(result_eval)
  93 + CompassLogger.debug("render_active_record_model:#{result_eval}")
  94 + "<font color='YellowGreen'>#{result_eval.class} </font><br>#{highlight(result_eval)} "
  95 + end
  96 + #****************************************************************************
  97 + def render_array(result_eval)
  98 + result="#{result_eval.class.to_s}<br>"
  99 + CompassLogger.debug("render_array:#{result_eval}")
  100 + count=0
  101 + result_eval.each do |array_element|
  102 + if(array_element.is_a? ActiveRecord::Base)
  103 + result<< "<font color='YellowGreen'>#{array_element.class}<font color='yellow'>[</font><font color='white'>#{count}</font><font color='yellow'>]</font> </font>#{highlight(array_element)} <br><br>"
  104 + else
  105 + result<<"<font color='YellowGreen'>#{array_element.class}<font color='yellow'>[</font><font color='white'>#{count}</font><font color='yellow'>]</font> </font>#{array_element} <br><br>"
  106 + end
  107 + count=count+1
  108 + end
  109 + return result
  110 + end
  111 + #****************************************************************************
  112 + def render_hash(result_eval)
  113 + CompassLogger.debug("render_hash:#{result_eval}")
  114 + result="#{result_eval.class.to_s}<br>"
  115 + count=0
  116 + result_eval.keys.each do |hash_key|
  117 + symbol_modifier=''
  118 + if(hash_key.is_a? Symbol)
  119 + symbol_modifier=':'
  120 + end
  121 + if(hash_key.is_a? ActiveRecord::Base)
  122 +
  123 + result<< "<font color='YellowGreen'>#{result_eval.class}<font color='yellow'>[</font><font color='white'>#{symbol_modifier}#{hash_key}</font><font color='yellow'>] => </font> </font>#{highlight(result_eval[hash_key])} <br>"
  124 + else
  125 + result<<"<font color='YellowGreen'>#{result_eval.class}<font color='yellow'>[</font><font color='white'>#{symbol_modifier}#{hash_key}</font><font color='yellow'>] => </font> </font>#{result_eval[hash_key]} <br>"
  126 + end
  127 + count=count+1
  128 + end
  129 + return result
  130 + end
  131 +end
12 engines/console/config/routes.rb
... ... @@ -0,0 +1,12 @@
  1 +
  2 +ActionController::Routing::Routes.draw do |map|
  3 +
  4 +
  5 +
  6 +
  7 + map.connect '/console/:action.:format', :controller => 'erp_app/desktop/console/base'
  8 +
  9 +
  10 +end
  11 +
  12 +
20 engines/console/db/data_migrations/20110824020426_create_desktop_app_console.rb
... ... @@ -0,0 +1,20 @@
  1 +class CreateDesktopAppConsole
  2 + def self.up
  3 + app = DesktopApplication.create(
  4 + :description => 'Desktop Console',
  5 + :icon => 'icon-console',
  6 + :javascript_class_name => 'Compass.ErpApp.Desktop.Applications.Console',
  7 + :internal_identifier => 'console',
  8 + :shortcut_id => 'console-win'
  9 + )
  10 +
  11 + #PreferenceType.iid('desktop_shortcut').preferenced_records << app
  12 + #PreferenceType.iid('autoload_application').preferenced_records << app
  13 +
  14 + app.save
  15 + end
  16 +
  17 + def self.down
  18 + DesktopApplication.destroy_all(['internal_identifier = ?','console'])
  19 + end
  20 +end
BIN  engines/console/public/images/console_background.png
BIN  engines/console/public/images/icons/console/console_16x16.png
BIN  engines/console/public/images/icons/console/console_32x32.png
BIN  engines/console/public/images/icons/console/console_48x48.png
180 engines/console/public/javascripts/erp_app/desktop/applications/console/module.js
... ... @@ -0,0 +1,180 @@
  1 +//**************************************************
  2 +// Compass Desktop Console
  3 +//**************************************************
  4 +var desktop_console_history=new Array();
  5 +var desktop_console_history_index=0;
  6 +//----------------------------------
  7 +// add startsWith method to string
  8 +String.prototype.startsWith = function (str){
  9 + return this.indexOf(str) == 0;
  10 +};
  11 +//---------------------------------
  12 +var startup_heading="<font color='goldenrod'><b>Desktop Console Version 0.01</b>&nbsp;(<font color='white'>-help</font> for Help)</font><br>"
  13 +//---------------------------------
  14 +function sendCommand(destination,command){
  15 + update_history_panel("<font color='white'>"+command+"</font>")
  16 +
  17 + if(command.startsWith("-clear")){
  18 + clear_history_panel(startup_heading+"<br><hr>");
  19 + }else{
  20 +
  21 + Ext.Ajax.request({
  22 + url: '/erp_app/desktop/console/command',
  23 + params: {
  24 +
  25 + command_message: command
  26 + },
  27 + success: function(response){
  28 + var text = response.responseText;
  29 + var result =Ext.JSON.decode(text)
  30 + update_history_panel("<font color='yellow'>"+result.success+"</font>")
  31 + }
  32 + });
  33 + }
  34 +}
  35 +//---------------------------------
  36 +function clear_history_panel(text){
  37 + var panel=Ext.getCmp('console_history_panel');
  38 +
  39 + panel.update(""+text+"<br>");
  40 +
  41 + var d = panel.body.dom;
  42 + d.scrollTop = d.scrollHeight - d.offsetHeight+10;
  43 + panel.doLayout();
  44 +}
  45 +//---------------------------------
  46 +function update_history_panel(text){
  47 + var panel=Ext.getCmp('console_history_panel');
  48 + var old = panel.body.dom.innerHTML;
  49 + panel.update(old+""+text+"<br>");
  50 +
  51 + var d = panel.body.dom;
  52 + d.scrollTop = d.scrollHeight - d.offsetHeight+10;
  53 + panel.doLayout();
  54 +}
  55 +
  56 +//---------------------------------
  57 +var console_history_panel ={
  58 + xtype: 'panel',
  59 +
  60 + id : 'console_history_panel',
  61 + region: 'center',
  62 + bodyStyle: "background-image:url(/images/console_background.png) !important",
  63 + autoScroll:true,
  64 + html : startup_heading
  65 +}
  66 +
  67 +//---------------------------------
  68 +
  69 +var console_text_area ={
  70 + xtype: 'textarea',
  71 + region : 'south',
  72 + style: "{background-image:url(/images/console_background.png)}",
  73 + autoscroll: true,
  74 + id: "console_text_area",
  75 + enableKeyEvents: true,
  76 + listeners: {
  77 + afterrender: function(field) {
  78 + field.focus();
  79 + },
  80 +
  81 +
  82 + // use key-up for textarea since ENTER does not affect focus traversal
  83 + keyup: function(field, e){
  84 + //console.log("textarea keyup:"+e);
  85 + if (e.getKey() == e.ENTER){
  86 +
  87 + sendCommand('console_text_area',field.getValue());
  88 + // add to history
  89 + desktop_console_history[desktop_console_history.length]=field.getValue().substring(0,field.getValue().length-1);
  90 + //update index
  91 + desktop_console_history_index=desktop_console_history.length
  92 + field.setValue("");
  93 + }else if (e.getKey() == e.UP){
  94 +
  95 + if(desktop_console_history.length==0){
  96 + // no history to display
  97 + }else{
  98 + desktop_console_history_index--;
  99 + if(desktop_console_history_index >=0){
  100 +
  101 + }
  102 + else{
  103 + desktop_console_history_index=desktop_console_history.length-1
  104 + }
  105 + field.setValue(desktop_console_history[desktop_console_history_index]);
  106 + }
  107 +
  108 + }else if (e.getKey() == e.DOWN){
  109 +
  110 + if(desktop_console_history.length==0){
  111 + // no history to display
  112 + }else{
  113 + desktop_console_history_index++;
  114 + if(desktop_console_history_index >=(desktop_console_history.length)){
  115 + desktop_console_history_index=0
  116 + }
  117 + else{
  118 + //desktop_console_history_index=desktop_console_history.length-1
  119 + }
  120 + field.setValue(desktop_console_history[desktop_console_history_index]);
  121 + }
  122 + }
  123 + }
  124 +
  125 + }
  126 +}
  127 +//---------------------------------
  128 +var console_panel={
  129 + xtype: 'panel',
  130 + layout: 'border',
  131 + items :[ console_history_panel,console_text_area]
  132 +
  133 +}
  134 +
  135 +//---------------------------------
  136 +
  137 +Ext.define("Compass.ErpApp.Desktop.Applications.Console",{
  138 + extend:"Ext.ux.desktop.Module",
  139 + id:'console-win',
  140 + init : function(){
  141 + this.launcher = {
  142 + text: 'Desktop Console',
  143 + iconCls:'console_icon',
  144 + handler: this.createWindow,
  145 + scope: this
  146 + }
  147 + },
  148 +
  149 + createWindow : function(){
  150 + var desktop = this.app.getDesktop();
  151 + var win = desktop.getWindow('console');
  152 + if(!win){
  153 + win = desktop.createWindow({
  154 + id: 'console',
  155 + title:'Desktop Console',
  156 + width:1000,
  157 + height:670,
  158 + iconCls: 'console_icon',
  159 + shim:false,
  160 + animCollapse:false,
  161 + resizable : false,
  162 + constrainHeader:true,
  163 + layout: 'fit',
  164 + items:[console_panel]
  165 + ,
  166 + tools:[
  167 + {
  168 + type:'help',
  169 + tooltip: 'about',
  170 + handler: function(event, toolEl, panel){
  171 + Ext.Msg.alert("About","<center><b>Desktop Console</b><br><i>Version 0.01</i>")
  172 + }
  173 + }]
  174 +
  175 + });
  176 + }
  177 + win.show();
  178 + sendCommand('console_text_area',"Rails.version ");
  179 + }
  180 +});
7 engines/console/public/stylesheets/erp_app/desktop/applications/console/console.css
... ... @@ -0,0 +1,7 @@
  1 +.console_icon-desktop-icon{
  2 + background-image: url(/images/icons/console/console.png) !important;
  3 +}
  4 +
  5 +.console_icon {
  6 + background-image: url(/images/icons/console/console_16x16.png) !important;
  7 +}
BIN  engines/erp_app/public/javascripts/erp_app/ckeditor/plugins/syntax_highlighter/images/Thumbs.db
Binary file not shown

No commit comments for this range

Something went wrong with that request. Please try again.