@@ -11,16 +11,76 @@ goat.utils = {
11
11
}
12
12
return arr ;
13
13
} ,
14
+ debugFormSubmission : false ,
15
+ // pre-submit callback
16
+ showRequest : function ( formData , jqForm , options ) {
17
+ if ( goat . utils . debugFormSubmission ) {
18
+ // formData is an array; here we use $.param to convert it to a string to display it
19
+ // but the form plugin does this for you automatically when it submits the data
20
+ var queryString = $ . param ( formData ) ;
21
+
22
+ // jqForm is a jQuery object encapsulating the form element. To access the
23
+ // DOM element for the form do this:
24
+ // var formElement = jqForm[0];
25
+
26
+ alert ( 'About to submit: \n\n' + queryString ) ;
27
+ }
28
+ // here we could return false to prevent the form from being submitted;
29
+ // returning anything other than false will allow the form submit to continue
30
+ return true ;
31
+ } ,
32
+ // post-submit callback
33
+ showResponse : function ( responseText , statusText , xhr , $form ) {
34
+ // for normal html responses, the first argument to the success callback
35
+ // is the XMLHttpRequest object's responseText property
36
+
37
+ // if the ajaxForm method was passed an Options Object with the dataType
38
+ // property set to 'xml' then the first argument to the success callback
39
+ // is the XMLHttpRequest object's responseXML property
40
+
41
+ // if the ajaxForm method was passed an Options Object with the dataType
42
+ // property set to 'json' then the first argument to the success callback
43
+ // is the json data object returned by the server
44
+ if ( goat . utils . debugFormSubmission ) {
45
+ alert ( 'status: ' + statusText + '\n\nresponseText: \n' + responseText +
46
+ '\n\nThe output div should have already been updated with the responseText.' ) ;
47
+ }
48
+ // update lesson cookies and params
49
+ // make any embedded forms ajaxy
50
+ goat . utils . showLessonCookiesAndParams ( ) ;
51
+ // forms and links are now hooked with each standard lesson render (see Java class Screen.getContent())
52
+ // but these are safe to call twice
53
+ goat . utils . makeFormsAjax ( ) ;
54
+ goat . utils . ajaxifyAttackHref ( ) ; //TODO find some way to hook scope for current menu. Likely needs larger refactor which is already started/stashed
55
+ //refresh menu
56
+ angular . element ( $ ( '#leftside-navigation' ) ) . scope ( ) . renderMenu ( ) ;
57
+ } ,
14
58
makeFormsAjax : function ( ) {
59
+ // make all forms ajax forms
60
+ var options = {
61
+ target : '#lesson_content' , // target element(s) to be updated with server response
62
+ beforeSubmit : goat . utils . showRequest , // pre-submit callback, comment out after debugging
63
+ success : goat . utils . showResponse // post-submit callback, comment out after debugging
64
+
65
+ // other available options:
66
+ //url: url // override for form's 'action' attribute
67
+ //type: type // 'get' or 'post', override for form's 'method' attribute
68
+ //dataType: null // 'xml', 'script', or 'json' (expected server response type)
69
+ //clearForm: true // clear all form fields after successful submit
70
+ //resetForm: true // reset the form after successful submit
71
+
72
+ // $.ajax options can be used here too, for example:
73
+ //timeout: 3000
74
+ } ;
15
75
//console.log("Hooking any lesson forms to make them ajax");
16
76
$ ( "form" ) . ajaxForm ( options ) ;
17
77
} ,
18
- displayButton : function ( id , show ) {
19
- if ( $ ( '#' + id ) ) {
78
+ displayButton : function ( id , show ) {
79
+ if ( $ ( '#' + id ) ) {
20
80
if ( show ) {
21
- $ ( '#' + id ) . show ( ) ;
81
+ $ ( '#' + id ) . show ( ) ;
22
82
} else {
23
- $ ( '#' + id ) . hide ( ) ;
83
+ $ ( '#' + id ) . hide ( ) ;
24
84
}
25
85
}
26
86
} ,
@@ -52,17 +112,17 @@ goat.utils = {
52
112
$ ( '#lesson_plan_row' ) . show ( ) ;
53
113
goat . utils . scrollToHelp ( ) ;
54
114
} ,
55
- scrollToHelp :function ( ) {
56
- $ ( '#leftside-navigation' ) . height ( $ ( '#main-content' ) . height ( ) + 15 )
115
+ scrollToHelp : function ( ) {
116
+ $ ( '#leftside-navigation' ) . height ( $ ( '#main-content' ) . height ( ) + 15 )
57
117
var target = $ ( '#lessonHelpsWrapper' ) ;
58
118
goat . utils . scrollEasy ( target ) ;
59
119
} ,
60
120
scrollToTop : function ( ) {
61
121
$ ( '.lessonHelp' ) . hide ( ) ;
62
- var target = $ ( '#container' ) ;
122
+ var target = $ ( '#container' ) ;
63
123
goat . utils . scrollEasy ( target ) ;
64
124
} ,
65
- scrollEasy :function ( target ) {
125
+ scrollEasy : function ( target ) {
66
126
$ ( 'html,body' ) . animate ( {
67
127
scrollTop : target . offset ( ) . top
68
128
} , 1000 ) ;
@@ -73,7 +133,7 @@ goat.utils = {
73
133
}
74
134
var params = url . split ( '?' ) [ 1 ] . split ( '&' ) ;
75
135
var paramsArr = [ ] ;
76
- for ( var i = 0 ; i < params . length ; i ++ ) {
136
+ for ( var i = 0 ; i < params . length ; i ++ ) {
77
137
var paramObj = { } ;
78
138
paramObj . name = params [ i ] . split ( '=' ) [ 0 ] ;
79
139
paramObj . value = params [ i ] . split ( '=' ) [ 1 ] ;
@@ -84,33 +144,35 @@ goat.utils = {
84
144
highlightCurrentLessonMenu : function ( id ) {
85
145
//TODO: move selectors in first two lines into goatConstants
86
146
$ ( 'ul li.selected' ) . removeClass ( goatConstants . selectedMenuClass )
87
- $ ( 'ul li.selected a.selected' ) . removeClass ( goatConstants . selectedMenuClass )
88
- $ ( '#' + id ) . addClass ( goatConstants . selectedMenuClass ) ;
89
- $ ( '#' + id ) . parent ( ) . addClass ( goatConstants . selectedMenuClass ) ;
147
+ $ ( 'ul li.selected a.selected' ) . removeClass ( goatConstants . selectedMenuClass )
148
+ $ ( '#' + id ) . addClass ( goatConstants . selectedMenuClass ) ;
149
+ $ ( '#' + id ) . parent ( ) . addClass ( goatConstants . selectedMenuClass ) ;
90
150
} ,
91
- makeId : function ( lessonName ) {
92
- return lessonName . replace ( / \s | \( | \) | \! | \: | \; | \@ | \# | \$ | \% | \^ | \& | \* / g, '' ) ; //TODO move the replace routine into util function
151
+ makeId : function ( lessonName ) {
152
+ return lessonName . replace ( / \s | \( | \) | \! | \: | \; | \@ | \# | \$ | \% | \^ | \& | \* / g, '' ) ; //TODO move the replace routine into util function
93
153
} ,
94
- ajaxifyAttackHref : function ( ) {
154
+ ajaxifyAttackHref : function ( ) {
95
155
/* Jason I commented this implementation out
96
156
* I think we should show the attack link on the lessons that need it by modifying the lesson
97
157
* itself or we could add a new button up top for "show lesson link"
98
- $.each($('a[href^="attack?"]'),
99
- function(i,el) {
100
- var url = $(el).attr('href');
101
- $(el).attr('href','#');
102
- $(el).attr('link',url);
103
- //TODO pull currentMenuId
104
- $(el).click(
105
- function() {
106
- var _url = $(el).attr('link');
107
- $.get(_url, {success:showResponse});
108
- }
109
- )
110
- }
111
- );
112
- */
113
- // alternate implementation
158
+ $.each($('a[href^="attack?"]'),
159
+ function(i,el) {
160
+ var url = $(el).attr('href');
161
+ $(el).attr('href','#');
162
+ $(el).attr('link',url);
163
+ //TODO pull currentMenuId
164
+ $(el).click(
165
+ function() {
166
+ var _url = $(el).attr('link');
167
+ $.get(_url, {success:showResponse});
168
+ }
169
+ )
170
+ }
171
+ );
172
+ */
173
+ // alternate implementation
174
+ // unbind any bound events so we are safe to be called twice
175
+ $ ( '#lesson_content a' ) . unbind ( 'click' ) ;
114
176
$ ( '#lesson_content a' ) . bind ( 'click' , function ( event ) {
115
177
event . preventDefault ( ) ;
116
178
$ . get ( this . href , { } , function ( response ) {
0 commit comments