Liferay Dev Studio provides extended front-end development tools to assist in Liferay development. You now have access to code inferencing and code completion features for AlloyUI, JavaScript, CSS, and jQuery.
This tutorial covers how to use the code assist features in Dev Studio for
Each language is covered in its own section, so you can navigate to the language you’re most interested in. There are many languages, including the four listed above, that Dev Studio provides code assist for. This is provided by Dev Studio’s integration of Tern. To access these features, you must be working in a file those languages are expected for (e.g., JavaScript, JSP, HTML, CSS, etc.).
You must have Tern features enabled in your project in order to use them. You should also have the appropriate Tern modules enabled based on the language you’re writing in. For example, if you’re writing in a jQuery file, you must apply the Tern jQuery module to use code assist for that language. See the Enabling Code Assist Features in your Project tutorial to learn how to enable Tern features for your projects.
You’ll begin testing the AlloyUI code assist features first.
AlloyUI Code Assist Features
There are several helpful code assist features that can improve your
productivity when writing code for AlloyUI. Before beginning, enable the Tern
modules required to use AlloyUI features: AlloyUI, Browser, JSCS,
Liferay, and YUI Library. The example below shows how to access the AlloyUI
code assist features in the main.js
of your project:
-
Open your project’s
main.js
file and type the following code:AUI().
-
Press Ctrl+Space with your cursor to the right of
AUI().
. This brings up the code inference for theAUI()
global object. Notice the AlloyUI framework’s own API documentation is also displayed. Press Enter to use code completion.
By default, code inference is triggered by a keystroke combination; however, you can enable auto activation in Dev Studio’s Preferences menu. Follow the steps below to enable auto activation:
-
Navigate to Window → Preferences → JavaScript → Editor → Content Assist.
-
Check the Enable auto activation box and click Apply and Close.
The figure below shows how to enable auto activation:
Now, if you follow the previous example, code inference activates as soon as you press the trigger key, which in this case is the . (period) key.
In addition to general code inference for AlloyUI, you have access to code
templates. AUI JavaScript templates are available in Eclipse’s JavaScript editor
as well as in the HTML/JSP editor when working with <script>
and
<aui-script>
tags. Follow the steps below to use AUI code templates:
-
Type the following code in your
main.js
:AUI
-
Press Ctrl+Space to bring up the code inference for
AUI
, and you’ll see a list of all the available AlloyUI code templates, along with documentation. -
Select your template and hit Enter to paste its contents into your
main.js
.
In addition to code inference in your JS files, you can also use code inference
in your JSP/HTML files using <aui:script>
tags.
Open one of your project’s JSPs and add the AUI taglib directive if it’s not already in your JSP:
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
You can also add the import from the Snippets menu (Window → Show View → Snippets) under Taglib imports → Liferay AUI Taglib Import v6.0.
-
Add an
<aui:script>
tag inside your JSP and configure it to look like the following code:<aui:script> aui </aui:script>
-
Press Ctrl+Space with your cursor placed to the right of
aui
to bring up code inference.
There you go! Whether in a JavaScript file or inside a JSP, you now have access to code assist features that improve your workflow.
Next, you’ll examine the JavaScript code assist features for Dev Studio.
JavaScript Code Assist Features
In addition to AlloyUI code assist features, you also have access to code
inference and completion using raw JavaScript. This code assist feature is
available in your project because the Tern module Liferay
is enabled. This
plugin provides code completions for the static JavaScript object APIs available
to portlets when running in Liferay Portal. To learn more about enabling Tern
modules in Eclipse, refer to the
Enabling Code Assist Features in Your Project
tutorial.
The example below shows how you can use code assist features to easily access functions in your portlet project.
-
Open the
main.js
of your portlet and add the following function:function say(text){ alert(text); }
-
Add the following button to the
view.jsp
of your portlet:<aui:button onClick=""/>
-
Place your cursor within the quotation marks of the
onClick
attribute and press Ctrl+Space. The code inference dialog pops up with a list of possible JavaScript functions available for you to use. -
Type say and you’ll notice the list is narrowed down to your new
say(text)
function. -
Select the
say(text)
function, and you’ll notice that it’s accompanied by documentation that provides the parameter for the function, as well as the file path where the function is located. -
Press Enter to use code completion and add the function to your button.
As you can see, JavaScript development is a breeze using Dev Studio’s code assist features. Now that you know how to use the AlloyUI and JavaScript code assist features, you can learn how to use the CSS code assist features next.
CSS Code Assist Features
Dev Studio offers code inference and completion tools for CSS. In order to use these tools, you’ll need to install an additional plugin.
Follow the steps below to install the plugin in Dev Studio:
-
Go to Help → Install New Software….
-
Paste the following link into the
Work with:
input field:http://oss.opensagres.fr/eclipse-wtp-webresources/1.1.0/
-
Click Add… and check the box next to WTP HTML - Web Resources.
-
Click Next and follow the installation instructions.
Now that your plugin is installed, you’ll need to enable the CSS features in your project. Right-click your project and go to Properties → Web Resources → CSS. Check both boxes to enable CSS features in your project.
You have successfully installed and enabled the new CSS features in your project!
Now that you have the CSS features enabled, you’ll find out how to use them next. Follow the steps below to use the CSS code assist features to locate a CSS class. Note that the process below can also be used to locate an ID.
-
Open your
main.css
file and add the following class to it:.sample-class { background-color:green; }
-
Inside your
view.jsp
add an<aui:button/>
tag and configure it to match the following code:<aui:button name="test" value="test" cssClass=""/>
-
Within the quotations of the
cssClass
attribute, press Ctrl+Space to bring up the code inference for CSS. Begin typing sample-class to narrow down the classes to the one you’re looking for.Notice, along with code inference, you can also view the styling you have for the class, as well as the file in which it is located.
-
Press Enter to use code completion and add the CSS class to the JSP.
If you look at the code inference dialog for CSS classes, you’ll also notice that in addition to your own CSS classes, you also have access to Bootstrap CSS classes found in Liferay Portal.
Lastly, you’ll learn about the code assist features for jQuery.
jQuery Code Assist Features
You can also use code assist with jQuery. To do this, you must enable the
jQuery
Tern module. Follow the instructions in the
Enabling Code Assist Features in Your Project
tutorial to learn how to enable Tern modules in your project.
The jQuery Tern plugin gives type information for the jQuery framework. In the example below, you’ll test the jQuery code assist feature.
-
Open your project’s
jquery.js
file. -
In the file, type the following sample variable:
var form =
-
Press Ctrl+Space to bring up the code inference for the variable you’re declaring, and you’ll see a list of everything that is available. Also notice jQuery documentation is available for each method. Take a look at the figure below for an example of using code assit in jQuery.
Furthermore, for jQuery callback handlers, the type information for parameters is also made available.
Excellent! You now know how to use Dev Studio’s front-end development code assist features to improve your workflow.