In the first tutorial, we have seen the basic rules to create a mobile widget. This tutorial will show how to make a widget both on web and mobile.
To illustrate how it is simple to create widgets and synchronize them, we will create a translator widget.
This part is just an advise to manage architecture environment. You can choose another structure.
To separate the two worlds ( Mobile/Web ), we create one folder for Web and one sub folder ( /mob ) for mobile.
For installation, you must have a network access to put all resources to an accessible server directory. Local network does not work. In this tutorial, we assume you have access to “http://yourServer.yourDomain.com/translator/”.
At the root folder, we put Web files :
In the sub folder ”/mob”, we put Mobile files :
We obtain this structure :
For the mobile part, we illustrate our example by using a physical handset or by using a simulator.
<?xml version="1.0"?>
<widget>
<title>Translator</title>
<config platform="web">
<icon>http://yourServer.yourDomain.com/translator/icon.png</icon>
</config>
<config platform="mobile">
<icon>http://yourServer.yourDomain.com/translator/mob/icon.png</icon>
<snapshot>http://yourServer.yourDomain.com/translator/mob/snapshot.png</snapshot>
<src>http://yourServer.yourDomain.com/translator/mob/translator.xml</src>
</config>
<script>
<![CDATA[
]]>
</script>
</widget>
Note : You just have to replace path names for your own widget.
Note : Please see Web Widget part for full script description. For now, we just want to know how to install widgets. Full version describe behavior of web widget.
In this script we can see 4 parts :
Once you have your structure stored on a web server, you can install widgets.
All this procedure has been done just one time for installation. For further update, you just have to use console.
Once application has been loaded, you can activate the debug console into your simulator or your handset by typing “999”. You will see all the command available as below :
Each time you want to see some informations, type the appropriate code to display them.
Let's start by create the mobile widget of our translator. The mobile version is quite simple.
On the dashboard, we will have the translatorBG.png image and by clicking on it, we open the second level materialized by a view element. In this view, we will have four lines ( text element ) and an image element. By clicking on the image, we will open a form to enter the word or sentence to translate. Clicking on the “translate” softKey will send a request to a dedicated server and the answer will be displayed in the previous view.
Note : softkeys are special keys usually located at bottom of a phone's screen, on left and right. They're often used on phones for actions such as “Ok”, “Select”, “Back”, “Cancel”, …
To do this, we just have to edit the translator.xml file from mobile folder ( /mob/translator.xml ). Keep in mind that a widget is composed of two parts :
<widget name="translator" width="29" height="33" version="1.0" about="Translate a word or sentence from english to french!" >
<image name="translatorBG" x="0" y="0" src="translatorBG.png" />
<view name="translatorView" mode="full" bgColor="#E0F4B0" >
<text name="text_label" x="6" y="5" data="Search:" color="0x000000" />
<text name="text_src" x="6" y="25" data="" color="0x0000FF" />
<text name="translatedtext_label" x="6" y="50" data="Answer:" color="0x000000" />
<text name="translatedtext_src" x="6" y="70" data="" color="0xFF0000" />
<image name="buttonEdit" x="6" y="100" src="buttonEdit.png" selectable="true" onselected="translate()" />
</view>
<form name="translatorForm" title="Translator" softKeyOk="Translate" >
<input name="question" type="text" label="Search:" value="" />
</form>
<script><![CDATA[
//-----------------------------------------------------------------------------
function onSelected( state ){
if ( state == "true" ){
showView( "translatorView" );
}
}
//-----------------------------------------------------------------------------
function translate(){
showView( "translatorForm" );
}
//-----------------------------------------------------------------------------
function onFormValidated( formName, command ){
if ( formName == "translatorForm" ){
if ( command == "Translate" ){
translatorView.text_src.data = translatorForm.question.value;
translationId = XMLHttpRequest( "http://www.google.fr/translate_a/t?client=ig&sl=en&tl=fr&text=" + translatorForm.question.value );
}
}
}
//-----------------------------------------------------------------------------
function onUrlFetched( id, inValue, code ){
if ( id == translationId ){
if ( code != 200 ){
translatorView.translatedtext_src.data = "Error";
}
else{
if ( inValue != "" ){
translatorView.translatedtext_src.data = inValue;
}
}
}
}
//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
var translationId = -1;
]]>
</script>
</widget>
Note : by default a form has two softKey ( Ok and Cancel ). In our example, we change Ok softKey by label Translate in declaration part.
So if user click on the right softKey, a request is sent to the server we choose for translation : http://www.google.fr/translate_a/t?client=ig&sl=en&tl=fr&text=.
Note : We have seen in the previous tutorial ( see Parsing an xml ) how to parse an XML request. In our example, we don't receive answer under XML format, so we don't need to use method xml_evaluate.
In this part, we will see how to make a widget Web. This tutorial doesn't discribe the JavaScript language but only the structure need to edit a widget inside Webwag environment. So for more informations about JavaScript Framework used in Webwag, please see this documentation : Prototype API.
The Web version is quite simple too !
We want to make a widget as simple as possible. To do this, we choose to display three parts :
That's all !!
As on Mobile, we just have to edit the translator.xml file from web folder ( /translator.xml ).
We already seen in Installation -> preparation the first part of the web widget to manage installation. In this part, we will see the part missing.
Just to remember :
<?xml version="1.0"?> <widget> <title>Translator</title> <config platform="web"> <icon>http://yourServer.yourDomain.com/translator/icon.png</icon> </config> <config platform="mobile"> <icon>http://yourServer.yourDomain.com/translator/mob/icon.png</icon> <snapshot>http://yourServer.yourDomain.com/translator/mob/snapshot.png</snapshot> <src>http://yourServer.yourDomain.com/translator/mob/translator.xml</src> </config>
Here is the missing part.
<content>
<![CDATA[
]]>
</content>
<style>
<![CDATA[
.answerBox {
padding:5px;
margin:5px;
background-color:#eee;
height:20px;
}
]]>
</style>
<script>
<![CDATA[
////////////////////////////////
// define widget's methods
////////////////////////////////
widget.displaySearch = function() {
var output = "";
output += "<textarea id=\""+widget.id+"_text\" name=\""+widget.id+"_text\" rows=\"2\" cols=\"40\">";
output += "</textarea><br/>";
output += "<div class=\"answerBox\" id=\""+widget.id+"_translatedText\">";
output += "</div>";
output += "<input type=\"button\" value=\"Translate\" onclick=\"widget.translate();\"><br/>";
widget.setContent(output);
};
widget.translate = function() {
widget.setAnswerText("Translating...");
var searchText = $(widget.id+"_text");
var value = searchText.value.replace(/\s/g, "+");
var url = "http://www.google.fr/translate_a/t?client=ig&sl=en&tl=fr&text=" + value;
widget.request(url, widget.receive);
};
widget.setAnswerText = function(text) {
var translatedTextElem = $(widget.id+"_translatedText");
translatedTextElem.innerHTML = text;
};
widget.receive = function(request) {
widget.setAnswerText(request.responseText);
};
////////////////////////////////
// define widget's events
////////////////////////////////
widget.onLoad = function() {
widget.displaySearch();
};
]]>
</script>
<prefs>
</prefs>
</widget>
Three main tags :
You can right-click on each image to get them.