diff --git a/app/assets/javascripts/admin/commons/content_editor/offcanvas.js b/app/assets/javascripts/admin/commons/content_editor/offcanvas.js index 5c68188352d0119fa80c5d725b008938b9c00625..7d1984b2a5b75fdff964b0f45d5a8ddd98adba05 100644 --- a/app/assets/javascripts/admin/commons/content_editor/offcanvas.js +++ b/app/assets/javascripts/admin/commons/content_editor/offcanvas.js @@ -1,11 +1,10 @@ +/*global bootstrap */ window.osuny.contentEditor.offcanvas = { init: function () { 'use strict'; this.editor = document.getElementById('offcanvasEditor'); this.editorBootstrap = new bootstrap.Offcanvas(this.editor); this.iframe = document.getElementById('offcanvasEditorIframe'); - this.editButtons = document.querySelectorAll('.js-content-editor__element__edit'); - this.addBlockButton = document.querySelector('.js-content-editor__add-block'); this.initButtons(); }, @@ -13,24 +12,21 @@ window.osuny.contentEditor.offcanvas = { 'use strict'; var i, button; + this.editButtons = document.querySelectorAll('.js-content-editor__element__edit'); + this.addBlockButton = document.querySelector('.js-content-editor__add-block'); for (i = 0; i < this.editButtons.length; i += 1) { button = this.editButtons[i]; - button.addEventListener('click', this.onEditButtonClick.bind(this)); + button.addEventListener('click', this.onBlockActionClick.bind(this)); + } + if (this.addBlockButton) { + this.addBlockButton.addEventListener('click', this.onBlockActionClick.bind(this)); } - this.addBlockButton.addEventListener('click', this.onAddBlockButtonClick.bind(this)); - }, - - onEditButtonClick: function (event) { - 'use strict'; - event.preventDefault(); - window.open(event.target.href, 'editor'); - this.editorBootstrap.show(); }, - onAddBlockButtonClick: function (event) { + onBlockActionClick: function (event) { 'use strict'; event.preventDefault(); - window.open(event.target.href, 'editor'); + this.iframe.contentWindow.location.replace(event.target.href); this.editorBootstrap.show(); }, @@ -45,19 +41,19 @@ window.osuny.contentEditor.offcanvas = { }, addNewSnippet: function () { - this.editorBootstrap.hide(); + 'use strict'; window.osuny.contentEditor.tabs.reload(); }, updateExistingSnippet: function (snippet, blockPath) { 'use strict'; var request = new XMLHttpRequest(); - request.onreadystatechange = function() { - if (request.status == 200) { + request.onreadystatechange = function () { + if (request.status === 200) { snippet.innerHTML = request.responseText; } }; - request.open("GET", blockPath); + request.open('GET', blockPath); request.send(); this.editorBootstrap.hide(); }, @@ -66,7 +62,8 @@ window.osuny.contentEditor.offcanvas = { 'use strict'; return { init: this.init.bind(this), + initButtons: this.initButtons.bind(this), onBlockSave: this.onBlockSave.bind(this) }; } -}.invoke(); \ No newline at end of file +}.invoke(); diff --git a/app/assets/javascripts/admin/commons/content_editor/sort.js b/app/assets/javascripts/admin/commons/content_editor/sort.js index 66de9ba1cc21b0783f3f7911be8f81175e4fc659..1cb1564cb9e0024a4e1fa95bfdf77cc548d6c6f5 100644 --- a/app/assets/javascripts/admin/commons/content_editor/sort.js +++ b/app/assets/javascripts/admin/commons/content_editor/sort.js @@ -1,4 +1,4 @@ -/* global Sortable */ +/* global $, Sortable */ window.osuny.contentEditor.sort = { init: function () { 'use strict'; @@ -68,4 +68,4 @@ window.osuny.contentEditor.sort = { init: this.init.bind(this) }; } -}.invoke(); \ No newline at end of file +}.invoke(); diff --git a/app/assets/javascripts/admin/commons/content_editor/tabs.js b/app/assets/javascripts/admin/commons/content_editor/tabs.js index 89f906c5d7066e2e70296ef2513610ad4817f944..841b29d03a6a4f4130318936223bc8ea13a8773c 100644 --- a/app/assets/javascripts/admin/commons/content_editor/tabs.js +++ b/app/assets/javascripts/admin/commons/content_editor/tabs.js @@ -26,7 +26,7 @@ window.osuny.contentEditor.tabs = { tabChanged: function (event) { 'use strict'; - if (event.target.id == 'mode-write-tab') { + if (event.target.id === 'mode-write-tab') { this.loadWrite(); } else { this.loadStructure(); @@ -34,18 +34,21 @@ window.osuny.contentEditor.tabs = { }, loadWrite: function () { + 'use strict'; this.target = this.modeWriteContainer; this.source = this.modeWriteSource; this.loadCurrentTab(); }, loadStructure: function () { + 'use strict'; this.target = this.modeStructureContainer; this.source = this.modeStructureSource; this.loadCurrentTab(); }, loadCurrentTab: function () { + 'use strict'; this.target.innerHTML = ''; this.xhr = new XMLHttpRequest(); this.xhr.open('GET', this.source, true); @@ -54,6 +57,7 @@ window.osuny.contentEditor.tabs = { }, reload: function () { + 'use strict'; this.loadCurrentTab(); }, @@ -63,7 +67,7 @@ window.osuny.contentEditor.tabs = { if (this.xhr.status === 200) { this.target.innerHTML = this.xhr.responseText; window.osuny.contentEditor.sort.init(); - window.osuny.contentEditor.offcanvas.init(); + window.osuny.contentEditor.offcanvas.initButtons(); } } }, @@ -72,7 +76,7 @@ window.osuny.contentEditor.tabs = { 'use strict'; return { init: this.init.bind(this), - reload: this.reload.bind(this), + reload: this.reload.bind(this) }; } -}.invoke(); \ No newline at end of file +}.invoke(); diff --git a/app/assets/javascripts/admin/utils/validate_click_form.js b/app/assets/javascripts/admin/utils/validate_click_form.js index c3e0bab8b708c8ed827052cf81525034e64079ed..61b06bb7617cd7e0335dd8347adf9730ff20fadf 100644 --- a/app/assets/javascripts/admin/utils/validate_click_form.js +++ b/app/assets/javascripts/admin/utils/validate_click_form.js @@ -1,24 +1,24 @@ -/* global $ */ +/* global */ window.osuny.validateFromClickManager = { init: function () { 'use strict'; this.elements = document.querySelectorAll('.js-validate-form-click'); - + if (this.elements.length > 0) { this.listen(); } }, - listen: function() { + listen: function () { 'use strict'; - this.elements.forEach(function(element) { - this.bindClick(element) + this.elements.forEach(function (element) { + this.bindClick(element); }.bind(this)); }, - bindClick: function(element) { + bindClick: function (element) { 'use strict'; var form = element.querySelector('form'); @@ -26,9 +26,10 @@ window.osuny.validateFromClickManager = { return; } - element.style.cursor = "pointer" - element.addEventListener('click', function() { - form.submit(); + element.style.cursor = 'pointer'; + element.addEventListener('click', function () { + var submitBtn = form.querySelector('input[type="submit"]'); + submitBtn.click(); }); }, diff --git a/app/controllers/admin/communication/blocks_controller.rb b/app/controllers/admin/communication/blocks_controller.rb index 5682a69980a49925c4d382c5ef5572e2d2a6d96f..c33b597230db03445061bebf9a533548cac109d7 100644 --- a/app/controllers/admin/communication/blocks_controller.rb +++ b/app/controllers/admin/communication/blocks_controller.rb @@ -42,22 +42,42 @@ class Admin::Communication::BlocksController < Admin::Communication::Application def create if @block.save - redirect_to [:edit, :admin, @block], - notice: t('admin.successfully_created_html', model: @block.to_s) + respond_to do |format| + format.html { + redirect_to [:edit, :admin, @block], + notice: t('admin.successfully_created_html', model: @block.to_s) + } + format.js { render json: {}, status: :created, location: [:edit, :admin, @block] } + end else - breadcrumb - render :new, status: :unprocessable_entity + respond_to do |format| + format.html { + breadcrumb + render :new, status: :unprocessable_entity + } + format.js { head :unprocessable_entity } + end end end def update if @block.update(block_params) - redirect_to about_path, - notice: t('admin.successfully_updated_html', model: @block.to_s) + respond_to do |format| + format.html { + redirect_to about_path, + notice: t('admin.successfully_updated_html', model: @block.to_s) + } + format.js { head :ok } + end else - breadcrumb - add_breadcrumb t('edit') - render :edit, status: :unprocessable_entity + respond_to do |format| + format.html { + breadcrumb + add_breadcrumb t('edit') + render :edit, status: :unprocessable_entity + } + format.js { head :unprocessable_entity } + end end end diff --git a/app/views/admin/communication/blocks/new.html.erb b/app/views/admin/communication/blocks/new.html.erb index b7186757a8c8967c159c71e5e4f6cf2ccc24ba67..3da4a5fc1f56a15d2c159611694a6b2cc7eefd4d 100644 --- a/app/views/admin/communication/blocks/new.html.erb +++ b/app/views/admin/communication/blocks/new.html.erb @@ -21,7 +21,7 @@ <p class="mb-0 block__description"> <%= t "admin.communication.blocks.templates.#{kind}.description" %> </p> - <%= simple_form_for [:admin, @block], html: { class: 'mt-auto'} do |f| %> + <%= simple_form_for [:admin, @block], remote: true, html: { class: 'mt-auto new-block-form', id: "new-block-#{kind}" } do |f| %> <%= f.input :about_type, as: :hidden %> <%= f.input :about_id, as: :hidden %> <%= f.input :template_kind, as: :hidden %> @@ -34,3 +34,14 @@ </div> </section> <% end %> + +<script nonce="<%= request.content_security_policy_nonce %>"> + window.addEventListener('load', function(){ + setTimeout(function() { + $(".new-block-form").on("ajax:success", function(e, data, status, xhr) { + parent.window.osuny.contentEditor.offcanvas.onBlockSave(null, null); + window.location.replace(xhr.getResponseHeader('Location')); + }); + }, 100); + }); +</script> \ No newline at end of file