11/**
2- * Created by IntelliJ IDEA.
2+ *
33 * User: bmiller
4- * Date: 4/20/11
4+ * Original: 2011-04-20
5+ * Date: 2019-06-14
56 * Time: 2:01 PM
6- * To change this template use File | Settings | File Templates.
7+ * This change marks the beginning of version 4.0 of the runestone components
8+ * Login/logout is no longer handled through javascript but rather server side.
9+ * Many of the components depend on the runestone:login event so we will keep that
10+ * for now to keep the churn fairly minimal.
711 */
812
913/*
@@ -54,67 +58,12 @@ $(function () {
5458
5559
5660//
57- // Logging functions
61+ // Page decoration functions
5862//
5963
64+ function addReadingList ( ) {
6065
61-
62- //
63- // Grading functions
64- //
65-
66- function comment ( blockid ) {
67- $ . modal ( '<iframe width="600" height="400" src="/getcomment?id=' + blockid + '" style="background-color: white">' , {
68- //$.modal('<form><textarea name="content"></textarea><input type="submit" name="submit" > </form>', {
69- overlayClose : true ,
70- closeHTML : "" ,
71- containerCss : {
72- width : 600 ,
73- height : 400 ,
74- backgroundColor : "#fff"
75- }
76- } ) ;
77- }
78-
79- function sendGrade ( grade , sid , acid , id ) {
80- var data = { 'sid' : sid , 'acid' : acid , 'grade' : grade , 'id' : id } ;
81- jQuery . get ( eBookConfig . ajaxURL + 'savegrade' , data ) ;
82- }
83-
84- function sendComment ( comment , sid , acid , id ) {
85- var data = { 'sid' : sid , 'acid' : acid , 'comment' : comment , 'id' : id } ;
86- jQuery . get ( eBookConfig . ajaxURL + 'savegrade' , data ) ;
87- }
88-
89- //
90- // User login and page decoration functions
91- //
92-
93- var rb = new RunestoneBase ( ) ;
94-
95- function gotUser ( data , status , whatever ) {
96- var mess = '' ;
97- var caughtErr = false ;
98- var d ;
99- try {
100- d = eval ( data ) [ 0 ] ;
101- } catch ( err ) {
102- if ( eBookConfig . loginRequired ) {
103- if ( confirm ( "Error: " + err . toString ( ) + "Please report this error! Click OK to continue without logging in. Cancel to retry." ) ) {
104- caughtErr = true ;
105- mess = "Not logged in" ;
106- $ ( document ) . trigger ( "runestone:logout" )
107- $ ( 'li.loginout' ) . html ( '<a href="' + eBookConfig . app + '/default/user/login">Login</a>' )
108- } else {
109- window . location . href = eBookConfig . app + '/default/user/login?_next=' + window . location . href
110- }
111- }
112- }
113- if ( d . course_list && d . course_list . indexOf ( eBookConfig . course ) < 0 ) {
114- alert ( `Hey there you appear to have wandered into ${ eBookConfig . course } But you are not registered for this course. Sending you to safety.` )
115- window . location . href = eBookConfig . app + '/default/courses' ;
116- }
117- if ( d . readings ) {
66+ if ( eBookConfig . readings ) {
11867 cur_path_parts = window . location . pathname . split ( '/' ) ;
11968 name = cur_path_parts [ cur_path_parts . length - 2 ] + '/' + cur_path_parts [ cur_path_parts . length - 1 ] ;
12069 position = d . readings . indexOf ( name ) ;
@@ -136,32 +85,7 @@ function gotUser(data, status, whatever) {
13685 }
13786 $ ( "#main-content" ) . append ( l ) ;
13887 }
139- if ( d . redirect ) {
140- if ( eBookConfig . loginRequired ) {
141- window . location . href = eBookConfig . app + '/default/user/login?_next=' + window . location . href
142- } else {
143- mess = "Not logged in" ;
144- $ ( document ) . trigger ( "runestone:logout" )
145- $ ( 'li.loginout' ) . html ( '<a href="' + eBookConfig . app + '/default/user/login">Login</a>' )
146- }
147- } else {
148- if ( ! caughtErr ) {
149- mess = "username: " + d . nick ;
150- // If the user is not an instructor then remove the link to the instructors page
151- if ( ! eBookConfig . isInstructor ) {
152- $ ( "#ip_dropdown_link" ) . remove ( )
153- }
154- $ ( document ) . trigger ( "runestone:login" )
155- timedRefresh ( ) ;
156- }
157- }
158- $ ( ".loggedinuser" ) . html ( mess ) ;
159- rb . logBookEvent ( {
160- 'event' : 'page' ,
161- 'act' : 'view' ,
162- 'div_id' : window . location . pathname
163- } )
164- notifyRunestoneComponents ( ) ;
88+
16589}
16690
16791
@@ -180,34 +104,30 @@ function timedRefresh() {
180104 $ . idleTimer ( timeoutPeriod ) ;
181105}
182106
183- function shouldLogin ( ) {
184- var sli = true ;
185-
186- if ( window . location . href . indexOf ( 'file://' ) > - 1 || ( ! eBookConfig . useRunestoneServices ) ) {
187- sli = false ;
188- }
189107
190- return sli ;
191- }
108+ function handlePageSetup ( ) {
192109
193- function isLoggedIn ( ) {
194- if ( typeof eBookConfig . isLoggedIn !== undefined ) {
195- return eBookConfig . isLoggedIn ;
196- }
197- return false ;
198- }
110+ let data = { timezoneoffset : ( new Date ( ) ) . getTimezoneOffset ( ) / 60 }
111+ jQuery . get ( eBookConfig . ajaxURL + 'set_tz_offset' , data , function ( ) { } ) . error ( notifyRunestoneComponents ) ;
199112
200- function handleLoginLogout ( ) {
201- if ( shouldLogin ( ) ) {
202- data = { timezoneoffset : ( new Date ( ) ) . getTimezoneOffset ( ) / 60 }
203- jQuery . get ( eBookConfig . ajaxURL + 'getuser' , data , gotUser ) . error ( notifyRunestoneComponents ) ;
113+ if ( eBookConfig . isLoggedIn ) {
114+ mess = `username: ${ eBookConfig . username } `
115+ if ( ! eBookConfig . isInstructor ) {
116+ $ ( "#ip_dropdown_link" ) . remove ( )
117+ }
118+ $ ( document ) . trigger ( "runestone:login" )
119+ addReadingList ( ) ;
120+ // timedRefresh() ??
204121 } else {
122+ mess = 'Not logged in'
205123 $ ( document ) . trigger ( "runestone:logout" )
206- // Let runestone components know they can run their javascript now
207- notifyRunestoneComponents ( ) ;
208124 }
125+ $ ( ".loggedinuser" ) . html ( mess ) ;
126+
127+ notifyRunestoneComponents ( ) ;
209128}
210129
130+
211131function setupNavbarLoggedIn ( ) {
212132 $ ( '#profilelink' ) . show ( ) ;
213133 $ ( '#passwordlink' ) . show ( ) ;
@@ -224,28 +144,9 @@ function setupNavbarLoggedOut() {
224144 $ ( '#ip_dropdown_link' ) . hide ( ) ;
225145 $ ( 'li.loginout' ) . html ( '<a href="' + eBookConfig . app + '/default/user/login">Login</a>' )
226146 $ ( ".footer" ) . html ( 'user not logged in' ) ;
227- rb . logBookEvent ( { 'event' : 'page' , 'act' : 'view' , 'div_id' : window . location . pathname } )
228147}
229148$ ( document ) . bind ( "runestone:logout" , setupNavbarLoggedOut ) ;
230149
231- function getOnlineUsers ( ) {
232- let MSCACHE = 60 * 1000 * 10 ;
233- if ( eBookConfig . useRunestoneServices ) {
234- let cacheValue = JSON . parse ( localStorage . getItem ( "users_online" ) ) ;
235- if ( cacheValue == null || cacheValue . timestamp < ( Date . now ( ) - MSCACHE ) ) {
236- $ . getJSON ( eBookConfig . ajaxURL + 'getnumonline' , setOnlineUsers )
237- } else {
238- $ ( "#numuserspan" ) . text ( cacheValue . onlineCount ) ;
239- }
240- }
241- }
242-
243- function setOnlineUsers ( data ) {
244- var d = data [ 0 ] ;
245- $ ( "#numuserspan" ) . text ( d . online ) ;
246- localStorage . setItem ( "users_online" , JSON . stringify ( { onlineCount : d . online , timestamp : Date . now ( ) } ) )
247- }
248-
249150
250151function notifyRunestoneComponents ( ) {
251152 // Runestone components wait until login process is over to load components because of storage issues
@@ -254,58 +155,15 @@ function notifyRunestoneComponents() {
254155 $pjQ ( document ) . trigger ( "runestone:login-complete" ) ; // for parsons components which are using a different version of jQuery
255156}
256157
257- //
258- // Nice interface for localstore -- Thanks acbart
259- //
260- //
261-
262- var storage = {
263- set : function ( directive , value ) {
264- localStorage . setItem ( directive + "_value" , value ) ;
265- localStorage . setItem ( directive + "_timestamp" , $ . now ( ) ) ;
266- } ,
267- remove : function ( directive ) {
268- localStorage . removeItem ( directive + "_value" ) ;
269- localStorage . removeItem ( directive + "_timestamp" ) ;
270- } ,
271- get : function ( directive ) {
272- return localStorage . getItem ( directive + "_value" ) ;
273- } ,
274- has : function ( directive ) {
275- return localStorage . getItem ( directive + "_value" ) !== null ;
276- } ,
277- // Tests whether the server has the newer version
278- is_new : function ( directive , server_time ) {
279- var stored_time = localStorage . getItem ( directive + "_timestamp" ) ;
280- return ( server_time >= stored_time + 5000 ) ;
281- } ,
282- } ;
283-
284- //
285- // delay function used by VT to autosave some component data
286- //
287-
288- var timers = { } ;
289- function addDelay ( directive , action , delay ) {
290- if ( delay === undefined ) {
291- delay = 400 ;
292- }
293- clearTimeout ( timers [ directive ] ) ;
294- timers [ directive ] = setTimeout ( action , delay ) ;
295- }
296-
297-
298158
299159// initialize stuff
300160$ ( document ) . ready ( function ( ) {
301161 if ( eBookConfig && eBookConfig . useRunestoneServices ) {
302- $ ( document ) . ready ( handleLoginLogout ) ;
303- $ ( document ) . ready ( getOnlineUsers ) ;
162+ handlePageSetup ( ) ;
304163 } else {
305164 if ( typeof eBookConfig === 'undefined' ) {
306165 console . log ( "eBookConfig is not defined. This page must not be set up for Runestone" ) ;
307166 }
308- notifyRunestoneComponents ( ) ;
309167 }
310168} ) ;
311169
0 commit comments