Commit f90b6200 authored by winniehell's avatar winniehell

Clean up common_utils.js (!7318)

parent 629624f3
...@@ -56,7 +56,13 @@ ...@@ -56,7 +56,13 @@
/*= require es6-promise.auto */ /*= require es6-promise.auto */
(function () { (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('hashchange', gl.utils.handleLocationHash);
window.addEventListener('load', function onLoad() { window.addEventListener('load', function onLoad() {
window.removeEventListener('load', onLoad, false); window.removeEventListener('load', onLoad, false);
...@@ -76,7 +82,15 @@ ...@@ -76,7 +82,15 @@
// Set the default path for all cookies to GitLab's root directory // Set the default path for all cookies to GitLab's root directory
Cookies.defaults.path = gon.relative_url_root || '/'; 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({ $('.nav-sidebar').niceScroll({
cursoropacitymax: '0.4', cursoropacitymax: '0.4',
cursorcolor: '#FFF', cursorcolor: '#FFF',
......
...@@ -5,6 +5,10 @@ ...@@ -5,6 +5,10 @@
window.GitLab = {}; window.GitLab = {};
} }
function sanitize(str) {
return str.replace(/<(?:.|\n)*?>/gm, '');
}
GitLab.GfmAutoComplete = { GitLab.GfmAutoComplete = {
dataLoading: false, dataLoading: false,
dataLoaded: false, dataLoaded: false,
...@@ -160,8 +164,8 @@ ...@@ -160,8 +164,8 @@
return { return {
username: m.username, username: m.username,
avatarTag: autoCompleteAvatar.length === 1 ? txtAvatar : imgAvatar, avatarTag: autoCompleteAvatar.length === 1 ? txtAvatar : imgAvatar,
title: gl.utils.sanitize(title), title: sanitize(title),
search: gl.utils.sanitize(m.username + " " + m.name) search: sanitize(m.username + " " + m.name)
}; };
}); });
} }
...@@ -195,7 +199,7 @@ ...@@ -195,7 +199,7 @@
} }
return { return {
id: i.iid, id: i.iid,
title: gl.utils.sanitize(i.title), title: sanitize(i.title),
search: i.iid + " " + i.title search: i.iid + " " + i.title
}; };
}); });
...@@ -228,7 +232,7 @@ ...@@ -228,7 +232,7 @@
} }
return { return {
id: m.iid, id: m.iid,
title: gl.utils.sanitize(m.title), title: sanitize(m.title),
search: "" + m.title search: "" + m.title
}; };
}); });
...@@ -263,7 +267,7 @@ ...@@ -263,7 +267,7 @@
} }
return { return {
id: m.iid, id: m.iid,
title: gl.utils.sanitize(m.title), title: sanitize(m.title),
search: m.iid + " " + m.title search: m.iid + " " + m.title
}; };
}); });
...@@ -284,9 +288,9 @@ ...@@ -284,9 +288,9 @@
var sanitizeLabelTitle; var sanitizeLabelTitle;
sanitizeLabelTitle = function(title) { sanitizeLabelTitle = function(title) {
if (/[\w\?&]+\s+[\w\?&]+/g.test(title)) { if (/[\w\?&]+\s+[\w\?&]+/g.test(title)) {
return "\"" + (gl.utils.sanitize(title)) + "\""; return "\"" + (sanitize(title)) + "\"";
} else { } else {
return gl.utils.sanitize(title); return sanitize(title);
} }
}; };
return $.map(merges, function(m) { return $.map(merges, function(m) {
......
...@@ -33,10 +33,6 @@ ...@@ -33,10 +33,6 @@
}); });
}; };
w.gl.utils.split = function(val) {
return val.split(/,\s*/);
};
w.gl.utils.extractLast = function(term) { w.gl.utils.extractLast = function(term) {
return this.split(term).pop(); return this.split(term).pop();
}; };
...@@ -67,33 +63,6 @@ ...@@ -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 // automatically adjust scroll position for hash urls taking the height of the navbar into account
// https://github.com/twitter/bootstrap/issues/1768 // https://github.com/twitter/bootstrap/issues/1768
w.gl.utils.handleLocationHash = function() { w.gl.utils.handleLocationHash = function() {
...@@ -124,32 +93,9 @@ ...@@ -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() { gl.utils.getPagePath = function() {
return $('body').data('page').split(':')[0]; 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) { gl.utils.isMetaKey = function(e) {
return e.metaKey || e.ctrlKey || e.altKey || e.shiftKey; return e.metaKey || e.ctrlKey || e.altKey || e.shiftKey;
......
...@@ -220,7 +220,8 @@ ...@@ -220,7 +220,8 @@
// We extract pathname for the current Changes tab anchor href // We extract pathname for the current Changes tab anchor href
// some pages like MergeRequestsController#new has query parameters on that anchor // 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({ return this._get({
url: (url.pathname + ".json") + this._location.search, 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