diff --git a/app/assets/javascripts/my_modules/results.js.erb b/app/assets/javascripts/my_modules/results.js.erb index 9f0c1221c..3f8d801c0 100644 --- a/app/assets/javascripts/my_modules/results.js.erb +++ b/app/assets/javascripts/my_modules/results.js.erb @@ -5,7 +5,6 @@ 'use strict'; global.Results = (function() { - var self = this; var ResultTypeEnum = Object.freeze({ FILE: 0, TABLE: 1, @@ -14,10 +13,10 @@ }); function init() { - self.initHandsOnTables($(document)); + initHandsOnTables($(document)); _expandAllResults(); _initTutorial(); - self.applyCollapseLinkCallBack(); + applyCollapseLinkCallBack(); Comments.bindNewElement(); Comments.initialize(); @@ -143,7 +142,7 @@ var steps = [{ element: $('#results-toolbar')[0], intro: I18n.t('tutorial.module_results_html') - }, {ResultTypeEnum + }, { element: samplesTab[0], intro: I18n.t('tutorial.module_results_click_samples_html'), position: 'left' @@ -158,7 +157,7 @@ $form.clearFormErrors(); switch(resultTypeEnum) { - case self.ResultTypeEnum.FILE: + case ResultTypeEnum.FILE: var $nameInput = $form.find('#result_name'); var nameValid = textValidator(ev, $nameInput, 0, <%= Constants::NAME_MAX_LENGTH %>); @@ -175,19 +174,19 @@ } break; - case self.ResultTypeEnum.TABLE: + case ResultTypeEnum.TABLE: var $nameInput = $form.find('#result_name'); var nameValid = textValidator(ev, $nameInput, 0, <%= Constants::NAME_MAX_LENGTH %>); break; - case self.ResultTypeEnum.TEXT: + 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 self.ResultTypeEnum.COMMENT: + case ResultTypeEnum.COMMENT: var $commentInput = $form.find('#comment_message'); var commentValid = textValidator(ev, $commentInput, 1, <%= Constants::TEXT_MAX_LENGTH %>); diff --git a/app/assets/javascripts/results/result_assets.js b/app/assets/javascripts/results/result_assets.js index 0e729fc74..10ffad4e8 100644 --- a/app/assets/javascripts/results/result_assets.js +++ b/app/assets/javascripts/results/result_assets.js @@ -1,93 +1,93 @@ (function(global) { 'use strict'; - var ResutlAssetHandler = (function() { - var self = this; - var publicAPI = Object.freeze({ - initNewResultAsset: function initNewResultAsset() { - // New result asset behaviour - $('#new-result-asset').on('ajax:success', function(e, data) { - var $form = $(data.html); - $('#results').prepend($form); - debugger; - self.formAjaxResultAsset($form); + var ResutlAssets = (function() { + // New result asset behaviour + function initNewResultAsset() { + $('#new-result-asset').on('ajax:success', function(e, data) { + var $form = $(data.html); + $('#results').prepend($form); - // Cancel button - $form.find('.cancel-new').click(function () { - $form.remove(); - toggleResultEditButtons(true); - }); + _formAjaxResultAsset($form); - toggleResultEditButtons(false); - - $('#result_name').focus(); + // Cancel button + $form.find('.cancel-new').click(function () { + $form.remove(); + Results.toggleResultEditButtons(true); }); - $('#new-result-asset') - .on('ajax:error', function(e, xhr, status, error) { - animateSpinner(null, false); - }); - }, - applyEditResultAssetCallback: 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 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(); - toggleResultEditButtons(false); + _formAjaxResultAsset($form); - $('#result_name').focus(); - }); - - $('.edit-result-asset') - .on('ajax:error', function(e, xhr, status, error) { - animateSpinner(null, false); - }); - }, - formAjaxResultAsset: function formAjaxResultAsset($form) { - $form.on('ajax:success', function(e, data) { - $form.after(data.html); - var $newResult = $form.next(); - initFormSubmitLinks($newResult); - $(this).remove(); + // Cancel button + $form.find('.cancel-edit').click(function () { + $form.after($prevResult); + $form.remove(); applyEditResultAssetCallback(); - Results.applyCollapseLinkCallBack(); - - toggleResultEditButtons(true); - expandResult($newResult); + Results.toggleResultEditButtons(true); initPreviewModal(); - Comments.initialize(); - }).on('ajax:error', function(e, data) { - // This check is here only because of remotipart bug, which returns - // HTML instead of JSON, go figure - var errors = ''; - if (data.errors) - errors = data.errors; - else - errors = data.responseJSON.errors; - $form.renderFormErrors('result', errors, true, e); - animateSpinner(null, false); }); - } + + Results.toggleResultEditButtons(false); + + $('#result_name').focus(); + }).on('ajax:error', function(e, xhr, status, error) { + animateSpinner(null, false); + }); + } + + 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(); + }).on('ajax:error', function(e, data) { + // This check is here only because of remotipart bug, which returns + // HTML instead of JSON, go figure + var errors = ''; + if (data.errors) + errors = data.errors; + else + errors = data.responseJSON.errors; + $form.renderFormErrors('result', errors, true, e); + animateSpinner(null, false); + }); + } + + var publicAPI = Object.freeze({ + initNewResultAsset: initNewResultAsset, + applyEditResultAssetCallback: applyEditResultAssetCallback }); return publicAPI; })(); - ResutlAssetHandler.initNewResultAsset(); - ResutlAssetHandler.applyEditResultAssetCallback(); - global.initPreviewModal(); + $(document).ready(function() { + ResutlAssets.initNewResultAsset(); + ResutlAssets.applyEditResultAssetCallback(); + global.initPreviewModal(); + }); })(window); diff --git a/app/assets/javascripts/results/result_tables.js.erb b/app/assets/javascripts/results/result_tables.js.erb index c9c05b8fc..c2c6a2c59 100644 --- a/app/assets/javascripts/results/result_tables.js.erb +++ b/app/assets/javascripts/results/result_tables.js.erb @@ -1,111 +1,124 @@ -// 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(); + }); + $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('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(); + Results.toggleResultEditButtons(true); + }); + + Results.toggleResultEditButtons(false); + + $('#result_name').focus(); + }); + } + + 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(); - Results.applyCollapseLinkCallBack(); - Results.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 466a68ded..0c7bb9403 100644 --- a/app/assets/javascripts/results/result_texts.js +++ b/app/assets/javascripts/results/result_texts.js @@ -1,81 +1,96 @@ -// 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('ajax:success', function(e, data) { + var $form = $(data.html); + $('#results').prepend($form); - // Cancel button - $form.find(".cancel-new").click(function () { - $form.remove(); - toggleResultEditButtons(true); - }); - toggleResultEditButtons(false); - TinyMCE.refresh(); - $("#result_name").focus(); -}); + _formAjaxResultText($form); -$("#new-result-text").on("ajax:error", function(e, xhr, status, error) { - //TODO: Add error handling -}); + // Cancel button + $form.find('.cancel-new').click(function() { + $form.remove(); + Results.toggleResultEditButtons(true); + }); + Results.toggleResultEditButtons(false); + TinyMCE.refresh(); + TinyMCE.highlight(); + $('#result_name').focus(); + }).on('ajax:error', function() { + TinyMCE.refresh(); + }); + } -// 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); + // 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(); - // Cancel button - $form.find(".cancel-edit").click(function () { - $form.after($prevResult); - $form.remove(); - applyEditResultTextCallback(); - toggleResultEditButtons(true); + _formAjaxResultText($form); + + // Cancel button + $form.find('.cancel-edit').click(function () { + $form.after($prevResult); + $form.remove(); + applyEditResultTextCallback(); + Results.toggleResultEditButtons(true); + }); + Results.toggleResultEditButtons(false); + TinyMCE.refresh(); + $('#result_name').focus(); }); - toggleResultEditButtons(false); - TinyMCE.refresh(); - $("#result_name").focus(); + } + + // 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(); + Results.applyCollapseLinkCallBack(); + Results.toggleResultEditButtons(true); + Results.expandResult(newResult); + TinyMCE.destroyAll(); + Comments.initialize(); + }); + $form.on('ajax:error', function(e, xhr, status, error) { + var data = xhr.responseJSON; + $form.renderFormErrors('result', data); + TinyMCE.highlight(); + 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'] + ''); + } + }); + } + + var publicAPI = Object.freeze({ + initNewReslutText: initNewReslutText, + applyEditResultTextCallback: applyEditResultTextCallback }); - $(".edit-result-text").on("ajax:error", function(e, xhr, status, error) { - //TODO: Add error handling - }); -} + return publicAPI; + })(); -// 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(); + $(document).ready(function() { + ResultText.initNewReslutText(); + ResultText.applyEditResultTextCallback(); + }); - applyEditResultTextCallback(); - Results.applyCollapseLinkCallBack(); - toggleResultEditButtons(true); - expandResult(newResult); - TinyMCE.destroyAll(); - Comments.initialize(); - }); - $form.on("ajax:error", function(e, xhr, status, error) { - var data = xhr.responseJSON; - $form.renderFormErrors("result", data); - TinyMCE.highlight(); - 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"] + ""); - } - }); -} - -$(document).ready(function() { - TinyMCE.highlight(); -}); -applyEditResultTextCallback(); +})(); 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 %>