@@ -6,6 +6,11 @@ var resgrid;
66 ( function ( edittraining ) {
77 var i18n = ( typeof resgridTrainingsI18n !== 'undefined' ) ? resgridTrainingsI18n : { } ;
88
9+ function escapeHtml ( str ) {
10+ if ( ! str ) return '' ;
11+ return String ( str ) . replace ( / & / g, '&' ) . replace ( / < / g, '<' ) . replace ( / > / g, '>' ) . replace ( / " / g, '"' ) . replace ( / ' / g, ''' ) ;
12+ }
13+
914 $ ( document ) . ready ( function ( ) {
1015 resgrid . common . analytics . track ( 'Training - Edit' ) ;
1116
@@ -46,7 +51,7 @@ var resgrid;
4651 $ ( '#rolesToAdd' ) . prop ( 'disabled' , false ) . trigger ( 'change.select2' ) ;
4752 $ ( '#usersToAdd' ) . prop ( 'disabled' , false ) . trigger ( 'change.select2' ) ;
4853 }
49- } ) ;
54+ } ) . trigger ( 'change' ) ;
5055
5156 function initSelect2 ( selector , placeholder , url ) {
5257 $ ( selector ) . select2 ( {
@@ -81,41 +86,34 @@ var resgrid;
8186 resgrid . training . edittraining . questionsCount = maxIndex ;
8287 } ) ;
8388 function addQuestion ( ) {
84- var removeTooltip = i18n . removeQuestionTooltip || 'Remove this question' ;
89+ var removeTooltip = escapeHtml ( i18n . removeQuestionTooltip || 'Remove this question' ) ;
8590 resgrid . training . edittraining . questionsCount ++ ;
8691 $ ( '#questions tbody' ) . first ( ) . append ( "<tr><td style='max-width: 215px;'><textarea id='question_" + edittraining . questionsCount + "' name='question_" + edittraining . questionsCount + "' rows='4' cols='40'></textarea></td><td>" + resgrid . training . edittraining . generateAnswersTable ( edittraining . questionsCount ) + "</td><td style='text-align:center;'><a onclick='$(this).parent().parent().remove();' class='tip-top' data-original-title='" + removeTooltip + "'><i class='fa fa-minus' style='color: red;'></i></a></td></tr>" ) ;
8792 }
8893 edittraining . addQuestion = addQuestion ;
8994 function generateAnswersTable ( count ) {
90- var addAnswerLabel = i18n . addAnswer || 'Add Answer' ;
91- var addAnswerTooltip = i18n . addQuestionTooltip || 'Add Answers to Question' ;
92- var correctLabel = i18n . correct || 'Correct' ;
93- var answerTextLabel = i18n . answerText || 'Answer Text' ;
94- var answersTable = '<table id="answersTable_' + count + '" class="table table-striped table-bordered"><thead><tr><th style="max-width:35px;font-size: 14px;" >' + correctLabel + '</th><th style = "font-size: 14px;" >' + answerTextLabel + '</th><th style = "font-size: 16px;" ><a id="addAnswerButton " class="btn btn-success btn-xs" onclick="resgrid.training.edittraining.addAnswer(' + count + ');" data-original-title="' + addAnswerTooltip + '" ><i class="icon-plus" ></i> ' + addAnswerLabel + '</a></th></tr></thead><tbody></tbody></table>' ;
95+ var addAnswerLabel = escapeHtml ( i18n . addAnswer || 'Add Answer' ) ;
96+ var addAnswerTooltip = escapeHtml ( i18n . addAnswerTooltip || 'Add Answers to Question' ) ;
97+ var correctLabel = escapeHtml ( i18n . correct || 'Correct' ) ;
98+ var answerTextLabel = escapeHtml ( i18n . answerText || 'Answer Text' ) ;
99+ var answersTable = '<table id="answersTable_' + count + '" class="table table-striped table-bordered"><thead><tr><th style="max-width:35px;font-size: 14px;" >' + correctLabel + '</th><th style = "font-size: 14px;" >' + answerTextLabel + '</th><th style = "font-size: 16px;" ><a id="addAnswerButton_' + count + ' " class="btn btn-success btn-xs" onclick="resgrid.training.edittraining.addAnswer(' + count + ');" data-original-title="' + addAnswerTooltip + '" ><i class="icon-plus" ></i> ' + addAnswerLabel + '</a></th></tr></thead><tbody></tbody></table>' ;
95100 return answersTable ;
96101 }
97102 edittraining . generateAnswersTable = generateAnswersTable ;
98103 function addAnswer ( count ) {
99104 var id = generate ( 4 ) ;
100- var answerRequired = i18n . answerRequired || 'Answer is required' ;
101- var removeAnswerTooltip = i18n . removeAnswerTooltip || 'Remove this answer from the question' ;
105+ var answerRequired = escapeHtml ( i18n . answerRequired || 'Answer is required' ) ;
106+ var removeAnswerTooltip = escapeHtml ( i18n . removeAnswerTooltip || 'Remove this answer from the question' ) ;
102107 $ ( '#answersTable_' + count + ' tbody' ) . append ( "<tr><td><input type='radio' name='answer_" + count + "' value='answerForQuestion_" + count + "_" + id + "'></td><td><textarea rows='3' cols='30' data-bv-notempty data-bv-notempty-message='" + answerRequired + "' id='answerForQuestion_" + count + "_" + id + "' name='answerForQuestion_" + count + "_" + id + "'></textarea></td><td style='text-align:center;'><a onclick='$(this).parent().parent().remove();' class='tip-top' data-original-title='" + removeAnswerTooltip + "'><i class='fa fa-minus' style='color: red;'></i></a></td></tr>" ) ;
103108 }
104109 edittraining . addAnswer = addAnswer ;
105110 function removeQuestion ( index ) {
106111 $ ( '#questionRow_' + index ) . remove ( ) ;
107112 }
108113 edittraining . removeQuestion = removeQuestion ;
109- function generate ( length ) {
110- var arr = [ ] ;
111- var n ;
112- for ( var i = 0 ; i < length ; i ++ ) {
113- do
114- n = Math . floor ( Math . random ( ) * 20 + 1 ) ;
115- while ( arr . indexOf ( n ) !== - 1 ) ;
116- arr [ i ] = n ;
117- }
118- return arr . join ( '' ) ;
114+ var _answerIdCounter = 0 ;
115+ function generate ( ) {
116+ return ++ _answerIdCounter ;
119117 }
120118 edittraining . generate = generate ;
121119 } ) ( edittraining = training . edittraining || ( training . edittraining = { } ) ) ;
0 commit comments