GruntJS for CSS minification

With GruntJS, as we saw in the last post, we can minify javascript files, the next in line is the CSS files.
I have updated the Github repo for using CSS along with Javascript files.
The basic setup is not different from javascript setup. Here we have to use

grunt-contrib-cssmin

The confusing part is, for minification we have a lot of options in NPM, and one of them is

grunt-contrib-mincss

So at first I was confused and it did not work for me. Seems like “mincss” is old and have not been updated for a long time. So I am with “cssmin” and that just works.
As usual, install cssmin locally, with

npm install grunt-contrib-cssmin

A note is, if you have “package.json” and “Gruntfile.js” file in the same location, take those files somewhere else while installing new packages from NPM, else it throws some errors. Once installation is complete, bring those files back and that should work as it was before.

Happy grunting.

Phonegap ( cordova ) and iOS longtap

This is perhaps the most significant effect of using a webview for iOS development. When working with Phonegap(cordova) which uses WebView, the user gets a longtap event as it use to on a web browser. All sort of long tap menus will come up in an application using Phonegap (ex : select, copy etc).
To suppress these unwanted menus on a native application, which uses Phonegap(Cordova), there are a number of options found throughout the internet, but the one which worked for me is a style declaration as below.

/* Disabling the default iOS bevaviour of copy-paste on long tap */
*:not(input):not(textarea) {
	-webkit-user-select: none; /* disable selection/Copy of UIWebView */
    -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */

}

Happy coding :)

First book review : Aptana Studio Beginner's guide

This came to me as a surprise that I have been asked to review a book and write something about it. I started of with excitement as to finish the book as soon as possible and write something about it. So here it goes.
The book : Aptana Studio Beginner’s Guide
A nice book, as it states from its title. Which is unlikely, is its not only for “beginners only”. This is all you need to get started with Aptana studio but in general Eclipse IDE of any kind.
Aptana Studio itself provides a very solid IDE which is both free and opensource. I love this IDE as I do HTML and JS projects with this IDE. The most appealing thing about this IDE is its cross-platform, so one IDE, one experience everywhere. I must say its a must have for anyone who is serious about Javascript development in general.
Now to get the most out of this nice IDE, this companion book is a must have I would say to get you started and to know what can you do with the IDE.
This book starts with very basic things as to menus and workspace and then move on to deeper topics as to configure it to suite your working style. After working with this IDE for such a long time, I found this book reveals a lot more which came as surprise to me. Things like saving your own customised look and preferences, making shortcuts, the command-line interface inside the IDE and lot more are covered in detail, from a beginner’s perspective.

All in all a nice book to have if you want to get used to Aptana IDE in particular or Eclipse IDE in general.

Thanks a lot to Kenny Dias from Packt Publishing, who gave me this opportunity to review this book.

Things to consider for Cordova (phonegap) development

This came to me as a learning from my first Cordova(phonegap) application development.
The first thing one comes across while developing in Phonegap is “the need for a GUI lib” and the first thing to get in a search result is jQuery, its mobile version and the UI lib. There are many other good libs out there, but this one seems quite ok. While one may choose to use a UI lib or build own lib its up to the developer. In my experience its easy to get started with jQueryMobile and its UI, which uses “Theme roller” to theme the UI.
If one is using jQueryMobile then the trick is to try and solve all the UI problems with this lib itself (not hacking the CSS and JS all over the place in the project). That way the performance will be more and pain to solve all the UI problems is less.

One can just start to make one Cordova application without any libraries, but to be productive soon, the minimum required libraries are
1. jQuery
2. jQueryMobile
3. cordova

Now the most important library which might not be required for the project as a project dependency but is a real time saver is
1. UnderscoreJS
After some playing around with the library and from the suggestion from the community I found Lo-Dash library, which is a drop-in replacement for UnderscoreJS with performance enhancements. So I would recommend using
1. Lo-Dash

Now to coding tips, if one knows the “scope” and “this” in javascript, then writing javascript applications are just fun. So prepare yourself with javascript and how it works, so as to make your life easier developing the application.

