From 2ad7bccc62646f31e8952501212a233e14b6c2f4 Mon Sep 17 00:00:00 2001 From: Mehmet Kamil Sulubulut Date: Sat, 1 Aug 2015 18:36:48 +0300 Subject: [PATCH] Preparing for 0.3 --- module.js | 25 ++++++++++------- services/Auth.js | 19 ++++--------- services/AuthInterceptor.js | 6 ++-- services/{DataSource.js => EntityBridge.js} | 18 ++++++------ services/LocalStore.js | 12 ++++---- services/Request.js | 11 ++------ services/Socket.js | 17 +++++------ services/UserSettings.js | 31 ++++++++++++--------- 8 files changed, 69 insertions(+), 70 deletions(-) rename services/{DataSource.js => EntityBridge.js} (74%) diff --git a/module.js b/module.js index 5e77a89..30ab7d3 100644 --- a/module.js +++ b/module.js @@ -1,20 +1,25 @@ -angular.module('basyt.angular', ['ui.router']) +angular.module('basyt-angular', ['ui.router']) .config(['$httpProvider', function ($httpProvider) { - $httpProvider.interceptors.push('AuthInterceptor'); + $httpProvider.interceptors.push('BasytAuthInterceptor'); }]) .value('BasytAnonState', 'login') .value('BasytAuthMessages', { - 'loginRequired': 'Login Required', - 'loginSuccess': 'Login Successful', - 'loginFailed': 'Login Failed', - 'logoutSuccess': 'Logout Successful', - 'authFailed': 'Authorization Failed' + loginRequired: 'Login Required', + loginSuccess: 'Login Successful', + loginFailed: 'Login Failed', + logoutSuccess: 'Logout Successful', + authFailed: 'Authorization Failed' }) - .run(['$rootScope', 'Auth', '$state','BasytAnonState', '$injector', 'BasytAuthMessages', function($rootScope, Auth, $state, BasytAnonState, $injector, BasytAuthMessages){ + .value('BasytServer', { + host: window.API_URL, + socket: window.SOCKET_URL, + socketOptions: window.SOCKET_OPTS + }) + .run(['$rootScope', '$state', '$injector', 'BasytAuth', 'BasytAnonState', 'BasytAuthMessages', function($rootScope, $state, BasytAuth, $injector, BasytAnonState, BasytAuthMessages){ var $alert = $injector.get('$alert'); $rootScope.$on("$stateChangeStart", function(event, next) { if (next.role) { - if (!Auth.isAuthenticated(next.role)) { + if (!BasytAuth.isAuthenticated(next.role)) { if($alert) { $alert({ title: BasytAuthMessages.authFailed, @@ -38,4 +43,4 @@ angular.module('basyt.angular', ['ui.router']) } $state.go(BasytAnonState); }); - }]); \ No newline at end of file + }]); diff --git a/services/Auth.js b/services/Auth.js index c4dd895..23ca84b 100644 --- a/services/Auth.js +++ b/services/Auth.js @@ -1,5 +1,5 @@ -angular.module('basyt.angular') - .factory('Auth', ['BasytLocalStore', 'Request', '$q', '$rootScope', '$injector', 'BasytAuthMessages', function (BasytLocalStore, Request, $q, $rootScope, $injector, BasytAuthMessages) { +angular.module('basyt-angular') + .factory('BasytAuth', ['BasytLocalStore', 'BasytRequest', '$q', '$rootScope', '$injector', 'BasytAuthMessages', function (BasytLocalStore, BasytRequest, $q, $rootScope, $injector, BasytAuthMessages) { var AnonUser = { user_state: 'ANON' }, @@ -54,13 +54,6 @@ angular.module('basyt.angular') return deferred.promise; }, Auth = { - authorize: function (access) { - if (access) { - return this.isAuthenticated(access); - } else { - return true; - } - }, isAuthenticated: function (access, remote) { if (BasytLocalStore.get('auth_token')) { if (remote) Auth.authenticate(); @@ -102,18 +95,18 @@ angular.module('basyt.angular') login: function (credentials, rememberMe) { logout(true); //if(rememberMe) - return Request('user:login', {data: credentials}).then(login, logoutReject); + return BasytRequest('user:login', {data: credentials}).then(login, logoutReject); }, logout: logout, register: function (formData) { logout(true); - return Request('user:register', {user: formData}).then(login, logoutReject); + return BasytRequest('user:register', {user: formData}).then(login, logoutReject); }, authenticate: function () { - return Request('user:authorize').then(function () { + return BasytRequest('user:authenticate').then(function () { }, logout); } }; $rootScope.activeUser = User; return Auth; - }]); \ No newline at end of file + }]); diff --git a/services/AuthInterceptor.js b/services/AuthInterceptor.js index a28bf90..0bb221a 100644 --- a/services/AuthInterceptor.js +++ b/services/AuthInterceptor.js @@ -1,5 +1,5 @@ -angular.module('basyt.angular') - .factory('AuthInterceptor', ['$q', 'BasytLocalStore', '$rootScope', function ($q, BasytLocalStore, $rootScope) { +angular.module('basyt-angular') + .factory('BasytAuthInterceptor', ['$q', 'BasytLocalStore', '$rootScope', function ($q, BasytLocalStore, $rootScope) { return { request: function (config) { var token; @@ -20,4 +20,4 @@ angular.module('basyt.angular') return $q.reject(response); } }; - }]); \ No newline at end of file + }]); diff --git a/services/DataSource.js b/services/EntityBridge.js similarity index 74% rename from services/DataSource.js rename to services/EntityBridge.js index 4f7d3ec..5677132 100644 --- a/services/DataSource.js +++ b/services/EntityBridge.js @@ -1,6 +1,6 @@ -angular.module('basyt.angular') - .service('DataSource', ['Request', '$rootScope', 'filterFilter', 'Socket', '$q', function (Request, $rootScope, filterFilter, Socket, $q) { - var DataSource = function (entityName, map) { +angular.module('basyt-angular') + .factory('BasytEntityBridge', ['BasytRequest', 'BasytSocket', '$rootScope', 'filterFilter', '$q', function (BasytRequest, BasytSocket, $rootScope, filterFilter, $q) { + var EntityBridge = function (entityName, map) { var that = this; this.endpoint = entityName + ':list'; this.socketChannel = 'entity:' + entityName; @@ -8,13 +8,13 @@ angular.module('basyt.angular') this.isLoaded = false; this.listeners = []; this.value = []; - Socket.on('entity:update:' + entityName, function (message) { + BasytSocket.on('entity:update:' + entityName, function (message) { that.reload(false); }); this.reload(true); }; - DataSource.prototype.bind = function () { + EntityBridge.prototype.bind = function () { var deferred = $q.defer(), that = this, promise = deferred.promise; this.listeners.push(deferred); promise.list = this.value; @@ -33,9 +33,9 @@ angular.module('basyt.angular') }; return promise; }; - DataSource.prototype.reload = function (subscribe) { + EntityBridge.prototype.reload = function (subscribe) { var that = this; - Request(this.endpoint, {params: {deep: true}}) + BasytRequest(this.endpoint, {params: {deep: true}}) .then(function (data) { if (that.map) { angular.forEach(data.result, that.map); @@ -46,7 +46,7 @@ angular.module('basyt.angular') deferred.notify(that.value); }); if (subscribe) { - Socket.subscribe(that.socketChannel); + BasytSocket.subscribe(that.socketChannel); } }, function () { @@ -55,5 +55,5 @@ angular.module('basyt.angular') }); }; - return DataSource; + return EntityBridge; }]); diff --git a/services/LocalStore.js b/services/LocalStore.js index 5df9ec0..624f3ad 100644 --- a/services/LocalStore.js +++ b/services/LocalStore.js @@ -1,14 +1,14 @@ -angular.module('basyt.angular') - .factory('BasytLocalStore', function() { +angular.module('basyt-angular') + .factory('BasytLocalStore', ['$window', function($window) { return { get: function(key) { - return localStorage.getItem(key); + return $window.localStorage.getItem(key); }, set: function(key, val) { - return localStorage.setItem(key, val); + return $window.localStorage.setItem(key, val); }, unset: function(key) { - return localStorage.removeItem(key); + return $window.localStorage.removeItem(key); } }; - }); \ No newline at end of file + }]); diff --git a/services/Request.js b/services/Request.js index 5b78c4b..c2e442a 100644 --- a/services/Request.js +++ b/services/Request.js @@ -1,10 +1,5 @@ -angular.module('basyt.angular') - .constant('BasytServer', { - host: window.API_URL, - socket: window.SOCKET_URL, - socketOptions: window.SOCKET_OPTS - }) - .factory('Request', ['$http', 'BasytServer', '$q', '$rootScope', '$urlMatcherFactory', function ($http, BasytServer, $q, $rootScope, $urlMatcherFactory) { +angular.module('basyt-angular') + .factory('BasytRequest', ['$http', '$q', '$rootScope', '$urlMatcherFactory', 'BasytServer', function ($http, $q, $rootScope, $urlMatcherFactory, BasytServer) { var endpoints, initialized = false, future, deferred = $q.defer(); $http.get(BasytServer.host) @@ -63,4 +58,4 @@ angular.module('basyt.angular') } ); }; - }]); \ No newline at end of file + }]); diff --git a/services/Socket.js b/services/Socket.js index 3df4b99..f43e4ae 100644 --- a/services/Socket.js +++ b/services/Socket.js @@ -1,5 +1,5 @@ -angular.module('basyt.angular') - .factory('Socket', ['BasytServer', 'BasytLocalStore', '$rootScope', '$q', 'Auth', function (BasytServer, BasytLocalStore, $rootScope, $q) { +angular.module('basyt-angular') + .factory('BasytSocket', ['$rootScope', '$q', 'BasytServer', 'BasytLocalStore', function ($rootScope, $q, BasytServer, BasytLocalStore) { var connection, future = $q.defer(); connect = function () { connection = io.connect(BasytServer.socket, BasytServer.socketOptions); @@ -11,7 +11,8 @@ angular.module('basyt.angular') .emit('authenticate', {token: BasytLocalStore.get('auth_token')}); //send the jwt }; - connect(); + if(angular.isDefined(io)) + connect(); return { on: function (event, cb) { if (connection) { @@ -26,15 +27,15 @@ angular.module('basyt.angular') connection.removeAllListeners(event, cb); } }, - subscribe: function (event, data) { - connection.emit('subscribe', {resource: event, data: data}); + subscribe: function (channel, data) { + connection.emit('subscribe', {resource: channel, data: data}); }, - unsubscribe: function (event, data) { - connection.emit('unsubscribe', {resource: event, data: data}); + unsubscribe: function (channel, data) { + connection.emit('unsubscribe', {resource: channel, data: data}); }, emit: function (label, data) { connection.emit(label, data); }, connect: connect }; - }]); \ No newline at end of file + }]); diff --git a/services/UserSettings.js b/services/UserSettings.js index 9228010..49fbe3b 100644 --- a/services/UserSettings.js +++ b/services/UserSettings.js @@ -1,5 +1,5 @@ -angular.module('basyt.angular') - .factory('UserSettings', ['Request', '$rootScope', function (Request, $rootScope) { +angular.module('basyt-angular') + .factory('BasytUserSettings', ['$rootScope', 'BasytRequest', 'BasytAuth', function ($rootScope, BasytRequest, BasytAuth) { var settings, ready = false, service = { isReady: function () { @@ -9,20 +9,25 @@ angular.module('basyt.angular') return ready ? settings : {}; }, reload: function () { - Request('user_settings:get') - .then( - function (data) { + BasytRequest('user_settings:get') + .then(function (data) { settings = data.result || {}; ready = true; - $rootScope.$broadcast('user:registered'); - }, - function(){ - $rootScope.$broadcast('user:anonymous'); + $rootScope.$broadcast('basyt:user_settings:ready'); }); } }; - - service.reload(); - + if(BasytAuth.isAuthenticated) { + service.reload(); + } + $rootScope.$on('user:login', function(){ + service.reload(); + }); + $rootScope.$on('user:logout', function(){ + ready = false; + }); + $rootScope.$on('user:anonymous', function(){ + ready = false; + }); return service; - }]); \ No newline at end of file + }]);