Home

Custom Action - type javascript

You are here

10 posts / 0 new
Last post
Custom Action - type javascript

Hi,

I'm creating a custom action that executes a javascript function, but I don't know where I should place it.
So far I have in my share-config-custom.xml file:

<alfresco-config>
    ...
    <config evaluator="string-compare" condition="DocLibActions">
        <actions>
            <action id="set-bill-type" type="javascript" label="label.action_setBillType">
                <param name="function">onActionSetBillType</param>
                <evaluator>evaluator.doclib.action.isContentType</evaluator>
            </action>
        </actions>
        <actionGroups>
            <actionGroup id="document-browse">
                <action index="500" id="set-bill-type" />
            </actionGroup>
        </actionGroups>
    </config>
    ...

Where I should define the onActionSetBillType javascript function? As a script in Repository > Data Dictionary > Scripts ?

Thanks,
Jordi.

Jordi Vilaplana - Software Engineer & Web Developer

Re: Custom Action - type javascript

The onActionSetBillType JavaScript function is client-side, not server-side. So, it needs to go in a client-side JavaScript file. What I'm not clear on off the top of my head is whether you need to extend Alfresco.doclib.Actions and add your custom action handler there or not. You probably do.

Your function name makes me think you might be trying to set some data on the repository tier. If that's the case, your client-side JavaScript function is going to have to do something to invoke code on the repository tier. For example, maybe you are using an AJAX call to invoke a repository tier web script or something.

If you've written repository-tier logic to set the bill type, you might be better off implementing that as a repository tier action. Then, you could configure Share using "onActionSimpleRepoAction" and just give it the name of the repository tier action you are invoking.

If none of that is making sense, you might want to look at this tutorial.

I could be over-thinking what you're trying to do. If so, sorry!

Jeff

Chief Community Officer
Alfresco Software
Blog: ecmarchitect.com | Twitter: jeffpotts01
CMIS APIs: Apache Chemistry | CMIS and Apache Chemistry in Action
Alfresco tutorials: Alfresco Developer Series

Re: Custom Action - type javascript

Thanks for your response Jeff!

My objective was to specialize the type of a document to my custom types directly from the document library by adding a new menu item (action).
Now I've achieved to add the "Change Type" functionality that was only available in the document details:

I attach the code, maybe it will be useful for someone.

In share-config-custom.xml (located at ${TOMCAT_HOME}/shared/classes/alfresco/extension)I've added:

    <config evaluator="string-compare" condition="DocLibActions">
<actions>
<action id="change-type" type="javascript" label="label.my_action_changeType">
<param name="function">onActionChangeType</param>
<permissions>
<permission allow="true">Write</permission>
</permissions>
<evaluator>evaluator.doclib.action.isContentType</evaluator>
</action>
</actions>
<actionGroups>
<actionGroup id="document-browse">
<action index="400" id="change-type" />
</actionGroup>
</actionGroups>
</config>
<config evaluator="string-compare" condition="DocLibCustom" replace="true">
<dependencies>
<js src="/js/custom-documentlibrary-actions.js" />
</dependencies>
</config>

Then in ${TOMCAT_HOME}/webapps/share/js I've added the file [i]custom-documentlibrary-actions.js using the code from the 'Change Type' action:

YAHOO.Bubbling.fire("registerAction", {
actionName: "onActionChangeType",
fn: function my_onActionChangeType(record) {
var jsNode = record.jsNode;
var currentType = jsNode.type;
var displayName = record.displayName;
var actionUrl = Alfresco.constants.PROXY_URI + "slingshot/doclib/type/node/" + jsNode.nodeRef.uri;
this.modules.changeType = new Alfresco.module.SimpleDialog(this.id + "-changeType").setOptions({
width: "30em",
templateUrl: Alfresco.constants.URL_SERVICECONTEXT + "modules/documentlibrary/change-type?currentType=" + encodeURIComponent(currentType),
actionUrl: actionUrl,
firstFocus: this.id + "-changeType-type",
onSuccess: {
fn: function my_onActionChangeType_success(response) {
YAHOO.Bubbling.fire("metadataRefresh", { highlightFile: displayName });
Alfresco.util.PopupManager.displayMessage({ text: "El tipus del document " + displayName + " s'ha canviat correctament" });
},
scope: this
},
onFailure: {
fn: function my_onActionChangeType_failure(response) {
Alfresco.util.PopupManager.displayMessage({ text: "Error en canviar el tipus del document " + displayName });
},
scope: this
}
});
this.modules.changeType.show();
}
});

