Ejemplo n.º 1
0
exports.twitter = function(args) {
	args = parseArgs(args);

	// Native iOS Dialog
	if (
		OS_IOS && args.useApp !== true
		&& (dkNappSocial != null && dkNappSocial.isTwitterSupported())
	) {
		dkNappSocial.twitter({
			text: args.text,
			image: args.image,
			url: args.url
		});
		return true;
	}

	// iOS Native
	var nativeIOSURL = 'twitter://post' + Util.buildQuery({ message: args.fullText });
	if (OS_IOS && Ti.Platform.canOpenURL(nativeIOSURL)) {
		Ti.Platform.openURL(nativeIOSURL);
		return true;
	}

	// Fallback
	Ti.Platform.openURL('http://www.twitter.com/intent/tweet' + Util.buildQuery({
		 text: args.text,
		 url: args.url
	}));
};
exports.twitter = function(args) {
	args = parseArgs(args);
	GA.social('twitter', 'share', args.url);

	var text = (args.tweetText || args.text);
	var textWithUrl = text;
	if (args.url != null) {
		textWithUrl = text ? (text + ' (' + args.url + ')') : args.url;
	}

	// Native iOS Dialog
	if (OS_IOS && (dkNappSocial != null && dkNappSocial.isTwitterSupported())) {
		dkNappSocial.twitter({
			text: text,
			image: args.image,
			url: args.url
		});
		return true;
	}

	// iOS Tweetbot App
	if (OS_IOS) {
		var tweetbotNativeURL = 'tweetbot:///post' + Util.buildQuery({
			text: textWithUrl
		});
		if (Ti.Platform.canOpenURL(tweetbotNativeURL)) {
			Ti.Platform.openURL(tweetbotNativeURL);
			return true;
		}
	}

	// iOS Twitter App
	if (OS_IOS) {
		var twitterNativeURL = 'twitter://post' + Util.buildQuery({
			message: textWithUrl
		});
		if (Ti.Platform.canOpenURL(twitterNativeURL)) {
			Ti.Platform.openURL(twitterNativeURL);
			return true;
		}
	}

	// Fallback
	Ti.Platform.openURL('http://www.twitter.com/intent/tweet' + Util.buildQuery({
		 text: text,
		 url: args.url
	}));
};
Ejemplo n.º 3
0
exports.facebook = function(args) {
	args = parseArgs(args);

	// Native iOS dialog
	if (
		OS_IOS && args.useApp !== true
	 	&& (dkNappSocial != null && dkNappSocial.isFacebookSupported())
	 	&& false === /https?\:\/\/(www\.)?facebook\.com/.test(args.url) // iOS share dialog doesn't share Facebook links
	) {
		dkNappSocial.facebook({
			text: args.text,
			image: args.image,
			url: args.url
		});
		return true;
	}

	// SDK
	if (FB != null && _.isFunction(FB.share)) {
		FB.share({
			url: args.url,
			title: args.title,
			description: args.description
		});
		return true;
	}

	// Fallback
	Ti.Platform.openURL('http://www.facebook.com/dialog/share' + Util.buildQuery({
		app_id: Ti.App.Properties.getString('ti.facebook.appid'),
		display: 'touch',
		redirect_uri: Ti.App.url,
		href: args.url
	}));
};
Ejemplo n.º 4
0
exports.googleplus = function(args) {
	args = parseArgs(args);

	Ti.Platform.openURL('https://plus.google.com/share' + Util.buildQuery({
		url: args.url
	}));
};
Ejemplo n.º 5
0
function HTTPRequest(opt) {
	if (opt.url == null) {
		throw new Error('HTTP.Request: URL not set');
	}

	this.opt = opt;

	// if the url is not matching a protocol, assign the base URL
	if (true === /\:\/\//.test(opt.url)) {
		this.url = opt.url;
	} else {
		this.url = HTTP.config.base.replace(/\/$/, '') + '/' + opt.url.replace(/^\//, '');
	}

	this.method = opt.method != null ? opt.method.toUpperCase() : 'GET';
	this.headers = _.extend(HTTP.getHeaders(), opt.headers);
	this.timeout = opt.timeout != null ? opt.timeout : HTTP.config.timeout;

	this.onSuccess = _.isFunction(opt.success) ? opt.success : null;
	this.onComplete = _.isFunction(opt.complete) ? opt.complete : null;
	this.onError = _.isFunction(opt.error) ? opt.error : null;

	// Rebuild the URL if is a GET and there's data
	if (opt.data != null) {
		if (this.method === 'GET' && _.isObject(opt.data)) {
			this.url = this.url + Util.buildQuery(opt.data);
		} else {
			this.data = opt.data;
		}
	}

	this.hash = this._calculateHash();
}
exports.googleplus = function(args) {
	args = parseArgs(args);
	GA.social('googleplus', 'share', args.url);

	Ti.Platform.openURL('https://plus.google.com/share' + Util.buildQuery({
		url: args.url
	}));

	return true;
};
exports.message = exports.sms = function(args) {
	args = parseArgs(args);
	GA.social('message', 'sent', args.url);

	// iOS Native modal
	if (OS_IOS && benCodingSMS !== null) {
		var recipients = null;
		if (args.recipients != null) recipients = _.isArray(args.recipients) ? args.recipients : [ args.recipients ];

		var $dialog = benCodingSMS.createSMSDialog({
			toRecipients: recipients,
			messageBody: args.fullText
		});

		$dialog.addEventListener('completed', function(){
			onSocialComplete({
				success: true,
				platform: 'messages'
			});
		});

		$dialog.addEventListener('cancelled', function(){
			onSocialCancel({
				success: false,
				platform: 'messages'
			});
		});

		$dialog.addEventListener('error', function(){
			onSocialComplete({
				success: false,
				platform: 'messages'
			});
		});

		$dialog.open({
			animated: true
		});

		return true;
	}

	// Android Native
	if (OS_ANDROID) {
		var url = 'sms:';
		if (args.recipients != null) url += _.isArray(args.recipients) ? args.recipients[0] : args.recipients;

		url += Util.buildQuery({
			body: args.fullText
		});

		return Ti.Platform.openURL(url);
	}
};
exports.telegram = function(args) {
	args = parseArgs(args);
	GA.social('telegram', 'share', args.url);

	Ti.Platform.openURL('https://telegram.me/share/url' + Util.buildQuery({
		url: args.url,
		text: args.text
	}));

	return true;
};
Ejemplo n.º 9
0
function HTTPRequest(opt) {
	var self = this;
	if (opt.url == null) {
		throw new Error('HTTP.Request: URL not set');
	}

	this.opt = opt;

	// if the url is not matching a protocol, assign the base URL
	if (/\:\/\//.test(opt.url)) {
		this.url = opt.url;
	} else {
		this.url = exports.config.base.replace(/\/$/, '') + '/' + opt.url.replace(/^\//, '');
	}

	this.domain = Util.getDomainFromURL(this.url);
	this.method = opt.method != null ? opt.method.toUpperCase() : 'GET';

	// Construct headers: global + per-domain + local
	this.headers = _.extend({}, exports.getHeaders(), exports.getHeaders(this.domain), opt.headers);
	this.timeout = opt.timeout != null ? opt.timeout : exports.config.timeout;

	// Rebuild the URL if is a GET and there's data
	if (opt.data != null) {
		if (this.method === 'GET') {
			if (typeof opt.data === 'object') {
				var exQuery = /\?.*/.test(this.url);
				this.url = this.url + Util.buildQuery(opt.data, exQuery ? '&' : '?');
			}
		} else {
			if (exports.config.bodyEncodingInJSON == true || opt.bodyEncodingInJSON == true) {
				this.headers['Content-Type'] = 'application/json';
				this.data = JSON.stringify(opt.data);
			} else {
				this.data = opt.data;
			}
		}
	}

	this.hash = this._calculateHash();
	this.uniqueId = exports.getUniqueId();

	// Fill the defer, we will manage the callbacks through it
	this.defer = Q.defer();
	this.defer.promise.then(function() { self._onSuccess.apply(self, arguments); });
	this.defer.promise.catch(function() { self._onError.apply(self, arguments); });
	this.defer.promise.finally(function() { self._onFinally.apply(self, arguments); });

	Ti.API.debug('HTTP: <' + this.uniqueId + '>', this.method, this.url, this.data);
}
exports.facebook = function(args) {
	args = parseArgs(args);
	GA.social('facebook', 'share', args.url);

	// Native iOS dialog
	if (OS_IOS && (dkNappSocial != null && dkNappSocial.isFacebookSupported()) &&
		false === /https?\:\/\/(www\.)?facebook\.com/.test(args.url) // BUG: iOS share dialog doesn't share Facebook links
	) {
		dkNappSocial.facebook({
			text: args.text,
			image: args.image,
			url: args.url
		});
		return true;
	}

	// SDK
	if (FB != null) {
		FB.presentShareDialog({
			link: args.url,
			title: args.title,
			description: args.description || args.text
		});
		return true;
	}

	// Android Native intent
	if (OS_ANDROID) {
		try {
			var intent = Ti.Android.createIntent({
				action: Ti.Android.ACTION_SEND,
				packageName: 'com.facebook.katana',
				type: 'text/plain'
			});
			intent.putExtra(Ti.Android.EXTRA_TEXT, args.url);
			Ti.Android.currentActivity.startActivity(intent);

			return true;
		} catch (err) {}
	}

	// Fallback
	Ti.Platform.openURL('http://www.facebook.com/dialog/share' + Util.buildQuery({
		app_id: (Alloy.CFG.T.fb || Alloy.CFG.T.facebook || {}).appID,
		display: 'touch',
		redirect_uri: args.redirect_uri || Ti.App.url,
		href: args.url
	}));
};
exports.sync = function(method, model, opt) {
	var adapter_cfg = _.extend({}, exports.config, model.config.adapter);
	var url = adapter_cfg.base + adapter_cfg.name;

	if (model instanceof Backbone.Model) {
		if (opt.id != null) {
			url += '/' + opt.id;
		} else if (model.id != null) {
			url += '/' + model.id;
		}
	}

	if (opt.query != null) {
		if (_.isObject(opt.query) || _.isArray(opt.query)) {
			url += Util.buildQuery(opt.query);
		} else if (_.isString(opt.query)) {
			url += opt.query.substr(0,1) === '?' ? opt.query : ('?'+opt.query);
		}
	}

	if (opt.patch) method = 'patch';

	var httpOpt = _.extend({}, model.config.http, opt.http, {
		url: url,
		method: CRUD_to_REST[method],
		format: 'json'
	}, opt.httpOverride);

	switch (method) {

		case 'patch':

		httpOpt = _.extend(httpOpt, { data: _.pick(model.attributes, _.keys(opt.changes)) });

		HTTP.send(httpOpt)
		.success(function(resp) {
			if (resp != null && resp[ adapter_cfg.idAttribute ] != null) {
				opt.success(resp);
			} else {
				opt.error(resp);
			}
		})
		.error(opt.error);
		break;

		case 'update':
		case 'create':

		httpOpt = _.extend(httpOpt, { data: model.toJSON() });

		HTTP.send(httpOpt)
		.success(function(resp) {
			if (resp != null && resp[ adapter_cfg.idAttribute ] != null) {
				opt.success(resp);
			} else {
				opt.error(resp);
			}
		})
		.error(opt.error);
		break;

		case 'read':

		HTTP.send(httpOpt)
		.success(function(resp) {

			if (resp != null) {
				if (model instanceof Backbone.Collection) {

					if (_.isArray(resp)) {
						opt.success(resp);
					} else if (_.isObject(resp)){
						opt.success(resp[ adapter_cfg.collectionWrapper ]);
					} else {
						opt.error(resp);
					}

				} else {

					if (resp != null && resp[ adapter_cfg.idAttribute ] != null) {
						opt.success(resp);
					} else {
						opt.error(resp);
					}

				}
			} else {
				opt.error(resp);
			}

		})
		.error(opt.error);
		break;

		case 'delete':

		HTTP.send(httpOpt)
		.success(opt.success)
		.error(opt.error);
		break;

	}
};
Ejemplo n.º 12
0
			callback: function() {
				Ti.Platform.openURL('http://maps.apple.com/' + Util.buildQuery(query));
			}
Ejemplo n.º 13
0
			callback: function() {
				Ti.Platform.openURL('comgooglemapsurl://' + Util.buildQuery(query));
			}
Ejemplo n.º 14
0
exports.startNavigator = function(lat, lng, mode) {
	var query = {
		directionsmode: mode || 'walking',
		daddr: lat + ',' + lng
	};

	if (OS_IOS && Ti.Platform.canOpenURL('comgooglemapsurl://')) {
		Dialog.option(L('open_with', 'Open with...'), [{
			title: 'Google Maps',
			callback: function() {
				Ti.Platform.openURL('comgooglemapsurl://' + Util.buildQuery(query));
			}
		}, {
			title: 'Apple Maps',
			callback: function() {
				Ti.Platform.openURL('http://maps.apple.com/' + Util.buildQuery(query));
			}
		}, {
			title: L('cancel', 'Cancel'),
			cancel: true
		}]);
	} else {
		Ti.Platform.openURL((OS_IOS ? 'http://maps.apple.com/' : 'https://maps.google.com/maps/') + Util.buildQuery(query));
	}
};
exports.sync = function(method, model, opt) {
	var url = (model.config.adapter.baseUrl || '/') + model.config.adapter.name;
	var query = null;

	if (model instanceof Backbone.Model) {
		url += model.id != null ? ('/' + model.id) : '';
		query = model.get('query');
	} else if (model.first() != null) {
		query = model.first().get('query');
	}

	query = query || opt.query;

	if (query != null) {
		if (_.isObject(query) || _.isArray(query)) {
			url += Util.buildQuery(query);
		} else if (_.isString(query)) {
			url += query.substr(0,1) === '?' ? query : ('?'+query);
		}
	}

	var httpOpt = _.extend({}, model.config.http, opt.http, {
		url: url,
		method: CRUD_to_REST[method] || 'GET',
		format: 'json'
	});

	if (Alloy.Backbone.emulateHTTP) {
		if ([ 'DELETE', 'PUT', 'PATCH' ].indexOf(httpOpt.method) !== -1) {
			httpOpt.method = 'POST';
			httpOpt.headers = _.extend({}, httpOpt.headers, {
				'X-HTTP-Method-Override': httpOpt.method
			});
		}
	}

	switch (method) {

		case 'create':

		_.extend(httpOpt, { data: JSON.stringify(model.toJSON()) });
		HTTP.send(httpOpt).success(function(resp) {

			if (resp != null && resp.id != null) {
				opt.success(resp);
			} else {
				opt.error();
			}

		}).error(opt.error);
		break;

		case 'read':

		HTTP.send(httpOpt).success(function(resp) {

			if (resp != null && _.isObject(resp)) {
				if (resp[ATTRIBUTES] && resp[ATTRIBUTES].type && resp[ATTRIBUTES].type == ERROR_TYPE) {
					opt.error();
				}

				if (resp[CONTENT] != null) {
					var content = resp[CONTENT];

					if (_.isArray(content)){
						if (model instanceof Backbone.Collection) {
							opt.success(content);
						} else {
							opt.success(_.first(content));
						}
					} else {
						opt.error();
					}
				}
			} else {
				opt.error();
			}

		}).error(function(err) {
			opt.error(err);
		});
		break;

	}
};