function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
(function ($) {
"use strict"; // *******************
// create
// *****************
$('body').on('click', '#add_multiple_question', function (e) {
e.preventDefault();
var quizId = $(this).attr('data-quiz-id');
var multipleQuestionModal = $('.multipleQuestionModal' + quizId);
var clone = multipleQuestionModal.clone();
var id = 'correctAnswerSwitch' + randomString();
clone.find('label.js-switch').attr('for', id);
clone.find('input.js-switch').attr('id', id);
var random_id = randomString();
clone.find('.panel-file-manager').attr('data-input', random_id);
clone.find('.main-answer-row').removeClass('main-answer-row').addClass('main-answer-box');
var copyHtml = clone.prop('innerHTML');
copyHtml = copyHtml.replaceAll('record', random_id);
copyHtml = copyHtml.replaceAll('ans_tmp', 'ans_temp');
clone.html(copyHtml);
Swal.fire({
html: clone.html(),
showCancelButton: false,
showConfirmButton: false,
customClass: {
content: 'p-0 text-left'
},
width: '48rem'
});
});
$('body').on('click', '.add-answer-btn', function (e) {
e.preventDefault();
var mainRow = $('.add-answer-container .main-answer-box');
var copy = mainRow.clone();
copy.removeClass('main-answer-box');
copy.find('.answer-remove').removeClass('d-none');
var id = 'correctAnswerSwitch' + randomString();
copy.find('label.js-switch').attr('for', id);
copy.find('input.js-switch').attr('id', id);
copy.find('input[type="checkbox"]').prop('checked', false);
var copyHtml = copy.prop('innerHTML');
var nameId = randomString();
copyHtml = copyHtml.replaceAll('ans_temp', nameId);
copyHtml = copyHtml.replace(/\[\d+\]/g, '[' + nameId + ']');
copy.html(copyHtml);
copy.find('input[type="checkbox"]').prop('checked', false);
copy.find('input[type="text"]').val('');
mainRow.parent().append(copy);
});
$('body').on('click', '.answer-remove', function (e) {
e.preventDefault();
$(this).closest('.add-answer-card').remove();
});
function randomString() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < 5; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
$('body').on('click', '#add_descriptive_question', function (e) {
e.preventDefault();
var quizId = $(this).attr('data-quiz-id');
var multipleQuestionModal = $('.descriptiveQuestionModal' + quizId);
var clone = multipleQuestionModal.clone();
var random_id = randomString();
var copyHtml = clone.prop('innerHTML');
copyHtml = copyHtml.replaceAll('record', random_id);
copyHtml = copyHtml.replaceAll('ans_tmp', 'ans_temp');
clone.html(copyHtml);
Swal.fire({
html: clone.html(),
showCancelButton: false,
showConfirmButton: false,
customClass: {
content: 'p-0 text-left'
},
width: '48rem'
});
});
$('body').on('change', '.js-switch', function () {
var $this = $(this);
var parent = $this.closest('.js-switch-parent');
if (this.checked) {
$('.js-switch').each(function () {
var switcher = $(this);
var switcher_parent = switcher.closest('.js-switch-parent');
var switcher_input = switcher_parent.find('input[type="checkbox"]');
switcher_input.prop('checked', false);
});
$this.prop('checked', true);
}
});
$('body').on('click', '.save-question', function (e) {
e.preventDefault();
var $this = $(this);
var form = $this.closest('.quiz-questions-form');
var data = serializeObjectByTag(form);
var action = form.attr('data-action');
$this.addClass('loadingbar primary').prop('disabled', true);
form.find('input').removeClass('is-invalid');
form.find('textarea').removeClass('is-invalid');
$.post(action, data, function (result) {
if (result && result.code === 200) {
Swal.fire({
icon: 'success',
html: '
' + saveSuccessLang + '
',
showConfirmButton: false,
width: '25rem'
});
setTimeout(function () {
window.location.reload();
}, 500);
}
}).fail(function (err) {
$this.removeClass('loadingbar primary').prop('disabled', false);
var errors = err.responseJSON;
if (errors && errors.errors) {
Object.keys(errors.errors).forEach(function (key) {
var error = errors.errors[key];
var element = form.find('.js-ajax-' + key);
element.addClass('is-invalid');
element.parent().find('.invalid-feedback').text(error[0]);
});
}
});
});
$('body').on('click', '.js-submit-quiz-form', function (e) {
e.preventDefault();
var $this = $(this);
var form = $this.closest('.quiz-form');
var data = serializeObjectByTag(form);
var action = form.attr('data-action');
$this.addClass('loadingbar primary').prop('disabled', true);
form.find('input').removeClass('is-invalid');
form.find('textarea').removeClass('is-invalid');
$.post(action, data, function (result) {
if (result && result.code === 200) {
Swal.fire({
icon: 'success',
html: '' + saveSuccessLang + '
',
showConfirmButton: false
});
setTimeout(function () {
if (result.redirect_url && result.redirect_url !== '') {
window.location.href = result.redirect_url;
} else {
window.location.reload();
}
}, 2000);
}
}).fail(function (err) {
$this.removeClass('loadingbar primary').prop('disabled', false);
var errors = err.responseJSON;
if (errors && errors.errors) {
Object.keys(errors.errors).forEach(function (key) {
var error = errors.errors[key];
var element = form.find('.js-ajax-' + key);
element.addClass('is-invalid');
element.parent().find('.invalid-feedback').text(error[0]);
});
}
});
}); // *******************
// edit
// *****************
$('body').on('click', '.edit_question', function (e) {
e.preventDefault();
var $this = $(this);
var question_id = $this.attr('data-question-id');
loadingSwl();
$.get(adminPanelPrefix + '/quizzes-questions/' + question_id + '/edit', function (result) {
if (result && result.html) {
var $html = '' + result.html + '
';
Swal.fire({
html: $html,
showCancelButton: false,
showConfirmButton: false,
customClass: {
content: 'p-0 text-left'
},
width: '48rem',
onOpen: function onOpen() {
var editModal = $('#editQuestion');
editModal.find('.main-answer-row').removeClass('main-answer-row').addClass('main-answer-box');
var random_id = randomString();
editModal.find('.admin-file-manager').first().attr('data-input', random_id);
editModal.find('.lfm-input').first().attr('id', random_id);
var id = 'correctAnswerSwitch' + randomString();
editModal.find('label.js-switch').first().attr('for', id);
editModal.find('input.js-switch').first().attr('id', id);
}
});
}
});
});
$('body').on('change', '.js-quiz-question-locale', function (e) {
e.preventDefault();
var $this = $(this);
var $form = $(this).closest('.quiz-questions-form');
var locale = $this.val();
var item_id = $this.attr('data-id');
$this.addClass('loadingbar gray');
var path = adminPanelPrefix + '/quizzes-questions/' + item_id + '/getQuestionByLocale?locale=' + locale;
$.get(path, function (result) {
var question = result.question;
if (question.type === 'descriptive') {
var fields = ['title', 'correct'];
Object.keys(question).forEach(function (key) {
var value = question[key];
if ($.inArray(key, fields) !== -1) {
var element = $form.find('.js-ajax-' + key);
element.val(value);
}
});
} else {
$form.find('.js-ajax-title').val(question.title);
if (question.quizzes_questions_answers && question.quizzes_questions_answers.length) {
var answers = question.quizzes_questions_answers;
var _iterator = _createForOfIteratorHelper(answers),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var answer = _step.value;
if (answer) {
$form.find('.js-ajax-answer-title-' + answer.id).val(answer.title);
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
}
$this.removeClass('loadingbar gray');
}).fail(function (err) {
$this.removeClass('loadingbar gray');
});
});
$('body').on('change', '.js-ajax-display_limited_questions', function () {
var $input = $('.js-display-limited-questions-count-field');
$input.find('input').val('');
if (this.checked) {
$input.removeClass('d-none');
} else {
$input.addClass('d-none');
}
});
$(document).ready(function () {
var style = getComputedStyle(document.body);
var primaryColor = style.getPropertyValue('--primary');
function updateToDatabase(table, quizId, idString) {
$.post(adminPanelPrefix + '/quizzes/' + quizId + '/order-items', {
table: table,
items: idString
}, function (result) {
if (result && result.title && result.msg) {
$.toast({
heading: result.title,
text: result.msg,
bgColor: primaryColor,
textColor: 'white',
hideAfter: 10000,
position: 'bottom-right',
icon: 'success'
});
}
});
}
function setSortable(target) {
if (target.length) {
target.sortable({
group: 'no-drop',
handle: '.move-icon',
axis: "y",
update: function update(e, ui) {
var sortData = target.sortable('toArray', {
attribute: 'data-id'
});
var table = e.target.getAttribute('data-order-table');
var quizId = e.target.getAttribute('data-quiz');
updateToDatabase(table, quizId, sortData.join(','));
}
});
}
}
var items = [];
var draggableContentLists = $('.draggable-questions-lists');
if (draggableContentLists.length) {
var _iterator2 = _createForOfIteratorHelper(draggableContentLists),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var item = _step2.value;
items.push($(item).attr('data-drag-class'));
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
}
if (items.length) {
var _iterator3 = _createForOfIteratorHelper(items),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var _item = _step3.value;
var tag = $('.' + _item);
if (tag.length) {
setSortable(tag);
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
}
});
/* feather icons */
// **
// **
feather.replace();
})(jQuery);