Skip to content

Commit

Permalink
added main functions, except local database write operations
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierlacot committed Feb 14, 2011
1 parent f9ddab0 commit a37976e
Show file tree
Hide file tree
Showing 9 changed files with 798 additions and 9 deletions.
29 changes: 20 additions & 9 deletions Resources/app.js
@@ -1,14 +1,16 @@
Ti.include('js/lib/vendor/redux/redux.js');
Ti.include('redux.js');
includeGlobal('js/lib/vendor/joli.js/joli.js');


includeGlobal('js/lib/model/models.js');
Ti.include('js/lib/initialize.js');
Ti.include('js/lib/install.js');
includeGlobal('js/lib/xavcc.js');

// create tab group
var tabGroup = Titanium.UI.createTabGroup();

// add windows and tabs
var win1 = Titanium.UI.createWindow({
url:'javascript/views/encode.js',
url:'js/views/encode.js',
title:'Create a new short url',
backgroundImage:'images/background.png'
});
Expand All @@ -19,7 +21,7 @@ var tab1 = Titanium.UI.createTab({
});

var win2 = Titanium.UI.createWindow({
url:'javascript/views/decode.js',
url:'js/views/decode.js',
title:'Decode a short url',
backgroundImage:'images/background.png'
});
Expand All @@ -30,7 +32,7 @@ var tab2 = Titanium.UI.createTab({
});

var win3 = Titanium.UI.createWindow({
url:'javascript/views/history.js',
url:'js/views/history.js',
title:'Short urls history',
backgroundColor:'#fff'
});
Expand All @@ -41,13 +43,13 @@ var tab3 = Titanium.UI.createTab({
});

var win4 = Titanium.UI.createWindow({
url:'javascript/views/settings.js',
title:'Configure',
url:'js/views/settings.js',
title:'Settings',
backgroundImage:'images/background.png'
});
var tab4 = Titanium.UI.createTab({
icon:'images/icons/config.png',
title:'Config',
title:'Settings',
window:win4
});

Expand All @@ -57,6 +59,15 @@ tabGroup.addTab(tab2);
tabGroup.addTab(tab3);
tabGroup.addTab(tab4);

tabGroup.addEventListener('focus', function(e) {
if (Titanium.Platform.name != 'android') {
if (e.index < 2) {
Titanium.UI.iPhone.statusBarHidden = true;
} else {
Titanium.UI.iPhone.statusBarHidden = false;
}
}
});

// open tab group
tabGroup.open();
22 changes: 22 additions & 0 deletions Resources/js/lib/initialize.js
@@ -0,0 +1,22 @@
/**
* Allows to initialize the application, in case it has not been made before.
*/

if (!Titanium.App.Properties.hasProperty('site_name')) {
Titanium.App.Properties.setString('site_name', 'xav.cc');
Titanium.App.Properties.setString('site_url', 'http://xav.cc/');
}

Titanium.App.Properties.setString('api_url', 'http://api.xav.cc/');

if (!Titanium.App.Properties.hasProperty('auto_copy')) {
Titanium.App.Properties.setBool('auto_copy', true);
}

if (!Titanium.App.Properties.hasProperty('auto_paste')) {
Titanium.App.Properties.setBool('auto_paste', true);
}

if (!Titanium.App.Properties.hasProperty('use_history')) {
Titanium.App.Properties.setBool('use_history', true);
}
2 changes: 2 additions & 0 deletions Resources/js/lib/install.js
@@ -0,0 +1,2 @@
// create the tables if required
joli.models.initialize();
21 changes: 21 additions & 0 deletions Resources/js/lib/model/models.js
@@ -0,0 +1,21 @@
// open database
joli.connection = new joli.Connection('xavcc');

