2016-07-29 08:57:28 -05:00
|
|
|
/**
|
|
|
|
* Summary Page Answer Editor
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function($){
|
|
|
|
$.fn.setTextAreaHeight = function(input) {
|
|
|
|
return this.each(function(){
|
|
|
|
var lineHeight = parseInt($(this).css('line-height'));
|
|
|
|
var rows = Math.ceil(input / lineHeight);
|
2016-08-04 22:28:20 -05:00
|
|
|
rows = rows === 0 ? 1 : rows;
|
2016-07-29 08:57:28 -05:00
|
|
|
|
|
|
|
$(this).attr('rows', rows);
|
2016-08-04 22:28:20 -05:00
|
|
|
});
|
|
|
|
};
|
2016-07-29 08:57:28 -05:00
|
|
|
|
|
|
|
$('input[type="radio"]').on('change', function() {
|
|
|
|
var inputName = $(this).attr('name');
|
|
|
|
//$('input[name="'+inputName+'"]').attr('checked', false);
|
|
|
|
var value = $(this).attr('value');
|
|
|
|
$('input[name="'+inputName+'"][value="'+value+'"]').attr("checked",true);
|
|
|
|
$('input[name="'+inputName+'"]').each(function() {
|
|
|
|
if($(this).val() != value) {
|
|
|
|
$(this).attr('checked', false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// $('.run-js').hide().delay();
|
|
|
|
}(jQuery));
|
|
|
|
|
|
|
|
var existingValue = [];
|
|
|
|
|
|
|
|
var editClickHandler = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var thisEd = $(e.delegateTarget);
|
|
|
|
var height = thisEd.find('p').height();
|
|
|
|
thisEd.data('answer', thisEd.find('p').text());
|
|
|
|
if(thisEd.find('input').attr('type') == 'radio') {
|
|
|
|
existingValue = thisEd.find('input:checked').val();
|
|
|
|
}
|
|
|
|
else if(thisEd.find('input').attr('type') == 'checkbox') {
|
|
|
|
$(thisEd.find('input')).each(function() {
|
2016-08-04 22:28:20 -05:00
|
|
|
if($(this).prop('checked') === true) {
|
2016-07-29 08:57:28 -05:00
|
|
|
existingValue.push($(this).val());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-08-05 09:52:11 -05:00
|
|
|
else if (thisEd.find('textarea:not(.code-answer)')) {
|
|
|
|
existingValue = thisEd.find('textarea:not(.code-answer)').val();
|
|
|
|
thisEd.find('.chars.hidden').removeClass('hidden');
|
|
|
|
}
|
2016-07-29 08:57:28 -05:00
|
|
|
|
|
|
|
$('.button-edit, .submit-button').addClass('disabled-button');
|
|
|
|
thisEd.addClass('editable');
|
2016-08-05 09:52:11 -05:00
|
|
|
// thisEd.find('.text-answer:not(.code-answer)').replaceWith('<textarea class="answer-block">' + $.trim(thisEd.data('answer')) + '</textarea>');
|
2016-07-29 08:57:28 -05:00
|
|
|
thisEd.find('.answer-block, .code-answer').prop('disabled', false);
|
|
|
|
thisEd.find('textarea').setTextAreaHeight(height);
|
|
|
|
thisEd.find('textarea.answer-block').focus();
|
|
|
|
thisEd.find('.button-edit').hide().delay();
|
|
|
|
thisEd.find('.button-save, .button-cancel').show().delay();
|
|
|
|
// thisEd.find('button.run-js').show().delay();
|
|
|
|
};
|
|
|
|
|
|
|
|
var cancelClickHandler = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var thisEd = $(e.delegateTarget);
|
|
|
|
if(thisEd.find('input').attr('type') == 'radio') {
|
|
|
|
$(thisEd.find('input')).each(function() {
|
|
|
|
if($(this).val()!=existingValue) {
|
|
|
|
$(this).attr('checked', false).prop('checked', false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$(this).prop('checked', true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if(thisEd.find('input').attr('type') == 'checkbox') {
|
|
|
|
$(existingValue).each(function(index, value) {
|
|
|
|
thisEd.find('input[value="'+value+'"]').prop('checked', true);
|
|
|
|
});
|
|
|
|
}
|
2016-08-05 09:52:11 -05:00
|
|
|
else if (thisEd.find('textarea:not(.code-answer)')) {
|
|
|
|
thisEd.find('textarea:not(.code-answer)').val(existingValue);
|
|
|
|
thisEd.find('.chars').addClass('hidden');
|
|
|
|
}
|
2016-07-29 08:57:28 -05:00
|
|
|
$('.success, .error').remove();
|
|
|
|
$('.button-edit, .submit-button').removeClass('disabled-button');
|
|
|
|
thisEd.removeClass('editable');
|
2016-08-05 09:52:11 -05:00
|
|
|
// thisEd.find('textarea:not(.code-answer)').replaceWith('<p class="text-answer answer-container">' + $.trim(thisEd.data('answer')) + '</p>');
|
2016-07-29 08:57:28 -05:00
|
|
|
thisEd.find('.answer-block, .code-answer').prop('disabled', true);
|
|
|
|
thisEd.find('.button-edit').show();
|
|
|
|
thisEd.find('.button-save, .button-cancel').hide();
|
|
|
|
// thisEd.find('button.run-js').hide();
|
|
|
|
existingValue = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
var saveClickHandler = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var thisEd = $(e.delegateTarget);
|
|
|
|
var data =[];
|
|
|
|
var executeQuery;
|
2016-08-04 22:28:20 -05:00
|
|
|
var questionId = thisEd.find('.button-edit').attr('data-questionId');
|
|
|
|
var answerId = thisEd.find('.button-edit').attr('data-answerId');
|
2016-07-29 14:51:20 -05:00
|
|
|
|
2016-08-01 15:58:20 -05:00
|
|
|
if (thisEd.hasClass('live_code-type')) {
|
2016-07-29 14:51:20 -05:00
|
|
|
var htmlAnswer = $(thisEd.find('textarea.code-html')[0]).val();
|
|
|
|
var cssAnswer = $(thisEd.find('textarea.code-css')[0]).val();
|
|
|
|
var jsAnswer = $(thisEd.find('textarea.code-js')[0]).val();
|
|
|
|
data = {
|
2016-08-02 13:49:03 -05:00
|
|
|
'live_code': {
|
|
|
|
'html': htmlAnswer,
|
|
|
|
'css': cssAnswer,
|
|
|
|
'js': jsAnswer
|
|
|
|
}
|
2016-08-04 22:28:20 -05:00
|
|
|
};
|
2016-08-01 15:58:20 -05:00
|
|
|
} else if(thisEd.hasClass('radio-type')) {
|
2016-07-29 08:57:28 -05:00
|
|
|
$(thisEd.find('input')).each(function() {
|
2016-08-04 22:28:20 -05:00
|
|
|
if($(this).prop('checked') === true) {
|
2016-08-01 15:58:20 -05:00
|
|
|
data = ({
|
|
|
|
'radio': $(this).val()
|
2016-08-04 22:28:20 -05:00
|
|
|
});
|
2016-07-29 08:57:28 -05:00
|
|
|
}
|
|
|
|
});
|
2016-08-01 15:58:20 -05:00
|
|
|
} else if(thisEd.hasClass('checkbox-type')) {
|
|
|
|
data = {'checkbox': []};
|
|
|
|
|
2016-07-29 08:57:28 -05:00
|
|
|
$(thisEd.find('input')).each(function() {
|
2016-08-04 22:28:20 -05:00
|
|
|
if($(this).prop('checked') === true) {
|
2016-08-01 15:58:20 -05:00
|
|
|
data.checkbox.push($(this).val());
|
2016-07-29 08:57:28 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2016-08-01 15:58:20 -05:00
|
|
|
data = {'text': thisEd.find('textarea').val()};
|
2016-07-29 08:57:28 -05:00
|
|
|
}
|
2016-08-04 22:28:20 -05:00
|
|
|
|
|
|
|
if(data === '') {
|
2016-07-29 08:57:28 -05:00
|
|
|
$(thisEd).before('<div class="error">Please select or enter a value.</div>');
|
|
|
|
} else {
|
2016-07-29 09:15:58 -05:00
|
|
|
thisEd.find('textarea:not(.code-answer)').replaceWith('<p class="text-answer answer-container">' + $.trim(thisEd.find('textarea').val()) + '</p>');
|
2016-08-01 15:58:20 -05:00
|
|
|
url = thisEd.closest('form').attr('action');
|
|
|
|
|
2016-07-29 08:57:28 -05:00
|
|
|
$.ajax({
|
|
|
|
type: "POST",
|
2016-08-01 15:58:20 -05:00
|
|
|
url: url,
|
|
|
|
data: ({
|
2016-08-04 22:28:20 -05:00
|
|
|
'answer': $.extend(data, {'question_id': questionId, 'answer_id': answerId}),
|
2016-08-01 15:58:20 -05:00
|
|
|
'submit': true
|
|
|
|
}),
|
2016-07-29 08:57:28 -05:00
|
|
|
success: function(data){
|
|
|
|
executeQuery = true;
|
|
|
|
},
|
|
|
|
error: function(data){
|
|
|
|
executeQuery = false;
|
|
|
|
}
|
|
|
|
}).done(function() {
|
2016-08-04 22:28:20 -05:00
|
|
|
if(executeQuery === true) {
|
2016-07-29 08:57:28 -05:00
|
|
|
$('.success, .error').remove();
|
|
|
|
$(thisEd).before('<div class="success">Your answer has been updated successfully!</div>');
|
|
|
|
$(thisEd).find('.code-answer').attr('disabled', true);
|
|
|
|
}
|
2016-08-04 22:28:20 -05:00
|
|
|
if(executeQuery === false) {
|
2016-07-29 08:57:28 -05:00
|
|
|
$('.error, .success').remove();
|
|
|
|
$(thisEd).before('<div class="error">Oops! There was an error processing your request. Please try again.</div>');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.button-edit, .submit-button').removeClass('disabled-button');
|
|
|
|
thisEd.removeClass('editable');
|
|
|
|
thisEd.find('.answer-block').prop('disabled', true);
|
|
|
|
thisEd.find('.button-edit').show();
|
|
|
|
thisEd.find('.button-save, .button-cancel').hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$('.answer-block').prop('disabled', true);
|
|
|
|
|
|
|
|
// Question events
|
2016-08-02 16:26:55 -05:00
|
|
|
$('.answer-sec')
|
2016-07-29 08:57:28 -05:00
|
|
|
.find('.button-cancel, .button-save').hide().end()
|
|
|
|
.on('click', '.button-edit', editClickHandler)
|
|
|
|
.on('click', '.button-cancel', cancelClickHandler)
|
|
|
|
.on('click', '.button-save', saveClickHandler);
|