[TASK] Migrate SplitButtons to TypeScript 06/55906/3
authorAndreas Fernandez <a.fernandez@scripting-base.de>
Mon, 26 Feb 2018 15:27:01 +0000 (16:27 +0100)
committerBenni Mack <benni@typo3.org>
Mon, 26 Feb 2018 20:18:40 +0000 (21:18 +0100)
The SplitButtons module is migrated to TypeScript. Additionally, some
parameters of the Icons module are declared as optional.

Change-Id: I61f883e8e496e018a45f63c303de66274d071d94
Resolves: #82602
Releases: master
Reviewed-on: https://review.typo3.org/55906
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/backend/Resources/Private/TypeScript/SplitButtons.ts [new file with mode: 0644]
typo3/sysext/backend/Resources/Public/JavaScript/SplitButtons.js

diff --git a/typo3/sysext/backend/Resources/Private/TypeScript/SplitButtons.ts b/typo3/sysext/backend/Resources/Private/TypeScript/SplitButtons.ts
new file mode 100644 (file)
index 0000000..9e4ffe2
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with DocumentHeader source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+import * as $ from 'jquery';
+import Icons = require('./Icons');
+
+class SplitButtons {
+  private preSubmitCallbacks: Array<Function> = [];
+
+  constructor() {
+    $((): void => {
+      this.initializeSaveHandling();
+    });
+  }
+
+  /**
+   * Adds a callback being executed before submit
+   *
+   * @param {Function} callback
+   */
+  public addPreSubmitCallback(callback: Function): void {
+    if (typeof callback !== 'function') {
+      throw 'callback must be a function.';
+    }
+
+    this.preSubmitCallbacks.push(callback);
+  }
+
+  /**
+   * Initializes the save handling
+   */
+  private initializeSaveHandling(): void {
+    let preventExec = false;
+    const elements = [
+      'button[form]',
+      'button[name^="_save"]',
+      'a[data-name^="_save"]',
+      'button[name="CMD"][value^="save"]',
+      'a[data-name="CMD"][data-value^="save"]',
+      'button[name^="_translation_save"]',
+      'a[data-name^="_translation_save"]',
+      'button[name="CMD"][value^="_translation_save"]',
+      'a[data-name="CMD"][data-value^="_translation_save"]'
+    ].join(',');
+
+    $('.t3js-module-docheader').on('click', elements, (e: JQueryEventObject): boolean => {
+      // prevent doubleclick double submission bug in chrome,
+      // see https://forge.typo3.org/issues/77942
+      if (!preventExec) {
+        preventExec = true;
+        const $me = $(e.currentTarget);
+        const linkedForm = $me.attr('form') || $me.attr('data-form') || null;
+        const $form = linkedForm ? $('#' + linkedForm) : $me.closest('form');
+        const name = $me.data('name') || e.currentTarget.getAttribute('name');
+        const value = $me.data('value') || e.currentTarget.getAttribute('value');
+        const $elem = $('<input />').attr('type', 'hidden').attr('name', name).attr('value', value);
+
+        // Run any preSubmit callbacks
+        for (let i = 0; i < this.preSubmitCallbacks.length; ++i) {
+          this.preSubmitCallbacks[i](e);
+
+          if (e.isPropagationStopped()) {
+            preventExec = false;
+            return false;
+          }
+        }
+        $form.append($elem);
+        // Disable submit buttons
+        $form.on('submit', (): boolean => {
+          if ($form.find('.has-error').length > 0) {
+            preventExec = false;
+            return false;
+          }
+
+          let $affectedButton: JQuery;
+          const $splitButton = $me.closest('.t3js-splitbutton');
+
+          if ($splitButton.length > 0) {
+            $splitButton.find('button').prop('disabled', true);
+            $affectedButton = $splitButton.children().first();
+          } else {
+            $me.prop('disabled', true);
+            $affectedButton = $me;
+          }
+
+          Icons.getIcon('spinner-circle-dark', Icons.sizes.small).done((markup: string): void => {
+            $affectedButton.find('.t3js-icon').replaceWith(markup);
+          });
+
+          return true;
+        });
+
+        if ((e.currentTarget.tagName === 'A' || $me.attr('form')) && !e.isDefaultPrevented()) {
+          $form.submit();
+          e.preventDefault();
+        }
+      }
+
+      return true;
+    });
+  }
+}
+
+export = new SplitButtons();
index 702be26..eed1873 100644 (file)
@@ -6,110 +6,8 @@
  * of the License, or any later version.
  *
  * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with DocumentHeader source code.
+ * LICENSE.txt file that was distributed with this source code.
  *
  * The TYPO3 project - inspiring people to share!
  */
