Appcelerator Titainium Activity Indicator During Json Load - appcelerator

how to give activity indicator while load json??here my code,,
i want to give activity indicator during my apps reading json :)
data will load with 20 section length,,(from json) and when my apps try to read it, i want to give activity indicator that make user not waiting in a blank screen :)
// Create variable "win" to refer to current window
var win = Titanium.UI.currentWindow;
///window startup
///////////
function loadTweets() {
// Empty array "rowData" for our tableview
var rowData = [];
// Create our HTTP Client and name it "loader"
var loader = Ti.Network.createHTTPClient({
onerror : function(e) {
alert('Koneksi Time Out');
},
timeout : 30000
});
// Sets the HTTP request method, and the URL to get data from
loader.open("GET", "http://www.sportku.com/api/get_latest_news/" + win.id);
// Runs the function when the data is ready for us to process
loader.onload = function() {
var tweets = eval('(' + this.responseText + ')');
for(var i = 0; i < tweets.count; i++) {
var tweet = tweets.posts[i].excerpt;
// The tweet message
var user = tweets.posts[i].title;
// The screen name of the user
var avatar = tweets.posts[i].thubmnail;
// The profile image
var url = tweets.posts[i].url;
// Create a row and set its height to auto
var row = Titanium.UI.createTableViewRow({
height : 'auto'
});
// Create the view that will contain the text and avatar
var post_view = Titanium.UI.createView({
height : 'auto',
layout : 'vertical',
top : 5,
right : 5,
bottom : 5,
left : 5
});
// Create image view to hold profile pic
var av_image = Titanium.UI.createImageView({
image : avatar, // the image for the image view
top : 0,
left : 0,
height : 58,
width : 58
});
post_view.add(av_image);
// Create the label to hold the screen name
var user_lbl = Titanium.UI.createLabel({
text : user,
left : 64,
width : 226,
top : -58,
bottom : 2,
height : 44,
textAlign : 'left',
color : 'black',
font : {
fontFamily : 'Arial',
fontWeight : 'bold',
fontSize : 12
}
});
post_view.add(user_lbl);
// Create the label to hold the tweet message
var tweet_lbl = Titanium.UI.createLabel({
text : tweet,
left : 64,
top : 0,
bottom : 2,
height : 'auto',
width : 226,
textAlign : 'left',
font : {
fontSize : 11
}
});
post_view.add(tweet_lbl);
var urls = Titanium.UI.createLabel({
text : url,
});
post_view.add(urls);
// Add the post view to the row
row.add(post_view);
row.hasChild = true;
//row.hasChild=true;
// Give each row a class name
row.className = "item" + i;
// Add row to the rowData array
rowData[i] = row;
post_view.addEventListener('click', function(e) {
var url_output = e.row.children[0].children[3].text;
//alert(url_output);
var webview = Titanium.UI.createWebView({
url : url_output
});
var window = Titanium.UI.createWindow({
backgroundColor : "#fff",
barImage : 'image/navbar.png',
titleImage : 'image/logo.png',
});
window.add(webview);
window.open({
modal : true,
modalTransitionStyle : Ti.UI.iPhone.MODAL_TRANSITION_STYLE_FLIP_HORIZONTAL,
modalStyle : Ti.UI.iPhone.MODAL_TRANSITION_STYLE_FLIP_HORIZONTAL,
});
var buttonImage = [{
image : 'image/share.png',
width : 30,
height : 30
}]
var back = Ti.UI.createButtonBar({
labels : buttonImage,
backgroundColor : 'black',
style : Titanium.UI.iPhone.SystemButtonStyle.BAR
});
back.addEventListener('click', function() {
window.close();
});
window.leftNavButton = back;
});
}
// Create the table view and set its data source to "rowData" array
var tableView = Titanium.UI.createTableView({
data : rowData
});
//Add the table view to the window
win.add(tableView);
};
// Send the HTTP request
loader.send();
}
loadTweets();
i've try to give my code, but always error :),,