var models = (function() {
var m = {};

// shorturl model
m.shorturl = new joli.model({
table: 'shorturl',
columns: {
id: 'INTEGER',
shorturl: 'TEXT',
longurl: 'TEXT',
viewcount: 'INTEGER',
title: 'TEXT',
position: 'INTEGER'
}
});

return m;
})();
148 changes: 148 additions & 0 deletions Resources/js/lib/xavcc.js
@@ -0,0 +1,148 @@
var xavcc = (function() {
var api = {};

// create http client
var client = Titanium.Network.createHTTPClient();
client.timeout = 10000; // 10 s. timeout


api.decode = function(alias) {
var escapedUrl = api.encodeUrl(alias);
var url = Titanium.App.Properties.getString('api_url', 'http://api.xav.cc/');
url = url + 'simple/decode?alias=' + escapedUrl;

client.onreadystatechange = function() {
Titanium.API.log('this.readyState: ' + this.readyState, 'info');
if (this.readyState == 4) {
Ti.App.fireEvent('xavcc.decode.result', { result: this.responseText });
}
};

if (Titanium.Platform.name != 'android') {
client.open('GET', url, true);
} else {
client.open('GET', url, false);
}

this.showIndicator();
client.send(null);
};


api.encode = function(longurl, alias) {
var escapedUrl = api.encodeUrl(longurl);
var url = Titanium.App.Properties.getString('api_url', 'http://api.xav.cc/');
url = url + 'simple/encode?url=' + escapedUrl;

if (alias) {
url = url + '&alias=' + api.encodeUrl(alias);
}

client.onreadystatechange = function() {
Titanium.API.log('this.readyState: ' + this.readyState, 'info');
if (this.readyState == 4) {
Ti.App.fireEvent('xavcc.encode.result', { result: this.responseText });
}
};

if (Titanium.Platform.name != 'android') {
client.open('GET', url, true);
} else {
client.open('GET', url, false);
}

this.showIndicator();
client.send(null);
};


api.encodeUrl = function(url) {
if (!url) {
return '';
}

return encodeURIComponent(url).replace( /%2F/g, '/');
};


api.hideIndicator = function() {
if ((Titanium.Platform.name != 'android') && Titanium.UI.currentWindow) {
Titanium.UI.currentWindow.setToolbar(null,{animated:true});
}
};


api.showIndicator = function() {
var toolActInd;

if ((Titanium.Platform.name != 'android') && Titanium.UI.currentWindow) {
toolActInd = Titanium.UI.createActivityIndicator();
toolActInd.style = Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN;
toolActInd.font = {fontFamily:'Helvetica Neue', fontSize:15,fontWeight:'bold'};
toolActInd.color = 'white';
toolActInd.message = 'Chargement...';
Titanium.UI.currentWindow.setToolbar([toolActInd],{animated:true});
toolActInd.show();
}

if ((Titanium.Platform.name == 'android') && Titanium.UI.currentWindow) {
toolActInd = Titanium.UI.createActivityIndicator({
bottom:10,
height:50,
width:10,
style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
});
Titanium.UI.currentWindow.add(toolActInd);
toolActInd.show();
toolActInd.message = 'Loading...';

setTimeout(function() {
toolActInd.hide();
}, 1000);
}
};


api.showResponse = function(label, shorturl) {
var length = Math.max(8, Math.min(30, shorturl.length));
var size = Math.ceil(12 + (30 - length) * (15 / 10));
label.font = {'fontSize':size};
label.text = shorturl;
};


api.strpos = function (haystack, needle) {
var i = (haystack + '').indexOf(needle, 0);
return i === -1 ? false : i;
};


api.trim = function(str) {
if (!str) {
return str;
}

str = str.replace(/^\s+/, '');

for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}

return str;
};


api.ucfirst = function(text) {
if (!text) {
return text;
}

return text.charAt(0).toUpperCase() + text.substr(1);
};


return api;
})();
109 changes: 109 additions & 0 deletions Resources/js/views/decode.js
@@ -0,0 +1,109 @@
Ti.include('../../redux.js');
var win = Titanium.UI.currentWindow;
var clipboard = require('com.xavcc.Clipboard');

if (Titanium.Platform.name != 'android') {
win.hideNavBar(); // full screen app
}



// label and first field
var l1a = Titanium.UI.createLabel({
text:'Short url to decode*',
width:250,
height:35,
top:90,
left:30,
color:'#fff',
textAlign:'left'
});
var l1b = Titanium.UI.createLabel({
text:Titanium.App.Properties.getString('site_url', 'http://xav.cc/'),
width:100,
height:35,
top:120,
left:30,
color:'#fff',
textAlign:'left'
});
var tf1 = Titanium.UI.createTextField({
color:'#192578',
height:35,
top:120,
left:130,
width:150,
autocapitalization: Titanium.UI.TEXT_AUTOCAPITALIZATION_NONE,
returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});

// second label, for the result
var l2 = Titanium.UI.createLabel({
text:'',
width:250,
height:90,
top:300,
color:'#fff',
textAlign:'left',
font:{'fontSize':17}
});

// submit button
var b1 = Titanium.UI.createButton({
title:'Decode this short url',
height:30,
width:250,
left:30,
top:200
});

// define logic
var doDecode = function() {
var alias = xavcc.trim(tf1.value);

if (!alias) {
alert('Please write an alias!');
tf1.focus();
return;
}

xavcc.decode(alias);
tf1.blur();
};

// add event listeners
tf1.addEventListener('return', doDecode);
b1.addEventListener('click', doDecode);

Ti.App.addEventListener('xavcc.change_site_url', function(event) {
l1b.text = event.title;
});

Ti.App.addEventListener('xavcc.decode.result', function(event) {
xavcc.hideIndicator();
var url = event.result;

if (url) {
if (url.indexOf('http://') == 0) {
xavcc.showResponse(l2, url);

if (Titanium.App.Properties.getBool('auto_copy', true)) {
// put the shortened url in the clipboard
clipboard.setText(url);
}
} else if (url.length > 0) {
// something went wrong : display an alert message
alert(url);
} else {
alert('no response found');
}
}
});


win.add(l1a);
win.add(l1b);
win.add(tf1);
win.add(l2);
win.add(b1);

0 comments on commit a37976e

Please sign in to comment.