Commit 29e1a63d authored by Phil Hughes's avatar Phil Hughes

Export JS classes as modules

#38869
parent c96e2edc
/* eslint-disable no-param-reassign, class-methods-use-this */ /* eslint-disable no-param-reassign, class-methods-use-this */
/* global Pager */
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
import Pager from './pager';
import { localTimeAgo } from './lib/utils/datetime_utility'; import { localTimeAgo } from './lib/utils/datetime_utility';
export default class Activities { export default class Activities {
......
/* eslint-disable func-names, wrap-iife, consistent-return, /* eslint-disable func-names, wrap-iife, consistent-return,
no-return-assign, no-param-reassign, one-var-declaration-per-line, no-unused-vars, no-return-assign, no-param-reassign, one-var-declaration-per-line, no-unused-vars,
prefer-template, object-shorthand, prefer-arrow-callback */ prefer-template, object-shorthand, prefer-arrow-callback */
/* global Pager */
import { pluralize } from './lib/utils/text_utility'; import { pluralize } from './lib/utils/text_utility';
import { localTimeAgo } from './lib/utils/datetime_utility'; import { localTimeAgo } from './lib/utils/datetime_utility';
import Pager from './pager';
export default (function () { export default (function () {
const CommitsList = {}; const CommitsList = {};
......
...@@ -7,8 +7,8 @@ import IssuableForm from './issuable_form'; ...@@ -7,8 +7,8 @@ import IssuableForm from './issuable_form';
import LabelsSelect from './labels_select'; import LabelsSelect from './labels_select';
/* global MilestoneSelect */ /* global MilestoneSelect */
import NewBranchForm from './new_branch_form'; import NewBranchForm from './new_branch_form';
/* global NotificationsForm */ import NotificationsForm from './notifications_form';
/* global NotificationsDropdown */ import notificationsDropdown from './notifications_dropdown';
import groupAvatar from './group_avatar'; import groupAvatar from './group_avatar';
import GroupLabelSubscription from './group_label_subscription'; import GroupLabelSubscription from './group_label_subscription';
/* global LineHighlighter */ /* global LineHighlighter */
...@@ -414,7 +414,7 @@ import Activities from './activities'; ...@@ -414,7 +414,7 @@ import Activities from './activities';
const newGroupChildWrapper = document.querySelector('.js-new-project-subgroup'); const newGroupChildWrapper = document.querySelector('.js-new-project-subgroup');
shortcut_handler = new ShortcutsNavigation(); shortcut_handler = new ShortcutsNavigation();
new NotificationsForm(); new NotificationsForm();
new NotificationsDropdown(); notificationsDropdown();
new ProjectsList(); new ProjectsList();
if (newGroupChildWrapper) { if (newGroupChildWrapper) {
...@@ -617,7 +617,7 @@ import Activities from './activities'; ...@@ -617,7 +617,7 @@ import Activities from './activities';
break; break;
case 'profiles': case 'profiles':
new NotificationsForm(); new NotificationsForm();
new NotificationsDropdown(); notificationsDropdown();
break; break;
case 'projects': case 'projects':
new Project(); new Project();
...@@ -640,7 +640,7 @@ import Activities from './activities'; ...@@ -640,7 +640,7 @@ import Activities from './activities';
case 'show': case 'show':
new Star(); new Star();
new ProjectNew(); new ProjectNew();
new NotificationsDropdown(); notificationsDropdown();
break; break;
case 'wikis': case 'wikis':
new Wikis(); new Wikis();
......
...@@ -34,16 +34,11 @@ import { getLocationHash, visitUrl } from './lib/utils/url_utility'; ...@@ -34,16 +34,11 @@ import { getLocationHash, visitUrl } from './lib/utils/url_utility';
import './behaviors/'; import './behaviors/';
// everything else // everything else
import './activities';
import './admin';
import loadAwardsHandler from './awards_handler'; import loadAwardsHandler from './awards_handler';
import bp from './breakpoints'; import bp from './breakpoints';
import './confirm_danger_modal'; import './confirm_danger_modal';
import Flash, { removeFlashClickListener } from './flash'; import Flash, { removeFlashClickListener } from './flash';
import './gl_dropdown'; import './gl_dropdown';
import './gl_field_error';
import './gl_field_errors';
import './gl_form';
import initTodoToggle from './header'; import initTodoToggle from './header';
import initImporterStatus from './importer_status'; import initImporterStatus from './importer_status';
import './layout_nav'; import './layout_nav';
...@@ -54,11 +49,7 @@ import './merge_request'; ...@@ -54,11 +49,7 @@ import './merge_request';
import './merge_request_tabs'; import './merge_request_tabs';
import './milestone_select'; import './milestone_select';
import './notes'; import './notes';
import './notifications_dropdown';
import './notifications_form';
import './pager';
import './preview_markdown'; import './preview_markdown';
import './project_import';
import './projects_dropdown'; import './projects_dropdown';
import './render_gfm'; import './render_gfm';
import './right_sidebar'; import './right_sidebar';
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
import 'vendor/jquery.waitforimages'; import 'vendor/jquery.waitforimages';
import TaskList from './task_list'; import TaskList from './task_list';
import './merge_request_tabs'; import MergeRequestTabs from './merge_request_tabs';
import IssuablesHelper from './helpers/issuables_helper'; import IssuablesHelper from './helpers/issuables_helper';
import { addDelimiter } from './lib/utils/text_utility'; import { addDelimiter } from './lib/utils/text_utility';
...@@ -51,7 +51,7 @@ import { addDelimiter } from './lib/utils/text_utility'; ...@@ -51,7 +51,7 @@ import { addDelimiter } from './lib/utils/text_utility';
if (window.mrTabs) { if (window.mrTabs) {
window.mrTabs.unbindEvents(); window.mrTabs.unbindEvents();
} }
window.mrTabs = new gl.MergeRequestTabs(this.opts); window.mrTabs = new MergeRequestTabs(this.opts);
}; };
MergeRequest.prototype.showAllCommits = function() { MergeRequest.prototype.showAllCommits = function() {
......
/* eslint-disable func-names, space-before-function-paren, wrap-iife, one-var, no-var, one-var-declaration-per-line, no-unused-vars, consistent-return, prefer-arrow-callback, no-else-return, max-len */
import Flash from './flash'; import Flash from './flash';
(function() { export default function notificationsDropdown() {
this.NotificationsDropdown = (function() { $(document).on('click', '.update-notification', function updateNotificationCallback(e) {
function NotificationsDropdown() { e.preventDefault();
$(document).off('click', '.update-notification').on('click', '.update-notification', function(e) { if ($(this).is('.is-active') && $(this).data('notification-level') === 'custom') {
var form, label, notificationLevel; return;
e.preventDefault();
if ($(this).is('.is-active') && $(this).data('notification-level') === 'custom') {
return;
}
notificationLevel = $(this).data('notification-level');
label = $(this).data('notification-title');
form = $(this).parents('.notification-form:first');
form.find('.js-notification-loading').toggleClass('fa-bell fa-spin fa-spinner');
form.find('#notification_setting_level').val(notificationLevel);
return form.submit();
});
$(document).off('ajax:success', '.notification-form').on('ajax:success', '.notification-form', function(e, data) {
if (data.saved) {
return $(e.currentTarget).closest('.js-notification-dropdown').replaceWith(data.html);
} else {
return new Flash('Failed to save new settings', 'alert');
}
});
} }
return NotificationsDropdown; const notificationLevel = $(this).data('notification-level');
})(); const form = $(this).parents('.notification-form:first');
}).call(window);
form.find('.js-notification-loading').toggleClass('fa-bell fa-spin fa-spinner');
form.find('#notification_setting_level').val(notificationLevel);
form.submit();
});
$(document).on('ajax:success', '.notification-form', (e, data) => {
if (data.saved) {
$(e.currentTarget).closest('.js-notification-dropdown').replaceWith(data.html);
} else {
Flash('Failed to save new settings', 'alert');
}
});
}
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, one-var, one-var-declaration-per-line, newline-per-chained-call, comma-dangle, consistent-return, prefer-arrow-callback, max-len */ export default class NotificationsForm {
(function() { constructor() {
this.NotificationsForm = (function() { this.toggleCheckbox = this.toggleCheckbox.bind(this);
function NotificationsForm() { this.initEventListeners();
this.toggleCheckbox = this.toggleCheckbox.bind(this); }
this.removeEventListeners();
this.initEventListeners();
}
NotificationsForm.prototype.removeEventListeners = function() { initEventListeners() {
return $(document).off('change', '.js-custom-notification-event'); $(document).on('change', '.js-custom-notification-event', this.toggleCheckbox);
}; }
NotificationsForm.prototype.initEventListeners = function() { toggleCheckbox(e) {
return $(document).on('change', '.js-custom-notification-event', this.toggleCheckbox); const $checkbox = $(e.currentTarget);
}; const $parent = $checkbox.closest('.checkbox');
NotificationsForm.prototype.toggleCheckbox = function(e) { this.saveEvent($checkbox, $parent);
var $checkbox, $parent; }
$checkbox = $(e.currentTarget);
$parent = $checkbox.closest('.checkbox');
return this.saveEvent($checkbox, $parent);
};
NotificationsForm.prototype.showCheckboxLoadingSpinner = function($parent) { // eslint-disable-next-line class-methods-use-this
return $parent.addClass('is-loading').find('.custom-notification-event-loading').removeClass('fa-check').addClass('fa-spin fa-spinner').removeClass('is-done'); showCheckboxLoadingSpinner($parent) {
}; $parent.addClass('is-loading')
.find('.custom-notification-event-loading')
.removeClass('fa-check')
.addClass('fa-spin fa-spinner')
.removeClass('is-done');
}
NotificationsForm.prototype.saveEvent = function($checkbox, $parent) { saveEvent($checkbox, $parent) {
var form; const form = $parent.parents('form:first');
form = $parent.parents('form:first');
return $.ajax({
url: form.attr('action'),
method: form.attr('method'),
dataType: 'json',
data: form.serialize(),
beforeSend: (function(_this) {
return function() {
return _this.showCheckboxLoadingSpinner($parent);
};
})(this)
}).done(function(data) {
$checkbox.enable();
if (data.saved) {
$parent.find('.custom-notification-event-loading').toggleClass('fa-spin fa-spinner fa-check is-done');
return setTimeout(function() {
return $parent.removeClass('is-loading').find('.custom-notification-event-loading').toggleClass('fa-spin fa-spinner fa-check is-done');
}, 2000);
}
});
};
return NotificationsForm; return $.ajax({
})(); url: form.attr('action'),
}).call(window); method: form.attr('method'),
dataType: 'json',
data: form.serialize(),
beforeSend: () => {
this.showCheckboxLoadingSpinner($parent);
},
}).done((data) => {
$checkbox.enable();
if (data.saved) {
$parent.find('.custom-notification-event-loading').toggleClass('fa-spin fa-spinner fa-check is-done');
setTimeout(() => {
$parent.removeClass('is-loading')
.find('.custom-notification-event-loading')
.toggleClass('fa-spin fa-spinner fa-check is-done');
}, 2000);
}
});
}
}
import { getParameterByName } from '~/lib/utils/common_utils'; import { getParameterByName } from '~/lib/utils/common_utils';
import { removeParams } from './lib/utils/url_utility'; import { removeParams } from './lib/utils/url_utility';
(() => { const ENDLESS_SCROLL_BOTTOM_PX = 400;
const ENDLESS_SCROLL_BOTTOM_PX = 400; const ENDLESS_SCROLL_FIRE_DELAY_MS = 1000;
const ENDLESS_SCROLL_FIRE_DELAY_MS = 1000;
const Pager = { export default {
init(limit = 0, preload = false, disable = false, prepareData = $.noop, callback = $.noop) { init(limit = 0, preload = false, disable = false, prepareData = $.noop, callback = $.noop) {
this.url = $('.content_list').data('href') || removeParams(['limit', 'offset']); this.url = $('.content_list').data('href') || removeParams(['limit', 'offset']);
this.limit = limit; this.limit = limit;
this.offset = parseInt(getParameterByName('offset'), 10) || this.limit; this.offset = parseInt(getParameterByName('offset'), 10) || this.limit;
this.disable = disable; this.disable = disable;
this.prepareData = prepareData; this.prepareData = prepareData;
this.callback = callback; this.callback = callback;
this.loading = $('.loading').first(); this.loading = $('.loading').first();
if (preload) { if (preload) {
this.offset = 0; this.offset = 0;
this.getOld(); this.getOld();
} }
this.initLoadMore(); this.initLoadMore();
}, },
getOld() { getOld() {
this.loading.show(); this.loading.show();
$.ajax({ $.ajax({
type: 'GET', type: 'GET',
url: this.url, url: this.url,
data: `limit=${this.limit}&offset=${this.offset}`, data: `limit=${this.limit}&offset=${this.offset}`,
dataType: 'json', dataType: 'json',
error: () => this.loading.hide(), error: () => this.loading.hide(),
success: (data) => { success: (data) => {
this.append(data.count, this.prepareData(data.html)); this.append(data.count, this.prepareData(data.html));
this.callback(); this.callback();
// keep loading until we've filled the viewport height // keep loading until we've filled the viewport height
if (!this.disable && !this.isScrollable()) { if (!this.disable && !this.isScrollable()) {
this.getOld(); this.getOld();
} else { } else {
this.loading.hide(); this.loading.hide();
} }
}, },
}); });
}, },
append(count, html) { append(count, html) {
$('.content_list').append(html); $('.content_list').append(html);
if (count > 0) { if (count > 0) {
this.offset += count; this.offset += count;
} else { } else {
this.disable = true; this.disable = true;
} }
}, },
isScrollable() { isScrollable() {
const $w = $(window); const $w = $(window);
return $(document).height() > $w.height() + $w.scrollTop() + ENDLESS_SCROLL_BOTTOM_PX; return $(document).height() > $w.height() + $w.scrollTop() + ENDLESS_SCROLL_BOTTOM_PX;
}, },
initLoadMore() { initLoadMore() {
$(document).unbind('scroll'); $(document).unbind('scroll');
$(document).endlessScroll({ $(document).endlessScroll({
bottomPixels: ENDLESS_SCROLL_BOTTOM_PX, bottomPixels: ENDLESS_SCROLL_BOTTOM_PX,
fireDelay: ENDLESS_SCROLL_FIRE_DELAY_MS, fireDelay: ENDLESS_SCROLL_FIRE_DELAY_MS,
fireOnce: true, fireOnce: true,
ceaseFire: () => this.disable === true, ceaseFire: () => this.disable === true,
callback: () => { callback: () => {
if (!this.loading.is(':visible')) { if (!this.loading.is(':visible')) {
this.loading.show(); this.loading.show();
this.getOld(); this.getOld();
} }
}, },
}); });
}, },
}; };
window.Pager = Pager;
})();
/* eslint-disable no-unused-expressions, no-prototype-builtins, no-new, no-shadow, max-len */ /* eslint-disable no-unused-expressions, no-prototype-builtins, no-new, no-shadow, max-len */
import 'vendor/jquery.endless-scroll'; import 'vendor/jquery.endless-scroll';
import '~/pager';
import Activities from '~/activities'; import Activities from '~/activities';
(() => { (() => {
......
import 'vendor/jquery.endless-scroll'; import 'vendor/jquery.endless-scroll';
import '~/pager';
import CommitsList from '~/commits'; import CommitsList from '~/commits';
describe('Commits List', () => { describe('Commits List', () => {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
/* global Notes */ /* global Notes */
import * as urlUtils from '~/lib/utils/url_utility'; import * as urlUtils from '~/lib/utils/url_utility';
import '~/merge_request_tabs'; import MergeRequestTabs from '~/merge_request_tabs';
import '~/commit/pipelines/pipelines_bundle'; import '~/commit/pipelines/pipelines_bundle';
import '~/breakpoints'; import '~/breakpoints';
import '~/lib/utils/common_utils'; import '~/lib/utils/common_utils';
...@@ -32,7 +32,7 @@ import 'vendor/jquery.scrollTo'; ...@@ -32,7 +32,7 @@ import 'vendor/jquery.scrollTo';
); );
beforeEach(function () { beforeEach(function () {
this.class = new gl.MergeRequestTabs({ stubLocation: stubLocation }); this.class = new MergeRequestTabs({ stubLocation: stubLocation });
setLocation(); setLocation();
this.spies = { this.spies = {
......
/* global fixture */ /* global fixture */
import * as utils from '~/lib/utils/url_utility'; import * as utils from '~/lib/utils/url_utility';
import '~/pager'; import Pager from '~/pager';
describe('pager', () => { describe('pager', () => {
const Pager = window.Pager;
it('is defined on window', () => {
expect(window.Pager).toBeDefined();
});
describe('init', () => { describe('init', () => {
const originalHref = window.location.href; const originalHref = window.location.href;
......
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