Target platform and the library support :
The current Cordova lib might not support your required target platform !! Yeah, that is another thing which might take up sometime to know in the beginning. With each release these libraries kind of deprecate support for old platforms, so check that out in the beginning itself.
The same goes for jQuery and jQueryMbile. Check out whether the latest version of the library supports your target platform.

Native Notifications in Cordova is not supported :
Cordova (phonegap) does not support native notifications. It only supports “alerts”,”confirm” (kind of alert only),”beep”(sound) and “vibrate” (device vibration). But then if you expect to do “alert” notification as iOS and Android devices do (like showing the alerts to user when the application is running in background), then Cordova (phonegap) does not help. One has to write native libraries in native languages and call them from javascript through Cordova.

jQuery tips :
Adding an element to a page and displaying it :
jQuery “append()” and “prepend()” does not update the UI (or DOM), so the added elements are not visible by default. we can hack it by dispatching a jQuery event called “create”, the code will look as below

$("#p_4_content").prepend("hello World").trigger( "create" );

That will tell jQuery to render the UI again, resulting in displaying the added item.

Defining EventHandler Scope :
Another nice thing is calling the event handler in required “scope”. The jQuery library provides a handy method for adding event event handlers as

$('#id_btn').on( 'tap', onBtnTapped );

But then it will be scoped to the same scope as where its being written. If you are writing things in an Object, you might want to get the “scope” in the handler(which is “this”), in order to do that, jQuery provides a “third” parameter option, and one can pass the scope in there. The optional parameter is at the middle instead of at the end (as comparing to other object oriented programming convention)!! But then its the way to go. So the modified code will look as below

$('#id_btn').on( 'tap', this, onBtnTapped ); //Look at the middle parameter
//and the handler code looks as below
onBtnTapped :function(event){
    	//just saving the scope, may be we need it in future here
    	var that = event.data; //here event.data=this (which we passed as second param)
    }

That way inside the handler we can get the “scope” as below

var that = event.data;

The second parameter sent while binding the handler to the event, can be got inside the event handler as “event.data”. Since we are just passing “this”, we can get that in the handler as the example shown above.

Writing the EventHandler :
Well, now the trick writing an eventhandler without making the application behave unreasonably is a challenge. It might sound silly but it is not. If you write an event handler as

onBtnTapped: function(event){
    	//Do something
    }

Then its bound to get in your way of the behavior of your application. The jQuery fix is to do a “return false” in all the handlers. But then that might not be the case of your handler as that might return in between the code itself. So the fix is make a call to “preventDefault” as soon as you are inside the handler method. the modified code will look as below

onBtnTapped: function(event){
        event.preventDefault();
    	//Do something
    }

That is the start, I will post more tips as I fight through the scenarios.

haXe : Conditional compiling

A nice and exciting feature about haXe is conditional compilation. That means one can specify which code to compile depending upon the target chosen at the compile time. Hah, that does not make sense ? Ya, I know. Let me explain again.
There is a compiler and while providing argument to it we have to specify which target we are compiling to. Like in case of haXe and NME we can compile it to a lot of different targets. The compiler argument for flash or swf output looks as below

haxelib run nme test app.nmml flash

Now we can compile the code to HTML5 with the following argument

haxelib run nme test app.nmml html5

Thats the beauty of the language. Most of the time its the same code, but then there are times when we need to write different codes for different targets (may be layout, view elements, can be anything). But then one does not need to keep a separate project for all the different cases. The solution is conditional compiling. What it does is depending upon the target type specified to compiler, it picks up the right code from the same file. Whoaa!! Magic :)
How does it do it or how we make it aware of this ?! You asked it. Lets see it.

#if flash
//Code for SWF output
#elseif js
//Code for HTML5/js output
#else
#end

Thats simple. Its one file, completely in itself, but when it comes to compiler it picks up the right code part depending upon the target type.

One thing to remember though, that when specifying HTML5 target, it picks up conditions from “js”, it does not have a “html5″ option. This particular thing took me sometime to figure out. Just thought would point out here saving someone’s time out there.

Happy haxe -ing :)

haXe : doing a regular update and NME

