Commit 8f29c332 authored by Alfredo Sumaran's avatar Alfredo Sumaran

Wrap classes with IIFE and define it inside gl namespace

parent 45454c3c
...@@ -173,8 +173,8 @@ ...@@ -173,8 +173,8 @@
new Search(); new Search();
break; break;
case 'projects:protected_branches:index': case 'projects:protected_branches:index':
new ProtectedBranchCreate(); new gl.ProtectedBranchCreate();
new ProtectedBranchEditList(); new gl.ProtectedBranchEditList();
break; break;
} }
switch (path.first()) { switch (path.first()) {
......
class ProtectedBranchAccessDropdown { (global => {
constructor(options) { global.gl = global.gl || {};
const { $dropdown, data, onSelect } = options;
$dropdown.glDropdown({ gl.ProtectedBranchAccessDropdown = class {
data: data, constructor(options) {
selectable: true, const { $dropdown, data, onSelect } = options;
fieldName: $dropdown.data('field-name'),
toggleLabel(item) { $dropdown.glDropdown({
return item.text; data: data,
}, selectable: true,
clicked(item, $el, e) { fieldName: $dropdown.data('field-name'),
e.preventDefault(); toggleLabel(item) {
onSelect(); return item.text;
} },
}); clicked(item, $el, e) {
e.preventDefault();
onSelect();
}
});
}
} }
}
})(window);
class ProtectedBranchCreate { (global => {
constructor() { global.gl = global.gl || {};
this.$wrap = this.$form = $('#new_protected_branch');
this.buildDropdowns();
}
buildDropdowns() { gl.ProtectedBranchCreate = class {
// Allowed to Merge dropdown constructor() {
const $allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge'); this.$wrap = this.$form = $('#new_protected_branch');
const $allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push'); this.buildDropdowns();
}
new ProtectedBranchAccessDropdown({
$dropdown: $allowedToMergeDropdown,
data: gon.merge_access_levels,
onSelect: this.onSelect.bind(this)
});
// Select default
$allowedToMergeDropdown.data('glDropdown').selectRowAtIndex(0);
// Allowed to Push dropdown
new ProtectedBranchAccessDropdown({
$dropdown: $allowedToPushDropdown,
data: gon.push_access_levels,
onSelect: this.onSelect.bind(this)
});
// Select default
$allowedToPushDropdown.data('glDropdown').selectRowAtIndex(0);
// Protected branch dropdown
new ProtectedBranchDropdown({
$dropdown: this.$wrap.find('.js-protected-branch-select'),
onSelect: this.onSelect.bind(this)
});
}
// This will run after clicked callback buildDropdowns() {
onSelect() { // Allowed to Merge dropdown
// Enable submit button const $allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge');
const $branchInput = this.$wrap.find('input[name="protected_branch[name]"]'); const $allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push');
const $allowedToMergeInput = this.$wrap.find('input[name="protected_branch[merge_access_level_attributes][access_level]"]');
const $allowedToPushInput = this.$wrap.find('input[name="protected_branch[push_access_level_attributes][access_level]"]'); new gl.ProtectedBranchAccessDropdown({
$dropdown: $allowedToMergeDropdown,
data: gon.merge_access_levels,
onSelect: this.onSelect.bind(this)
});
// Select default
$allowedToMergeDropdown.data('glDropdown').selectRowAtIndex(0);
// Allowed to Push dropdown
new gl.ProtectedBranchAccessDropdown({
$dropdown: $allowedToPushDropdown,
data: gon.push_access_levels,
onSelect: this.onSelect.bind(this)
});
// Select default
$allowedToPushDropdown.data('glDropdown').selectRowAtIndex(0);
// Protected branch dropdown
new ProtectedBranchDropdown({
$dropdown: this.$wrap.find('.js-protected-branch-select'),
onSelect: this.onSelect.bind(this)
});
}
// This will run after clicked callback
onSelect() {
// Enable submit button
const $branchInput = this.$wrap.find('input[name="protected_branch[name]"]');
const $allowedToMergeInput = this.$wrap.find('input[name="protected_branch[merge_access_level_attributes][access_level]"]');
const $allowedToPushInput = this.$wrap.find('input[name="protected_branch[push_access_level_attributes][access_level]"]');
if ($branchInput.val() && $allowedToMergeInput.val() && $allowedToPushInput.val()){ if ($branchInput.val() && $allowedToMergeInput.val() && $allowedToPushInput.val()){
this.$form.find('[type="submit"]').removeAttr('disabled'); this.$form.find('[type="submit"]').removeAttr('disabled');
}
} }
} }
}
})(window);
class ProtectedBranchEdit { (global => {
constructor(options) { global.gl = global.gl || {};
this.$wrap = options.$wrap;
this.$allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge');
this.$allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push');
this.buildDropdowns(); gl.ProtectedBranchEdit = class {
} constructor(options) {
this.$wrap = options.$wrap;
this.$allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge');
this.$allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push');
buildDropdowns() { this.buildDropdowns();
}
// Allowed to merge dropdown
new ProtectedBranchAccessDropdown({ buildDropdowns() {
$dropdown: this.$allowedToMergeDropdown,
data: gon.merge_access_levels, // Allowed to merge dropdown
onSelect: this.onSelect.bind(this) new gl.ProtectedBranchAccessDropdown({
}); $dropdown: this.$allowedToMergeDropdown,
data: gon.merge_access_levels,
// Allowed to push dropdown onSelect: this.onSelect.bind(this)
new ProtectedBranchAccessDropdown({ });
$dropdown: this.$allowedToPushDropdown,
data: gon.push_access_levels,
onSelect: this.onSelect.bind(this)
});
}
onSelect() { // Allowed to push dropdown
const $allowedToMergeInput = $(`input[name="${this.$allowedToMergeDropdown.data('fieldName')}"]`); new gl.ProtectedBranchAccessDropdown({
const $allowedToPushInput = $(`input[name="${this.$allowedToPushDropdown.data('fieldName')}"]`); $dropdown: this.$allowedToPushDropdown,
data: gon.push_access_levels,
$.ajax({ onSelect: this.onSelect.bind(this)
type: 'POST', });
url: this.$wrap.data('url'), }
dataType: 'json',
data: { onSelect() {
_method: 'PATCH', const $allowedToMergeInput = $(`input[name="${this.$allowedToMergeDropdown.data('fieldName')}"]`);
id: this.$wrap.data('banchId'), const $allowedToPushInput = $(`input[name="${this.$allowedToPushDropdown.data('fieldName')}"]`);
protected_branch: {
merge_access_level_attributes: { $.ajax({
access_level: $allowedToMergeInput.val() type: 'POST',
}, url: this.$wrap.data('url'),
push_access_level_attributes: { dataType: 'json',
access_level: $allowedToPushInput.val() data: {
_method: 'PATCH',
id: this.$wrap.data('banchId'),
protected_branch: {
merge_access_level_attributes: {
access_level: $allowedToMergeInput.val()
},
push_access_level_attributes: {
access_level: $allowedToPushInput.val()
}
} }
},
success: () => {
this.$wrap.effect('highlight');
},
error() {
$.scrollTo(0);
new Flash('Failed to update branch!');
} }
}, });
success: () => { }
this.$wrap.effect('highlight');
},
error() {
$.scrollTo(0);
new Flash('Failed to update branch!');
}
});
} }
}
})(window);
class ProtectedBranchEditList { (global => {
constructor() { global.gl = global.gl || {};
this.$wrap = $('.protected-branches-list');
// Build edit forms gl.ProtectedBranchEditList = class {
this.$wrap.find('.js-protected-branch-edit-form').each((i, el) => { constructor() {
new ProtectedBranchEdit({ this.$wrap = $('.protected-branches-list');
$wrap: $(el)
// Build edit forms
this.$wrap.find('.js-protected-branch-edit-form').each((i, el) => {
new gl.ProtectedBranchEdit({
$wrap: $(el)
});
}); });
}); }
} }
}
})(window);
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