What you should do is create the activity indicator before anything else. Activity indicators don't need to be added to a window, you just need to call the show()method, so do that prior to executing the xhr.send() method. than the last line of your onLoad function should execute the indicator.hide() method.

Related

Replicate event pinch from a div to openlayer map

Good afternoon,
I have a div over the ol3 map with one list of marker. When the user clic on a marker, the center's map is updated with the coordinate of this marker.
I tried that the user have the possibility to zoom on the map when he "pinch" on the layer on top.
I successed to intercept the event on the "layerOnTop" and replicate it to the map layer. With console.log, I saw that the job is correctly done but there is any reaction on the map.
You can see the code : http://jsfiddle.net/2ek4j3a4/
var center = ol.proj.transform([4.90756, 45.5172], 'EPSG:4326', 'EPSG:3857');
var map = new ol.Map({
layers: [new ol.layer.Tile({source: new ol.source.OSM()})],
target: 'map',
controls: ol.control.defaults({attributionOptions: ({ collapsible: false})}),
view: new ol.View({center: center,zoom: 12})
});
$('#layerOnTop').on("touchmove", function(event) {
var scale = event.originalEvent.touches;
if (scale.length==2) {
event.preventDefault();
var bottomEvent = new $.Event("touchmove");
bottomEvent.pageX = event.pageX;
bottomEvent.pageY = event.pageY;
$("#map").trigger(bottomEvent);
}
});
$('#map').on("touchmove", function(event) {console.log('ok')})
I did my test with an Android phone.
Somebody have an idea, please ?
I finally found :) There was 2 errors in my code :
1) target should be the canvas element in div#map and not #map himself
2) I fixed the touch identifier by Date.now() and add +1 for the second, it was certainely too big
See below the code if one day somebody search ...
$('#layerOnTop').on("touchstart touchmove touchend", function(event) {
view = document.getElementById('map').ownerDocument.defaultView;
if (event.originalEvent.touches.length==2) {
var target = document.getElementById('map').getElementsByTagName('canvas')[0];
var pageX1 = event.originalEvent.touches[0].pageX;
var pageY1 = event.originalEvent.touches[0].pageY;
var pageX2 = event.originalEvent.touches[1].pageX;
var pageY2 = event.originalEvent.touches[1].pageY;
var touch1 = document.createTouch(view, target, 0, pageX1, pageY1, 0, 0);
var touch2 = document.createTouch(view, target, 1, pageX2, pageY2, 0, 0);
var touchList = document.createTouchList(touch1, touch2);
var touchEvent = new TouchEvent(event.type, {cancelable: true, bubbles: true, touches: touchList, targetTouches: touchList, changedTouches: touchList})
target.dispatchEvent(touchEvent);
}
});
http://jsfiddle.net/2ek4j3a4/7/

creating dropdown using table view in titanium

