// Variable Declarations const $root = $('#editor-root') const target = $root.data('target') const id = $root.data('id') const $control_panel = $('.control-panel') const $info_panel = $('.info-panel') const $viewer_panel = $('.viewer-panel') const $editor_panel = $('.editor-panel') var toggle_info = false var toggle_viewer = false var element_index = 0 // Initialise Sortable and trigger renumbering on end of drag Sortable.create($root.get(0), {handle: '.move-handle', onEnd: function(evt) {renumber_blocks()}}) // Info and Viewer Button Listener $control_panel.find('button').click(function(event){ var action = $(this).data('action'); if (action == 'info') { if ($info_panel.is(":hidden")) { if ($viewer_panel.is(":visible")) { toggle_viewer = true $viewer_panel.hide() } $editor_panel.hide() $info_panel.fadeIn() $(window).scrollTop(0) toggle_info = false $(this).addClass('active') } else { $info_panel.hide() if (toggle_viewer) { render_viewer() $(window).scrollTop(0) toggle_viewer = false } else { $editor_panel.fadeIn() $(window).scrollTop(0) } $(this).removeClass('active') } } else if (action == 'view') { if ($viewer_panel.is(":hidden")) { if ($info_panel.is(':visible')) { toggle_info = true $info_panel.hide() } $editor_panel.hide() render_viewer() $(window).scrollTop(0) toggle_viewer = false $(this).addClass('active') } else { $viewer_panel.hide() if (toggle_info) { $info_panel.fadeIn() $(window).scrollTop(0) toggle_info = false } else { $editor_panel.fadeIn() $(window).scrollTop(0) } $(this).removeClass('active') } } event.preventDefault() }) // Control Button Listeners $root.on('click', '.block-controls > a', function(event){ event.preventDefault() var action = $(this).data('action') var root_accordion = $(this).closest('div').siblings('.accordion') if (action == 'add-question') { var question = generate_single_question(root_container_id=`#${root_accordion.attr('id')}`) $(question).appendTo(root_accordion).hide().fadeIn() if (root_accordion.children().length > 1 ) { root_accordion.find('.panel-controls > a[data-action="delete"]').removeClass('disabled') } else { root_accordion.find('.panel-controls > a[data-action="delete"]').addClass('disabled') } renumber_blocks() } }) $root.on('click', '.panel-controls > a', function(event) { event.preventDefault() event.stopPropagation() var action = $(this).data('action') var element = $(this).closest('.accordion-item') var root_container = $(this).closest('.accordion') if (action == 'delete') { element.fadeOut(function(){ $(this).remove() renumber_blocks() if (root_container.get(0) != $root.get(0) && root_container.children().length < 2 ) { root_container.find('.panel-controls > a[data-action="delete"]').addClass('disabled') } }) } else if (action == 'add-question') { var question = generate_single_question(root_container_id=`#${root_container.attr('id')}`) $(question).insertBefore(element).hide().fadeIn() if (root_container.get(0) != $root.get(0) && root_container.children().length > 1 ) { root_container.find('.panel-controls > a[data-action="delete"]').removeClass('disabled') } } else if (action == 'add-block') { var block = generate_block(root_container_id=`#${root_container.attr('id')}`) $(block).insertBefore(element).hide().fadeIn() var block_container = $(`#element${element_index-1}`).children().find('.accordion') Sortable.create(block_container.get(0), {handle: '.move-handle', onEnd: function(evt) {renumber_blocks()}}) var question = generate_single_question(root_container_id=`#${block_container.attr('id')}`) block_container.append(question) block_container.find('.panel-controls > a[data-action="delete"]').addClass('disabled') } renumber_blocks() }) $root.on('click', '.option-controls > a', function(event) { event.preventDefault() var action = $(this).data('action') var options = $(this).closest('div.option-controls').siblings('.options') var length = options.children().length var correct = $(this).closest('div.option-controls').siblings().find('.question-correct') if (action == 'delete') { if (length > 2) { options.children().last().fadeOut(function(){ $(this).remove() length = options.children().length if (length <= 2) { options.siblings('div.option-controls').children('a[data-action="delete"]').addClass('disabled') } else { options.siblings('div.option-controls').children('a[data-action="delete"]').removeClass('disabled') } }) correct.children().last().fadeOut(function(){ $(this).remove() }) } } else { var opt = `