Commit 2f91c0ee authored by Alfredo Sumaran's avatar Alfredo Sumaran

Merge branch 'cleanup-common_utils.js' into 'master'

Clean up common_utils.js

## What does this MR do?

- Remove unused functions from `common_utils.js`.
- Move functions which are used from only one file to that file.

## Why was this MR needed?

Because some utils weren't that common (anymore).

See merge request !7318
parents af19d56f f90b6200
......@@ -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',
......
......@@ -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) {
......
......@@ -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;
......
......@@ -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,
......
---
title: Clean up common_utils.js
merge_request: 7318
author: winniehell
/* 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);
%a#test-link.btn.disabled{:href => "/foo"} Test link
%button#test-button.btn.disabled Test Button
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment