~~Title: Javascript Tutorial~~ ==== Javascript Tutorial [DRAFT]==== This Javascript tutorial describe the basics to compiles and run a Javascript example using Elementary in Node.Js === Prerequisite === Before you start you will want about how compile a EFL library * Installed from source: [[docs/efl/start|Get started with EFL]]. === Dependencies === You will need additional dependencies to Javascript Bindings: * [[https://nodejs.org|node]] (4.2 or better) === Compilation === == Efl == To configure efl sources with bindings to use in nodejs add ''//––with-js=nodejs//'' in configure flags to generate node files ./configure --with-js=nodejs #in efl source path and compile normally make make install == Elementary == In Elementary all you need is run ''./autotools'' and compile/install ./autotools #in elementary source path make make install === Node.Js === After everything is compiled and installed, it is time to use the modules in node.js. The default node.js files installed by efl/elm stay in prefix /lib path (if you don't define a prefix path is ''/usr/local/lib'') Node.js needs to know where find efl/elm node modules, to that export NODE_PATH with the file path to modules. export NODE_PATH=/usr/local/lib #if necessary replace with your prefix path Now you are ready to use the modules in node.js, use ''require'' to import the modules ~$ node > efl = require("efl") > elm = require("elm") ====Button Example==== You find this example in ''elementary_source/src/examples/button_example_00.js'' Import Elementary module elm = require('elm'); Create a new Window with //auto hide// (the window is automatically hidden when close is pressed): win = new elm.Elm.WinStandard(null); win.setTitle("Hello, World!"); win.setAutohide(true); Create a new Button (passed window has parent) and set new label text to button: btn = new elm.Elm.Button(win); btn.setText(null, "Good-Bye, World!"); Set button basic size and positions: btn.setSize(120, 30); btn.setPosition(60, 15); btn.setSizeHintWeight(1.0, 1.0); btn.setSizeHintAlign(1.0, 1.0); The method ''on'' is used to register callback on events, in this case we need catch the ''clicked'' event, we register a function (second argument) to print a log message when button ''btn'' is clicked btn.on('clicked', function () { console.log('clicked'); } ); To finish, is necessary show all elements, for that use ''setVisible(true)'' btn.setVisible(true); win.setSize(240, 60); win.setVisible(true); ====Twitter Example==== You find complete twitter example in ''elementary_source/src/examples/twitter_example_01.js'' This is a more complex example, with edje theme and some external requirements. We need install ''request'' and ''twitter'' module, for this use the ''npm'' (node.js package manager) it downloads and installs all necessary to use this modules. npm install request npm install twitter ''npm'' is installed by default with Node.js Import all modules and initialize necessary variables to connect in twitter API elm = require('elm'); util = require('util'); fs = require('fs'); request = require('request'); //need "npm install request" Twitter = require('twitter'); //need "npm install twitter" var twit = new Twitter({ consumer_key: 'ZbSM93w5Sp2cyZ2SG0XuCvoHV', consumer_secret: 'g8N7EEQLpdKPnAsS9hWuQV29FYjBkhH62jhZzXyYymDw87DKye', access_token_key: '222611263-pPhKKjYh59uuNLP0b86sP7aAtLhdecjVQaEsCDCv', access_token_secret: 'l7ccNKXTVv6cymfSD1gQH61tmfixkdna2QmOjPtpVxSHD' }); user_acount = 'EnlightenmentKo' icon_array = new Array(); Create a new Window (it has previous example): win = new elm.Elm.WinStandard(null); win.setTitle("Twitter App"); win.setAutohide(true); Create and show a //box// and //list// elementary widget containers box = new elm.Elm.Box(win); box.setSizeHintWeight(1.0, 1.0); win.resizeObjectAdd(box); box.setVisible(true); list = new elm.Elm.List(win); list.setSizeHintWeight(1.0, 1.0); list.setSizeHintAlign(-1.0, -1.0); box.packEnd(list); list.setVisible(true); Getting twitter user timeline asynchronous, using callback twit.get('statuses/user_timeline', {screen_name: user_acount, count:10}, function(error, tweets, response) { Make a new file stream with ''fs.createWriteStream'', download the image to file using ''request'' module and register the ''_img_load'' function to called when image download has finished file = fs.createWriteStream('/tmp/twitter_pic.jpg'); file.on('finish', _img_load); if (tweets.length > 0) { request(tweets[0].user.profile_image_url).pipe(file); } For each tweet we make a new Elm.Layout and set a theme using ''setfile'', ''setText'' is used to define a new text to Edje elements in theme. To have a formatted text we use Elm.Entry to main tweet text and add this widget into theme using ''setContent'' layout method for(i=0; i < tweets.length; i++){ var layout = new elm.Elm.Layout(win); layout.setFile("twitter_example_01.edj", "tweet"); layout.setText("user_name", screen_name); layout.setText("screen_name", " - @"+screen_name); var entry = new elm.Elm.Entry(win); entry.setText("elm.text", text); console.log(text); layout.contentSet("tweet_text", entry); Add a layout widget to Elm.List and call ''list.go()'' to start displaying the list properly. item = list.itemAppend("", layout, null, null, null); } list.go(); }; win.setVisible(true); You will find this and more examples in directory ''src/examples'' in efl and elementary source code. All you need to run is: node