44 */
55
66//Module Declaration
7- var openNote = angular . module ( "openNote" , [ "ngRoute" , "ngResource" , "ngSanitize" , "ngAnimate" , "ui.tree" ] ) ;
7+ var openNote = angular . module ( "openNote" , [ "ngRoute" ,
8+ "ngResource" ,
9+ "ngSanitize" ,
10+ "ngAnimate" ,
11+ "ui.tree" ] ) ;
812
913/**
1014 * Used to redirect users to login if their token has expired
1115 * Runs on every route
1216 */
13- openNote . run ( function ( $rootScope , $location , userService , config , serverConfigService , $http ) {
17+ openNote . run ( function ( $rootScope ,
18+ $location ,
19+ userService ,
20+ config ,
21+ serverConfigService ,
22+ $http ) {
1423 $rootScope . $on ( "$routeChangeStart" , function ( event ) {
1524 //server config values
1625 serverConfigService . getConfig ( ) . then ( function ( config ) {
@@ -20,47 +29,64 @@ openNote.run(function ($rootScope, $location, userService, config, serverConfigS
2029 $rootScope . serverConfig = config ;
2130 } ) ; //attach server config to root scope
2231
23- /**
24- * Initial entry if not logged in
25- */
26- if ( ! userService . hasValidToken ( ) && $location . path ( ) != "/" ) {
27- event . preventDefault ( ) ;
28- $location . path ( "/" ) ;
29- }
30- else {
32+ if ( isLoggedInOrIsOnLoginScreen ( ) ) //Initial entry if not logged in
33+ forceLogin ( ) ;
34+ else //Initial entry after if logged in
35+ if ( $location . path ( ) != "/" && ! $rootScope . showMenu && ! $rootScope . showSideBar ) //make sure we only fade in/run once
36+ $rootScope . $emit ( "init" ) ;
37+
38+ } ) ;
39+
40+ /**
41+ * Check to see if user is logged in or on the login screen
42+ */
43+ var isLoggedInOrIsOnLoginScreen = function ( ) {
44+ return ! userService . hasValidToken ( ) && $location . path ( ) != "/" ;
45+ }
46+
47+ /**
48+ * Force user to login
49+ */
50+ var forceLogin = function ( ) {
51+ event . preventDefault ( ) ;
52+ $location . path ( "/" ) ;
53+ }
54+
55+ /**
56+ * Initialize app and start fade in
57+ */
58+ $rootScope . $on ( "init" , function ( ) {
59+ userService . useAPITokenHeader ( ) ; //use token
60+
61+ $rootScope . $on ( "$viewContentLoaded" , function ( ) { //wait for page to load before requesting list view
62+ $rootScope . $emit ( "reloadListView" ) ; //send an event to tell the list view to reload
63+ } ) ;
64+
65+ $rootScope . showMenu = true ;
66+ $rootScope . showSideBar = true ;
67+
68+ //options for humans
69+ $rootScope . helpContent = config . getHelpContent ( ) ;
70+
71+ $rootScope . showHelpButton = config . showHelpButton ( ) ;
72+ $rootScope . showLogOutButton = config . showLogOutButton ( ) ;
73+
3174 /**
32- * Initial entry after if logged in
75+ * Log out function
3376 */
34- if ( $location . path ( ) != "/" && ! $rootScope . showMenu && ! $rootScope . showSideBar ) { //make sure we only fade in/run once
35- userService . useAPITokenHeader ( ) ; //use token
36- $rootScope . $emit ( "reloadListView" , { } ) ; //send and event to tell the list view to reload
37- $rootScope . showMenu = true ;
38- $rootScope . showSideBar = true ;
39-
40- //options for humans
41- $rootScope . helpContent = config . getHelpContent ( ) ;
42-
43- $rootScope . showHelpButton = config . showHelpButton ( ) ;
44- $rootScope . showLogOutButton = config . showLogOutButton ( ) ;
45-
46- /**
47- * Log out function
48- */
49- $rootScope . logOut = function ( ) {
50- userService . destroyTokenHeader ( ) ;
51- window . location . href = '#/' ;
52- $rootScope . showMenu = false ;
53- $rootScope . showSideBar = false ;
54- }
55-
56- //Check for updates
57- $http . get ( config . getUpdateURL ( ) ) . then (
58- function ( response ) { //Successful
59- if ( response . data . version != config . getVersion ( ) )
60- alertify . log ( "<a href='" + response . data . updateURL + "' target='_blank'>" + response . data . updateText + "</a>" , "" , 0 ) ;
61- }
62- ) ;
63- }
64- }
65- } ) ;
77+ $rootScope . logOut = function ( ) {
78+ userService . destroyTokenHeader ( ) ;
79+ window . location . href = '#/' ;
80+ $rootScope . showMenu = false ;
81+ $rootScope . showSideBar = false ;
82+ }
83+
84+ //Check for updates
85+ $http . get ( config . getUpdateURL ( ) ) . then (
86+ function ( response ) { //Successful
87+ if ( response . data . version != config . getVersion ( ) )
88+ alertify . log ( "<a href='" + response . data . updateURL + "' target='_blank'>" + response . data . updateText + "</a>" , "" , 0 ) ;
89+ }
90+ ) ;
91+ } )
6692} ) ;
0 commit comments