i am creating a form containing a dropdown with default value and options to choose. and form should have reset button to clear choosen option from dropdownor tableview.when i click on dropdown it should expand containing options to select and when user selects option it is collapsed.and if reset button is pressed dropdown value is reset to default value.
i searched in google and i got some code below in this code when i executed i am just getting a table view with expanded ,when i select a row it still remains expanded.
var win = Ti.UI.createWindow({
title: 'Title goes here',
backgroundColor: '#123456'
});
var checkFlag = true;
var picker = Ti.UI.createPicker({ width:110,left:190,top:150});
var data = [];
data.push(Titanium.UI.createPickerRow({title:'Karnataka'}));
data.push(Titanium.UI.createPickerRow({title:'tamilnadu'}));
data.push(Titanium.UI.createPickerRow({title:'kerala'}));
data.push(Titanium.UI.createPickerRow({title:'goa'}));
picker.add(data);
win.add(picker);
var resetbtn = Ti.UI.createButton({
top : '100',
width : '50',
height : '35',
title : 'Reset'
});
win.add(resetbtn);
resetbtn.addEventListener('click', function(){
Ti.API.info('checkFlag = ' + checkFlag);
if(checkFlag) {
picker.hide();
checkFlag = false;
} else {
picker.show();
checkFlag = true;
}
});
win.open();
and is it good to create dropdown using table view or picker and how to set to default value on pressing reset button.please help me i am new to titanium.
It seems like you are getting confused in TableView and dropdown. These two are different elements and have different usage.
I will suggest to use Titanium.UI.Picker to create dropdown list. Also to make some value of the picker to be selected programmatically you can use setSelectedRow() method of picker.
Following code will help to start things :
Ti.UI.backgroundColor = 'white';
var win = Ti.UI.createWindow({
exitOnClose: true,
layout: 'vertical'
});
var picker = Ti.UI.createPicker({
top:50
});
var data = [];
data[0]=Ti.UI.createPickerRow({title:'Bananas'});
data[1]=Ti.UI.createPickerRow({title:'Strawberries'});
data[2]=Ti.UI.createPickerRow({title:'Mangos'});
data[3]=Ti.UI.createPickerRow({title:'Grapes'});
picker.add(data);
picker.selectionIndicator = true;
win.add(picker);
win.open();
var resetbtn = Ti.UI.createButton({
bottom: '10',
width : '50',
height : '35',
title : 'Reset'
});
win.add(resetbtn);
resetbtn.addEventListener('click', resetToDefault);
function resetToDefault() {
picker.setSelectedRow(0, 0, false); // select Bananas, i.e index 0
}

JavaScript- calling functions from within functions

I am making an HTML5 canvas based game using JavaScript. The game is just a simple one, where a number of images are displayed on the canvas, along with a number of description boxes. The user is required to drag and drop each image to its corresponding description box.
Currently, I have the canvas displaying the images and description boxes. These are displayed straight away, as soon as the page loads, however, I want to add a 'start button' which the user will be required to click before the images and description boxes etc are displayed and they start playing the game.
The code for my page currently looks like this:
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
canvas{
border: 1px solid #9C9898;
background:#F5F5F5;
}
</style>
<div id="container"></div>
<script src="kinetic.js"></script>
<script src="drawdescriptionboxes.js"></script>
<script src="drawLevelOneElements.js"></script>
<script src="startGameDrawGameElementsDrawStartButton.js"></script>
<script>
/*Add the game elements' global variables */
var currentLevel = 1;
var totalLevels = 3;
var currentScore = 0;
var currentScorePositionX = 950;
var currentScorePositionY = 10;
/*Add code to draw images to random locations here */
//var imageX = Math.floor(Math.random()*950);
//var imageY = Math.floor(Math.random()*450);
var stage = new Kinetic.Stage({
container: "container",
width: 1000,
height: 500
});
var imagesLayer = new Kinetic.Layer();
var canvas = imagesLayer.getCanvas();
var context = canvas.getContext("2d");
console.log("Foo ");
/*Load the images from the HTML into the JavaScript */
function loadImages(sources, callback){
var imagesDir = "";
var images = {};
var loadedImages = 0;
var numImages = 0;
//console.log("length " + sources.length);
for (var src in sources){
numImages++;
}
//console.log("Num Images " + numImages);
var index=0;
console.log("length " + sources.length);
for (index=0;index < numImages ;index++){
console.log(index);
images[index] = new Image();
images[index].src = sources[index];
console.log("Adding " + sources[index]);
callback(images[index]);
console.log("sources array length = " + sources.length);
}
stage.add(imagesLayer); // should only be added once!!
}
/*Function to check whether the item being dragged is near its description box */
function isNearDescriptionBox(itemImage, descriptionBox){
var ii = itemImage;
var db = descriptionBox;
if(ii.attrs.x > db.x - 20 && ii.attrs.x < db.x + 20 && ii.attrs.y > db.y - 20 && ii.attrs.y < db.y +20){
return true;
}else{
return false;
}
}
/* This function draws the game elements */
function drawGameElements(){
/* Draw a line for the 'score bar'. */
context.moveTo(0, 25);
context.lineTo(1000, 25);
context.stroke();
/* Draw current level/ total levels on the left, and current score on the right. */
context.font = "11pt Calibri"; /* Text font & size */
context.strokeStyle = "black"; /* Font colour */
context.strokeText(currentLevel + "/" + totalLevels, 10, 15);
context.strokeText(currentScore, 750, 15);
}
function initStage(images){
var stage = new Kinetic.Stage({
container: "container",
width: 1000,
height: 500
});
var descriptionLayer = new Kinetic.Layer();
//var imagesLayer = new Kinetic.Layer();
var allImages = [];
var currentScore = 0;
var descriptionBoxes = {
assetsDescriptionBox: {
x: 70,
y: 400
},
liabilitiesDescriptionBox: {
x: 300,
y: 400
},
incomeDescriptionBox: {
x: 530,
y: 400
},
expenditureDescriptionBox: {
x: 760,
y: 400
},
};
/*Code to detect whether image has been dragged to correct description box */
for (var key in sources){
/*Anonymous function to induce scope */
(function(){
var privateKey = key;
var imageSource = sources[key];
/*Check if image has been dragged to the correct box, and add it to that box's
array and remove from canvas if it has */
canvasImage.on("dragend", function(){
var descriptionBox = descriptionBoxes[privateKey];
if(!canvasImage.inRightPlace && isNearDescriptionBox(itemImage, descriptionBox)){
context.remove(canvasImage);
/*Will need to add a line in here to add the image to the box's array */
}
})
})();
}
}
function drawImage(imageObj) {
//var layer = new Kinetic.Layer();
//var imageX = Math.floor(Math.random()*950);
//var imageY = Math.floor(Math.random()*450);
/*Got the code from generating random coordinates from:
http://stackoverflow.com/questions/4959975/random-between-two-numbers-in-javascript*/
function randomPosition(from, to){
return Math.floor(Math.random()*(to-from+1)+from);
}
var canvasImage = new Kinetic.Image({
image: imageObj,
width: 50,
height: 50,
// puts the image in teh middle of the canvas
x: randomPosition(0, 950), //imageX, //stage.getWidth() / 2 - 50 / 2,
y: randomPosition(30, 350), //imageY, //stage.getHeight() / 2 - 50 / 2,
draggable: true
});
// add cursor styling
canvasImage.on('mouseover', function() {
document.body.style.cursor = 'pointer';
});
canvasImage.on('mouseout', function() {
document.body.style.cursor = 'default';
});
imagesLayer.add(canvasImage);
}
/*This code loads the images to the canvas when the browser window loads */
window.onload = function(){
var sources = [];
sources[0] = document.getElementById("building").src,
sources[1] = document.getElementById("chair").src,
sources[2] = document.getElementById("drink").src,
sources[3] = document.getElementById("food").src,
sources[4] = document.getElementById("fridge").src,
sources[5] = document.getElementById("land").src,
sources[6] = document.getElementById("money").src,
sources[7] = document.getElementById("oven").src,
sources[8] = document.getElementById("table").src,
sources[9] = document.getElementById("van").src,
sources[10] = document.getElementById("burger").src,
sources[11] = document.getElementById("chips").src,
sources[12] = document.getElementById("drink").src,
sources[13] = document.getElementById("franchiseFee").src,
sources[14] = document.getElementById("wages").src,
sources[15] = document.getElementById("admin").src,
sources[16] = document.getElementById("cleaners").src,
sources[17] = document.getElementById("electricity").src,
sources[18] = document.getElementById("insurance").src,
sources[19] = document.getElementById("manager").src,
sources[20] = document.getElementById("rates").src,
sources[21] = document.getElementById("training").src,
sources[22] = document.getElementById("water").src,
sources[23] = document.getElementById("burger").src,
sources[24] = document.getElementById("chips").src,
sources[25] = document.getElementById("drink").src,
sources[26] = document.getElementById("creditors").src,
sources[27] = document.getElementById("electricity").src,
sources[28] = document.getElementById("food").src,
sources[29] = document.getElementById("hirePurchase").src,
sources[30] = document.getElementById("loan").src,
sources[31] = document.getElementById("overdraft").src,
sources[32] = document.getElementById("payeTax").src,
sources[33] = document.getElementById("tax").src;
drawStartButton();
loadImages(sources, drawImage);
//drawGameElements();
//drawDescriptionBoxes();
};
I also have a hidden section in the <body></body> of the page, where I've loaded all of the images to be used into the HTML first, before manipulating them using the JS.
Currently, when I view the page in a browser, all of the images from the sources array are displayed on the canvas in random locations, along with the description boxes, score bar, and start button.
However, what I want to happen, is that when the page initially loads, only the start button is displayed on the canvas, and then when the user clicks this, everything else is displayed.
Presumably, to do this, I would want to delete/ comment out the calls to all of the functions except drawStartButton() at the end of the windows.onload = function(){...} function, and then have calls to the other function in an onmousedown event attached to the start button?
However, for some reason, when I comment out the call to loadImages(sources, drawImage); at the end of this function, and then view my page in the browser again, the canvas is no longer displayed on the page. I would have expected the canvas to still be there, just without the images displayed on it. Does anyone have any idea why this isn't happening, and how I can put it right?
The code for my loadImages(sources, drawImage); function is:
function loadImages(sources, callback){
var imagesDir = "";
var images = {};
var loadedImages = 0;
var numImages = 0;
//console.log("length " + sources.length);
for (var src in sources){
numImages++;
}
//console.log("Num Images " + numImages);
var index=0;
console.log("length " + sources.length);
for (index=0;index < numImages ;index++){
console.log(index);
images[index] = new Image();
images[index].src = sources[index];
console.log("Adding " + sources[index]);
callback(images[index]);
console.log("sources array length = " + sources.length);
}
stage.add(imagesLayer); // should only be added once!!
}
I can't see why removing a call to this function would stop the canvas from being displayed on the page... any ideas?
The last line of the function,
stage.add(imagesLayer); // should only be added once!!
adds the layer to the stage. While the stage is a container (div, for example),
the layer is the actual canvas.
If you don't call loadImages(), the layer never gets added to the DOM, because that line never runs.

