julieth756 / share-extras

Automatically exported from code.google.com/p/share-extras
0 stars 0 forks source link

Extensible js console API #64

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I would suggest that set of API available through javascript console should be 
extensible. At the moment it's impossible to use <import…> syntax via js 
console (see this report 
http://code.google.com/p/share-extras/issues/detail?id=62), so the end-user may 
only call those APIs that are built in. I think, it'd be good to add some kind 
of configuration for js console to provide a way to customize a number of 
available APIs. Here is an idea of simple implementation: add <import 
resource="classpath:alfresco/templates/jsconsole-import-scripts.js"> to 
execute.post.js file, and then add all imports to file 
jsconsole-import-scripts.js. The problem of this implementation is that 
jsconsole have to be distributed with a blank import file that should be placed 
under tomcat/shared path.

P.S. I'm going to implement few functions to help admin navigate through 
repository and change files easily right from js console, so i really need a 
way to expose my API to js console.

Original issue reported on code.google.com by fuf...@gmail.com on 31 Jan 2012 at 6:23

GoogleCodeExporter commented 9 years ago
I understand the requirement but I'm not yet sure what the best solution would 
be.
1. The import statement should work (Issue 62) to be able to run any Alfresco 
script but this is probably not the most convenient solution for your usecase.
2. There should be a location where you can place script files that will be 
included automatically. I'm think of /Company Home/Data Dictionary/Javascript 
Console Imports/*.js. All files placed here will be run before the actual 
script (ordered by name). 

- Would that work for you, or would you prefer a filesystem folder?
- Is the Javascript Console the right place for the extension or should your 
custom code be available in Javascript Actions and Webscripts as well?
- Just as a reminder: Java logic can already be made available via 
configuration (BaseProcessorExtension) in the Javascript API.

Original comment by Florian....@gmail.com on 31 Jan 2012 at 10:20

GoogleCodeExporter commented 9 years ago
According to Alfresco wiki 
(http://wiki.alfresco.com/wiki/4.0_JavaScript_API#Importing_Scripts) import 
statement will not work until js console wraps code to be executed with a 
function statement. Correct me if I'm wrong, but it looks possible to 
«import» script with eval function, so the idea is to define an API import 
function that takes a path to the script inside repository as an argument and 
imports it using eval. Using this function you may implement auto import of the 
scripts from repository path, I think it should work in almost all cases.

I'm going to implement shell-like commands (ls, cd, pwd, cat etc), so I think 
js console is the right place for my extension. I thought about additional 
root-scope object implementation, but there are two reasons why I don't want to 
solve a problem this way:
— I would like to use print function and space variable available during 
script execution via  js console, so new root scope object will not have access 
to the scope these function and variable defined in;
— these functions are interactive, so there is no need of them in another 
place.

Original comment by fuf...@gmail.com on 1 Feb 2012 at 6:01

GoogleCodeExporter commented 9 years ago
Eval will not work since that import syntax is not valid Javascript. It is an 
invention by Alfresco. They parsed the script in the RhinoScriptProcessor class 
before compiling and caching the script. 

The Rhino engine actually knows a load("myscript.js") command that would be 
helpful but its not implemented in Alfresco AFAIK.

For the cd command you will need to alter the space variable in the browser. 
Currently you can not transfer any state between script executions. At one time 
I had an object called ctx that would be sent to the webscript, could be 
changed, transferred back to the browser and then saved to localstorage in the 
browser - but I think I removed that.

Original comment by Florian....@gmail.com on 1 Feb 2012 at 3:57

GoogleCodeExporter commented 9 years ago
When writing about eval i mean a construction like this:

eval('(fucntion(){'+companyhome.childByNamePath("…").content+'})()').

Yes, you're right about space variable. It'd be nice if you add a ctx object 
— i think it'll help making js console even more powerful! :)

Original comment by fuf...@gmail.com on 1 Feb 2012 at 4:38

GoogleCodeExporter commented 9 years ago
If you build the JS Console from source you can now change the space variable 
and have a folder for includes: /Data Dictionary/Scripts/Javascript Console 
Includes/* 

It works but it needs some polishing before I'll release a new version.

Original comment by Florian....@gmail.com on 1 Feb 2012 at 8:41

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I guess the approach outlined in #4 is fine to get to a quick solution for the  
repository/js console scenario.

A long-term solution should imho be independent of the environment (repo or 
surf) and implemented as a library call - i.e. "load('something.js')" or 
require('something.js')" as provided by node.js or rhino.

Original comment by deas0...@googlemail.com on 2 Feb 2012 at 9:44

GoogleCodeExporter commented 9 years ago
#5: Florian, thank you, I'll let you know if find any problems.
#7: Totally agree. I'm not sure what the best next step is. I'm not ready to 
patch Alfresco sources to provide such a functionality, but I may raise an 
issue at Jira.

Original comment by fuf...@gmail.com on 2 Feb 2012 at 11:30

GoogleCodeExporter commented 9 years ago
Upload new version 0.4.3 which supports /Data Dictionary/Javascript Console 
Includes/*  and regular <import> statements.

Closing this issue.

Original comment by Florian....@gmail.com on 7 Feb 2012 at 4:30

GoogleCodeExporter commented 9 years ago
With version 0.4.3 I have the following error when trying to execute any code 
from js console:

500 Internal Error
An error inside the HTTP server which prevented it from fulfilling the request.
01110006 Wrapped Exception (with status template): Error executing one if the 
included scripts from the data dictionary.
JavaException: 
org.alfresco.service.cmr.repository.DuplicateChildNodeNameException: Duplicate 
child name not allowed: Javascript Console Includes 
(jar:file:/home/lx/alfresco-4.0.d/tomcat/shared/lib/javascript-console-0.4.3.jar
!/alfresco/templates/webscripts/de/fme/jsconsole/execute.post.js#252)

Here is the output from js debugger:

% search.xpathSearch("/app:company_home/app:dictionary/app:jsincludes").length
0
% 
search.xpathSearch("/app:company_home/app:dictionary")[0].childByNamePath("Javas
cript Console Includes")
Node Type: {http://www.alfresco.org/model/content/1.0}folder, Node Aspects: 
[{http://www.alfresco.org/model/content/1.0}auditable, 
{http://www.alfresco.org/model/system/1.0}referenceable, 
{http://www.alfresco.org/model/system/1.0}localized]

I've attached a patch to provide a quick fix for the problem. I'm not sure what 
this error is caused by.

Original comment by fuf...@gmail.com on 11 Feb 2012 at 10:55

Attachments:

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I to got a 500 error when trying to use 0.4.3 of the console, and on 
investigation it turned out that however you tried to execute 
search.xpathSearch I got an error from Alfresco. I'm using 4.0a and there is a 
JIRA about xpath not working as a search type on the node browser, but it 
suggests that this search type just shouldn't be in the DDL and nothing else. 

To make 0.4.3 work under Alfresco 4.0a I had to alter the search.xpathSearch to 
a search.query.

I do not know if this is a bug in Alfresco 4.0a in the search.xpathSearch java 
code or if it is resolved in later versions, but from my experience xpathSearch 
doesn't work in 4.0a.

A patch file and a version of execute.post.js that does work with 4.0a is 
attached.

Original comment by coolhand...@googlemail.com on 29 Feb 2012 at 5:50

Attachments:

GoogleCodeExporter commented 9 years ago
With the changes proposed in https://issues.alfresco.com/jira/browse/ALF-13631 
it should now be possible to include arbitrary scripts in any JavaScript 
executed by the Repository, including input via the JavaScript console. 
Unfortunately, I have not found a way to make it fully independent of the 
environment, e.g. there is a slight API difference between the Repository and 
Share tier due to existing extensibility features (script search path) in Surf 
that I didn't want to touch.

Original comment by axel.fa...@prodyna.de on 9 Apr 2012 at 7:29

GoogleCodeExporter commented 9 years ago
Hi Axel, I support your proposal (let's see if we can get some votes on that 
ticket ;-) .. but I believe it will not be easy to get this in the core. Since 
there is no way to do the dynamic execution without modifying the 
ScriptProcessor I probably won't get it into the Javascript Console without 
that core change.

Original comment by Florian....@gmail.com on 9 Apr 2012 at 9:07