Skip to content

Commit

Permalink
JsCodegen convert to CommonJS style and TiMock support
Browse files Browse the repository at this point in the history
  • Loading branch information
daoki2 committed May 4, 2012
1 parent 3288c7f commit 5c6c2a6
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 96 deletions.
162 changes: 82 additions & 80 deletions JsCodegen.as
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* JavaScript Code Generator for Titanium Mobile
* @author Copyright (c) 2010 daoki2
* @version 1.2.0
* @author Copyright (c) 2010-2012 daoki2
* @version 2.0.0
*
* Copyright (c) 2010 daoki2
* Copyright (c) 2010-2012 daoki2
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -94,6 +94,11 @@ package {
}
}

public static function trim(name:String):String {
var trim:Array = name.split(' ');
return trim.join("_");
}

/**
* cleanup the code generator
*/
Expand Down Expand Up @@ -121,19 +126,28 @@ package {
return null;
}

_appJs.code += "(function() {\n";
if (val.name != "") {
result = createFile(val.name + ".js");
result = createFile("ui/" + val.name + ".js");
_objName = val.name;
_appJs.code += "var " + _objName + " = Titanium.UI.createWindow({\n";
_appJs.code += " url: '" + _objName + ".js" + "'\n";
_appJs.code += "});\n\n";
result.code += "var " + _objName + " = Titanium.UI.currentWindow;\n\n";
_appJs.code += " var " + _objName + " = require('ui/" + _objName + "');\n";
_appJs.code += " // new " + _objName + "().open();\n";
_appJs.code += " return new " + _objName + "(); /* uncomment above and remove this in real application */\n";
result.code += "function " + _objName + "() {\n";
result.code += " var self = Ti.UI.createWindow();\n";
result.code += createSubviews("self", val.subviews);
result.code += " return self;\n";
result.code += "}\n";
result.code += "module.exports = " + _objName + ";\n";
} else {
_objName = "win" + _id++;
_appJs.code += "var " + _objName + " = Titanium.UI.createWindow();\n\n";
result = getFile("app.js");
result.code += createSubviews(_objName, val.subviews);
result.code += "// " + _objName + ".open();\n";
result.code += "return " + (val.name != "" ? "new " + _objName + "()" : _objName) + "; /* uncomment above and remove this in real application */\n";
}
result.code += createSubviews(_objName, val.subviews);
_appJs.code += "}());\n";
return _objName;
}