struts 2 jquery grid - editGridRow on double click on subgrid row not showing data

I am using struts2 jquery grid 3.1.0 version. I have requirement to display the row details on double click. I use the following code to display the edited row. But the dialog shows up an empty dialog on double click. Any help on this would be appreciated. The code is attached below
var parentSelRow = jQuery(gridFn.$id).jqGrid('getGridParam',
'selrow');
var grid = $(gridFn.$id);
var gridSub = $(gridFn.$id + "_" + parentSelRow + "_table");
gridSub.dblclick(function(e) {
var td = e.target;
var ptr = $(td, gridSub[0].rows).closest("tr.jqgrow");
if ($(ptr).length === 0) {
return false;
}
var ri = ptr[0].rowIndex;
var ci = $.jgrid.getCellIndex(td);
var rowId = $(ptr).attr("id");
alert(rowId);
var selectedRow = gridSub.jqGrid('getGridParam', ('selrow'));
if (selectedRow != null && selectedRow.length > 0) {
gridSub.jqGrid('editGridRow', rowId, {
editCaption : "View Record",
width : 660,
recreateForm : true,
recreateFilter : true,
viewPagerButtons : true,
onInitializeForm : gridFn.initializeViewForm,
beforeShowForm : gridFn.beforeViewForm,
afterShowForm : gridFn.afterViewForm,
onClose : gridFn.onCloseViewForm,
closeAfterAdd : true,
closeOnEscape : true,
errorTextFormat : gridFn.errorTextFormat}); }

titanium mobile:retrieve row title from tableview and its display in next window issue

Hello friends,
I am developing an app which displays category types using custom cell in tableview and then selects a row its go to detail which selected category type but I'm facing a problem that is I can't get row title in the next window. So please give me idea how to solve it.
Ti.include('PlacesTypeCustomCell.js');
var currentWindow = Titanium.UI.currentWindow;
var tableData = [];
// Create table
var tableData = new PlacesTypeCells();
var myTableView = Titanium.UI.createTableView
({
data:tableData,
top:45,
height:368
});
currentWindow.add(myTableView);
myTableView.addEventListener('click',function(e)
{
var index = e.index;
Ti.API.log('Row at index:'+index);
Titanium.App.Properties.setInt('index',index);
v//get each row title from tableview
var pageTitle = Titanium.App.Properties.setString('title',e.rowData.pageTitle);
Ti.API.log('Page Title:'+pageTitle);
var pageAddress = Titanium.App.Properties.setString('address',e.rowData.pageAddress);
Ti.API.log('Page Address:'+pageAddress);
var win = Titanium.UI.createWindow
({
url:'PlacesList.js',
title:'Place List'
});
Titanium.UI.currentTab.open(win,{animated:true});
});
//Custom Cell PlacesTypeCustomCell
var tableData=[];
var loader = Titanium.Network.createHTTPClient();
var url = "https://maps.googleapis.com/maps/api/place/search/json?";
url = url + "location=" + lat + ',' + lon;
url = url + "&radius=" + radius;
url = url + "&name=" + name;
url = url + "&sensor=" + sensor;
url = url + "&key=" + key;
Ti.API.info(url);
// Sets the HTTP request method, and the URL to get data from
loader.open("GET",url);
// Create our HTTP Client and name it "loader"
// Runs the function when the data is ready for us to process
loader.onload = function()
{
var obj = JSON.parse(this.responseText);
Ti.API.log(obj);
var results = obj.results;
Ti.API.log(results);
for (var i = 0; i < results.length; i++)
{
categoryName = obj.results[i].name;
reference = obj.results[i].reference;
Ti.API.log('Refernce:'+reference);
getDetailsData();
// Create a row and set its height to auto
row = Titanium.UI.createTableViewRow({height:'78'});
var placeImage = Titanium.UI.createImageView
({
image:'../iphone/appicon.png',
width:70,
height:50,
top:12,
left:5
});
// Create the label to hold the tweet message
var nameLabel = Titanium.UI.createLabel({
//text:name,
left:80,
top:5,
height:'auto',
width:200,
textAlign:'left',
font:{fontSize:12}
});
// Create the label to hold the tweet message
addressLabel = Titanium.UI.createLabel({
left:80,
top:25,
height:'auto',
width:200,
textAlign:'left',
font:{fontSize:14}
});
var arrowImage = Ti.UI.createImageView
({
image:'../iphone/appicon.png',
width:20,
height:20,
left:280,
top:30
});
nameLabel.text = categoryName;
getDetailsData(addressLabel);
row.add(placeImage);
row.add(nameLabel);
row.add(addressLabel);
row.add(arrowImage);
tableData[i] = row;
//set page title for each row
row.pageTitle = tableData[i];
row.pageAddress = tableData[i];
}
tableView.setData(tableData);
};
//PlaceList.js
var currentWindow = Titanium.UI.currentWindow;
//retrive index value
var index = Titanium.App.Properties.getInt('index');
Ti.API.log('NextView Index:'+index);
var title = Titanium.App.Properties.getString('title');
Ti.API.log('CheckInView Title:'+title);
var address = Titanium.App.Properties.getString('address');
Ti.API.log('CheckInView Address:'+address);
My problem is that I can't get row title in next window but I get index value of that row so please give me idea how to fetch it.
Just do this after the following line:
var row = Titanium.UI.createTableViewRow({height:'auto'});
Set a title as property of your row like this:
row.pageTitle = tableData[i];
And in click event you can get this property like this:
myTableView.addEventListener('click',function(e)
{
e.rowData.pageTitle
}

Resources