From f90b6200e450f6b87cc310ab5734a09719d891d3 Mon Sep 17 00:00:00 2001
From: winniehell <git@winniehell.de>
Date: Mon, 7 Nov 2016 01:16:39 +0100
Subject: [PATCH] Clean up common_utils.js (!7318)

---
 app/assets/javascripts/application.js         | 18 ++++++-
 .../javascripts/gfm_auto_complete.js.es6      | 18 ++++---
 .../javascripts/lib/utils/common_utils.js     | 54 -------------------
 app/assets/javascripts/merge_request_tabs.js  |  3 +-
 .../unreleased/cleanup-common_utils-js.yml    |  4 ++
 spec/javascripts/application_spec.js          | 37 -------------
 .../fixtures/application.html.haml            |  2 -
 7 files changed, 33 insertions(+), 103 deletions(-)
 create mode 100644 changelogs/unreleased/cleanup-common_utils-js.yml
 delete mode 100644 spec/javascripts/application_spec.js
 delete mode 100644 spec/javascripts/fixtures/application.html.haml

diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index cfab4721f4b..b7c4673c8e3 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -56,7 +56,13 @@
 /*= require es6-promise.auto */
 
 (function () {
-  document.addEventListener('page:fetch', gl.utils.cleanupBeforeFetch);
+  document.addEventListener('page:fetch', function () {
+    // Unbind scroll events
+    $(document).off('scroll');
+    // Close any open tooltips
+    $('.has-tooltip, [data-toggle="tooltip"]').tooltip('destroy');
+  });
+
   window.addEventListener('hashchange', gl.utils.handleLocationHash);
   window.addEventListener('load', function onLoad() {
     window.removeEventListener('load', onLoad, false);
@@ -76,7 +82,15 @@
     // Set the default path for all cookies to GitLab's root directory
     Cookies.defaults.path = gon.relative_url_root || '/';
 
-    gl.utils.preventDisabledButtons();
+    // prevent default action for disabled buttons
+    $('.btn').click(function(e) {
+      if ($(this).hasClass('disabled')) {
+        e.preventDefault();
+        e.stopImmediatePropagation();
+        return false;
+      }
+    });
+
     $('.nav-sidebar').niceScroll({
       cursoropacitymax: '0.4',
       cursorcolor: '#FFF',
diff --git a/app/assets/javascripts/gfm_auto_complete.js.es6 b/app/assets/javascripts/gfm_auto_complete.js.es6
index 10769b7fd4f..6f9d6283071 100644
--- a/app/assets/javascripts/gfm_auto_complete.js.es6
+++ b/app/assets/javascripts/gfm_auto_complete.js.es6
@@ -5,6 +5,10 @@
     window.GitLab = {};
   }
 
+  function sanitize(str) {
+    return str.replace(/<(?:.|\n)*?>/gm, '');
+  }
+
   GitLab.GfmAutoComplete = {
     dataLoading: false,
     dataLoaded: false,
@@ -160,8 +164,8 @@
               return {
                 username: m.username,
                 avatarTag: autoCompleteAvatar.length === 1 ?  txtAvatar : imgAvatar,
-                title: gl.utils.sanitize(title),
-                search: gl.utils.sanitize(m.username + " " + m.name)
+                title: sanitize(title),
+                search: sanitize(m.username + " " + m.name)
               };
             });
           }
@@ -195,7 +199,7 @@
               }
               return {
                 id: i.iid,
-                title: gl.utils.sanitize(i.title),
+                title: sanitize(i.title),
                 search: i.iid + " " + i.title
               };
             });
@@ -228,7 +232,7 @@
               }
               return {
                 id: m.iid,
-                title: gl.utils.sanitize(m.title),
+                title: sanitize(m.title),
                 search: "" + m.title
               };
             });
@@ -263,7 +267,7 @@
               }
               return {
                 id: m.iid,
-                title: gl.utils.sanitize(m.title),
+                title: sanitize(m.title),
                 search: m.iid + " " + m.title
               };
             });
@@ -284,9 +288,9 @@
             var sanitizeLabelTitle;
             sanitizeLabelTitle = function(title) {
               if (/[\w\?&]+\s+[\w\?&]+/g.test(title)) {
-                return "\"" + (gl.utils.sanitize(title)) + "\"";
+                return "\"" + (sanitize(title)) + "\"";
               } else {
-                return gl.utils.sanitize(title);
+                return sanitize(title);
               }
             };
             return $.map(merges, function(m) {
diff --git a/app/assets/javascripts/lib/utils/common_utils.js b/app/assets/javascripts/lib/utils/common_utils.js
index c5846068b07..29cba1a49dd 100644
--- a/app/assets/javascripts/lib/utils/common_utils.js
+++ b/app/assets/javascripts/lib/utils/common_utils.js
@@ -33,10 +33,6 @@
       });
     };
 
-    w.gl.utils.split = function(val) {
-      return val.split(/,\s*/);
-    };
-
     w.gl.utils.extractLast = function(term) {
       return this.split(term).pop();
     };
@@ -67,33 +63,6 @@
       });
     };
 
