Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added main functions, except local database write operations
- Loading branch information
1 parent
f9ddab0
commit a37976e
Showing
9 changed files
with
798 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// create the tables if required | ||
joli.models.initialize(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
Oops, something went wrong.