Blame platform-demos/de/weatherAppMain.js.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" type="topic" style="task" id="weatherAppMain.js" xml:lang="de">
Packit 1470ea
  <info>
Packit 1470ea
    <link type="guide" xref="weatherApp.js#main" group="#first"/>
Packit 1470ea
    <revision version="0.1" date="2012-03-09" status="stub"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Susanna Huhtanen</name>
Packit 1470ea
      <email its:translate="no">ihmis.suski@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc/>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Mario Blättermann</mal:name>
Packit 1470ea
      <mal:email>mario.blaettermann@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011, 2013</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>The main program file</title>
Packit 1470ea
  <synopsis>
Packit 1470ea
    

In this part of the guide well construct the main program file of the weather application. To write and run all the code examples yourself, you need an editor to write code in, Terminal and GNOME 3 or higher installed into your computer. In this part we we'll go through the following parts:

Packit 1470ea
    <list>
Packit 1470ea
      <item>

<link xref="#script">Script for running the application</link>

</item>
Packit 1470ea
      <item>

<link xref="#imports">Libraries to import</link>

</item>
Packit 1470ea
      <item>

<link xref="#mainwindow">Creating the main window for the application</link>

</item>
Packit 1470ea
      <item>

<link xref="#widgets">Adding a grid and all the necessary widgets to it</link>

</item>
Packit 1470ea
      <item>

<link xref="#asynccall">Requesting the weather information asynchronously</link>

</item>
Packit 1470ea
      <item>

<link xref="#connectingbuttons">Connecting signals to button and entry</link>.

</item>
Packit 1470ea
      <item>

<link xref="#weatherapp.js">weatherapp.js</link>

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </synopsis>
Packit 1470ea
  <section id="script">
Packit 1470ea
    <title>Skript zum Ausführen der Anwendung</title>
Packit 1470ea
    
Packit 1470ea
  #!/usr/bin/gjs
Packit 1470ea
    

This line tells how to run the script. It needs to be the first line of the code and it needs to be executable. To get the execution rights go to Terminal and run in right folder: chmod +x scriptname. Or you can use the graphical filemanager. Just go to the right folder where your code is, right click you code file, choose properties, click the permissions tab and check the box for allow executing file as a program

Packit 1470ea
    

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="imports">
Packit 1470ea
    <title>Zu importierende Bibliotheken</title>
Packit 1470ea
    
Packit 1470ea
var Gtk = imports.gi.Gtk;
Packit 1470ea
const WeatherService = imports.geonames;
Packit 1470ea
    

In order to have a working program we need to import a GObject Introspection -library to our use. For working UI, we need Gtk. Gtk is imported in the beginning so we have it in our use everywhere. We also import our own local JavaScript library geonames to our use here.

Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
   <section id="mainwindow">
Packit 1470ea
    <title>Erstellen des Hauptfensters der Anwendung</title>
Packit 1470ea
    
Packit 1470ea
// Initialize the gtk
Packit 1470ea
Gtk.init(null, 0);
Packit 1470ea
//create your window, name it and connect the x to quit function. Remember that window is a taken word
Packit 1470ea
var weatherwindow = new Gtk.Window({type: Gtk.WindowType.TOPLEVEL});
Packit 1470ea
weatherwindow.title = "Todays weather";
Packit 1470ea
//Window only accepts one widget and a title. Further structure with Gtk.boxes of similar
Packit 1470ea
weatherwindow.connect("destroy", function(){Gtk.main_quit()});
Packit 1470ea
Packit 1470ea
weatherwindow.show_all();
Packit 1470ea
//and run it
Packit 1470ea
Gtk.main();
Packit 1470ea
  </section>
Packit 1470ea
  <section id="widgets">
Packit 1470ea
  <title>Adding a grid and all the necessary widgets to it</title>
Packit 1470ea
  
Packit 1470ea
var grid = new Gtk.Grid();
Packit 1470ea
weatherwindow.add(grid);
Packit 1470ea
Packit 1470ea
//We initialize the icon here, but deside the file later in geonames.js.
Packit 1470ea
var weatherIcon = new Gtk.Image();
Packit 1470ea
Packit 1470ea
//Set some labels to your window
Packit 1470ea
var label1 = new Gtk.Label({label: ""});
Packit 1470ea
var label2 = new Gtk.Label({label: "Looking in the sky..."});
Packit 1470ea
var label3 = new Gtk.Label({label: ""});
Packit 1470ea
Packit 1470ea
var entry = new Gtk.Entry();
Packit 1470ea
entry.set_width_chars(4);
Packit 1470ea
entry.set_max_length(4);
Packit 1470ea
var label4 = new Gtk.Label({label: "Enter ICAO station for weather: "});
Packit 1470ea
var button1 = new Gtk.Button({label: "search!"});
Packit 1470ea
Packit 1470ea
grid.attach(label4, 2, 1, 1, 1);
Packit 1470ea
grid.attach_next_to(label1,label4,3,1,1);
Packit 1470ea
grid.attach_next_to(label2,label1,3,1,1);
Packit 1470ea
grid.attach_next_to(label3,label2,3,1,1);
Packit 1470ea
grid.attach_next_to(entry,label4,1,1,1);
Packit 1470ea
grid.attach_next_to(button1,entry,1,1,1);
Packit 1470ea
grid.attach_next_to(weatherIcon,label2,1,1,1)
Packit 1470ea
Packit 1470ea
    

