Skip to content

Commit ace3fe6

Browse files
committed
Extract Functions.checkIndexName() to a module
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent e7c7e6e commit ace3fe6

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

js/src/modules/functions.js

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import getImageTag from './functions/getImageTag.js';
1212
import handleRedirectAndReload from './functions/handleRedirectAndReload.js';
1313
import refreshMainContent from './functions/refreshMainContent.js';
1414
import checkIndexType from './indexes/checkIndexType.js';
15+
import checkIndexName from './indexes/checkIndexName.js';
1516

1617
/* global DatabaseStructure */ // js/database/structure.js
1718
/* global firstDayOfCalendar, themeImagePath */ // templates/javascript/variables.twig
@@ -2575,36 +2576,6 @@ Functions.onloadEnumSetEditor = function () {
25752576
});
25762577
};
25772578

2578-
/**
2579-
* Ensures indexes names are valid according to their type and, for a primary
2580-
* key, lock index name to 'PRIMARY'
2581-
* @param {string} formId Variable which parses the form name as
2582-
* the input
2583-
* @return {boolean} false if there is no index form, true else
2584-
*/
2585-
Functions.checkIndexName = function (formId) {
2586-
if ($('#' + formId).length === 0) {
2587-
return false;
2588-
}
2589-
2590-
// Gets the elements pointers
2591-
var $theIdxName = $('#input_index_name');
2592-
var $theIdxChoice = $('#select_index_choice');
2593-
2594-
// Index is a primary key
2595-
if ($theIdxChoice.find('option:selected').val() === 'PRIMARY') {
2596-
$theIdxName.val('PRIMARY');
2597-
$theIdxName.prop('disabled', true);
2598-
} else {
2599-
if ($theIdxName.val() === 'PRIMARY') {
2600-
$theIdxName.val('');
2601-
}
2602-
$theIdxName.prop('disabled', false);
2603-
}
2604-
2605-
return true;
2606-
};
2607-
26082579
/**
26092580
* Handler for adding more columns to an index in the editor
26102581
* @return {function}
@@ -2760,7 +2731,7 @@ Functions.indexRenameDialog = function (url, title, callbackSuccess, callbackFai
27602731

27612732
Functions.showIndexEditDialog = function ($outer) {
27622733
checkIndexType();
2763-
Functions.checkIndexName('index_frm');
2734+
checkIndexName('index_frm');
27642735
var $indexColumns = $('#index_columns');
27652736
$indexColumns.find('td').each(function () {
27662737
$(this).css('width', $(this).width() + 'px');

js/src/modules/indexes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ajaxRemoveMessage, ajaxShowMessage } from './ajax-message.js';
88
import getJsConfirmCommonParam from './functions/getJsConfirmCommonParam.js';
99
import refreshMainContent from './functions/refreshMainContent.js';
1010
import checkIndexType from './indexes/checkIndexType.js';
11+
import checkIndexName from './indexes/checkIndexName.js';
1112

1213
/**
1314
* @fileoverview function used for index manipulation pages
@@ -371,7 +372,7 @@ Indexes.showAddIndexDialog = function (sourceArray, arrayIndex, targetColumns, c
371372
$('#addIndexModal').modal('show');
372373
$('#addIndexModalLabel').first().text(window.Messages.strAddIndex);
373374
$('#addIndexModal').find('.modal-body').first().html(data.message);
374-
Functions.checkIndexName('index_frm');
375+
checkIndexName('index_frm');
375376
Functions.showHints($div);
376377
$('#index_columns').find('td').each(function () {
377378
$(this).css('width', $(this).width() + 'px');
@@ -563,7 +564,7 @@ Indexes.on = () => function () {
563564
$(document).on('change', '#select_index_choice', function (event) {
564565
event.preventDefault();
565566
checkIndexType();
566-
Functions.checkIndexName('index_frm');
567+
checkIndexName('index_frm');
567568
});
568569

569570
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import $ from 'jquery';
2+
3+
/**
4+
* Ensures indexes names are valid according to their type and, for a primary
5+
* key, lock index name to 'PRIMARY'
6+
* @param {string} formId Variable which parses the form name as
7+
* the input
8+
* @return {boolean} false if there is no index form, true else
9+
*/
10+
export default function checkIndexName (formId) {
11+
if ($('#' + formId).length === 0) {
12+
return false;
13+
}
14+
15+
// Gets the elements pointers
16+
var $theIdxName = $('#input_index_name');
17+
var $theIdxChoice = $('#select_index_choice');
18+
19+
// Index is a primary key
20+
if ($theIdxChoice.find('option:selected').val() === 'PRIMARY') {
21+
$theIdxName.val('PRIMARY');
22+
$theIdxName.prop('disabled', true);
23+
} else {
24+
if ($theIdxName.val() === 'PRIMARY') {
25+
$theIdxName.val('');
26+
}
27+
$theIdxName.prop('disabled', false);
28+
}
29+
30+
return true;
31+
}

0 commit comments

Comments
 (0)