This came to me as a surprise while trying to write a script, which can run in iOS, HTML5, Flash and Neko. I knew it can be done by cross-compiling through NME to all these target platforms. While this hold good almost in every case but when it comes to regular stage update or the ENTER_FRAME event, it is a problem.
Well, if you are a flash developer and depend on ENTER_FRAME event for a long time, then it may come to you as a surprise like me. The game loop as the gaming engines call it, now has to be worked upon. Fortunately we have a timer function in haXe and that quite fits the game.
Before going ahead in timer, lets see the unreliable nature of ENTER_FRAME for all targets. Firstly I was creating different circles in stage area with an ENTER_FRAME, while that worked well in all platforms, I thought its good to go for all. But then I tried to rely upon the same ENTER_FRAME event with a drag and drop and position change of the elements. Whoa!! That does not work! I stopped relying on the ENTER_FRAME event and changed that with Timer and everything is back in the action again.
The haXe timer code is a little different than Flash timer and the code looks as below.

var timer = new Timer(10); //10 is the time difference in milliseconds between the timer call
timer.run = onEachTimerTick;//its kind of event listener
function onEachTimerTick():Void {}

For me, ENTER_FRAME event only worked for Flash targets and nothing else. So be careful if you want to target other delivery methods.

Happy coding!

NME 3.1 RC4 is released

Well moving ahead with haXe and NME is as exciting as the new release of NME library. Though this is a minor release to RC4, but looking at the speed of its release cycle, it seems we are just fine learning the new options in NME, where the NME development team is doing their best to give us new features soon.
If you are like me and coding in MAC then there is definitely a point when you think how to install the new libraries into my development environment. Actually its as simple as putting the downloaded library in right folder and setting the current environment to point to the latest version.
It may be required depending upon your machine settings that you may require to configure your machine so as to show all the hidden and system files, which are generally hidden.
All the supporting libraries of haXe, remains inside haXe folder. So just navigate to

/usr/lib/haxe/lib

And there you will find all the library folders.Now go inside “nme” folder and the current version of the library will be present there. All we have to do is, download a new version of the library and put it in this folder. I have recently downloaded the 3.1RC4 and extracted it into this folder. The main folder name I kept as “3,1,RC4″, keep a note these are comma (3,1,RC4) thats how haXe reads them. So its just a practice. Now there is a file inthis folder with name “.current”(This file is generally hidden). Here the currently active version of the lib is specified. Open this file in a text editor and remove everything else (If there is anything at all there). Then type “3.1.RC4″ as the only text in the file. Just remember when we name our folder as comma separated values, haXe reads them as dot(.) while reading from the “.current” file, so we have to just follow this instruction.
That all to it. If you fire up the Terminal in MAC and type

haxelib list

then the output will show something like

nme: 3.0.1 [3.1.RC4]

The version with a square bracket is the currently active version.
So get the latest lib and activate it in development environment and have fun :)

haXe : First HTML5 circles

We are going to write an application, which will just draw out different color circles to the HTML5 canvas. Once the number of circles reaches 1000, we are going to clean it up and start over again.
There are 2 files, the first one is the program entry point Main.hx and then there is an ApplicationStage.hx. Basically we are not going to write anything in the program entry class, rather we will keep it clean all the time. So for us, the first class to write some code is ApplicationStage.hx. Below is the code for the two files;

class Main 
{

	public static function main() 
	{
		new Main();
	}

	public function new()
	{
		var as:ApplicationStage = new ApplicationStage();
		Lib.current.addChild(as);
	}

}

We are not doing anything on Main class, rather creating an object of ApplicationStage and adding it to the stage. So the code for ApplicationStage looks as below.

package ;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;

class ApplicationStage extends Sprite
{
	private var count:Int;
	private var max:Int;

	public function new() 
	{
		super();
		this.init();
	}

	private function init():Void
	{
		this.count = 0;
		this.max = 1000;
		this.addEventListener(Event.ENTER_FRAME, onEachFrame);
	}