In this section we create the grid we are going to use for positioning the widgets. All the buttons, labels and entries are initialized and placed on the grid. As seen from the placing of the different widgets, they don't need to be related only to one widget. At this point some of the labels don't have any content. The content for those widgets is applied later. If you run the application at this stage, you have the UI ready, but the widgets are not connected to anything. For this we need to first build the weather searching local library, and then get the information we need asynchronously. When we have our local library ready we can connect it to the necessary widgets.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
     <section id="asynccall">
Packit 1470ea
  <title>Requesting the weather information asynchronously</title>
Packit 1470ea
  
Packit 1470ea
function getWeatherForStation() {
Packit 1470ea
  var station = entry.get_text();
Packit 1470ea
Packit 1470ea
  var GeoNames = new WeatherService.GeoNames(station); //"EFHF";
Packit 1470ea
Packit 1470ea
  GeoNames.getWeather(function(error, weather) {
Packit 1470ea
    //this here works bit like signals. This code will be run when we have weather.
Packit 1470ea
    if (error) {
Packit 1470ea
      label2.set_text("Suggested ICAO station does not exist Try EFHF");
Packit 1470ea
    return; }
Packit 1470ea
    weatherIcon.file = GeoNames.getIcon(weather);
Packit 1470ea
Packit 1470ea
    label1.set_text("Temperature is " + weather.weatherObservation.temperature + " degrees.");
Packit 1470ea
    if (weather.weatherObservation.weatherCondition !== "n/a"){
Packit 1470ea
      label2.set_text("Looks like there is " + weather.weatherObservation.weatherCondition + " in the sky.");
Packit 1470ea
      }
Packit 1470ea
    else {
Packit 1470ea
      label2.set_text("Looks like there is " + weather.weatherObservation.clouds + " in the sky.");
Packit 1470ea
    }
Packit 1470ea
    label3.set_text("Windspeed is " + weather.weatherObservation.windSpeed + " m/s")
Packit 1470ea
    // ...
Packit 1470ea
  });
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
  

This function is dedicated for calling for the weather information and updating labels and icons accordingly. In the beginning of the function we get the user input for the search. So here for the first time we use our own library and assign it to variable GeoNames. While assigning WeatherService we give it the station. The firs thing we do with GeoNames is to request weather. Everything after GeoNames.getWeather(function(error, weather) happens only if we either get an error message or weather information. If either doesn't come, the rest of the program works as normal, so main_Quit works.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="connectingbuttons">
Packit 1470ea
  <title>Connecting signals to button and entry.</title>
Packit 1470ea
  
Packit 1470ea
entry.connect("key_press_event", function(widget, event) {
Packit 1470ea
  if (entry.get_text().length === 4) {
Packit 1470ea
    // Enough is enough
Packit 1470ea
    getWeatherForStation();
Packit 1470ea
  }
Packit 1470ea
  return false;
Packit 1470ea
});
Packit 1470ea
Packit 1470ea
button1.connect("clicked", function(){
Packit 1470ea
  getWeatherForStation();
Packit 1470ea
});
Packit 1470ea
  

And finally we have the connections that make the whole application run as it should. We connect both the entry and the button to do the same thing, getting the weather. So it doesn't matter weather you press enter of click the search button.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="weatherapp.js">
Packit 1470ea
  <title>Weatherapp.js</title>
Packit 1470ea
  

Die Datei Weatherapp.js sieht so aus:

Packit 1470ea
  
Packit 1470ea
#!/usr/bin/gjs
Packit 1470ea
//The previous line is a hash bang tells how to run the script.
Packit 1470ea
// Note that the script has to be executable (run in terminal in the right folder: chmod +x scriptname)
Packit 1470ea
Packit 1470ea
var Gtk = imports.gi.Gtk;
Packit 1470ea
Packit 1470ea
const WeatherService = imports.geonames;
Packit 1470ea
//Bring your own library from same folder (as set in GJS_PATH). If using autotools .desktop will take care of this
Packit 1470ea
Packit 1470ea
// Initialize the gtk
Packit 1470ea
Gtk.init(null, 0);
Packit 1470ea
//create your window, name it and connect the x to quit function. Remember that window is a taken word
Packit 1470ea
var weatherwindow = new Gtk.Window({type: Gtk.WindowType.TOPLEVEL});
Packit 1470ea
weatherwindow.title = "Todays weather";
Packit 1470ea
//Window only accepts one widget and a title. Further structure with Gtk.boxes of similar
Packit 1470ea
weatherwindow.connect("destroy", function(){Gtk.main_quit()});
Packit 1470ea
//We initialize the icon here, but deside the file later in geonames.js.
Packit 1470ea
Packit 1470ea
var weatherIcon = new Gtk.Image();
Packit 1470ea
Packit 1470ea
//Set some labels to your window
Packit 1470ea
var label1 = new Gtk.Label({label: ""});
Packit 1470ea
var label2 = new Gtk.Label({label: "Looking in the sky..."});
Packit 1470ea
var label3 = new Gtk.Label({label: ""});
Packit 1470ea
Packit 1470ea
var grid = new Gtk.Grid();
Packit 1470ea
weatherwindow.add(grid);
Packit 1470ea
Packit 1470ea
var entry = new Gtk.Entry();
Packit 1470ea
entry.set_width_chars(4);
Packit 1470ea
entry.set_max_length(4);
Packit 1470ea
var label4 = new Gtk.Label({label: "Enter ICAO station for weather: "});
Packit 1470ea
var button1 = new Gtk.Button({label: "search!"});
Packit 1470ea
Packit 1470ea
//some weather
Packit 1470ea
Packit 1470ea
entry.connect("key_press_event", function(widget, event) {
Packit 1470ea
  // FIXME: Get weather on enter (key 13)
Packit 1470ea
  if (entry.get_text().length === 4) {
Packit 1470ea
    // Enough is enough
Packit 1470ea
    getWeatherForStation();
Packit 1470ea
  }
Packit 1470ea
  return false;
Packit 1470ea
});
Packit 1470ea
Packit 1470ea
button1.connect("clicked", function(){
Packit 1470ea
  getWeatherForStation();
Packit 1470ea
});
Packit 1470ea
Packit 1470ea
function getWeatherForStation() {
Packit 1470ea
  var station = entry.get_text();
Packit 1470ea
Packit 1470ea
  var GeoNames = new WeatherService.GeoNames(station); //"EFHF";
Packit 1470ea
Packit 1470ea
  GeoNames.getWeather(function(error, weather) {
Packit 1470ea
    //this here works bit like signals. This code will be run when we have weather.
Packit 1470ea
    if (error) {
Packit 1470ea
      label2.set_text("Suggested ICAO station does not exist Try EFHF");
Packit 1470ea
    return; }
Packit 1470ea
    weatherIcon.file = GeoNames.getIcon(weather);
Packit 1470ea
Packit 1470ea
    label1.set_text("Temperature is " + weather.weatherObservation.temperature + " degrees.");
Packit 1470ea
    if (weather.weatherObservation.weatherCondition !== "n/a"){
Packit 1470ea
      label2.set_text("Looks like there is " + weather.weatherObservation.weatherCondition + " in the sky.");
Packit 1470ea
      }
Packit 1470ea
    else {
Packit 1470ea
      label2.set_text("Looks like there is " + weather.weatherObservation.clouds + " in the sky.");
Packit 1470ea
    }
Packit 1470ea
    label3.set_text("Windspeed is " + weather.weatherObservation.windSpeed + " m/s")
Packit 1470ea
    // ...
Packit 1470ea
  });
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
grid.attach(label4, 2, 1, 1, 1);
Packit 1470ea
grid.attach_next_to(label1,label4,3,1,1);
Packit 1470ea
grid.attach_next_to(label2,label1,3,1,1);
Packit 1470ea
grid.attach_next_to(label3,label2,3,1,1);
Packit 1470ea
grid.attach_next_to(entry,label4,1,1,1);
Packit 1470ea
grid.attach_next_to(button1,entry,1,1,1);
Packit 1470ea
grid.attach_next_to(weatherIcon,label2,1,1,1)
Packit 1470ea
weatherwindow.show_all();
Packit 1470ea
//and run it
Packit 1470ea
Gtk.main();
Packit 1470ea
Packit 1470ea
  

Running until you have all the autotools files ready. :

Packit 1470ea
Packit 1470ea
  <screen> <output style="prompt">$ </output><input> GJS_PATH=`pwd` gjs weatherapp.js</input></screen>
Packit 1470ea
  

Use this command on terminal while developing your modules. When calling your program in this manner it knows where to find your custom JSlibraries, in this case geonames.js.

Packit 1470ea
  

Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
</page>