Expand All @@ -143,6 +157,10 @@ package {
private static function generateUITabBarController(val:Object):String {
trace("generate UITabBarController", val.name, val.id);
var _objName:String;
var result:Object;

_appJs.code += "(function() {\n";

if (val.name != "") {
_objName = val.name;
} else {
Expand Down Expand Up @@ -177,14 +195,18 @@ package {
} else {
_tabName = "tab" + _id++;
}
_appJs.code += "var " + _tabName + " = Titanium.UI.createTab({\n";
_appJs.code += "var " + trim(_tabName) + " = Titanium.UI.createTab({\n";
_appJs.code += " title: '" + _tabName + "',\n";
_appJs.code += " window: " + child + "\n";
_appJs.code += "});\n";
_appJs.code += (_objName + ".addTab(" + _tabName + ");\n\n");
_appJs.code += (_objName + ".addTab(" + trim(_tabName) + ");\n\n");
}
}
_appJs.code += _objName + ".open();\n\n";

_appJs.code += "// " + _objName + ".open();\n";
_appJs.code += "return " + _objName + "; /* uncomment above and remove this in real application */\n";

_appJs.code += "}());\n";

return _objName;
}
Expand All @@ -199,40 +221,36 @@ package {
trace("generate UIViewController", val.name, val.id);
var result:Object;
var _objName:String;
if (isRoot) {
_appJs.code += "/*\n";
}

if (val.name != "") {
result = createFile(val.name + ".js");
result = createFile("ui/" + val.name + ".js");
_objName = val.name;
_appJs.code += "var " + _objName + " = Titanium.UI.createWindow({\n";
_appJs.code += " url: '" + _objName + ".js" + "'\n";
_appJs.code += "});\n";
result.code += "var " + _objName + " = Titanium.UI.currentWindow;\n\n";
_appJs.code += "var " + _objName + " = require('ui/" + _objName + "');\n";
result.code += "function " + _objName + "() {\n";
result.code += "var self = Titanium.UI.createWindow();\n";
} else {
_objName = "win" + _id++;
_appJs.code += "var " + _objName + " = Titanium.UI.createWindow();\n";
result = getFile("app.js");
}

if (val.navItem != null) {
_appJs.code += _objName + ".title = '" + val.navItem.title + "';\n";
result.code += (val.name != "" ? "self" : _objName) + ".title = '" + val.navItem.title + "';\n";
} else {
_appJs.code += _objName + ".title = null;\n";
_appJs.code += _objName + ".navBarHidden = true;\n";
result.code += (val.name != "" ? "self" : _objName) + ".title = null;\n";
result.code += (val.name != "" ? "self" : _objName) + ".navBarHidden = true;\n";
}
_appJs.code += "\n";
result.code += "\n";

result.code += createView(_objName, val.view);
result.code += createView((val.name != "" ? "self" : _objName), val.view);

if (isRoot) {
result.code += "// Uncomment the following to display the window\n";
result.code += "// " + _objName + ".open();\n";
}
if (isRoot) {
_appJs.code += "*/\n";
if (val.name != "") {
result.code += "return self;\n";
result.code += "}\n";
result.code += "module.exports = " + _objName + ";\n";
}
return _objName;

return (val.name != "" ? "new " + _objName + "()" : _objName);
}

/**
Expand All @@ -246,25 +264,19 @@ package {
var result:Object;
var _objName:String;
var _winName:String;
if (isRoot) {
_appJs.code += "/*\n";
}

_appJs.code += "(function() {\n";
if (val.name != "") {
result = createFile(val.name + ".js");
_objName = val.name;
_winName = "win" + _id++;
_appJs.code += "var " + _objName + " = Titanium.UI.createWindow();\n";
_appJs.code += "var " + _winName + " = Titanium.UI.createWindow({\n";
_appJs.code += " url: '" + _objName + ".js" + "'\n";
_appJs.code += "});\n";
result.code += "var " + _winName + " = Titanium.UI.currentWindow;\n\n";
} else {
_objName = "win" + _id++;
_winName = "win" + _id++;
_appJs.code += "var " + _objName + " = Titanium.UI.createWindow();\n";
_appJs.code += "var " + _winName + " = Titanium.UI.createWindow();\n\n";
result = getFile("app.js");
_objName = "ApplicationNavWindow";
}
_winName = "ApplicationWindow";
_appJs.code += "var " + _objName + " = Titanium.UI.createWindow();\n";
_appJs.code += "var " + _winName + " = require('ui/" + _winName + "');\n";
result = createFile("ui/ApplicationWindow.js");
result.code += "function " + _winName + "() {\n";
result.code += " var self = Ti.UI.createWindow();\n";

trace("views=", val.views.length);
var _viewName:String;
Expand Down Expand Up @@ -299,30 +311,30 @@ package {
trace("UIImagePickerController not supported yet");
break;
}
result.code += _winName + ".add(" + _viewName + ");\n";
result.code += "self.add(" + _viewName + ");\n";
if (view.hasOwnProperty("navItem")) {
if (view.navItem != null) {
_appJs.code += _winName + ".title = '" + view.navItem.title + "';\n";
result.code += "self.title = '" + view.navItem.title + "';\n";
} else {
_appJs.code += _winName + ".title = null;\n";
_appJs.code += _winName + ".navBarHidden = true;\n";
result.code += "self.title = null;\n";
result.code += "self.navBarHidden = true;\n";
}
}
result.code += "return self;\n";
result.code += "};\n";
result.code += "module.exports = " + _winName + ";\n";
}

var _navName:String = "nav" + _id++;
var _navName:String = "navGroup";
_appJs.code += "var " + _navName + " = Titanium.UI.iPhone.createNavigationGroup({\n";
_appJs.code += " window: " + _winName + "\n";
_appJs.code += " window: new ApplicationWindow()\n";
_appJs.code += "});\n";
_appJs.code += _objName + ".add(" + _navName + ");\n\n";

_appJs.code += "// " + _objName + ".open();\n";
_appJs.code += "return " + _objName + "; /* uncomment above and remove this in real application */\n";
_appJs.code += "}());\n";

if (isRoot) {
result.code += "// Uncomment the following to display the window\n";
result.code += "// " + _winName + ".open();\n";
}
if (isRoot) {
_appJs.code += "*/\n";
}
return _objName;
}

