Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ALOY-650] Abstract all style handling code to single module #133

Merged
merged 8 commits into from May 23, 2013
399 changes: 15 additions & 384 deletions Alloy/commands/compile/compilerUtils.js

Large diffs are not rendered by default.

67 changes: 10 additions & 57 deletions Alloy/commands/compile/index.js
Expand Up @@ -4,6 +4,7 @@ var path = require('path'),
vm = require('vm'),
uglifyjs = require('uglify-js'),
sourceMapper = require('./sourceMapper'),
styler = require('./styler'),
_ = require("../../lib/alloy/underscore")._,
logger = require('../../common/logger'),
CompilerMakeFile = require('./CompilerMakeFile'),
Expand All @@ -16,7 +17,6 @@ var alloyRoot = path.join(__dirname,'..','..'),
viewRegex = new RegExp('\\.' + CONST.FILE_EXT.VIEW + '$'),
controllerRegex = new RegExp('\\.' + CONST.FILE_EXT.CONTROLLER + '$'),
modelRegex = new RegExp('\\.' + CONST.FILE_EXT.MODEL + '$'),
styleOrderBase = 1,
compileConfig = {},
buildPlatform,
theme;
Expand Down Expand Up @@ -183,7 +183,8 @@ module.exports = function(args, program) {
logger.info('----- MVC GENERATION -----');

// create the global style, if it exists
loadGlobalStyles(paths.app, theme);
styler.setPlatform(buildPlatform);
styler.loadGlobalStyles(paths.app, theme ? {theme:theme} : {});

// Create collection of all widget and app paths
var widgetDirs = U.getWidgetDirectories(paths.project, paths.app);
Expand Down Expand Up @@ -295,10 +296,9 @@ function parseAlloyComponent(view,dir,manifest,noView) {
files = {};

// reset the bindings map
CU.bindingsMap = {};
styler.bindingsMap = {};
CU.destroyCode = '';
CU.postCode = '';
CU.styleOrderCounter = styleOrderBase;
CU.currentManifest = manifest;

// create a list of file paths
Expand Down Expand Up @@ -340,7 +340,8 @@ function parseAlloyComponent(view,dir,manifest,noView) {
}

// load global style, if present
state.styles = compileConfig && compileConfig.globalStyle ? compileConfig.globalStyle : [];
//state.styles = compileConfig && compileConfig.globalStyle ? compileConfig.globalStyle : [];
state.styles = styler.globalStyle || [];

// Load the style and update the state
if (files.STYLE) {
Expand All @@ -349,7 +350,7 @@ function parseAlloyComponent(view,dir,manifest,noView) {
if (fs.existsSync(style.file)) {
logger.info(' style: "' +
path.relative(path.join(dir,CONST.DIR.STYLE),style.file) + '"');
state.styles = CU.loadAndSortStyle(style.file, manifest, {
state.styles = styler.loadAndSortStyle(style.file, {
existingStyle: state.styles,
platform: style.platform
});
Expand All @@ -365,15 +366,14 @@ function parseAlloyComponent(view,dir,manifest,noView) {

if (path.existsSync(themeStylesFile)) {
logger.info(' theme: "' + path.join(theme.toUpperCase(),theStyle) + '"');
// state.styles = U.deepExtend(state.styles, CU.loadAndSortStyle(themeStylesFile,manifest));
state.styles = CU.loadAndSortStyle(themeStylesFile, manifest, {
state.styles = styler.loadAndSortStyle(themeStylesFile, {
existingStyle: state.styles,
theme: true
});
}
if (path.existsSync(psThemeStylesFile)) {
logger.info(' theme: "' + path.join(theme.toUpperCase(),buildPlatform,theStyle) + '"');
state.styles = CU.loadAndSortStyle(psThemeStylesFile, manifest, {
state.styles = styler.loadAndSortStyle(psThemeStylesFile, {
existingStyle: state.styles,
platform: true,
theme: true
Expand Down Expand Up @@ -471,7 +471,7 @@ function parseAlloyComponent(view,dir,manifest,noView) {
bTemplate += "<%= model %>.transform()['<%= attr %>']:<%= model %>.get('<%= attr %>');";

// for each model variable in the bindings map...
_.each(CU.bindingsMap, function(mapping,modelVar) {
_.each(styler.bindingsMap, function(mapping,modelVar) {

// open the model binding handler
var handlerVar = CU.generateUniqueId();
Expand Down Expand Up @@ -625,53 +625,6 @@ function processModels(dirs) {
return models;
};

// Order of processing global styles:
// 1. global
// 2. global theme
// 3. global platform-specific
// 4. global theme platform-specific
function loadGlobalStyles(appPath, theme) {
compileConfig.globalStyle = [];
var apptss = CONST.GLOBAL_STYLE;
var stylesDir = path.join(appPath,CONST.DIR.STYLE);
if (theme) {
var themesDir = path.join(appPath,'themes',theme,CONST.DIR.STYLE);
}

var globalStyles = [];
globalStyles.push({
path: path.join(stylesDir,apptss),
msg: apptss
});
theme && globalStyles.push({
path: path.join(themesDir,apptss),
msg: apptss + '(theme:' + theme + ')',
obj: { theme: true }
});
globalStyles.push({
// TODO: get the real platforms object
path: path.join(stylesDir,buildPlatform,apptss),
msg: apptss + '(platform:' + buildPlatform + ')',
obj: { platform: true }
});
theme && globalStyles.push({
// TODO: get the real platforms object
path: path.join(themesDir,buildPlatform,apptss),
msg: apptss + '(theme:' + theme + ' platform:' + buildPlatform + ')',
obj: { platform: true, theme: true }
});

_.each(globalStyles, function(g) {
if (path.existsSync(g.path)) {
logger.info('[' + g.msg + '] global style processing...');
compileConfig.globalStyle = CU.loadAndSortStyle(g.path, undefined,
_.extend({existingStyle: compileConfig.globalStyle},g.obj||{}));
}
});

styleOrderBase = ++CU.styleOrderCounter;
}

function optimizeCompiledCode() {
var mods = [
'builtins',
Expand Down
@@ -1,5 +1,6 @@
var CU = require('../compilerUtils'),
U = require('../../../utils'),
styler = require('../styler'),
CONST = require('../../../common/constants'),
_ = require('../../../lib/alloy/underscore')._;

Expand All @@ -26,7 +27,7 @@ function parse(node, state, args) {

// apply usual style properties
var argsObject = {
properties: CU.generateStyleParams(
properties: styler.generateStyleParams(
state.styles,
args.classes,
args.id,
Expand Down
@@ -1,6 +1,7 @@
var _ = require('../../../lib/alloy/underscore')._,
U = require('../../../utils'),
CU = require('../compilerUtils'),
styler = require('../styler'),
logger = require('../../../common/logger');

function fixDefinition(def) {
Expand Down Expand Up @@ -77,7 +78,7 @@ function parse(node, state, args) {
});

if (extras.length) {
state.extraStyle = CU.createVariableStyle(extras);
state.extraStyle = styler.createVariableStyle(extras);
}
if (!def.inViewHierarchy) {
state.parent = {};
Expand Down
3 changes: 2 additions & 1 deletion Alloy/commands/compile/parsers/Alloy.Require.js
Expand Up @@ -3,6 +3,7 @@ var path = require('path'),
_ = require('../../../lib/alloy/underscore')._,
CU = require('../compilerUtils'),
U = require('../../../utils'),
styler = require('../styler'),
CONST = require('../../../common/constants'),
moduleRoot = path.join(__dirname,'..','..','..','..'),
TYPES = ['view','widget'];
Expand Down Expand Up @@ -95,7 +96,7 @@ function parse(node, state, args) {
args.createArgs = _.extend(args.createArgs || {}, xArgs);

// Generate runtime code
code += (state.local ? 'var ' : '') + args.symbol + " = Alloy." + method + "('" + src + "'," + extraArgs + CU.generateStyleParams(
code += (state.local ? 'var ' : '') + args.symbol + " = Alloy." + method + "('" + src + "'," + extraArgs + styler.generateStyleParams(
state.styles,
args.classes,
args.id,
Expand Down
3 changes: 2 additions & 1 deletion Alloy/commands/compile/parsers/Ti.Android.MenuItem.js
@@ -1,4 +1,5 @@
var CU = require('../compilerUtils'),
styler = require('../styler'),
_ = require('../../../lib/alloy/underscore')._;

exports.parse = function(node, state) {
Expand All @@ -7,7 +8,7 @@ exports.parse = function(node, state) {

function parse(node, state, args) {
var code = '';
var styleObjectCode = CU.generateStyleParams(
var styleObjectCode = styler.generateStyleParams(
state.styles,
args.classes,
args.id,
Expand Down
5 changes: 3 additions & 2 deletions Alloy/commands/compile/parsers/Ti.Map.Annotation.js
@@ -1,4 +1,5 @@
var CU = require('../compilerUtils');
var styler = require('../styler'),
CU = require('../compilerUtils');

exports.parse = function(node, state) {
return require('./base').parse(node, state, parse);
Expand All @@ -12,7 +13,7 @@ function parse(node, state, args) {
if (latitude) { extraStyle.push(['latitude', parseFloat(latitude)]); }
if (longitude) { extraStyle.push(['longitude', parseFloat(longitude)]); }
if (extraStyle.length > 0) {
state.extraStyle = CU.createVariableStyle(extraStyle);
state.extraStyle = styler.createVariableStyle(extraStyle);
}

var code = require('./default').parse(node, {
Expand Down
3 changes: 2 additions & 1 deletion Alloy/commands/compile/parsers/Ti.Map.View.js
@@ -1,4 +1,5 @@
var _ = require('../../../lib/alloy/underscore')._,
styler = require('../styler'),
U = require('../../../utils'),
CU = require('../compilerUtils');

Expand Down Expand Up @@ -45,7 +46,7 @@ function parse(node, state, args) {
}

// Create the initial Map code
state.extraStyle = CU.createVariableStyle('annotations', arrayName);
state.extraStyle = styler.createVariableStyle('annotations', arrayName);
var mapState = require('./default').parse(node, state);
code += mapState.code;

Expand Down
5 changes: 3 additions & 2 deletions Alloy/commands/compile/parsers/Ti.UI.Button.js
@@ -1,4 +1,5 @@
var CU = require('../compilerUtils'),
var styler = require('../styler'),
CU = require('../compilerUtils'),
U = require('../../../utils');

exports.parse = function(node, state) {
Expand All @@ -9,7 +10,7 @@ function parse(node, state, args) {
// Get button title from node text, if present
var nodeText = U.XML.getNodeText(node);
if (nodeText) {
state.extraStyle = CU.createVariableStyle('title', "'" + U.trim(nodeText.replace(/'/g, "\\'")) + "'");
state.extraStyle = styler.createVariableStyle('title', "'" + U.trim(nodeText.replace(/'/g, "\\'")) + "'");
}

// Generate runtime code using default
Expand Down
3 changes: 2 additions & 1 deletion Alloy/commands/compile/parsers/Ti.UI.DashboardView.js
@@ -1,4 +1,5 @@
var _ = require('../../../lib/alloy/underscore')._,
styler = require('../styler'),
U = require('../../../utils'),
CU = require('../compilerUtils'),
CONST = require('../../../common/constants');
Expand Down Expand Up @@ -44,7 +45,7 @@ function parse(node, state, args) {
node.removeAttribute(p);
});
}
state.extraStyle = CU.createVariableStyle('data', arrayName);
state.extraStyle = styler.createVariableStyle('data', arrayName);
var dashState = require('./default').parse(node, state);
code += dashState.code;

Expand Down
5 changes: 3 additions & 2 deletions Alloy/commands/compile/parsers/Ti.UI.Label.js
@@ -1,4 +1,5 @@
var CU = require('../compilerUtils'),
var styler = require('../styler'),
CU = require('../compilerUtils'),
U = require('../../../utils');

exports.parse = function(node, state) {
Expand All @@ -9,7 +10,7 @@ function parse(node, state, args) {
// Get label text from node text, if present
var nodeText = U.XML.getNodeText(node);
if (nodeText) {
state.extraStyle = CU.createVariableStyle('text', "'" + U.trim(nodeText.replace(/'/g, "\\'")) + "'");
state.extraStyle = styler.createVariableStyle('text', "'" + U.trim(nodeText.replace(/'/g, "\\'")) + "'");
}

// Generate runtime code using default
Expand Down
3 changes: 2 additions & 1 deletion Alloy/commands/compile/parsers/Ti.UI.ListItem.js
@@ -1,5 +1,6 @@
var CU = require('../compilerUtils'),
U = require('../../../utils'),
styler = require('../styler'),
CONST = require('../../../common/constants'),
_ = require('../../../lib/alloy/underscore')._;

Expand All @@ -17,7 +18,7 @@ function parse(node, state, args) {

// Generate runtime code
code += (state.local ? 'var ' : '') + args.symbol + " = "
code += CU.generateStyleParams(
code += styler.generateStyleParams(
state.styles,
args.classes,
args.id,
Expand Down
3 changes: 2 additions & 1 deletion Alloy/commands/compile/parsers/Ti.UI.ListView.js
@@ -1,4 +1,5 @@
var _ = require('../../../lib/alloy/underscore')._,
styler = require('../styler'),
U = require('../../../utils'),
CU = require('../compilerUtils'),
CONST = require('../../../common/constants');
Expand Down Expand Up @@ -66,7 +67,7 @@ function parse(node, state, args) {
var extras = [];
if (sectionArray) { extras.push(['sections', sectionArray]); }
if (templateObject) { extras.push(['templates', templateObject]) }
if (extras.length) { state.extraStyle = CU.createVariableStyle(extras); }
if (extras.length) { state.extraStyle = styler.createVariableStyle(extras); }

// create the ListView itself
var listState = require('./default').parse(node, state);
Expand Down
3 changes: 2 additions & 1 deletion Alloy/commands/compile/parsers/Ti.UI.ScrollableView.js
@@ -1,4 +1,5 @@
var _ = require('../../../lib/alloy/underscore')._,
styler = require('../styler'),
U = require('../../../utils'),
CU = require('../compilerUtils'),
CONST = require('../../../common/constants');
Expand Down Expand Up @@ -34,7 +35,7 @@ function parse(node, state, args) {
node.removeAttribute(p);
});
}
state.extraStyle = CU.createVariableStyle('views', arrayName);
state.extraStyle = styler.createVariableStyle('views', arrayName);
var scrollState = require('./default').parse(node, state);
code += scrollState.code;

Expand Down
3 changes: 2 additions & 1 deletion Alloy/commands/compile/parsers/Ti.UI.Tab.js
@@ -1,4 +1,5 @@
var _ = require('../../../lib/alloy/underscore')._,
styler = require('../styler'),
U = require('../../../utils'),
CU = require('../compilerUtils');

Expand Down Expand Up @@ -35,7 +36,7 @@ function parse(node, state, args) {
}

// create tab with window
state.extraStyle = CU.createVariableStyle('window', windowSymbol);
state.extraStyle = styler.createVariableStyle('window', windowSymbol);
code += require('./default').parse(node, state).code;

// Update the parsing state
Expand Down
3 changes: 2 additions & 1 deletion Alloy/commands/compile/parsers/Ti.UI.TableView.js
@@ -1,4 +1,5 @@
var _ = require('../../../lib/alloy/underscore')._,
styler = require('../styler'),
U = require('../../../utils'),
CU = require('../compilerUtils'),
CONST = require('../../../common/constants');
Expand Down Expand Up @@ -93,7 +94,7 @@ function parse(node, state, args) {
var extras = [];
if (arrayName) { extras.push(['data', arrayName]); }
if (searchBarName) { extras.push(['search', searchBarName]) }
if (extras.length) { state.extraStyle = CU.createVariableStyle(extras); }
if (extras.length) { state.extraStyle = styler.createVariableStyle(extras); }

// generate the code for the table itself
if (isDataBound) {
Expand Down
3 changes: 2 additions & 1 deletion Alloy/commands/compile/parsers/Ti.UI.iPad.SplitWindow.js
@@ -1,4 +1,5 @@
var _ = require('../../../lib/alloy/underscore')._,
styler = require('../styler'),
U = require('../../../utils'),
CU = require('../compilerUtils');

Expand Down Expand Up @@ -42,7 +43,7 @@ function parse(node, state, args) {
});

// The first window is the master, the second window is the detail
state.extraStyle = CU.createVariableStyle([
state.extraStyle = styler.createVariableStyle([
['masterView', subParents[0]],
['detailView', subParents[1]]
]);
Expand Down
@@ -1,4 +1,5 @@
var _ = require('../../../lib/alloy/underscore')._,
styler = require('../styler'),
U = require('../../../utils'),
CU = require('../compilerUtils');

Expand Down Expand Up @@ -35,7 +36,7 @@ function parse(node, state, args) {
}

// create navgroup with window
state.extraStyle = CU.createVariableStyle('window', windowSymbol);
state.extraStyle = styler.createVariableStyle('window', windowSymbol);
code += require('./default').parse(node, state).code;

// Update the parsing state
Expand Down