-    w.gl.utils.disableButtonIfAnyEmptyField = function(form, form_selector, button_selector) {
-      var closest_submit, updateButtons;
-      closest_submit = form.find(button_selector);
-      updateButtons = function() {
-        var filled;
-        filled = true;
-        form.find('input').filter(form_selector).each(function() {
-          return filled = this.rstrip($(this).val()) !== "" || !$(this).attr('required');
-        });
-        if (filled) {
-          return closest_submit.enable();
-        } else {
-          return closest_submit.disable();
-        }
-      };
-      updateButtons();
-      return form.keyup(updateButtons);
-    };
-
-    w.gl.utils.sanitize = function(str) {
-      return str.replace(/<(?:.|\n)*?>/gm, '');
-    };
-
-    w.gl.utils.unbindEvents = function() {
-      return $(document).off('scroll');
-    };
-
     // automatically adjust scroll position for hash urls taking the height of the navbar into account
     // https://github.com/twitter/bootstrap/issues/1768
     w.gl.utils.handleLocationHash = function() {
@@ -124,32 +93,9 @@
       }
     };
 
-    gl.utils.updateTooltipTitle = function($tooltipEl, newTitle) {
-      return $tooltipEl.tooltip('destroy').attr('title', newTitle).tooltip('fixTitle');
-    };
-    gl.utils.preventDisabledButtons = function() {
-      return $('.btn').click(function(e) {
-        if ($(this).hasClass('disabled')) {
-          e.preventDefault();
-          e.stopImmediatePropagation();
-          return false;
-        }
-      });
-    };
     gl.utils.getPagePath = function() {
       return $('body').data('page').split(':')[0];
     };
-    gl.utils.parseUrl = function (url) {
-      var parser = document.createElement('a');
-      parser.href = url;
-      return parser;
-    };
-    gl.utils.cleanupBeforeFetch = function() {
-      // Unbind scroll events
-      $(document).off('scroll');
-      // Close any open tooltips
-      $('.has-tooltip, [data-toggle="tooltip"]').tooltip('destroy');
-    };
 
     gl.utils.isMetaKey = function(e) {
       return e.metaKey || e.ctrlKey || e.altKey || e.shiftKey;
diff --git a/app/assets/javascripts/merge_request_tabs.js b/app/assets/javascripts/merge_request_tabs.js
index b1928f8d279..4a192ca5796 100644
--- a/app/assets/javascripts/merge_request_tabs.js
+++ b/app/assets/javascripts/merge_request_tabs.js
@@ -220,7 +220,8 @@
 
       // We extract pathname for the current Changes tab anchor href
       // some pages like MergeRequestsController#new has query parameters on that anchor
-      var url = gl.utils.parseUrl(source);
+      var url = document.createElement('a');
+      url.href = source;
 
       return this._get({
         url: (url.pathname + ".json") + this._location.search,
diff --git a/changelogs/unreleased/cleanup-common_utils-js.yml b/changelogs/unreleased/cleanup-common_utils-js.yml
new file mode 100644
index 00000000000..54d81b76c28
--- /dev/null
+++ b/changelogs/unreleased/cleanup-common_utils-js.yml
@@ -0,0 +1,4 @@
+---
+title: Clean up common_utils.js
+merge_request: 7318
+author: winniehell
diff --git a/spec/javascripts/application_spec.js b/spec/javascripts/application_spec.js
deleted file mode 100644
index 7e38abc608e..00000000000
--- a/spec/javascripts/application_spec.js
+++ /dev/null
@@ -1,37 +0,0 @@
-/* eslint-disable space-before-function-paren, one-var, no-var, one-var-declaration-per-line, no-return-assign, padded-blocks, max-len */
-
-/*= require lib/utils/common_utils */
-
-(function() {
-  describe('Application', function() {
-    return describe('disable buttons', function() {
-      fixture.preload('application.html');
-      beforeEach(function() {
-        return fixture.load('application.html');
-      });
-      it('should prevent default action for disabled buttons', function() {
-        var $button, isClicked;
-        gl.utils.preventDisabledButtons();
-        isClicked = false;
-        $button = $('#test-button');
-        expect($button).toExist();
-        $button.click(function() {
-          return isClicked = true;
-        });
-        $button.trigger('click');
-        return expect(isClicked).toBe(false);
-      });
-
-      it('should be on the same page if a disabled link clicked', function() {
-        var locationBeforeLinkClick, $link;
-        locationBeforeLinkClick = window.location.href;
-        gl.utils.preventDisabledButtons();
-        $link = $('#test-link');
-        expect($link).toExist();
-        $link.click();
-        return expect(window.location.href).toBe(locationBeforeLinkClick);
-      });
-    });
-  });
-
-}).call(this);
diff --git a/spec/javascripts/fixtures/application.html.haml b/spec/javascripts/fixtures/application.html.haml
deleted file mode 100644
index 3fc6114407d..00000000000
--- a/spec/javascripts/fixtures/application.html.haml
+++ /dev/null
@@ -1,2 +0,0 @@
-%a#test-link.btn.disabled{:href => "/foo"} Test link
-%button#test-button.btn.disabled Test Button
-- 
GitLab