-
-/**
- * Module: TYPO3/CMS/Backend/SplitButtons
- * Initializes global handling of split buttons.
- */
-define(['jquery', 'TYPO3/CMS/Backend/Icons'], function($, Icons) {
-  'use strict';
-
-  /**
-   *
-   * @type {{preSubmitCallbacks: Array}}
-   * @exports TYPO3/CMS/Backend/SplitButtons
-   */
-  var SplitButtons = {
-    preSubmitCallbacks: []
-  };
-
-  /**
-   * Initializes the save handling
-   */
-  SplitButtons.initializeSaveHandling = function() {
-    var preventExec = false;
-    var elements = [
-      'button[form]',
-      'button[name^="_save"]',
-      'a[data-name^="_save"]',
-      'button[name="CMD"][value^="save"]',
-      'a[data-name="CMD"][data-value^="save"]',
-      'button[name^="_translation_save"]',
-      'a[data-name^="_translation_save"]',
-      'button[name="CMD"][value^="_translation_save"]',
-      'a[data-name="CMD"][data-value^="_translation_save"]'
-    ].join(',');
-    $('.t3js-module-docheader').on('click', elements, function(e) {
-      // prevent doubleclick double submission bug in chrome,
-      // see https://forge.typo3.org/issues/77942
-      if (!preventExec) {
-        preventExec = true;
-        var $me = $(this),
-          linkedForm = $me.attr('form') || $me.attr('data-form') || null,
-          $form = linkedForm ? $('#' + linkedForm) : $me.closest('form'),
-          name = $me.data('name') || this.name,
-          value = $me.data('value') || this.value,
-          $elem = $('<input />').attr('type', 'hidden').attr('name', name).attr('value', value);
-
-        // Run any preSubmit callbacks
-        for (var i = 0; i < SplitButtons.preSubmitCallbacks.length; ++i) {
-          SplitButtons.preSubmitCallbacks[i](e);
-
-          if (e.isPropagationStopped()) {
-            preventExec = false;
-            return false;
-          }
-        }
-        $form.append($elem);
-        // Disable submit buttons
-        $form.on('submit', function() {
-          if ($form.find('.has-error').length > 0) {
-            preventExec = false;
-            return false;
-          }
-
-          var $affectedButton,
-            $splitButton = $me.closest('.t3js-splitbutton');
-
-          if ($splitButton.length > 0) {
-            $splitButton.find('button').prop('disabled', true);
-            $affectedButton = $splitButton.children().first();
-          } else {
-            $me.prop('disabled', true);
-            $affectedButton = $me;
-          }
-
-          Icons.getIcon('spinner-circle-dark', Icons.sizes.small).done(function(markup) {
-            $affectedButton.find('.t3js-icon').replaceWith(markup);
-          });
-        });
-
-        if ((e.currentTarget.tagName === 'A' || $me.attr('form')) && !e.isDefaultPrevented()) {
-          $form.submit();
-          e.preventDefault();
-        }
-      }
-    });
-  };
-
-  /**
-   * Adds a callback being executed before submit
-   *
-   * @param {function} callback
-   */
-  SplitButtons.addPreSubmitCallback = function(callback) {
-    if (typeof callback !== 'function') {
-      throw 'callback must be a function.';
-    }
-
-    SplitButtons.preSubmitCallbacks.push(callback);
-  };
-
-  $(SplitButtons.initializeSaveHandling);
-
-  return SplitButtons;
-});
+define(["require","exports","jquery","./Icons"],function(a,b,c,d){"use strict";var e=function(){function a(){var a=this;this.preSubmitCallbacks=[],c(function(){a.initializeSaveHandling()})}return a.prototype.addPreSubmitCallback=function(a){if("function"!=typeof a)throw"callback must be a function.";this.preSubmitCallbacks.push(a)},a.prototype.initializeSaveHandling=function(){var a=this,b=!1,e=["button[form]",'button[name^="_save"]','a[data-name^="_save"]','button[name="CMD"][value^="save"]','a[data-name="CMD"][data-value^="save"]','button[name^="_translation_save"]','a[data-name^="_translation_save"]','button[name="CMD"][value^="_translation_save"]','a[data-name="CMD"][data-value^="_translation_save"]'].join(",");c(".t3js-module-docheader").on("click",e,function(e){if(!b){b=!0;for(var f=c(e.currentTarget),g=f.attr("form")||f.attr("data-form")||null,h=g?c("#"+g):f.closest("form"),i=f.data("name")||e.currentTarget.getAttribute("name"),j=f.data("value")||e.currentTarget.getAttribute("value"),k=c("<input />").attr("type","hidden").attr("name",i).attr("value",j),l=0;l<a.preSubmitCallbacks.length;++l)if(a.preSubmitCallbacks[l](e),e.isPropagationStopped())return b=!1,!1;h.append(k),h.on("submit",function(){if(h.find(".has-error").length>0)return b=!1,!1;var a,c=f.closest(".t3js-splitbutton");return c.length>0?(c.find("button").prop("disabled",!0),a=c.children().first()):(f.prop("disabled",!0),a=f),d.getIcon("spinner-circle-dark",d.sizes.small).done(function(b){a.find(".t3js-icon").replaceWith(b)}),!0}),"A"!==e.currentTarget.tagName&&!f.attr("form")||e.isDefaultPrevented()||(h.submit(),e.preventDefault())}return!0})},a}();return new e});
\ No newline at end of file