Expand All @@ -340,7 +352,7 @@ package {
_appJs.code += "/*\n";
}
if (val.name != "") {
result = createFile(val.name + ".js");
result = createFile("ui/" + val.name + ".js");
_objName = val.name;
_appJs.code += "var " + _objName + " = Titanium.UI.createWindow({\n";
_appJs.code += " url: '" + _objName + ".js" + "'\n";
Expand Down Expand Up @@ -395,19 +407,6 @@ package {
// not supported yet
return "";

/*
if (val.name != "") {
_objName = val.name;
} else {
_objName = "win" + _id++;
}
if (isRoot) {
result.code += "// Uncomment the following to display the window\n";
result.code += "// " + _objName + ".open();\n";
}
return _objName;
*/
}

/**
Expand Down Expand Up @@ -725,11 +724,9 @@ package {
result += _viewName + ".addEventListener('click', function(e)\n";
result += "{\n";
result += " if (e.rowData.hasChild && e.rowData.test) {\n";
result += " var win = Titanium.UI.createWindow({\n";
result += " title: e.rowData.title,\n";
result += " url: e.rowData.test\n";
result += " });\n";
result += " Titanium.UI.currentTab.open(win, {animated:true});\n";
result += " var win = require(\"'ui/\" + e.rowData.test + \"'\").open();\n";
result += " win.title = e.rowData.title;\n";
result += " win.open({animated:true});\n";
result += " }\n";
result += "});\n";
break;
Expand Down Expand Up @@ -789,11 +786,16 @@ package {
result += createProperties(view.properties);
result += "});\n";
break;
case "NSSubviews":
result += createSubviews(parent, view.subviews);
break;
default:
trace("# skip", view.obj);
break;
}
result += parent + ".add(" + _viewName + ");\n\n";
if (_viewName != null) {
result += parent + ".add(" + _viewName + ");\n\n";
}
return result;
}

Expand Down
8 changes: 5 additions & 3 deletions Xib2Js-app.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<application xmlns="http://ns.adobe.com/air/application/2.0">
<application xmlns="http://ns.adobe.com/air/application/3.2">
<!-- application xmlns="http://ns.adobe.com/air/application/1.5" -->
<id>daoki2.XIB2JS</id>
<version>1.30</version>
<!-- version>2.00</version -->
<versionNumber>2.00</versionNumber>
<filename>Xib2Js</filename>
<name>Xib2Js</name>
<description>Xib to JavaScript translater for Titanum Mobile</description>
<copyright>2010-2011 daoki2</copyright>
<copyright>2010-2012 daoki2</copyright>
<supportedProfiles>extendedDesktop</supportedProfiles>
<initialWindow>
<title>Xib2Js for Titanium Mobile</title>
<content>Xib2Js.swf</content>
Expand Down
Loading

0 comments on commit 5c6c2a6

Please sign in to comment.