User:Thehayro/JavaScript interface: Difference between revisions

From KDE TechBase
(Created page with '{{TutorialBrowser| series=Plasma Tutorial| name=Writing an interface for JavaScript Addons| next=[http://techbase.kde.org/User:Thehayro/JavaScript_Addons Writing a JavaScript ...')
 
No edit summary
Line 13: Line 13:
= Introduction =
= Introduction =


This is a tutorial on how to write an interface for JavaScript addons for Plasma. Since the interface can be anything from a plasmoid to a dataengine and all the way to a service, this tutorial will focus on writing it with the help of a plasmoid.
This is a tutorial on how to write an interface for JavaScript addons for Plasma. Since the interface can be anything from a plasmoid to a dataengine and all the way to a service, this tutorial will focus on writing it with the help of a plasmoid. However, this tutorial does not cover the whole plasmoid tutorial. To read the full tutorial, please read [[Introduction To Writing Plasmoids with JavaScript]].


= Setting up =
= Setting up =
Line 35: Line 35:
Comment=My first JavaScript interface
Comment=My first JavaScript interface
 
 
X-KDE-ServiceTypes=Plasma/Plasmoid
X-KDE-ServiceTypes=Plasma/Applet
Type=Service
Type=Service
X-Plasma-API=javascript
X-Plasma-API=javascript
X-Plasma-MainScript=code/main.js
 
X-KDE-Library=
X-KDE-Library=
X-KDE-PluginInfo-Author=<Your name here>
X-KDE-PluginInfo-Author=<Your name here>

Revision as of 10:27, 25 September 2010

Writing an interface for JavaScript Addons
Tutorial Series   Plasma Tutorial
Previous   None
What's Next   Writing a JavaScript Addon

reading=

Further Reading   n/a


Introduction

This is a tutorial on how to write an interface for JavaScript addons for Plasma. Since the interface can be anything from a plasmoid to a dataengine and all the way to a service, this tutorial will focus on writing it with the help of a plasmoid. However, this tutorial does not cover the whole plasmoid tutorial. To read the full tutorial, please read Introduction To Writing Plasmoids with JavaScript.

Setting up

Before we start to program the little interface we need to do some setup. First of all, we create a directory called MyJSInterface, where our interface will be located, with the following directory structure:

MyJSInterface/
contents/
code/

metadata.desktop

In the root directory of our addon, we create a file metadata.desktop with the following content:

[Desktop Entry] Name=MyJSInterface Name[x-test]=xxMyJSInterfacexx

Comment=My first JavaScript interface

X-KDE-ServiceTypes=Plasma/Applet Type=Service X-Plasma-API=javascript X-Plasma-MainScript=code/main.js

X-KDE-Library= X-KDE-PluginInfo-Author=<Your name here> X-KDE-PluginInfo-Email=<Your email here> X-KDE-PluginInfo-Name=org.myorg.myJSInterface X-KDE-PluginInfo-Version=0.1 X-KDE-PluginInfo-Website=http://plasma.kde.org/ X-KDE-PluginInfo-Depends= X-KDE-PluginInfo-License=LGPL X-KDE-PluginInfo-EnabledByDefault=true

Important are the X-KDE-PluginInfo-Name. This section is a namespace for the addons to

A very simple Addon

Our Addon will be a simple JavaScript class that prints out "Hello World" when it gets loaded. Furthermore we will provide one method that prints out an entered text.

First of all we will define a constructor. In this case we name it MyJavaScriptAddon

function MyJavaScriptAddon() {

   print("Hello World");

}

This constructor gets called, when this addon is loaded by another JavaScript instance. In there we will get a console output with "Hello World".

The next step is to provide our Addon a method, which print out our input.

MyJavaScriptAddon.prototype.echo = function(text) {

   print(text);

}

In this part, the actual method is called echo, however having MyJavaScriptAddon.prototype prefixed is important for JavaScript to make sure that echo is a member of this class. To understand the concept of object orienting in JavaScript see this tutorial .

At the end of your Addon it is also important that you "publish" your class for other instances that would like to use it.

registerAddon(MyJavaScriptAddon)

Now, we should save our written code into MyFirstJSAddon/contents/code and name it main.js.

Installing

To install this addon, simply navigate to the root directory of our addon and type this into the console:

plasmapkg -t Plasma/JavascriptAddon -i MyJavaScriptAddon/