Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Git submodule for ServiceProviders

  • Loading branch information...
commit dcb7b3abc0426fc893aa574f038332b059ff6bf1 1 parent c76445f
@molily molily authored
View
3  .gitmodules
@@ -0,0 +1,3 @@
+[submodule "coffee/lib/services"]
+ path = coffee/lib/services
+ url = git@github.com:chaplinjs/service-providers.git
1  coffee/lib/services
@@ -0,0 +1 @@
+Subproject commit 32d97cfbdffebaf3a16fbb0ba3c97e10443dce6e
View
252 js/lib/services/facebook.js
@@ -0,0 +1,252 @@
+// Generated by CoffeeScript 1.3.3
+var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
+ __hasProp = {}.hasOwnProperty,
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
+
+define(['underscore', 'chaplin', 'lib/utils', 'lib/services/service_provider'], function(_, Chaplin, utils, ServiceProvider) {
+ 'use strict';
+
+ var Facebook, mediator;
+ mediator = Chaplin.mediator;
+ return Facebook = (function(_super) {
+ var facebookAppId, scope;
+
+ __extends(Facebook, _super);
+
+ facebookAppId = '115149731946795';
+
+ scope = 'user_likes';
+
+ Facebook.prototype.name = 'facebook';
+
+ Facebook.prototype.status = null;
+
+ Facebook.prototype.accessToken = null;
+
+ function Facebook() {
+ this.processUserData = __bind(this.processUserData, this);
+
+ this.facebookLogout = __bind(this.facebookLogout, this);
+
+ this.loginStatusAfterAbort = __bind(this.loginStatusAfterAbort, this);
+
+ this.loginHandler = __bind(this.loginHandler, this);
+
+ this.triggerLogin = __bind(this.triggerLogin, this);
+
+ this.loginStatusHandler = __bind(this.loginStatusHandler, this);
+
+ this.getLoginStatus = __bind(this.getLoginStatus, this);
+
+ this.saveAuthResponse = __bind(this.saveAuthResponse, this);
+
+ this.loadHandler = __bind(this.loadHandler, this);
+ Facebook.__super__.constructor.apply(this, arguments);
+ utils.deferMethods({
+ deferred: this,
+ methods: ['parse', 'subscribe', 'postToGraph', 'getAccumulatedInfo', 'getInfo'],
+ onDeferral: this.load
+ });
+ utils.wrapAccumulators(this, ['getAccumulatedInfo']);
+ this.subscribeEvent('logout', this.logout);
+ }
+
+ Facebook.prototype.load = function() {
+ if (this.state() === 'resolved' || this.loading) {
+ return;
+ }
+ this.loading = true;
+ window.fbAsyncInit = this.loadHandler;
+ return utils.loadLib('http://connect.facebook.net/en_US/all.js', null, this.reject);
+ };
+
+ Facebook.prototype.loadHandler = function() {
+ this.loading = false;
+ try {
+ delete window.fbAsyncInit;
+ } catch (error) {
+ window.fbAsyncInit = void 0;
+ }
+ FB.init({
+ appId: facebookAppId,
+ status: true,
+ cookie: true,
+ xfbml: false
+ });
+ this.registerHandlers();
+ return this.resolve();
+ };
+
+ Facebook.prototype.registerHandlers = function() {
+ this.subscribe('auth.logout', this.facebookLogout);
+ this.subscribe('edge.create', this.processLike);
+ return this.subscribe('comment.create', this.processComment);
+ };
+
+ Facebook.prototype.unregisterHandlers = function() {
+ this.unsubscribe('auth.logout', this.facebookLogout);
+ this.unsubscribe('edge.create', this.processLike);
+ return this.unsubscribe('comment.create', this.processComment);
+ };
+
+ Facebook.prototype.isLoaded = function() {
+ return Boolean(window.FB && FB.login);
+ };
+
+ Facebook.prototype.saveAuthResponse = function(response) {
+ var authResponse;
+ this.status = response.status;
+ authResponse = response.authResponse;
+ if (authResponse) {
+ return this.accessToken = authResponse.accessToken;
+ } else {
+ return this.accessToken = null;
+ }
+ };
+
+ Facebook.prototype.getLoginStatus = function(callback, force) {
+ if (callback == null) {
+ callback = this.loginStatusHandler;
+ }
+ if (force == null) {
+ force = false;
+ }
+ return FB.getLoginStatus(callback, force);
+ };
+
+ Facebook.prototype.loginStatusHandler = function(response) {
+ var authResponse;
+ this.saveAuthResponse(response);
+ authResponse = response.authResponse;
+ if (authResponse) {
+ this.publishSession(authResponse);
+ return this.getUserData();
+ } else {
+ return mediator.publish('logout');
+ }
+ };
+
+ Facebook.prototype.triggerLogin = function(loginContext) {
+ return FB.login(_(this.loginHandler).bind(this, loginContext), {
+ scope: scope
+ });
+ };
+
+ Facebook.prototype.loginHandler = function(loginContext, response) {
+ var authResponse, eventPayload, loginStatusHandler;
+ this.saveAuthResponse(response);
+ authResponse = response.authResponse;
+ eventPayload = {
+ provider: this,
+ loginContext: loginContext
+ };
+ if (authResponse) {
+ mediator.publish('loginSuccessful', eventPayload);
+ this.publishSession(authResponse);
+ return this.getUserData();
+ } else {
+ mediator.publish('loginAbort', eventPayload);
+ loginStatusHandler = _(this.loginStatusAfterAbort).bind(this, loginContext);
+ return this.getLoginStatus(loginStatusHandler, true);
+ }
+ };
+
+ Facebook.prototype.loginStatusAfterAbort = function(loginContext, response) {
+ var authResponse, eventPayload;
+ this.saveAuthResponse(response);
+ authResponse = response.authResponse;
+ eventPayload = {
+ provider: this,
+ loginContext: loginContext
+ };
+ if (authResponse) {
+ mediator.publish('loginSuccessful', eventPayload);
+ return this.publishSession(authResponse);
+ } else {
+ return mediator.publish('loginFail', eventPayload);
+ }
+ };
+
+ Facebook.prototype.publishSession = function(authResponse) {
+ return mediator.publish('serviceProviderSession', {
+ provider: this,
+ userId: authResponse.userID,
+ accessToken: authResponse.accessToken
+ });
+ };
+
+ Facebook.prototype.facebookLogout = function(response) {
+ return this.saveAuthResponse(response);
+ };
+
+ Facebook.prototype.logout = function() {
+ return this.status = this.accessToken = null;
+ };
+
+ Facebook.prototype.processLike = function(url) {
+ return mediator.publish('facebook:like', url);
+ };
+
+ Facebook.prototype.processComment = function(comment) {
+ return mediator.publish('facebook:comment', comment.href);
+ };
+
+ Facebook.prototype.parse = function(el) {
+ return FB.XFBML.parse(el);
+ };
+
+ Facebook.prototype.subscribe = function(eventType, handler) {
+ return FB.Event.subscribe(eventType, handler);
+ };
+
+ Facebook.prototype.unsubscribe = function(eventType, handler) {
+ return FB.Event.unsubscribe(eventType, handler);
+ };
+
+ Facebook.prototype.postToGraph = function(ogResource, data, callback) {
+ return FB.api(ogResource, 'post', data, function(response) {
+ if (callback) {
+ return callback(response);
+ }
+ });
+ };
+
+ Facebook.prototype.getAccumulatedInfo = function(urls, callback) {
+ if (typeof urls === 'string') {
+ urls = [urls];
+ }
+ urls = _(urls).reduce(function(memo, url) {
+ if (memo) {
+ memo += ',';
+ }
+ return memo += encodeURIComponent(url);
+ }, '');
+ return FB.api("?ids=" + urls, callback);
+ };
+
+ Facebook.prototype.getInfo = function(id, callback) {
+ return FB.api(id, callback);
+ };
+
+ Facebook.prototype.getUserData = function() {
+ return this.getInfo('/me', this.processUserData);
+ };
+
+ Facebook.prototype.processUserData = function(response) {
+ return mediator.publish('userData', response);
+ };
+
+ Facebook.prototype.dispose = function() {
+ if (this.disposed) {
+ return;
+ }
+ this.unregisterHandlers();
+ delete this.status;
+ delete this.accessToken;
+ return Facebook.__super__.dispose.apply(this, arguments);
+ };
+
+ return Facebook;
+
+ })(ServiceProvider);
+});
View
112 js/lib/services/google.js
@@ -0,0 +1,112 @@
+// Generated by CoffeeScript 1.3.3
+var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
+ __hasProp = {}.hasOwnProperty,
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
+
+define(['underscore', 'chaplin', 'lib/utils', 'lib/services/service_provider'], function(_, Chaplin, utils, ServiceProvider) {
+ 'use strict';
+
+ var Google, mediator;
+ mediator = Chaplin.mediator;
+ return Google = (function(_super) {
+ var clientId, scopes;
+
+ __extends(Google, _super);
+
+ function Google() {
+ this.loadHandler = __bind(this.loadHandler, this);
+ return Google.__super__.constructor.apply(this, arguments);
+ }
+
+ clientId = '365800635017.apps.googleusercontent.com';
+
+ scopes = 'https://www.googleapis.com/auth/userinfo.profile';
+
+ Google.prototype.name = 'google';
+
+ Google.prototype.load = function() {
+ if (this.state() === 'resolved' || this.loading) {
+ return;
+ }
+ this.loading = true;
+ window.googleClientLoaded = this.loadHandler;
+ return utils.loadLib('https://apis.google.com/js/client.js?onload=googleClientLoaded', null, this.reject);
+ };
+
+ Google.prototype.loadHandler = function() {
+ try {
+ delete window.googleClientLoaded;
+ } catch (error) {
+ window.googleClientLoaded = void 0;
+ }
+ return gapi.auth.init(this.resolve);
+ };
+
+ Google.prototype.isLoaded = function() {
+ return Boolean(window.gapi && gapi.auth && gapi.auth.authorize);
+ };
+
+ Google.prototype.triggerLogin = function(loginContext) {
+ return gapi.auth.authorize({
+ client_id: clientId,
+ scope: scopes,
+ immediate: false
+ }, _(this.loginHandler).bind(this, loginContext));
+ };
+
+ Google.prototype.loginHandler = function(loginContext, authResponse) {
+ if (authResponse) {
+ mediator.publish('loginSuccessful', {
+ provider: this,
+ loginContext: loginContext
+ });
+ return mediator.publish('serviceProviderSession', {
+ provider: this,
+ accessToken: authResponse.access_token
+ });
+ } else {
+ return mediator.publish('loginFail', {
+ provider: this,
+ loginContext: loginContext
+ });
+ }
+ };
+
+ Google.prototype.getLoginStatus = function(callback) {
+ return gapi.auth.authorize({
+ client_id: clientId,
+ scope: scopes,
+ immediate: true
+ }, callback);
+ };
+
+ Google.prototype.getUserInfo = function(callback) {
+ var request;
+ request = gapi.client.request({
+ path: '/oauth2/v2/userinfo'
+ });
+ return request.execute(callback);
+ };
+
+ Google.prototype.parsePlusOneButton = function(el) {
+ if (window.gapi && gapi.plusone && gapi.plusone.go) {
+ return gapi.plusone.go(el);
+ } else {
+ window.___gcfg = {
+ parsetags: 'explicit'
+ };
+ return utils.loadLib('https://apis.google.com/js/plusone.js', function() {
+ try {
+ delete window.___gcfg;
+ } catch (error) {
+ window.___gcfg = void 0;
+ }
+ return gapi.plusone.go(el);
+ });
+ }
+ };
+
+ return Google;
+
+ })(ServiceProvider);
+});
View
91 js/lib/services/service_provider.js
@@ -0,0 +1,91 @@
+// Generated by CoffeeScript 1.3.3
+
+define(['underscore', 'chaplin', 'lib/utils'], function(_, Chaplin, utils) {
+ 'use strict';
+
+ var ServiceProvider;
+ return ServiceProvider = (function() {
+
+ _(ServiceProvider.prototype).extend(Chaplin.Subscriber);
+
+ ServiceProvider.prototype.loading = false;
+
+ function ServiceProvider() {
+ /*console.debug 'ServiceProvider#constructor'
+ */
+ _(this).extend($.Deferred());
+ utils.deferMethods({
+ deferred: this,
+ methods: ['triggerLogin', 'getLoginStatus'],
+ onDeferral: this.load
+ });
+ }
+
+ ServiceProvider.prototype.disposed = false;
+
+ ServiceProvider.prototype.dispose = function() {
+ /*console.debug 'ServiceProvider#dispose'
+ */
+ if (this.disposed) {
+ return;
+ }
+ this.unsubscribeAllEvents();
+ this.disposed = true;
+ return typeof Object.freeze === "function" ? Object.freeze(this) : void 0;
+ };
+
+ return ServiceProvider;
+
+ })();
+ /*
+
+ Standard methods and their signatures:
+
+ load: ->
+ # Load a script like this:
+ utils.loadLib 'http://example.org/foo.js', @loadHandler, @reject
+
+ loadHandler: =>
+ # Init the library, then resolve
+ ServiceProviderLibrary.init(foo: 'bar')
+ @resolve()
+
+ isLoaded: ->
+ # Return a Boolean
+ Boolean window.ServiceProviderLibrary and ServiceProviderLibrary.login
+
+ # Trigger login popup
+ triggerLogin: (loginContext) ->
+ callback = _(@loginHandler).bind(this, loginContext)
+ ServiceProviderLibrary.login callback
+
+ # Callback for the login popup
+ loginHandler: (loginContext, response) =>
+
+ if response
+ # Publish successful login
+ mediator.publish 'loginSuccessful', {provider: this, loginContext}
+
+ # Publish the session
+ mediator.publish 'serviceProviderSession',
+ provider: this
+ userId: response.userId
+ accessToken: response.accessToken
+ # etc.
+
+ else
+ mediator.publish 'loginFail', {provider: this, loginContext}
+
+ getLoginStatus: (callback = @loginStatusHandler, force = false) ->
+ ServiceProviderLibrary.getLoginStatus callback, force
+
+ loginStatusHandler: (response) =>
+ return unless response
+ mediator.publish 'serviceProviderSession',
+ provider: this
+ userId: response.userId
+ accessToken: response.accessToken
+ # etc.
+ */
+
+});
View
131 js/lib/services/twitter.js
@@ -0,0 +1,131 @@
+// Generated by CoffeeScript 1.3.3
+var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
+ __hasProp = {}.hasOwnProperty,
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
+
+define(['chaplin', 'lib/utils', 'lib/services/service_provider'], function(Chaplin, utils, ServiceProvider) {
+ 'use strict';
+
+ var Twitter, mediator;
+ mediator = Chaplin.mediator;
+ return Twitter = (function(_super) {
+ var consumerKey;
+
+ __extends(Twitter, _super);
+
+ consumerKey = 'w0uohox91TgpKETJmscYIQ';
+
+ Twitter.prototype.name = 'twitter';
+
+ function Twitter() {
+ this.loginStatusHandler = __bind(this.loginStatusHandler, this);
+
+ this.loginHandler = __bind(this.loginHandler, this);
+
+ this.sdkLoadHandler = __bind(this.sdkLoadHandler, this);
+ Twitter.__super__.constructor.apply(this, arguments);
+ this.subscribeEvent('!logout', this.logout);
+ }
+
+ Twitter.prototype.loadSDK = function() {
+ if (this.state() === 'resolved' || this.loading) {
+ return;
+ }
+ this.loading = true;
+ return utils.loadLib("http://platform.twitter.com/anywhere.js?id=" + consumerKey + "&v=1", this.sdkLoadHandler, this.reject);
+ };
+
+ Twitter.prototype.sdkLoadHandler = function() {
+ var _this = this;
+ this.loading = false;
+ return twttr.anywhere(function(T) {
+ mediator.publish('sdkLoaded');
+ _this.T = T;
+ return _this.resolve();
+ });
+ };
+
+ Twitter.prototype.isLoaded = function() {
+ return Boolean(window.twttr);
+ };
+
+ Twitter.prototype.publish = function(event, callback) {
+ return this.T.trigger(event, callback);
+ };
+
+ Twitter.prototype.subscribe = function(event, callback) {
+ return this.T.bind(event, callback);
+ };
+
+ Twitter.prototype.unsubscribe = function(event) {
+ return this.T.unbind(event);
+ };
+
+ Twitter.prototype.triggerLogin = function(loginContext) {
+ var callback;
+ callback = _(this.loginHandler).bind(this, loginContext);
+ this.T.signIn();
+ this.subscribe('authComplete', function(event, currentUser, accessToken) {
+ return callback({
+ currentUser: currentUser,
+ accessToken: accessToken
+ });
+ });
+ return this.subscribe('signOut', function() {
+ return callback();
+ });
+ };
+
+ Twitter.prototype.publishSession = function(response) {
+ var user;
+ user = response.currentUser;
+ mediator.publish('serviceProviderSession', {
+ provider: this,
+ userId: user.id,
+ accessToken: response.accessToken || twttr.anywhere.token
+ });
+ return mediator.publish('userData', user.attributes);
+ };
+
+ Twitter.prototype.loginHandler = function(loginContext, response) {
+ if (response) {
+ mediator.publish('loginSuccessful', {
+ provider: this,
+ loginContext: loginContext
+ });
+ return this.publishSession(response);
+ } else {
+ return mediator.publish('loginFail', {
+ provider: this,
+ loginContext: loginContext
+ });
+ }
+ };
+
+ Twitter.prototype.getLoginStatus = function(callback, force) {
+ if (callback == null) {
+ callback = this.loginStatusHandler;
+ }
+ if (force == null) {
+ force = false;
+ }
+ return callback(this.T);
+ };
+
+ Twitter.prototype.loginStatusHandler = function(response) {
+ if (response.currentUser) {
+ return this.publishSession(response);
+ } else {
+ return mediator.publish('logout');
+ }
+ };
+
+ Twitter.prototype.logout = function() {
+ var _ref;
+ return typeof twttr !== "undefined" && twttr !== null ? (_ref = twttr.anywhere) != null ? typeof _ref.signOut === "function" ? _ref.signOut() : void 0 : void 0 : void 0;
+ };
+
+ return Twitter;
+
+ })(ServiceProvider);
+});
Please sign in to comment.
Something went wrong with that request. Please try again.