Skip to content

Commit

Permalink
Merge pull request #470 from skypanther/ALOY-994
Browse files Browse the repository at this point in the history
[ALOY-994] Add ActionBar tag to set actionBar properties
  • Loading branch information
skypanther committed Jul 4, 2014
2 parents e2a33cd + cddafe2 commit f0d31b4
Show file tree
Hide file tree
Showing 20 changed files with 179 additions and 1 deletion.
73 changes: 73 additions & 0 deletions Alloy/commands/compile/parsers/Ti.Android.ActionBar.js
@@ -0,0 +1,73 @@
var _ = require('../../../lib/alloy/underscore')._,
U = require('../../../utils'),
CU = require('../compilerUtils'),
CONST = require('../../../common/constants'),
logger = require('../../../logger');

exports.parse = function(node, state) {
return require('./base').parse(node, state, parse);
};

function parse(node, state, args) {
var eventObject = 'e',
code = '',
xmlStyles = {},
actionBarProperties = ['title', 'subtitle', 'backgroundImage', 'displayHomeAsUp', 'homeButtonEnabled', 'icon', 'logo', 'navigationMode', 'onHomeIconItemSelected'];

// if this isn't android, generate no code, but show a warning
var config = CU.getCompilerConfig(),
platform = config && config.alloyConfig ? config.alloyConfig.platform : undefined;
if (platform !== 'android') {
if (node.getAttribute('platform') !== 'android') {
logger.warn([
'<Activity> is only available in Android',
'To get rid of this warning, add platform="android" to your <Activity> element'
]);
}
return {
parent: {},
styles: state.styles,
code: ''
};
}

var activityTssStyles = _.filter(state.styles, function(elem) {
// generates a sorted array of styles filtered to include only elements
// associated with the activity (by ID, class, or API name)
return elem.key === node.getAttribute('id') || elem.key === node.getAttribute('class') || elem.key === node.nodeName;
});
// create an object holding all the actionBar-related properties set in the XML
_.each(actionBarProperties, function(prop) {
if(node.hasAttribute(prop)) {
xmlStyles[prop] = node.getAttribute(prop);
}
})
// to respect proper style hierarchy, take the last element in the array (which will be the highest priority)
var activityTssKey = _.isArray(activityTssStyles) ? activityTssStyles.length-1 : 0;
if(activityTssStyles[activityTssKey] && activityTssStyles[activityTssKey].style) {
_.defaults(xmlStyles, activityTssStyles[activityTssKey].style);
}
// generate the template code
if((_.filter(_.values(xmlStyles), function(val) { return val !== undefined; })).length > 0) {
if(xmlStyles.title) { code += state.parent.symbol + '.activity.actionBar.title = "' + xmlStyles.title + '";'; }
if(xmlStyles.subtitle) { code += state.parent.symbol + '.activity.actionBar.subtitle = "' + xmlStyles.subtitle + '";'; }
if(xmlStyles.backgroundImage) { code += state.parent.symbol + '.activity.actionBar.backgroundImage = "' + xmlStyles.backgroundImage + '";'; }
if(xmlStyles.displayHomeAsUp) { code += state.parent.symbol + '.activity.actionBar.displayHomeAsUp = ' + xmlStyles.displayHomeAsUp + ';'; }
if(xmlStyles.homeButtonEnabled) { code += state.parent.symbol + '.activity.actionBar.homeButtonEnabled = ' + xmlStyles.homeButtonEnabled + ';'; }
if(xmlStyles.icon) { code += state.parent.symbol + '.activity.actionBar.icon = "' + xmlStyles.icon + '";'; }
if(xmlStyles.logo) { code += state.parent.symbol + '.activity.actionBar.logo = "' + xmlStyles.logo + '";'; }
if(xmlStyles.navigationMode) { code += state.parent.symbol + '.activity.actionBar.navigationMode = ' + xmlStyles.navigationMode + ';'; }
if(xmlStyles.onHomeIconItemSelected) { code += state.parent.symbol + '.activity.actionBar.onHomeIconItemSelected = ' + xmlStyles.onHomeIconItemSelected + ';'; }
}
// Update the parsing state, and process the template
return {
parent: {},
styles: state.styles,
code: U.evaluateTemplate('Ti.Android.ActionBar.js', {
parent: state.parent.symbol || CONST.PARENT_SYMBOL_VAR,
code: code,
eventObject: eventObject,
openFunc: CU.generateUniqueId()
})
};
}
5 changes: 4 additions & 1 deletion Alloy/common/constants.js
Expand Up @@ -169,6 +169,7 @@ exports.IMPLICIT_NAMESPACES = {
// Ti.Android
Menu: NS_TI_ANDROID,
MenuItem: NS_TI_ANDROID,
ActionBar: NS_TI_ANDROID,

// Ti.Map
Annotation: NS_TI_MAP,
Expand Down Expand Up @@ -222,5 +223,7 @@ exports.IMPLICIT_NAMESPACES = {
// properties named with "on" that aren't used to signify event listeners
exports.SPECIAL_PROPERTY_NAMES = [
'onHomeIconItemSelected',
'onTintColor'
'onTintColor',
'onCreateOptionsMenu',
'onPrepareOptionsMenu'
];
11 changes: 11 additions & 0 deletions Alloy/template/Ti.Android.ActionBar.js
@@ -0,0 +1,11 @@
function <%= openFunc %>() {
<%= parent %>.removeEventListener('open', <%= openFunc %>);
if (<%= parent %>.activity) {
<%= code %>
} else {
Ti.API.warn('You attempted to access an Activity on a lightweight Window or other');
Ti.API.warn('UI component which does not have an Android activity. Android Activities');
Ti.API.warn('are valid with only windows in TabGroups or heavyweight Windows.');
}
}
<%= parent %>.addEventListener('open', <%= openFunc %>);
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/apps/testing/ALOY-994/config.json
@@ -0,0 +1,4 @@
{
"sourcemap": false,
"adapters": []
}
3 changes: 3 additions & 0 deletions test/apps/testing/ALOY-994/controllers/android/menu.js
@@ -0,0 +1,3 @@
function doClick(e) {
alert(e.source.title);
}
10 changes: 10 additions & 0 deletions test/apps/testing/ALOY-994/controllers/android/win2.js
@@ -0,0 +1,10 @@
$.win2.addEventListener('open', function() {
if($.win2.activity) {
$.win2.activity.actionBar.title = "Child window";
}
});

function doHomeIcon(e) {
$.win2.close();
}

9 changes: 9 additions & 0 deletions test/apps/testing/ALOY-994/controllers/index.js
@@ -0,0 +1,9 @@
function doClick(e) {
alert(e.source.title);
}

function openWin2(e) {
Alloy.createController('win2').getView().open();
}

$.index.open();
13 changes: 13 additions & 0 deletions test/apps/testing/ALOY-994/styles/app.tss
@@ -0,0 +1,13 @@
"Window": {
backgroundColor: '#000'
}

"Label": {
color: '#fff',
font: {
fontSize: '18dp',
fontWeight: 'bold'
},
height: Ti.UI.SIZE,
width: Ti.UI.SIZE
}
4 changes: 4 additions & 0 deletions test/apps/testing/ALOY-994/styles/index.tss
@@ -0,0 +1,4 @@
"#index": {
modal: false,
exitOnClose: true
}
3 changes: 3 additions & 0 deletions test/apps/testing/ALOY-994/styles/win2.tss
@@ -0,0 +1,3 @@
'#win2': {
modal: false
}
18 changes: 18 additions & 0 deletions test/apps/testing/ALOY-994/views/android/index.xml
@@ -0,0 +1,18 @@
<Alloy>
<Window>
<ActionBar
title="My App Title"
subtitle="App subtitle"
icon="/actionicon.png"
backgroundImage="/actionbackground.png" />
<!-- This will add an Android menu -->
<Menu title="Title from menu">
<MenuItem title="option 1" icon="/ic_menu_help.png" onClick="doClick"/>
<MenuItem title="option 2" icon="/ic_menu_home.png" onClick="openWin2"/>
</Menu>

<!-- Build the rest of your UI as usual -->
<Label>Window 1</Label>
<Label bottom="20dp">Press the menu button</Label>
</Window>
</Alloy>
6 changes: 6 additions & 0 deletions test/apps/testing/ALOY-994/views/android/menu.xml
@@ -0,0 +1,6 @@
<Alloy>
<Menu>
<MenuItem title="require 1" icon="/ic_menu_goto.png" onClick="doClick"/>
<MenuItem title="require 2" icon="/ic_menu_manage.png" onClick="doClick"/>
</Menu>
</Alloy>
15 changes: 15 additions & 0 deletions test/apps/testing/ALOY-994/views/android/win2.xml
@@ -0,0 +1,15 @@
<Alloy>
<Window id="win2">
<!-- actionbar title set in the win2.js file -->
<ActionBar
icon="/actionicon.png"
displayHomeAsUp="true"
onHomeIconItemSelected="doHomeIcon"
backgroundImage="/actionbackground.png" />
<Require src="menu"/>

<!-- Build the rest of your UI as usual -->
<Label>Window 2</Label>
<Label bottom="20dp">Press the menu button</Label>
</Window>
</Alloy>
6 changes: 6 additions & 0 deletions test/apps/testing/ALOY-994/views/index.xml
@@ -0,0 +1,6 @@
<Alloy>
<Window>
<!-- ActionBar/Menu code is in views/android/index.xml -->
<Label>ActionBar/Menu only supported on Android</Label>
</Window>
</Alloy>

0 comments on commit f0d31b4

Please sign in to comment.