The isContentType evaluator must be defined in custom-slingshot-application-context.xml file located at ${TOMCAT_HOME}/shared/classes/alfresco/web-extension :

    <bean id="evaluator.doclib.action.isContentType" parent="evaluator.doclib.action.nodeType">
<property name="allowSubtypes">
<value>false</value>
</property>
<property name="types">
<list>
<value>cm:content</value>
</list>
</property>
</bean>

Cheers,
Jordi

Jordi Vilaplana - Software Engineer & Web Developer

Re: Custom Action - type javascript

Hi Jordi,
Thanks for the post.
I tried your code for "specialize type".
But am getting the following error.

ERROR [web.context.ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'evaluator.doclib.action.isContentType' defined in file [C:\Alfresco\tomcat\shared\classes\alfresco\web-extension\custom-slingshot-application-context.xml]: Could not resolve parent bean definition 'evaluator.doclib.action.nodeType'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'evaluator.doclib.action.nodeType' is defined at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:1110)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:1055)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1041)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:537)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:261)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:192)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:905)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:740)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
at org.apache.catalina.core.StandardService.start(StandardService.java:519)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'evaluator.doclib.action.nodeType' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1041)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:827)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:1096)
... 32 more

Pls let me know if u have any idea about this.

regards,
Ranj

Re: Custom Action - type javascript

Hi Ranj,

That is very strange, because evaluator.doclib.action.nodeType is a predefined evaluator, so your Alfresco should find it :|

Maybe you are not using Alfresco Share v4.x? This won't work in older versions.

Cheers,
Jordi.

Jordi Vilaplana - Software Engineer & Web Developer

Re: Custom Action - type javascript

Hi Jordi,
:( but am using share 4.0.b..

I need to change the content type inside wcmqs root folder ( site root folder), but the content i create inside that folder does not list down the custom content types..i mean, not even the default types..the content i create automatically changes to ws:article type..i dont want that to happen..i have created some form models using fdk and i want to change the type of the content to that form type..so that i can have my own metadetails fields.

Any ideas about these?

Thanks,
Ranj

Re: Custom Action - type javascript

Hi Ranj,

I've replied to your post in the Web Quick Start section.

Cheers,
Jordi

Jordi Vilaplana - Software Engineer & Web Developer

Re: Custom Action - type javascript

Hi Jordi,

I am hoping that you can help. I have implemented this solution and it works near on flawlessly. I am struggling with identifying where to put the alias for the label.my_action_changeType label associated with the action. Could you please advise? I have a properties file associated with my custom model and that doesn't work, neither (apparently) does a custom.properties file in the {TOMCAT HOME}/shared/classes/alfresco/messages directory.

Also, I have a custom model with subtypes - I notice that in the evaluator.doclib.action.isContentType bean (in the custom-slingshot-application-context.xml file) you have a property called allowSubTypes. If I set this to true will the action propagate to the subtypes in the model?

Thanks in advance for your assistance

*** Update: I have succeeded in getting the internationalization working - I have no idea what I did - it just started working!!!. I would love to hear back from you on the subtypes question, though. ****

Re: Custom Action - type javascript

Yes, it should match any subtype of your model if you set it to true.
The allowSubTypes property defines whether subtypes of the node are also allowed. In my code, only nodes of type cm:content will match the evaluator, so any child node won't match it.

This property is explained in the Document Library Predefined Evaluators wiki page.

Hope it helps!
Jordi.

Jordi Vilaplana - Software Engineer & Web Developer

can someone please tell me

can someone please tell me how do I send json data with actionUrl because when I am trying the same I am not getting the data in post.json.js file. For change type action I am sending json data with actionUrl as given below but it is not working:

var actionUrl = YAHOO.lang.substitute(Alfresco.constants.PROXY_URI + $combine("slingshot/doclib/action/aspects/node/", jsNode.nodeRef.uri),
{
requestContentType: Alfresco.util.Ajax.JSON,
method: Alfresco.util.Ajax.POST,
dataObj:
{
htmlid: this.id + "-changeType",
dataObj:{

curTyp: this.currentType
}
}});

The json data I passed in dataObj, I am unable to get when actionUrl is executed.