diff --git a/app/assets/javascripts/my_modules/results.js.erb b/app/assets/javascripts/my_modules/results.js.erb index 7338c06de..ad3608600 100644 --- a/app/assets/javascripts/my_modules/results.js.erb +++ b/app/assets/javascripts/my_modules/results.js.erb @@ -1,199 +1,238 @@ //= require assets //= require comments -/** - * Initializes page - */ -function init() { - initHandsOnTables($(document)); - expandAllResults(); - initTutorial(); - applyCollapseLinkCallBack(); +(function(global) { + 'use strict'; - Comments.bindNewElement(); - Comments.initialize(); + global.Results = (function() { + var ResultTypeEnum = Object.freeze({ + FILE: 0, + TABLE: 1, + TEXT: 2, + COMMENT: 3 + }); - Comments.initCommentOptions("ul.content-comments"); - Comments.initEditComments("#results"); - Comments.initDeleteComments("#results"); + function init() { + initHandsOnTables($(document)); + _expandAllResults(); + _initTutorial(); + applyCollapseLinkCallBack(); - $(function () { - $("#results-collapse-btn").click(function () { - $('.result .panel-collapse').collapse('hide'); - $(document).find("span.collapse-result-icon").each(function() { - $(this).addClass("glyphicon-collapse-down"); - $(this).removeClass("glyphicon-collapse-up"); + Comments.bindNewElement(); + Comments.initialize(); + + Comments.initCommentOptions('ul.content-comments'); + Comments.initEditComments('#results'); + Comments.initDeleteComments('#results'); + + $(function () { + $('#results-collapse-btn').click(function () { + $('.result .panel-collapse').collapse('hide'); + $(document).find('span.collapse-result-icon').each(function() { + $(this).addClass('glyphicon-collapse-down'); + $(this).removeClass('glyphicon-collapse-up'); + }); + }); + + $('#results-expand-btn').click(_expandAllResults); }); - }); - $("#results-expand-btn").click(expandAllResults); - }); - - // This checks if the ctarget param exist in the rendered url and opens the - // comment tab - if( getParam('ctarget') ){ - var target = "#"+ getParam('ctarget'); - $(target).find('a.comment-tab-link').click(); - } -} - -function initHandsOnTables(root) { - root.find("div.hot-table").each(function() { - var $container = $(this).find(".step-result-hot-table"); - var contents = $(this).find('.hot-contents'); - - $container.handsontable({ - width: '100%', - startRows: 5, - startCols: 5, - rowHeaders: true, - colHeaders: true, - fillHandle: false, - formulas: true, - cells: function (row, col, prop) { - var cellProperties = {}; - - if (col >= 0) - cellProperties.readOnly = true; - else - cellProperties.readOnly = false; - - return cellProperties; + // This checks if the ctarget param exist in the rendered url and opens the + // comment tab + if( getParam('ctarget') ){ + var target = '#'+ getParam('ctarget'); + $(target).find('a.comment-tab-link').click(); } - }); - var hot = $container.handsontable('getInstance'); - var data = JSON.parse(contents.attr("value")); - hot.loadData(data.data); + } - }); -} + function initHandsOnTables(root) { + root.find('div.hot-table').each(function() { + var $container = $(this).find('.step-result-hot-table'); + var contents = $(this).find('.hot-contents'); -function applyCollapseLinkCallBack() { - $(".result-panel-collapse-link") - .on("ajax:success", function() { - var collapseIcon = $(this).find(".collapse-result-icon"); - var collapsed = $(this).hasClass("collapsed"); - // Toggle collapse button - collapseIcon.toggleClass("glyphicon-collapse-up", !collapsed); - collapseIcon.toggleClass("glyphicon-collapse-down", collapsed); - }); -} + $container.handsontable({ + width: '100%', + startRows: 5, + startCols: 5, + rowHeaders: true, + colHeaders: true, + fillHandle: false, + formulas: true, + cells: function (row, col, prop) { + var cellProperties = {}; -// Toggle editing buttons -function toggleResultEditButtons(show) { - if (show) { - $("#results-toolbar").show(); - $(".edit-result-button").show(); - } else { - $(".edit-result-button").hide(); - $("#results-toolbar").hide(); - } -} + if (col >= 0) + cellProperties.readOnly = true; + else + cellProperties.readOnly = false; -// Expand all results -function expandAllResults() { - $('.result .panel-collapse').collapse('show'); - $(document).find("span.collapse-result-icon").each(function() { - $(this).addClass("glyphicon-collapse-up"); - $(this).removeClass("glyphicon-collapse-down"); - }); - $(document).find("div.step-result-hot-table").each(function() { - renderTable(this); - }); -} + return cellProperties; + } + }); + var hot = $container.handsontable('getInstance'); + var data = JSON.parse(contents.attr('value')); + hot.loadData(data.data); + }); + } -function expandResult(result) { - $('.panel-collapse', result).collapse('show'); - $(result).find("span.collapse-result-icon").each(function() { - $(this).addClass("glyphicon-collapse-up"); - $(this).removeClass("glyphicon-collapse-down"); - }); - renderTable($(result).find("div.step-result-hot-table")); - animateSpinner(null, false); - setupAssetsLoading(); -} + function applyCollapseLinkCallBack() { + $('.result-panel-collapse-link') + .on('ajax:success', function() { + var collapseIcon = $(this).find('.collapse-result-icon'); + var collapsed = $(this).hasClass('collapsed'); + // Toggle collapse button + collapseIcon.toggleClass('glyphicon-collapse-up', !collapsed); + collapseIcon.toggleClass('glyphicon-collapse-down', collapsed); + }); + } -function renderTable(table) { - $(table).handsontable("render"); - // Yet another dirty hack to solve HandsOnTable problems - if (parseInt($(table).css("height"), 10) < parseInt($(table).css("max-height"), 10) - 30) { - $(table).find(".ht_master .wtHolder").css({ 'height': '100%', - 'width': '100%' }); - } -} - -/** - * Initializes tutorial - */ -function initTutorial() { - var stepNum = parseInt(Cookies.get('current_tutorial_step'), 10); - if (stepNum >= 15 && stepNum <= 16) { - var samplesTab = $('#module-samples-nav-tab'); - - var nextPage = samplesTab.find('a').attr('href'); - var steps = [{ - element: $('#results-toolbar')[0], - intro: I18n.t('tutorial.module_results_html') - }, { - element: samplesTab[0], - intro: I18n.t('tutorial.module_results_click_samples_html'), - position: 'left' - }]; - initPageTutorialSteps(15, 16, nextPage, function() {}, function() {}, - steps); - } -} - -var ResultTypeEnum = Object.freeze({ - FILE: 0, - TABLE: 1, - TEXT: 2, - COMMENT: 3 -}); - -function processResult(ev, resultTypeEnum, editMode) { - - var $form = $(ev.target.form); - $form.clearFormErrors(); - - switch(resultTypeEnum) { - case ResultTypeEnum.FILE: - var $nameInput = $form.find("#result_name"); - var nameValid = textValidator(ev, $nameInput, 0, - <%= Constants::NAME_MAX_LENGTH %>); - var $fileInput = $form.find("#result_asset_attributes_file"); - var filesValid = true; - if ($fileInput.val()) { - filesValid = filesValidator(ev, $fileInput, FileTypeEnum.FILE, - editMode); + // Toggle editing buttons + function toggleResultEditButtons(show) { + if(show) { + $('#results-toolbar').show(); + $('.edit-result-button').show(); + } else { + $('.edit-result-button').hide(); + $('#results-toolbar').hide(); } + } - if(nameValid && filesValid) { - // Local file uploading + // Expand all results + function _expandAllResults() { + $('.result .panel-collapse').collapse('show'); + $(document).find('span.collapse-result-icon').each(function() { + $(this).addClass('glyphicon-collapse-up'); + $(this).removeClass('glyphicon-collapse-down'); + }); + $(document).find('div.step-result-hot-table').each(function() { + _renderTable(this); + }); + } + + function expandResult(result) { + $('.panel-collapse', result).collapse('show'); + $(result).find('span.collapse-result-icon').each(function() { + $(this).addClass('glyphicon-collapse-up'); + $(this).removeClass('glyphicon-collapse-down'); + }); + _renderTable($(result).find('div.step-result-hot-table')); + animateSpinner(null, false); + setupAssetsLoading(); + } + + function _renderTable(table) { + $(table).handsontable('render'); + // Yet another dirty hack to solve HandsOnTable problems + if (parseInt($(table).css('height'), 10) < + parseInt($(table).css('max-height'), 10) - 30) { + $(table).find('.ht_master .wtHolder').css({ 'height': '100%', + 'width': '100%' }); + } + } + + /** + * Initializes tutorial + */ + function _initTutorial() { + var stepNum = parseInt(Cookies.get('current_tutorial_step'), 10); + if (stepNum >= 15 && stepNum <= 16) { + var samplesTab = $('#module-samples-nav-tab'); + + var nextPage = samplesTab.find('a').attr('href'); + var steps = [{ + element: $('#results-toolbar')[0], + intro: I18n.t('tutorial.module_results_html') + }, { + element: samplesTab[0], + intro: I18n.t('tutorial.module_results_click_samples_html'), + position: 'left' + }]; + initPageTutorialSteps(15, 16, nextPage, function() {}, function() {}, + steps); + } + } + + function processResult(ev, resultTypeEnum, editMode) { + var $form = $(ev.target.form); + $form.clearFormErrors(); + + switch(resultTypeEnum) { + case ResultTypeEnum.FILE: + var $nameInput = $form.find('#result_name'); + var nameValid = textValidator(ev, $nameInput, 0, + <%= Constants::NAME_MAX_LENGTH %>); + var $fileInput = $form.find('#result_asset_attributes_file'); + var filesValid = true; + if ($fileInput.val()) { + filesValid = filesValidator(ev, $fileInput, FileTypeEnum.FILE, + editMode); + } + + if(nameValid && filesValid) { + // Local file uploading + animateSpinner(); + + } + break; + case ResultTypeEnum.TABLE: + var $nameInput = $form.find('#result_name'); + var nameValid = textValidator(ev, $nameInput, 0, + <%= Constants::NAME_MAX_LENGTH %>); + break; + case ResultTypeEnum.TEXT: + var $nameInput = $form.find('#result_name'); + var nameValid = textValidator(ev, $nameInput, 0, + <%= Constants::NAME_MAX_LENGTH %>); + var $textInput = TinyMCE.getContent(); + textValidator(ev, $textInput, 1, <%= Constants::TEXT_MAX_LENGTH %>, false, true); + break; + case ResultTypeEnum.COMMENT: + var $commentInput = $form.find('#comment_message'); + var commentValid = textValidator(ev, $commentInput, 1, + <%= Constants::TEXT_MAX_LENGTH %>); + break; + } + } + + // init cancel button + function initCancelFormButton(form, callback) { + $(form).find('.cancel-new').click(function(event) { + event.preventDefault(); + event.stopPropagation(); + event.stopImmediatePropagation(); + $(form).remove(); + toggleResultEditButtons(true); + callback(); + }); + } + + function archive(e, element) { + e.preventDefault(); + e.stopPropagation(); + e.stopImmediatePropagation(); + var el = $(element); + if(confirm(el.data('confirm-text'))) { animateSpinner(); - + $('#' + el.data('form-id')).submit(); } - break; - case ResultTypeEnum.TABLE: - var $nameInput = $form.find("#result_name"); - var nameValid = textValidator(ev, $nameInput, 0, - <%= Constants::NAME_MAX_LENGTH %>); - break; - case ResultTypeEnum.TEXT: - var $nameInput = $form.find("#result_name"); - var nameValid = textValidator(ev, $nameInput, 0, - <%= Constants::NAME_MAX_LENGTH %>); - var $textInput = TinyMCE.getContent(); - textValidator(ev, $textInput, 1, <%= Constants::TEXT_MAX_LENGTH %>, false, true); - break; - case ResultTypeEnum.COMMENT: - var $commentInput = $form.find("#comment_message"); - var commentValid = textValidator(ev, $commentInput, 1, - <%= Constants::TEXT_MAX_LENGTH %>); - break; - } -} + } -$(document).ready(function(){ - init(); -}); + var publicAPI = Object.freeze({ + init: init, + initHandsOnTables: initHandsOnTables, + applyCollapseLinkCallBack: applyCollapseLinkCallBack, + toggleResultEditButtons: toggleResultEditButtons, + expandResult: expandResult, + processResult: processResult, + ResultTypeEnum: ResultTypeEnum, + initCancelFormButton: initCancelFormButton, + archive: archive + }); + + return publicAPI; + })(); + + $(document).ready(function(){ + Results.init(); + }); +})(window); diff --git a/app/assets/javascripts/results/result_assets.js b/app/assets/javascripts/results/result_assets.js index a97ecfc6f..93493a8a2 100644 --- a/app/assets/javascripts/results/result_assets.js +++ b/app/assets/javascripts/results/result_assets.js @@ -1,79 +1,97 @@ -// New result asset behaviour -$("#new-result-asset").on("ajax:success", function(e, data) { - var $form = $(data.html); - $("#results").prepend($form); +(function(global) { + 'use strict'; - formAjaxResultAsset($form); + var ResutlAssets = (function() { + // New result asset behaviour + function initNewResultAsset() { + $('#new-result-asset').on('click', function(event) { + event.preventDefault(); + event.stopImmediatePropagation(); + event.stopPropagation(); + var $btn = $(this); + $btn.off(); + animateSpinner(null, true); - // Cancel button - $form.find(".cancel-new").click(function () { - $form.remove(); - toggleResultEditButtons(true); - }); + // get new result form + $.ajax({ + url: $btn.data('href'), + method: 'GET', + success: function(data) { + var $form = $(data.html); + animateSpinner(null, false); + $('#results').prepend($form); + _formAjaxResultAsset($form); + Results.initCancelFormButton($form, initNewResultAsset); + Results.toggleResultEditButtons(false); + $('#result_name').focus(); + }, + error: function(xhr, status, e) { + $(this).renderFormErrors('result', xhr.responseJSON, true, e); + animateSpinner(null, false); + initNewResultAsset(); + } + }); + }); + } - toggleResultEditButtons(false); + function applyEditResultAssetCallback() { + $('.edit-result-asset').on('ajax:success', function(e, data) { + var $result = $(this).closest('.result'); + var $form = $(data.html); + var $prevResult = $result; + $result.after($form); + $result.remove(); - $("#result_name").focus(); -}).on('ajax:error', function(e, xhr) { - $(this).renderFormErrors('result', xhr.responseJSON, true, e); - animateSpinner(null, false); -}); + _formAjaxResultAsset($form); -$("#new-result-asset").on("ajax:error", function(e, xhr, status, error) { - animateSpinner(null, false); -}); + // Cancel button + $form.find('.cancel-edit').click(function () { + $form.after($prevResult); + $form.remove(); + applyEditResultAssetCallback(); + Results.toggleResultEditButtons(true); + initPreviewModal(); + }); -// Edit result asset button behaviour -function applyEditResultAssetCallback() { - $(".edit-result-asset").on("ajax:success", function(e, data) { - var $result = $(this).closest(".result"); - var $form = $(data.html); - var $prevResult = $result; - $result.after($form); - $result.remove(); + Results.toggleResultEditButtons(false); - formAjaxResultAsset($form); + $('#result_name').focus(); + }).on('ajax:error', function(e, xhr, status, error) { + animateSpinner(null, false); + }); + } - // Cancel button - $form.find(".cancel-edit").click(function () { - $form.after($prevResult); - $form.remove(); - applyEditResultAssetCallback(); - toggleResultEditButtons(true); - initPreviewModal(); + function _formAjaxResultAsset($form) { + $form.on('ajax:success', function(e, data) { + $form.after(data.html); + var $newResult = $form.next(); + initFormSubmitLinks($newResult); + $(this).remove(); + applyEditResultAssetCallback(); + Results.applyCollapseLinkCallBack(); + + Results.toggleResultEditButtons(true); + Results.expandResult($newResult); + initPreviewModal(); + Comments.initialize(); + initNewResultAsset(); + }).on('ajax:error', function(xhr, status, e) { + $form.renderFormErrors('result', xhr.responseJSON, true, e); + animateSpinner(null, false); + }); + } + + var publicAPI = Object.freeze({ + initNewResultAsset: initNewResultAsset, + applyEditResultAssetCallback: applyEditResultAssetCallback }); - toggleResultEditButtons(false); + return publicAPI; + })(); - $("#result_name").focus(); + $(document).ready(function() { + ResutlAssets.initNewResultAsset(); + ResutlAssets.applyEditResultAssetCallback(); + global.initPreviewModal(); }); - - $(".edit-result-asset").on("ajax:error", function(e, xhr, status, error) { - animateSpinner(null, false); - }); -} - -// Apply ajax callback to form -function formAjaxResultAsset($form) { - $form - .on("ajax:success", function(e, data) { - $form.after(data.html); - var $newResult = $form.next(); - initFormSubmitLinks($newResult); - $(this).remove(); - applyEditResultAssetCallback(); - applyCollapseLinkCallBack(); - - toggleResultEditButtons(true); - expandResult($newResult); - initPreviewModal(); - Comments.initialize(); - }) - .on('ajax:error', function(e, xhr) { - $form.renderFormErrors('result', xhr.responseJSON, true, e); - animateSpinner(null, false); - }); -} - -applyEditResultAssetCallback(); -initPreviewModal(); +})(window); diff --git a/app/assets/javascripts/results/result_tables.js.erb b/app/assets/javascripts/results/result_tables.js.erb index 8994d10e5..c14ae013f 100644 --- a/app/assets/javascripts/results/result_tables.js.erb +++ b/app/assets/javascripts/results/result_tables.js.erb @@ -1,111 +1,135 @@ -// Init handsontable which can be edited -function initEditableHandsOnTable(root) { - root.find(".editable-table").each(function() { - var $container = $(this).find(".hot"); - var data = null; - var contents = $(this).find('.hot-contents'); - if (contents.attr("value")) { - data = JSON.parse(contents.attr("value")).data; - } +(function() { + 'use strict'; - $container.handsontable({ - data: data, - startRows: <%= Constants::HANDSONTABLE_INIT_ROWS_CNT %>, - startCols: <%= Constants::HANDSONTABLE_INIT_COLS_CNT %>, - minRows: 1, - minCols: 1, - rowHeaders: true, - colHeaders: true, - contextMenu: true, - formulas: true, - preventOverflow: 'horizontal' - }); - }); -} + var ResultTables = (function() { + // Init handsontable which can be edited + function _initEditableHandsOnTable(root) { + root.find('.editable-table').each(function() { + var $container = $(this).find('.hot'); + var data = null; + var contents = $(this).find('.hot-contents'); + if (contents.attr('value')) { + data = JSON.parse(contents.attr('value')).data; + } -function onSubmitExtractTable($form) { - $form.submit(function(){ - var hot = $(".hot").handsontable('getInstance'); - var contents = $('.hot-contents'); - var data = JSON.stringify({data: hot.getData()}); - contents.attr("value", data); - return true; - }); -} + $container.handsontable({ + data: data, + startRows: <%= Constants::HANDSONTABLE_INIT_ROWS_CNT %>, + startCols: <%= Constants::HANDSONTABLE_INIT_COLS_CNT %>, + minRows: 1, + minCols: 1, + rowHeaders: true, + colHeaders: true, + contextMenu: true, + formulas: true, + preventOverflow: 'horizontal' + }); + }); + } -// Edit result table button behaviour -function applyEditResultTableCallback() { - $(".edit-result-table").on("ajax:success", function(e, data) { - var $result = $(this).closest(".result"); - var $form = $(data.html); - var $prevResult = $result; - $result.after($form); - $result.remove(); + function _onSubmitExtractTable($form) { + $form.submit(function(){ + var hot = $('.hot').handsontable('getInstance'); + var contents = $('.hot-contents'); + var data = JSON.stringify({data: hot.getData()}); + contents.attr('value', data); + return true; + }); + } - formAjaxResultTable($form); - initEditableHandsOnTable($form); - onSubmitExtractTable($form); + // Apply ajax callback to form + function _formAjaxResultTable($form) { + $form.on('ajax:success', function(e, data) { + var $result; + $form.after(data.html); + $result = $(this).next(); + initFormSubmitLinks($result); + $(this).remove(); - // Cancel button - $form.find(".cancel-edit").click(function () { - $form.after($prevResult); - $form.remove(); - applyEditResultTableCallback(); - toggleResultEditButtons(true); + applyEditResultTableCallback(); + Results.applyCollapseLinkCallBack(); + Results.initHandsOnTables($result); + Results.toggleResultEditButtons(true); + Results.expandResult($result); + Comments.initialize(); + initNewResultTable(); + }); + $form.on('ajax:error', function(e, xhr, status, error) { + var data = xhr.responseJSON; + $form.renderFormErrors('result', data); + }); + } + + // Edit result table button behaviour + function applyEditResultTableCallback() { + $('.edit-result-table').on('ajax:success', function(e, data) { + var $result = $(this).closest('.result'); + var $form = $(data.html); + var $prevResult = $result; + $result.after($form); + $result.remove(); + + _formAjaxResultTable($form); + _initEditableHandsOnTable($form); + _onSubmitExtractTable($form); + + // Cancel button + $form.find('.cancel-edit').click(function () { + $form.after($prevResult); + $form.remove(); + applyEditResultTableCallback(); + Results.toggleResultEditButtons(true); + }); + + Results.toggleResultEditButtons(false); + + $('#result_name').focus(); + }); + } + + // New result text behaviour + function initNewResultTable() { + $('#new-result-table').on('click', function(event) { + event.preventDefault(); + event.stopImmediatePropagation(); + event.stopPropagation(); + var $btn = $(this); + $btn.off(); + animateSpinner(null, true); + // get new result form + $.ajax({ + url: $btn.data('href'), + method: 'GET', + success: function(data) { + var $form = $(data.html); + animateSpinner(null, false); + $('#results').prepend($form); + _formAjaxResultTable($form); + _initEditableHandsOnTable($form); + _onSubmitExtractTable($form); + Results.initCancelFormButton($form, initNewResultTable); + Results.toggleResultEditButtons(false); + $('#result_name').focus(); + }, + error: function() { + animateSpinner(null, false); + initNewResultTable(); + } + }); + }); + } + + var publicAPI = Object.freeze({ + initNewResultTable: initNewResultTable, + applyEditResultTableCallback: applyEditResultTableCallback + }); + + return publicAPI; + })(); + + $(document).ready(function() { + ResultTables.initNewResultTable(); + ResultTables.applyEditResultTableCallback(); }); - toggleResultEditButtons(false); - - $("#result_name").focus(); - }); - - $(".edit-result-table").on("ajax:error", function(e, xhr, status, error) { - //TODO: Add error handling - }); -} -// New result text behaviour -$("#new-result-table").on("ajax:success", function(e, data) { - var $form = $(data.html); - $("#results").prepend($form); - - formAjaxResultTable($form); - initEditableHandsOnTable($form); - onSubmitExtractTable($form); - - // Cancel button - $form.find(".cancel-new").click(function () { - $form.remove(); - toggleResultEditButtons(true); - }); - - toggleResultEditButtons(false); - - $("#result_name").focus(); -}); - -$("#new-result-table").on("ajax:error", function(e, xhr, status, error) { - //TODO: Add error handling -}); - -// Apply ajax callback to form -function formAjaxResultTable($form) { - $form.on("ajax:success", function(e, data) { - $form.after(data.html); - $result = $(this).next(); - initFormSubmitLinks($result); - $(this).remove(); - - applyEditResultTableCallback(); - applyCollapseLinkCallBack(); - initHandsOnTables($result); - toggleResultEditButtons(true); - expandResult($result); - Comments.initialize(); - }); - $form.on("ajax:error", function(e, xhr, status, error) { - var data = xhr.responseJSON; - $form.renderFormErrors("result", data); - }); -} - -applyEditResultTableCallback(); +})(); diff --git a/app/assets/javascripts/results/result_texts.js b/app/assets/javascripts/results/result_texts.js index d13bb7d5d..8a8a83516 100644 --- a/app/assets/javascripts/results/result_texts.js +++ b/app/assets/javascripts/results/result_texts.js @@ -1,81 +1,106 @@ -// New result text behaviour -$("#new-result-text").on("ajax:success", function(e, data) { - var $form = $(data.html); - $("#results").prepend($form); +(function() { + 'use strict'; - formAjaxResultText($form); + var ResultText = (function() { + // New result text behaviour + function initNewReslutText() { + $('#new-result-text').on('click', function(event) { + event.preventDefault(); + event.stopImmediatePropagation(); + event.stopPropagation(); + var $btn = $(this); + $btn.off(); + animateSpinner(null, true); - // Cancel button - $form.find(".cancel-new").click(function () { - $form.remove(); - toggleResultEditButtons(true); - }); - toggleResultEditButtons(false); - TinyMCE.refresh(); - $("#result_name").focus(); -}); + // get new result form + $.ajax({ + url: $btn.data('href'), + method: 'GET', + success: function(data) { + var $form = $(data.html); + animateSpinner(null, false); + $('#results').prepend($form); + _formAjaxResultText($form); + Results.initCancelFormButton($form, initNewReslutText); + Results.toggleResultEditButtons(false); + TinyMCE.refresh(); + TinyMCE.highlight(); + $('#result_name').focus(); + }, + error: function() { + animateSpinner(null, false); + initNewReslutText(); + } + }) + }); + } -$("#new-result-text").on("ajax:error", function(e, xhr, status, error) { - //TODO: Add error handling -}); - -// Edit result text button behaviour -function applyEditResultTextCallback() { - $(".edit-result-text").on("ajax:success", function(e, data) { - var $result = $(this).closest(".result"); + // Edit result text button behaviour + function applyEditResultTextCallback() { + $('.edit-result-text').on('ajax:success', function(e, data) { + var $result = $(this).closest('.result'); var $form = $(data.html); var $prevResult = $result; $result.after($form); $result.remove(); - formAjaxResultText($form); + _formAjaxResultText($form); // Cancel button - $form.find(".cancel-edit").click(function () { - $form.after($prevResult); - $form.remove(); - applyEditResultTextCallback(); - toggleResultEditButtons(true); + $form.find('.cancel-edit').click(function() { + $form.after($prevResult); + $form.remove(); + applyEditResultTextCallback(); + Results.toggleResultEditButtons(true); }); - toggleResultEditButtons(false); + Results.toggleResultEditButtons(false); TinyMCE.refresh(); - $("#result_name").focus(); - }); + $('#result_name').focus(); + }); + } - $(".edit-result-text").on("ajax:error", function(e, xhr, status, error) { - //TODO: Add error handling - }); -} - -// Apply ajax callback to form -function formAjaxResultText($form) { - $form.on("ajax:success", function(e, data) { + // Apply ajax callback to form + function _formAjaxResultText($form) { + $form.on('ajax:success', function(e, data) { $form.after(data.html); var newResult = $form.next(); initFormSubmitLinks(newResult); $(this).remove(); applyEditResultTextCallback(); - applyCollapseLinkCallBack(); - toggleResultEditButtons(true); - expandResult(newResult); + Results.applyCollapseLinkCallBack(); + Results.toggleResultEditButtons(true); + Results.expandResult(newResult); TinyMCE.destroyAll(); Comments.initialize(); - }); - $form.on("ajax:error", function(e, xhr, status, error) { + initNewReslutText(); + }); + $form.on('ajax:error', function(e, xhr, status, error) { var data = xhr.responseJSON; - $form.renderFormErrors("result", data); + $form.renderFormErrors('result', data); TinyMCE.highlight(); - if (data["result_text.text"]) { - var $el = $form.find("textarea[name=result\\[result_text_attributes\\]\\[text\\]]"); + if (data['result_text.text']) { + var $el = $form.find( + 'textarea[name=result\\[result_text_attributes\\]\\[text\\]]' + ); - $el.closest(".form-group").addClass("has-error"); - $el.parent().append("" + data["result_text.text"] + ""); + $el.closest('.form-group').addClass('has-error'); + $el.parent().append('' + + data['result_text.text'] + ''); } - }); -} + }); + } -$(document).ready(function() { - TinyMCE.highlight(); -}); -applyEditResultTextCallback(); + var publicAPI = Object.freeze({ + initNewReslutText: initNewReslutText, + applyEditResultTextCallback: applyEditResultTextCallback + }); + + return publicAPI; + })(); + + $(document).ready(function() { + ResultText.initNewReslutText(); + ResultText.applyEditResultTextCallback(); + }); +})(); diff --git a/app/assets/javascripts/sitewide/image_preview.js b/app/assets/javascripts/sitewide/image_preview.js index 170e843df..06bfb80ad 100644 --- a/app/assets/javascripts/sitewide/image_preview.js +++ b/app/assets/javascripts/sitewide/image_preview.js @@ -1,43 +1,48 @@ -function initPreviewModal() { - $('.image-preview-link').off(); - $('.image-preview-link').click(function(e) { - e.preventDefault(); - var name = $(this).find('p').text(); - var url = $(this).find('img').data('preview-url'); - var downloadUrl = $(this).attr('href'); - var description = $(this).data('description'); - openPreviewModal(name, url, downloadUrl, description); - }); -} +(function(global) { + 'use strict'; -function openPreviewModal(name, url, downloadUrl, description) { - var modal = $('#imagePreviewModal'); - $.ajax({ - url: url, - type: 'GET', - dataType: 'json', - success: function(data) { - modal.find('.modal-body img').remove(); - modal.find('.image-name').text(name); - var link = modal.find('.image-download-link'); - link.attr('href', downloadUrl); - link.attr('data-no-turbolink', true); - link.attr('data-status', 'asset-present'); - modal.find('.modal-body').append($('') - .attr('src', data['large-preview-url']) - .attr('alt', name) - .click(function(ev) { - ev.stopPropagation(); - })); - modal.find('.modal-footer .image-description').text(description); - modal.find('.modal-body').click(function() { - modal.modal('hide'); - }); - modal.modal(); - $('.modal-backdrop').last().css('z-index', modal.css('z-index') - 1); - }, - error: function(ev) { - // TODO - } - }); -} + global.initPreviewModal = function initPreviewModal() { + var name, url, downloadUrl, description; + $('.image-preview-link').off(); + $('.image-preview-link').click(function(e) { + e.preventDefault(); + name = $(this).find('p').text(); + url = $(this).find('img').data('preview-url'); + downloadUrl = $(this).attr('href'); + description = $(this).data('description'); + openPreviewModal(name, url, downloadUrl, description); + }); + } + + function openPreviewModal(name, url, downloadUrl, description) { + var modal = $('#imagePreviewModal'); + $.ajax({ + url: url, + type: 'GET', + dataType: 'json', + success: function(data) { + modal.find('.modal-body img').remove(); + modal.find('.image-name').text(name); + var link = modal.find('.image-download-link'); + link.attr('href', downloadUrl); + link.attr('data-no-turbolink', true); + link.attr('data-status', 'asset-present'); + modal.find('.modal-body').append($('') + .attr('src', data['large-preview-url']) + .attr('alt', name) + .click(function(ev) { + ev.stopPropagation(); + })); + modal.find('.modal-footer .image-description').text(description); + modal.find('.modal-body').click(function() { + modal.modal('hide'); + }); + modal.modal(); + $('.modal-backdrop').last().css('z-index', modal.css('z-index') - 1); + }, + error: function(ev) { + // TODO + } + }); + } +})(window); diff --git a/app/views/my_modules/_result.html.erb b/app/views/my_modules/_result.html.erb index 07fcc5956..5c11d0553 100644 --- a/app/views/my_modules/_result.html.erb +++ b/app/views/my_modules/_result.html.erb @@ -19,7 +19,12 @@ <% end %> <% if can_archive_result(result) && !result.archived && result_unlocked?(result) %> - + <%= form_for :result, url: result_path_of_type(result), method: :patch, html: {id: 'result-archive-form-' + result.id.to_s } do |f| %> diff --git a/app/views/my_modules/results.html.erb b/app/views/my_modules/results.html.erb index f935e446e..f03a6b250 100644 --- a/app/views/my_modules/results.html.erb +++ b/app/views/my_modules/results.html.erb @@ -18,19 +18,28 @@ <% end %> <% if can_create_result_text_in_module(@my_module) %> - + <% end %> <% if can_create_result_table_in_module(@my_module) %> - + <% end %> <% if can_create_result_asset_in_module(@my_module) %> - + diff --git a/app/views/result_assets/_edit.html.erb b/app/views/result_assets/_edit.html.erb index 2de29c1e0..f27e4bee2 100644 --- a/app/views/result_assets/_edit.html.erb +++ b/app/views/result_assets/_edit.html.erb @@ -10,7 +10,9 @@ <%= ff.file_field :file %> <% end %>
- <%= f.submit t("result_assets.edit.update"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.FILE, true);" %> + <%= f.submit t("result_assets.edit.update"), + class: 'btn btn-primary save-result', + onclick: "Results.processResult(event, Results.ResultTypeEnum.FILE, true);" %> diff --git a/app/views/result_assets/_new.html.erb b/app/views/result_assets/_new.html.erb index d938ac740..d4fa8e0bf 100644 --- a/app/views/result_assets/_new.html.erb +++ b/app/views/result_assets/_new.html.erb @@ -4,7 +4,9 @@ <%= f.fields_for :asset do |ff| %> <%= ff.file_field :file %> <% end %> - <%= f.submit t("result_assets.new.create"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.FILE, false);" %> + <%= f.submit t("result_assets.new.create"), + class: 'btn btn-primary save-result', + onclick: "Results.processResult(event, Results.ResultTypeEnum.FILE, false);" %> diff --git a/app/views/result_comments/_index.html.erb b/app/views/result_comments/_index.html.erb index f68116d0d..12cd6d445 100644 --- a/app/views/result_comments/_index.html.erb +++ b/app/views/result_comments/_index.html.erb @@ -26,7 +26,8 @@ single_line: true, hide_label: true, placeholder: t("general.comment_placeholder"), - append: f.submit("+", onclick: "processResult(event, ResultTypeEnum.COMMENT, false);"), + append: f.submit("+", + onclick: "Results.processResult(event, Results.ResultTypeEnum.COMMENT, false);"), help: '.', data: { 'atwho-edit' => '' } %> <% end %> diff --git a/app/views/result_tables/_edit.html.erb b/app/views/result_tables/_edit.html.erb index 03d3897b0..d71d87624 100644 --- a/app/views/result_tables/_edit.html.erb +++ b/app/views/result_tables/_edit.html.erb @@ -9,7 +9,9 @@ <% end %>
- <%= f.submit t("result_tables.edit.update"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.TABLE, true);" %> + <%= f.submit t("result_tables.edit.update"), + class: 'btn btn-primary save-result', + onclick: "Results.processResult(event, Results.ResultTypeEnum.TABLE, true);" %> diff --git a/app/views/result_tables/_new.html.erb b/app/views/result_tables/_new.html.erb index 60037e93e..c76dc472e 100644 --- a/app/views/result_tables/_new.html.erb +++ b/app/views/result_tables/_new.html.erb @@ -8,7 +8,9 @@ <% end %> - <%= f.submit t("result_tables.new.create"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.TABLE, false);" %> + <%= f.submit t("result_tables.new.create"), + class: 'btn btn-primary save-result', + onclick: "Results.processResult(event, Results.ResultTypeEnum.TABLE, false);" %> diff --git a/app/views/result_texts/_edit.html.erb b/app/views/result_texts/_edit.html.erb index 10acf25ce..f49606663 100644 --- a/app/views/result_texts/_edit.html.erb +++ b/app/views/result_texts/_edit.html.erb @@ -9,7 +9,9 @@ object_id: @result.result_text.id }) %> <% end %>
- <%= f.submit t("result_texts.edit.update"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.TEXT, true);" %> + <%= f.submit t("result_texts.edit.update"), + class: 'btn btn-primary save-result', + onclick: "Results.processResult(event, Results.ResultTypeEnum.TEXT, true);" %> diff --git a/app/views/result_texts/_new.html.erb b/app/views/result_texts/_new.html.erb index fc340637c..8d97a98ef 100644 --- a/app/views/result_texts/_new.html.erb +++ b/app/views/result_texts/_new.html.erb @@ -7,7 +7,9 @@ object_id: @result.result_text.id }) %> <% end %>
- <%= f.submit t("result_texts.new.create"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.TEXT, false);" %> + <%= f.submit t("result_texts.new.create"), + class: 'btn btn-primary save-result', + onclick: "Results.processResult(event, Results.ResultTypeEnum.TEXT, false);" %>