From a4032f5cc629ad92a652fd3627af1dc08660d20e Mon Sep 17 00:00:00 2001 From: zmagod Date: Wed, 10 May 2017 13:53:32 +0200 Subject: [PATCH 1/4] first run --- .../javascripts/my_modules/results.js.erb | 393 +++++++++--------- .../javascripts/results/result_assets.js | 154 +++---- .../javascripts/results/result_tables.js.erb | 4 +- .../javascripts/results/result_texts.js | 2 +- .../javascripts/sitewide/image_preview.js | 89 ++-- 5 files changed, 336 insertions(+), 306 deletions(-) diff --git a/app/assets/javascripts/my_modules/results.js.erb b/app/assets/javascripts/my_modules/results.js.erb index 7338c06de..9f0c1221c 100644 --- a/app/assets/javascripts/my_modules/results.js.erb +++ b/app/assets/javascripts/my_modules/results.js.erb @@ -1,199 +1,214 @@ //= 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 self = this; + 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() { + self.initHandsOnTables($(document)); + _expandAllResults(); + _initTutorial(); + self.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); }); + + // 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; + } + }); + var hot = $container.handsontable('getInstance'); + var data = JSON.parse(contents.attr('value')); + hot.loadData(data.data); + }); + } + + 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); + }); + } + + // Toggle editing buttons + function toggleResultEditButtons(show) { + if (show) { + $('#results-toolbar').show(); + $('.edit-result-button').show(); + } else { + $('.edit-result-button').hide(); + $('#results-toolbar').hide(); + } + } + + // 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') + }, {ResultTypeEnum + 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 self.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 self.ResultTypeEnum.TABLE: + var $nameInput = $form.find('#result_name'); + var nameValid = textValidator(ev, $nameInput, 0, + <%= Constants::NAME_MAX_LENGTH %>); + break; + case self.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: + var $commentInput = $form.find('#comment_message'); + var commentValid = textValidator(ev, $commentInput, 1, + <%= Constants::TEXT_MAX_LENGTH %>); + break; + } + } + + var publicAPI = Object.freeze({ + init: init, + initHandsOnTables: initHandsOnTables, + applyCollapseLinkCallBack: applyCollapseLinkCallBack, + toggleResultEditButtons: toggleResultEditButtons, + expandResult: expandResult, + processResult: processResult, + ResultTypeEnum: ResultTypeEnum }); - $("#results-expand-btn").click(expandAllResults); + return publicAPI; + })(); + + $(document).ready(function(){ + Results.init(); }); - - // 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; - } - }); - var hot = $container.handsontable('getInstance'); - var data = JSON.parse(contents.attr("value")); - hot.loadData(data.data); - - }); -} - -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); - }); -} - -// Toggle editing buttons -function toggleResultEditButtons(show) { - if (show) { - $("#results-toolbar").show(); - $(".edit-result-button").show(); - } else { - $(".edit-result-button").hide(); - $("#results-toolbar").hide(); - } -} - -// 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); - } -} - -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); - } - - 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; - } -} - -$(document).ready(function(){ - init(); -}); +})(window); diff --git a/app/assets/javascripts/results/result_assets.js b/app/assets/javascripts/results/result_assets.js index 6fe3a6a18..0e729fc74 100644 --- a/app/assets/javascripts/results/result_assets.js +++ b/app/assets/javascripts/results/result_assets.js @@ -1,83 +1,93 @@ -// 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 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); - // Cancel button - $form.find(".cancel-new").click(function () { - $form.remove(); - toggleResultEditButtons(true); - }); + // Cancel button + $form.find('.cancel-new').click(function () { + $form.remove(); + toggleResultEditButtons(true); + }); - toggleResultEditButtons(false); + toggleResultEditButtons(false); - $("#result_name").focus(); -}); + $('#result_name').focus(); + }); -$("#new-result-asset").on("ajax:error", function(e, xhr, status, error) { - animateSpinner(null, false); -}); + $('#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(); -// 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(); + formAjaxResultAsset($form); - formAjaxResultAsset($form); + // Cancel button + $form.find('.cancel-edit').click(function () { + $form.after($prevResult); + $form.remove(); + applyEditResultAssetCallback(); + toggleResultEditButtons(true); + initPreviewModal(); + }); - // Cancel button - $form.find(".cancel-edit").click(function () { - $form.after($prevResult); - $form.remove(); - applyEditResultAssetCallback(); - toggleResultEditButtons(true); - initPreviewModal(); + toggleResultEditButtons(false); + + $('#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(); + applyEditResultAssetCallback(); + Results.applyCollapseLinkCallBack(); + + toggleResultEditButtons(true); + 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); + }); + } }); - toggleResultEditButtons(false); + return publicAPI; + })(); - $("#result_name").focus(); - }); - - $(".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, 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); - }); -} - -applyEditResultAssetCallback(); -initPreviewModal(); + ResutlAssetHandler.initNewResultAsset(); + ResutlAssetHandler.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 8994d10e5..c9c05b8fc 100644 --- a/app/assets/javascripts/results/result_tables.js.erb +++ b/app/assets/javascripts/results/result_tables.js.erb @@ -96,8 +96,8 @@ function formAjaxResultTable($form) { $(this).remove(); applyEditResultTableCallback(); - applyCollapseLinkCallBack(); - initHandsOnTables($result); + Results.applyCollapseLinkCallBack(); + Results.initHandsOnTables($result); toggleResultEditButtons(true); expandResult($result); Comments.initialize(); diff --git a/app/assets/javascripts/results/result_texts.js b/app/assets/javascripts/results/result_texts.js index d13bb7d5d..466a68ded 100644 --- a/app/assets/javascripts/results/result_texts.js +++ b/app/assets/javascripts/results/result_texts.js @@ -56,7 +56,7 @@ function formAjaxResultText($form) { $(this).remove(); applyEditResultTextCallback(); - applyCollapseLinkCallBack(); + Results.applyCollapseLinkCallBack(); toggleResultEditButtons(true); expandResult(newResult); TinyMCE.destroyAll(); 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); From 81a3cb0a418b57c2d9dd854aa4924af69d96664c Mon Sep 17 00:00:00 2001 From: zmagod Date: Thu, 11 May 2017 16:54:28 +0200 Subject: [PATCH 2/4] adds results js code in modules --- .../javascripts/my_modules/results.js.erb | 15 +- .../javascripts/results/result_assets.js | 146 ++++++------ .../javascripts/results/result_tables.js.erb | 221 +++++++++--------- .../javascripts/results/result_texts.js | 155 ++++++------ app/views/result_assets/_edit.html.erb | 4 +- app/views/result_assets/_new.html.erb | 4 +- app/views/result_comments/_index.html.erb | 3 +- app/views/result_tables/_edit.html.erb | 4 +- app/views/result_tables/_new.html.erb | 4 +- app/views/result_texts/_edit.html.erb | 4 +- app/views/result_texts/_new.html.erb | 4 +- 11 files changed, 302 insertions(+), 262 deletions(-) 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 %>
- <%= 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);" %> From 070a829c490c468a0eb3ffe7e026f013125d2d5d Mon Sep 17 00:00:00 2001 From: zmagod Date: Fri, 12 May 2017 10:02:33 +0200 Subject: [PATCH 3/4] change new results hendlers --- app/assets/javascripts/results/result_assets.js | 1 + app/views/my_modules/results.html.erb | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/results/result_assets.js b/app/assets/javascripts/results/result_assets.js index 10ffad4e8..f99113396 100644 --- a/app/assets/javascripts/results/result_assets.js +++ b/app/assets/javascripts/results/result_assets.js @@ -5,6 +5,7 @@ // New result asset behaviour function initNewResultAsset() { $('#new-result-asset').on('ajax:success', function(e, data) { + debugger; var $form = $(data.html); $('#results').prepend($form); 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) %> - + From d34b2520b51b06df77942b47ba23c3854aacf98d Mon Sep 17 00:00:00 2001 From: zmagod Date: Fri, 12 May 2017 15:59:58 +0200 Subject: [PATCH 4/4] refactor results javascript, fixes bug with results archive [fixes SCI-1262] prevents duplicate request [fixes SCI-1248] --- .../javascripts/my_modules/results.js.erb | 29 +++- .../javascripts/results/result_assets.js | 41 +++--- .../javascripts/results/result_tables.js.erb | 43 +++--- .../javascripts/results/result_texts.js | 130 ++++++++++-------- app/views/my_modules/_result.html.erb | 7 +- 5 files changed, 155 insertions(+), 95 deletions(-) diff --git a/app/assets/javascripts/my_modules/results.js.erb b/app/assets/javascripts/my_modules/results.js.erb index 3f8d801c0..ad3608600 100644 --- a/app/assets/javascripts/my_modules/results.js.erb +++ b/app/assets/javascripts/my_modules/results.js.erb @@ -88,7 +88,7 @@ // Toggle editing buttons function toggleResultEditButtons(show) { - if (show) { + if(show) { $('#results-toolbar').show(); $('.edit-result-button').show(); } else { @@ -194,6 +194,29 @@ } } + // 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(); + } + } + var publicAPI = Object.freeze({ init: init, initHandsOnTables: initHandsOnTables, @@ -201,7 +224,9 @@ toggleResultEditButtons: toggleResultEditButtons, expandResult: expandResult, processResult: processResult, - ResultTypeEnum: ResultTypeEnum + ResultTypeEnum: ResultTypeEnum, + initCancelFormButton: initCancelFormButton, + archive: archive }); return publicAPI; diff --git a/app/assets/javascripts/results/result_assets.js b/app/assets/javascripts/results/result_assets.js index f99113396..cf9f0e456 100644 --- a/app/assets/javascripts/results/result_assets.js +++ b/app/assets/javascripts/results/result_assets.js @@ -4,24 +4,32 @@ var ResutlAssets = (function() { // New result asset behaviour function initNewResultAsset() { - $('#new-result-asset').on('ajax:success', function(e, data) { - debugger; - var $form = $(data.html); - $('#results').prepend($form); + $('#new-result-asset').on('click', function(event) { + event.preventDefault(); + event.stopImmediatePropagation(); + event.stopPropagation(); + var $btn = $(this); + $btn.off(); + animateSpinner(null, true); - _formAjaxResultAsset($form); - - // Cancel button - $form.find('.cancel-new').click(function () { - $form.remove(); - Results.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() { + animateSpinner(null, false); + initNewResultAsset(); + } }); - - Results.toggleResultEditButtons(false); - - $('#result_name').focus(); - }).on('ajax:error', function(e, xhr, status, error) { - animateSpinner(null, false); }); } @@ -65,6 +73,7 @@ Results.expandResult($newResult); initPreviewModal(); Comments.initialize(); + initNewResultAsset(); }).on('ajax:error', function(e, data) { // This check is here only because of remotipart bug, which returns // HTML instead of JSON, go figure diff --git a/app/assets/javascripts/results/result_tables.js.erb b/app/assets/javascripts/results/result_tables.js.erb index c2c6a2c59..c14ae013f 100644 --- a/app/assets/javascripts/results/result_tables.js.erb +++ b/app/assets/javascripts/results/result_tables.js.erb @@ -52,6 +52,7 @@ Results.toggleResultEditButtons(true); Results.expandResult($result); Comments.initialize(); + initNewResultTable(); }); $form.on('ajax:error', function(e, xhr, status, error) { var data = xhr.responseJSON; @@ -88,23 +89,33 @@ // 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); + $('#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(); + } }); - - Results.toggleResultEditButtons(false); - - $('#result_name').focus(); }); } diff --git a/app/assets/javascripts/results/result_texts.js b/app/assets/javascripts/results/result_texts.js index 0c7bb9403..8a8a83516 100644 --- a/app/assets/javascripts/results/result_texts.js +++ b/app/assets/javascripts/results/result_texts.js @@ -4,80 +4,91 @@ 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); + $('#new-result-text').on('click', function(event) { + event.preventDefault(); + event.stopImmediatePropagation(); + event.stopPropagation(); + var $btn = $(this); + $btn.off(); + animateSpinner(null, true); - _formAjaxResultText($form); - - // 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(); + // 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(); + } + }) }); } - // 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(); + $('.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(); - Results.toggleResultEditButtons(true); - }); - Results.toggleResultEditButtons(false); - TinyMCE.refresh(); - $('#result_name').focus(); + // 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(); + }); } // 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(); + $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\\]]' - ); + applyEditResultTextCallback(); + Results.applyCollapseLinkCallBack(); + Results.toggleResultEditButtons(true); + Results.expandResult(newResult); + TinyMCE.destroyAll(); + Comments.initialize(); + initNewReslutText(); + }); + $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'] + ''); - } - }); + $el.closest('.form-group').addClass('has-error'); + $el.parent().append('' + + data['result_text.text'] + ''); + } + }); } var publicAPI = Object.freeze({ @@ -92,5 +103,4 @@ ResultText.initNewReslutText(); ResultText.applyEditResultTextCallback(); }); - })(); 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| %>