diff options
author | Marvin Borner | 2018-05-23 22:23:28 +0200 |
---|---|---|
committer | Marvin Borner | 2018-05-23 22:23:28 +0200 |
commit | b66a61addb6c8e66cb26fcf74b532d68891267e4 (patch) | |
tree | 05e9449ff25bdc98f68105f41923ccb9f6ef5095 /main/app/sprinkles/FormGenerator/assets | |
parent | 1d4ef435177a5f9b6d1a289800d933e49be0c550 (diff) |
Refactored code, many fixes and improvements in chat backend+frontend
Diffstat (limited to 'main/app/sprinkles/FormGenerator/assets')
-rw-r--r-- | main/app/sprinkles/FormGenerator/assets/js/widget-formGenerator.js | 101 |
1 files changed, 53 insertions, 48 deletions
diff --git a/main/app/sprinkles/FormGenerator/assets/js/widget-formGenerator.js b/main/app/sprinkles/FormGenerator/assets/js/widget-formGenerator.js index 52743f6..6cfcce8 100644 --- a/main/app/sprinkles/FormGenerator/assets/js/widget-formGenerator.js +++ b/main/app/sprinkles/FormGenerator/assets/js/widget-formGenerator.js @@ -10,20 +10,20 @@ * @license MIT */ -;(function($, window, document, undefined) { - "use strict"; +;(function ($, window, document, undefined) { + "use strict"; // Define plugin name and defaults. var pluginName = "formGenerator", defaults = { - DEBUG : false, - mainAlertElement : $('#alerts-page'), - redirectAfterSuccess : true, - autofocusModalElement : true + DEBUG: false, + mainAlertElement: $('#alerts-page'), + redirectAfterSuccess: true, + autofocusModalElement: true }; // Constructor - function Plugin (element, options) { + function Plugin(element, options) { this.elements = element; this.$elements = $(this.elements); this.settings = $.extend(true, {}, defaults, options); @@ -31,7 +31,11 @@ this._name = pluginName; // Detect changes to element attributes - this.$elements.attrchange({ callback: function (event) { this.elements = event.target; }.bind(this) }); + this.$elements.attrchange({ + callback: function (event) { + this.elements = event.target; + }.bind(this) + }); // Initialise ufAlerts if (!this.settings.mainAlertElement.data('ufAlerts')) { @@ -46,21 +50,21 @@ /** * Bind the display action for a form to the button */ - display: function() { + display: function () { this.$elements.on('click', $.proxy(this._fetchForm, this)); return this.$elements; }, /** * Bind the confirm action to the button */ - confirm: function() { + confirm: function () { this.$elements.on('click', $.proxy(this._fetchConfirmModal, this)); return this.$elements; }, /** * Fetch the form HTML */ - _fetchForm: function(event) { + _fetchForm: function (event) { // Get the button element var button = event.currentTarget; @@ -72,14 +76,15 @@ } // Delete any existing instance of the form with the same name - if($('#' + box_id).length) { + if ($('#' + box_id).length) { $('#' + box_id).remove(); } // Prepare the ajax payload var payload = $.extend({ - box_id: box_id} - , button.dataset); + box_id: box_id + } + , button.dataset); // Fetch and render the form $.ajax({ @@ -88,19 +93,19 @@ data: payload, cache: false }) - .done($.proxy(this._displayForm, this, box_id, button)) - .fail($.proxy(this._displayFailure, this, button)); + .done($.proxy(this._displayForm, this, box_id, button)) + .fail($.proxy(this._displayFailure, this, button)); }, /** * Displays the form modal and set up ufForm */ - _displayForm: function(box_id, button, data) { + _displayForm: function (box_id, button, data) { // Trigger pre-display event $(button).trigger("displayForm." + this._name); // Append the form as a modal dialog to the body - $( "body" ).append(data); + $("body").append(data); $('#' + box_id).modal('show'); // Set focus on first element @@ -111,7 +116,7 @@ } // Setup ufAlerts - var boxMsgTarget = $("#"+box_id+" #form-alerts"); + var boxMsgTarget = $("#" + box_id + " #form-alerts"); // Show the alert. We could have info alert coming in if (!boxMsgTarget.data('ufAlerts')) { @@ -122,15 +127,15 @@ // Setup the loaded form with ufForm $('#' + box_id).find("form").ufForm({ validators: validators, - msgTarget: $("#"+box_id+" #form-alerts") + msgTarget: $("#" + box_id + " #form-alerts") }) - .on("submitSuccess.ufForm", $.proxy(this._formPostSuccess, this, box_id, button)) - .on("submitError.ufForm", $.proxy(this._displayFormFaillure, this, box_id, button)); + .on("submitSuccess.ufForm", $.proxy(this._formPostSuccess, this, box_id, button)) + .on("submitError.ufForm", $.proxy(this._displayFormFaillure, this, box_id, button)); }, /** * Action done when a form is successful */ - _formPostSuccess: function(box_id, button, event, data) { + _formPostSuccess: function (box_id, button, event, data) { // Trigger success event $(button).trigger("formSuccess." + this._name, data); @@ -146,7 +151,7 @@ /** * Fetch confirmation modal */ - _fetchConfirmModal: function(event) { + _fetchConfirmModal: function (event) { // Get the button element var button = event.currentTarget; @@ -158,7 +163,7 @@ } // Delete any existing instance of the form with the same name - if($('#' + box_id).length) { + if ($('#' + box_id).length) { $('#' + box_id).remove(); } @@ -179,19 +184,19 @@ data: payload, cache: false }) - .done($.proxy(this._displayConfirmation, this, box_id, button)) - .fail($.proxy(this._displayFailure, this, button)); + .done($.proxy(this._displayConfirmation, this, box_id, button)) + .fail($.proxy(this._displayFailure, this, button)); }, /** * Display confirmation modal */ - _displayConfirmation: function(box_id, button, data) { + _displayConfirmation: function (box_id, button, data) { // Trigger pre-display event $(button).trigger("displayConfirmation." + this._name); // Append the form as a modal dialog to the body - $( "body" ).append(data); + $("body").append(data); $('#' + box_id).modal('show'); $('#' + box_id + ' .js-confirm').on('click', $.proxy(this._sendConfirmation, this, box_id, button)); @@ -199,7 +204,7 @@ /** * Send confirmation query */ - _sendConfirmation: function(box_id, button) { + _sendConfirmation: function (box_id, button) { // Prepare payload var url = $(button).data('postUrl'); @@ -212,17 +217,17 @@ // Send ajax $.ajax({ - type: method, - url: url, - data: data + type: method, + url: url, + data: data }) - .done($.proxy(this._confirmationSuccess, this, box_id, button)) - .fail($.proxy(this._displayConfirmationFaillure, this, box_id, button)); + .done($.proxy(this._confirmationSuccess, this, box_id, button)) + .fail($.proxy(this._displayConfirmationFaillure, this, box_id, button)); }, - /** + /** * Action done when a confirmation request is successful */ - _confirmationSuccess: function(box_id, button, data) { + _confirmationSuccess: function (box_id, button, data) { // Trigger success event $(button).trigger("confirmSuccess." + this._name, data); @@ -244,14 +249,14 @@ /** * Failure callback for ajax requests. Displays the error in the main alertElement */ - _displayFailure: function(button, response) { + _displayFailure: function (button, response) { $(button).trigger("error." + this._name); if ((typeof site !== "undefined") && site.debug.ajax && response.responseText) { document.write(response.responseText); document.close(); } else { if (this.settings.DEBUG) { - $.error("Error (" + response.status + "): " + response.responseText ); + $.error("Error (" + response.status + "): " + response.responseText); } this.settings.mainAlertElement.ufAlerts('clear').ufAlerts('fetch').ufAlerts('render'); } @@ -259,18 +264,18 @@ /** * Faillure callback for ajax requests to be displayed in a modal form */ - _displayFormFaillure: function(box_id, button) { + _displayFormFaillure: function (box_id, button) { $(button).trigger("error." + this._name); - $("#"+box_id+" #form-alerts").show(); + $("#" + box_id + " #form-alerts").show(); }, /** * Faillure callback for ajax requests to be displayed in a confirmation form */ - _displayConfirmationFaillure: function(box_id, button) { + _displayConfirmationFaillure: function (box_id, button) { $(button).trigger("error." + this._name); // Setup ufAlerts - var boxMsgTarget = $("#"+box_id+" #confirmation-alerts"); + var boxMsgTarget = $("#" + box_id + " #confirmation-alerts"); // Show the alert. We could have info alert coming in if (!boxMsgTarget.data('ufAlerts')) { @@ -281,7 +286,7 @@ /** * Completely destroy the ufAlerts plugin on the element. */ - destroy: function() { + destroy: function () { // Unbind any bound events this.$elements.off('.' + this._name); @@ -296,7 +301,7 @@ }); // Handles instantiation and access to non-private methods. - $.fn[pluginName] = function(methodOrOptions) { + $.fn[pluginName] = function (methodOrOptions) { // If the plugin is called on a non existing element, return nothing if (this.length == 0) { @@ -319,11 +324,11 @@ var options = Array.prototype.slice.call(arguments, 1)[0]; } else { - $.error( 'Method ' + methodOrOptions + ' is private!' ); + $.error('Method ' + methodOrOptions + ' is private!'); } } else { - $.error( 'Method ' + methodOrOptions + ' is invalid.' ); + $.error('Method ' + methodOrOptions + ' is invalid.'); } // Only initalise if not previously done. @@ -337,7 +342,7 @@ // Run the required method return instance[method](options); } else { - $.error( 'Method ' + method + ' does not exist.' ); + $.error('Method ' + method + ' does not exist.'); } }; |