	private function onEachFrame(e:Event):Void
	{
		//count
		this.count++;
		//draw
		var g:Graphics = this.graphics;
		g.beginFill(Math.round(Math.random()*(256*256*256)),Math.random());//Math.random() * 256, Math.random()
		//g.beginFill(0xFF0000,1);//Math.random() * 256, Math.random()
		g.drawCircle(Math.random() * 600, Math.random() * 400, Math.random() * 50);
		//g.drawCircle(20,40,10);
		g.endFill();
		//clear everything
		//trace(this.count);
		if (this.count>this.max)
		{
			g.clear();
			this.count = 0;
		}
	}

}

If you are coming from Flash, it should look as if you are writing Actioinscript ! So no worries.
Now the last thing is to write the nmml file for NME. We save it with name compile.nmml and it looks as below.

<pre>
<?xml version="1.0" encoding="utf-8"?>
<project>
 
	<app title="haXe_Day_8" 
		file="haXe_day_8"
		main="Main" 
		package="com.example.saumyaray" 
		version="1.0.0" 
		company="saumya"></app>
		
	<window width="600" height="400" 
			fps="30" 
			orientation="portrait" 
			resizable="false" ></window>
 
	<set name="BUILD_DIR" value="Export" ></set>
 
	<classpath name="src" ></classpath>
	<haxelib name="nme" ></haxelib>
 
	<ndll name="std" ></ndll>
	<ndll name="regexp" ></ndll>
	<ndll name="zlib" ></ndll>
	<ndll name="nme" haxelib="nme" ></ndll>
</project>
</pre>

Finally the command for compiling it to HTML5 will go as below in the commandline.

haxelib run nme test compile.nmml html5

Well, that all to it.
The source files are here for your reference.

haXe : Adding a display object

Well, in our last tutorial we have already written a fair entry point to any haXe application. Now lets see how we will add a custom display object to our project.
A new display object class is written as below and saved as “VisualBox.hx”.

import flash.display.Sprite;
class VisualBox extends Sprite
{
	public function new()
	{
		super();
		trace(this);
	}

}

Since this is a visual class, it will extend a display object and thats what we are doing already. Nice and fine, but one thing to remember is, we have write a method named “new()” in order for that display object constructor. Then the “super()” call is necessary as for any display object.
Now lets see how are Main.hx looks now.

package;
import flash.Lib;

class Main {
	public static function main() {
		trace("Hello From FDT haXe !");
		new Main();
	}
	public function new(){
		var vb:VisualBox=new VisualBox();
		Lib.current.addChild(vb);
	}
}

Here is something happening! First if we have to add something to the current stage, we have to get it as “Lib.current”. Since we are using Flash for output, the path looks as “flash.Lib.current”. We have already imported flash.Lib, so we just have to access it as “Lib.current”.
Another thing to note is that, since Main.hx and the “VisualBox.hx” are on the same package, the package definition for “VisualBox” is omited from the file. Just look at the code above and see that “VisualBox” is not nested inside another package definition!
Happy haXe ing!

haXe basics

The bare bone basic of haXe language is one must need an Entry point to the Application. This could be any class, but then one must specify the entry point function, which is “main()” , the signature of function looks like

public static function main() {}

Constructors in HAXE are written as “new()” as below

class GameStage
{
 public function new(){}
}

the package declaration ends with a semicolon (unlike any other major laguages, it does not contain curly braces)

package com.saumya.haxe.helloWorld;
class GameStage
{
	public function new() {}
}

If you are extending an display object, then “super” must be called from the constructor. It snot optional, its mandatory.
Every line of code must end with “;” semicolon, again this is mandatory not optional.
Every class file is saved with “.hx” extension.
The haxe compiler file is saved with “.hxml” extension.
The NME compiler file is saved as “.nmml” extension.
A typical “.hxml: file looks as

-swf bin/swf/inTheBigBrain_as3_FP10.swf
-main Main
-swf-header 800:600:30:000000
-cp src

Similarly to compile from NME, you have to create a “.nmml” file.
A typical “.nmml” file description is here.

for compiling a .hxml file, you fire up the following command,

haxe test.hxml

Have a look at the detailed options.

To compile a nmml file, you have to fire up one of these commands. But the very basic is

haxelib run nme test my.nmml flash

The detailed options are here.

Hope that gives you the very basics of the language.