Skip to content

Instantly share code, notes, and snippets.

@coolbung
Created April 21, 2016 12:31
Show Gist options
  • Save coolbung/c9f865a85694b966aababb821e921c22 to your computer and use it in GitHub Desktop.
Save coolbung/c9f865a85694b966aababb821e921c22 to your computer and use it in GitHub Desktop.
Push & Translation
var juniteApp = angular.module('junite', ['ngRoute', 'ngTouch', 'toaster', 'LocalStorageModule', 'junite-Service', 'junite-directives', 'juniteController', 'socialapp', 'pascalprecht.translate', 'loginController', 'loginService', 'lib-ui', 'textAngular', 'blogapp', 'jgive','ngCordova','ngTwitter','ngCordovaOauth']);
juniteApp.run(function ($rootScope, $location, localStorageService, $window, $route, toaster, checkConnection, $translate, juniteService) {
// Push custom app vars
$rootScope.is_custom = (custom_app.LOGINURL.length > 0);
$rootScope.custom_app = custom_app;
// $rootScope.messages = message;
$rootScope.fbConnect = false;
// Initialize loader object
$rootScope.loader = {
loading: false
};
// for ios google place autocomplete selection
$(document).on({
'DOMNodeInserted': function () {
$('.pac-item, .pac-item span', this).addClass('needsclick');
}
}, '.pac-container');
$(window).load(function () {
$('.video-js').prop("controls", true);
});
document.addEventListener("deviceready", onDeviceReady, false);
// Method to show loader
$rootScope.showloader = function () {
$rootScope.loader.loading = true;
};
//Method to set limit and version details
$rootScope.setAllLimit = function () {
var easysocialLimit = localStorageService.get("easysocial");
if (easysocialLimit != null) {
easysocial_count = angular.extend(easysocial_count, easysocialLimit);
}
var easyblogLimit = localStorageService.get("easyblog");
if (easyblogLimit != null) {
easyblog_count = angular.extend(easyblog_count, easyblogLimit);
}
};
// Method to hide loader
$rootScope.hideloader = function () {
$rootScope.loader.loading = false;
};
// Event which get executed on route change
$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
$rootScope.title = current.$$route.title;
});
//
// // Menu navigation method for handling menu item clicks
$rootScope.menunavigation = function (_path, _filter, _mainfilter) {
if (checkConnection()) {
if (localStorageService.get("auth_key")) {
if (_filter) {
localStorageService.set("currentuid", localStorageService.get("userid"));
localStorageService.set("currentuname", localStorageService.get("username"));
if (_mainfilter == "friends") {
localStorageService.set("friendlist", 1);
localStorageService.set("friendlistFilter", _filter);
} else if (_mainfilter == "grouplist") {
localStorageService.set("grouplistFilter", _filter);
} else if (_mainfilter == "discussions") {
localStorageService.set("discussionFilter", _filter);
} else if (_mainfilter == "allblogs") {
localStorageService.set("bloglistUserfilter", "");
} else {
localStorageService.set("newsfeedFilter", _filter);
localStorageService.set("startLimit", 0);
}
$route.reload();
}
$location.path(_path);
} else {
$rootScope.logoutApp();
}
}
};
//Menu selected active set
$rootScope.selected = 1;
$rootScope.select = function (item) {
$rootScope.selected = item.filterid;
};
$rootScope.logoutApp = function () {
//if(localStorageService.get("remember")==="true"){ //Remember for futher use
if (checkConnection()) {
juniteService.removeRegisterId().then(function (data) {
}, function (_error) {
$rootScope.showalert("warning", _error.message);
});
var urlIs = localStorageService.get("url");
var username = localStorageService.get("username");
var appRegid = localStorageService.get("appRegid");
localStorageService.clearAll();
localStorageService.set("url", urlIs);
localStorageService.set("username", username);
localStorageService.set("appRegid", appRegid);
$location.path(pathConstant.JUNITE_LOGIN_VIEW_PATH);
}
// openFB.getLoginStatus(function (response) {
//// alert(JSON.stringify(response));
// if (response.status == 'connected') {
// openFB.logout( function() {
//// alert('Logout Successfully.');
// },errorHandler);
// } else {
//// alert("Else:"+response.status);
// }
// });
};
//Notification setting open
$rootScope.toggleNotification = function (element) {
localStorageService.set("toggle", "");
$rootScope.toggle(element);
};
$rootScope.update = function (row) {
console.log(row);
};
//Initialize the notification setting.
$rootScope.initNotify = function (element) {
if (localStorageService.get("pushNotify") == 'false' || localStorageService.get("pushNotify") == false) {
$rootScope.pushNotify = false;
} else {
$rootScope.pushNotify = true;
}
};
$rootScope.backHistory = function () {
window.history.back();
};
//Change the notification on/off
$rootScope.updateNotify = function (element) {
if (checkConnection()) {
var notifyValue;
if (element.pushNotify == true || element.pushNotify == 'true') {
notifyValue = 1;
} else {
notifyValue = 0;
}
juniteService.postRegisterIdNotify(notifyValue).then(function (data) {
if (data.success == 'true' || data.success == true) {
localStorageService.set("pushNotify", element.pushNotify);
$rootScope.showalert("success", data.message);
}
}, function (_error) {
$rootScope.showalert("warning", _error.message);
});
}
};
$rootScope.openExternalLink = function (urlIs) {
if (urlIs) {
if (device.platform == "iOS") {
window.open(urlIs, "_system");
} else if (device.platform == "Android") {
navigator.app.loadUrl(urlIs, {
openExternal: true
});
} else {
window.open(urlIs, "_system");
}
return false;
} else {
}
};
var errorHandler = function (error) {
$rootScope.showalert("error", error.message);
}
function successHandler(result) {
//alert('Callback Success! Result = '+JSON.stringify(result));
}
// Check the authentication key of user
if (localStorageService.get("auth_key")) {
localStorageService.set("currentuid", localStorageService.get("userid"));
$location.path(custom_app.LOGIN_REDIRECT);
} else {
$location.path(pathConstant.JUNITE_LOGIN_VIEW_PATH);
}
//
// // Common alert method for creating notifiers with given msg
$rootScope.showalert = function (_type, _msg) {
toaster.clear();
toaster.pop(_type, "", "" + _msg);
};
function onDeviceReady() {
if(navigator && navigator.splashscreen)
navigator.splashscreen.hide();
$rootScope.online = navigator.onLine;
// Listener which get called when internet connection not available
$window.addEventListener("offline", function () {
$rootScope.$apply(function () {
$rootScope.online = false;
// $rootScope.showalert("error", $translate.instant('message.NO_INTERNET_CONNECTION'));
});
}, false);
// Listener which get called when internet connection available
$window.addEventListener("online", function () {
$rootScope.$apply(function () {
$rootScope.online = true;
});
}, false);
if (device.platform == "Android") {
var push = PushNotification.init({
"android": {
"senderID": $rootScope.custom_app.GCM_KEY
}
});
push.on('registration', function (data) {
localStorage.setItem("ls.appRegid", data.registrationId);
});
push.on('notification', function (data) {
push.finish(function () {
onNotificationRecieved(data);
console.log('finish successfully called');
});
});
push.on('error', function (e) {
console.log("push error");
});
} else if (device.platform == "iOS") {
var push = PushNotification.init({
"ios": {
"alert": "true",
"badge": "true",
"sound": "true"
}
});
push.on('registration', function (data) {
console.log(data);
localStorage.setItem("ls.appRegid", data.registrationId);
});
push.on('notification', function (data) {
push.finish(function () {
onNotificationRecieved(data);
console.log('finish successfully called');
});
});
push.on('error', function (e) {
console.log("push error");
});
}
}
});
function onNotificationRecieved(data) {
if (data.additionalData.foreground) {
//When app is already open
} else {
if (localStorage.getItem("ls.auth_key")) {
var href = data.additionalData.url;
var params = {},
queries, temp, i, l, action, caseid;
queries = href.split("&");
for (i = 0, l = queries.length; i < l; i++) {
temp = queries[i].split('=');
params[temp[0]] = temp[1];
}
var site_view = params.view;
var currentPath = window.location.hash;
if (site_view == "" || site_view == undefined || site_view == null) {
site_view = e.payload.view;
queries = href.split("/");
params.id = queries[queries.length - 1];
params.uid = queries[queries.length - 1];
}
switch (site_view) {
case 'profile':
localStorage.setItem("ls.currentuid", params.id);
window.location.hash = "#" + pathConstant.SOCIAL_VIEWPROFILE_VIEW_PATH;
break;
case 'groups':
var Id = params.id;
Id = Id.split(':');
localStorage.setItem("ls.groupid", Id[0]);
window.location.hash = "#" + pathConstant.SOCIAL_GROUPDETAIL_VIEW_PATH;
break;
case "apps":
var uId = params.uid;
uId = uId.split(':');
var Id = params.id;
Id = Id.split(':');
localStorage.setItem("ls.groupid", uId[0]);
localStorage.setItem("ls.discussionId", params.discussionId);
window.location.hash = "#" + pathConstant.SOCIAL_DISCUSSIONS_REPLY_VIEW_PATH;
break;
case 'conversations':
window.location.hash = "#" + pathConstant.SOCIAL_CONVERSATION_VIEW_PATH;
break;
case 'events':
localStorage.setItem("ls.eventid", params.id);
window.location.hash = "#" + pathConstant.SOCIAL_EVENTDETAIL_VIEW_PATH;
break;
case "albums":
var uId = params.uid;
uId = uId.split(':');
var Id = params.id;
Id = Id.split(':');
localStorage.setItem("ls.currentuid", uId[0]);
window.location.hash = "#" + pathConstant.SOCIAL_ALBUMSLIST_VIEW_PATH;
break;
case 'photos':
localStorage.setItem("ls.newsfeedFilter", "photos");
if (currentPath == "#" + pathConstant.SOCIAL_NEWSFEED_VIEW_PATH) {
location.reload();
} else {
window.location.hash = "#" + pathConstant.SOCIAL_NEWSFEED_VIEW_PATH;
}
break;
case 'stream':
default:
localStorage.setItem("ls.newsfeedFilter", "everyone");
if (currentPath == "#" + pathConstant.SOCIAL_NEWSFEED_VIEW_PATH) {
location.reload();
} else {
window.location.hash = "#" + pathConstant.SOCIAL_NEWSFEED_VIEW_PATH;
}
break;
}
}
}
}
//// Configure your routes here
juniteApp.config(function ($routeProvider, $httpProvider, $translateProvider) {
$translateProvider.useStaticFilesLoader({
files: [{
prefix: 'js/lang/easysocial_',
suffix: '.json'
},
{
prefix: 'components/blog/js/lang/blog_',
suffix: '.json'
},
{
prefix: 'components/jgive/js/lang/jgive_',
suffix: '.json'
}
]
});
$translateProvider.preferredLanguage('en');
// $translateProvider.useSanitizeValueStrategy('sanitize');
$routeProvider
// Route for the home page
.when(pathConstant.JUNITE_LOGIN_VIEW_PATH, {
templateUrl: templateConstant.JUNITE_LOGIN_TEMPLATE,
controller: 'LoginController',
title: titleConstant.JUNITE_LOGIN,
resolve: {
action: function () {
return 'loginview';
}
}
})
.when(pathConstant.JUNITE_REGISTERATION_VIEW_PATH, {
controller: 'LoginController',
templateUrl: templateConstant.JUNITE_REGISTERATION_VIEW_TEMPLATE,
title: titleConstant.JUNITE_REGISTRATION,
resolve: {
action: function () {
return 'register'
}
}
})
.when(pathConstant.JUNITE_TERMS_AND_CONDITIONS_VIEW_PATH, {
controller: 'LoginController',
templateUrl: templateConstant.JUNITE_TERMS_AND_CONDITIONS_VIEW_TEMPLATE,
title: titleConstant.JUNITE_TERMS_CONDITIONS,
resolve: {
action: function () {
return 'register'
}
}
})
// Route for the dashboard
.when(pathConstant.JUNITE_NOTIFY_SETTING_VIEW_PATH, {
templateUrl: templateConstant.JUNITE_NOTIFY_SETTING_TEMPLATE,
controller: 'LoginController',
title: titleConstant.JUNITE_DASHBOARDS,
resolve: {
action: function () {
return 'dashboardview';
}
}
})
// Route to default view
.otherwise({
redirectTo: pathConstant.JUNITE_LOGIN_VIEW_PATH
});
// Error handling service call
$httpProvider.interceptors.push('errorInterceptor');
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
// Attach back button listener for Android
document.addEventListener("backbutton", backButtonHandler, false);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment