Skip to content

Commit db30a4b

Browse files
committed
Extract functions from config.js file
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 90d0e85 commit db30a4b

1 file changed

Lines changed: 105 additions & 117 deletions

File tree

‎js/src/config.js‎

Lines changed: 105 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,6 @@ function isStorageSupported (type, warn = false) {
3131
return false;
3232
}
3333

34-
/**
35-
* Unbind all event handlers before tearing down a page
36-
*/
37-
window.AJAX.registerTeardown('config.js', function () {
38-
$('.optbox input[id], .optbox select[id], .optbox textarea[id]').off('change').off('keyup');
39-
$('.optbox input[type=button][name=submit_reset]').off('click');
40-
$('div.tab-content').off();
41-
$('#import_local_storage, #export_local_storage').off('click');
42-
$('form.prefs-form').off('change').off('submit');
43-
$(document).off('click', 'div.click-hide-message');
44-
$('#prefs_autoload').find('a').off('click');
45-
});
46-
47-
window.AJAX.registerOnload('config.js', function () {
48-
var $topmenuUpt = $('#user_prefs_tabs');
49-
$topmenuUpt.find('a.active').attr('rel', 'samepage');
50-
$topmenuUpt.find('a:not(.active)').attr('rel', 'newpage');
51-
});
52-
5334
// default values for fields
5435
var defaultValues = {};
5536

@@ -592,10 +573,6 @@ function setupValidation () {
592573
}
593574
}
594575

595-
window.AJAX.registerOnload('config.js', function () {
596-
setupValidation();
597-
});
598-
599576
//
600577
// END: Form validation and field operations
601578
// ------------------------------------------------------------------
@@ -610,28 +587,6 @@ function adjustPrefsNotification () {
610587
}
611588
}
612589

613-
window.AJAX.registerOnload('config.js', function () {
614-
adjustPrefsNotification();
615-
});
616-
617-
// ------------------------------------------------------------------
618-
// Form reset buttons
619-
//
620-
621-
window.AJAX.registerOnload('config.js', function () {
622-
$('.optbox input[type=button][name=submit_reset]').on('click', function () {
623-
var fields = $(this).closest('fieldset').find('input, select, textarea');
624-
for (var i = 0, imax = fields.length; i < imax; i++) {
625-
setFieldValue(fields[i], getFieldType(fields[i]), defaultValues[fields[i].id]);
626-
}
627-
setDisplayError();
628-
});
629-
});
630-
631-
//
632-
// END: Form reset buttons
633-
// ------------------------------------------------------------------
634-
635590
// ------------------------------------------------------------------
636591
// "Restore default" and "set value" buttons
637592
//
@@ -678,82 +633,10 @@ function setupRestoreField () {
678633
.css({ display: 'inline-block', opacity: 0.25 });
679634
}
680635

681-
window.AJAX.registerOnload('config.js', function () {
682-
setupRestoreField();
683-
});
684-
685636
//
686637
// END: "Restore default" and "set value" buttons
687638
// ------------------------------------------------------------------
688639

689-
// ------------------------------------------------------------------
690-
// User preferences import/export
691-
//
692-
693-
window.AJAX.registerOnload('config.js', function () {
694-
offerPrefsAutoimport();
695-
var $radios = $('#import_local_storage, #export_local_storage');
696-
if (!$radios.length) {
697-
return;
698-
}
699-
700-
// enable JavaScript dependent fields
701-
$radios
702-
.prop('disabled', false)
703-
.add('#export_text_file, #import_text_file')
704-
.on('click', function () {
705-
var enableId = $(this).attr('id');
706-
var disableId;
707-
if (enableId.match(/local_storage$/)) {
708-
disableId = enableId.replace(/local_storage$/, 'text_file');
709-
} else {
710-
disableId = enableId.replace(/text_file$/, 'local_storage');
711-
}
712-
$('#opts_' + disableId).addClass('disabled').find('input').prop('disabled', true);
713-
$('#opts_' + enableId).removeClass('disabled').find('input').prop('disabled', false);
714-
});
715-
716-
// detect localStorage state
717-
var lsSupported = isStorageSupported('localStorage', true);
718-
var lsExists = lsSupported ? (window.localStorage.config || false) : false;
719-
$('div.localStorage-' + (lsSupported ? 'un' : '') + 'supported').hide();
720-
$('div.localStorage-' + (lsExists ? 'empty' : 'exists')).hide();
721-
if (lsExists) {
722-
updatePrefsDate();
723-
}
724-
$('form.prefs-form').on('change', function () {
725-
var $form = $(this);
726-
var disabled = false;
727-
if (!lsSupported) {
728-
disabled = $form.find('input[type=radio][value$=local_storage]').prop('checked');
729-
} else if (!lsExists && $form.attr('name') === 'prefs_import' &&
730-
$('#import_local_storage')[0].checked
731-
) {
732-
disabled = true;
733-
}
734-
$form.find('input[type=submit]').prop('disabled', disabled);
735-
}).on('submit', function (e) {
736-
var $form = $(this);
737-
if ($form.attr('name') === 'prefs_export' && $('#export_local_storage')[0].checked) {
738-
e.preventDefault();
739-
// use AJAX to read JSON settings and save them
740-
savePrefsToLocalStorage($form);
741-
} else if ($form.attr('name') === 'prefs_import' && $('#import_local_storage')[0].checked) {
742-
// set 'json' input and submit form
743-
$form.find('input[name=json]').val(window.localStorage.config);
744-
}
745-
});
746-
747-
$(document).on('click', 'div.click-hide-message', function () {
748-
$(this)
749-
.hide()
750-
.parent('.card-body')
751-
.css('height', '')
752-
.next('form')
753-
.show();
754-
});
755-
});
756-
757640
/**
758641
* Saves user preferences to localStorage
759642
*
@@ -839,3 +722,108 @@ function offerPrefsAutoimport () {
839722
});
840723
$cnt.show();
841724
}
725+
726+
window.Config = {
727+
/**
728+
* @return {function}
729+
*/
730+
off: function () {
731+
return function () {
732+
$('.optbox input[id], .optbox select[id], .optbox textarea[id]').off('change').off('keyup');
733+
$('.optbox input[type=button][name=submit_reset]').off('click');
734+
$('div.tab-content').off();
735+
$('#import_local_storage, #export_local_storage').off('click');
736+
$('form.prefs-form').off('change').off('submit');
737+
$(document).off('click', 'div.click-hide-message');
738+
$('#prefs_autoload').find('a').off('click');
739+
};
740+
},
741+
/**
742+
* @return {function}
743+
*/
744+
on: function () {
745+
return function () {
746+
var $topmenuUpt = $('#user_prefs_tabs');
747+
$topmenuUpt.find('a.active').attr('rel', 'samepage');
748+
$topmenuUpt.find('a:not(.active)').attr('rel', 'newpage');
749+
750+
setupValidation();
751+
adjustPrefsNotification();
752+
753+
$('.optbox input[type=button][name=submit_reset]').on('click', function () {
754+
var fields = $(this).closest('fieldset').find('input, select, textarea');
755+
for (var i = 0, imax = fields.length; i < imax; i++) {
756+
setFieldValue(fields[i], getFieldType(fields[i]), defaultValues[fields[i].id]);
757+
}
758+
setDisplayError();
759+
});
760+
761+
setupRestoreField();
762+
763+
offerPrefsAutoimport();
764+
var $radios = $('#import_local_storage, #export_local_storage');
765+
if (!$radios.length) {
766+
return;
767+
}
768+
769+
// enable JavaScript dependent fields
770+
$radios
771+
.prop('disabled', false)
772+
.add('#export_text_file, #import_text_file')
773+
.on('click', function () {
774+
var enableId = $(this).attr('id');
775+
var disableId;
776+
if (enableId.match(/local_storage$/)) {
777+
disableId = enableId.replace(/local_storage$/, 'text_file');
778+
} else {
779+
disableId = enableId.replace(/text_file$/, 'local_storage');
780+
}
781+
$('#opts_' + disableId).addClass('disabled').find('input').prop('disabled', true);
782+
$('#opts_' + enableId).removeClass('disabled').find('input').prop('disabled', false);
783+
});
784+
785+
// detect localStorage state
786+
var lsSupported = isStorageSupported('localStorage', true);
787+
var lsExists = lsSupported ? (window.localStorage.config || false) : false;
788+
$('div.localStorage-' + (lsSupported ? 'un' : '') + 'supported').hide();
789+
$('div.localStorage-' + (lsExists ? 'empty' : 'exists')).hide();
790+
if (lsExists) {
791+
updatePrefsDate();
792+
}
793+
$('form.prefs-form').on('change', function () {
794+
var $form = $(this);
795+
var disabled = false;
796+
if (!lsSupported) {
797+
disabled = $form.find('input[type=radio][value$=local_storage]').prop('checked');
798+
} else if (!lsExists && $form.attr('name') === 'prefs_import' &&
799+
$('#import_local_storage')[0].checked
800+
) {
801+
disabled = true;
802+
}
803+
$form.find('input[type=submit]').prop('disabled', disabled);
804+
}).on('submit', function (e) {
805+
var $form = $(this);
806+
if ($form.attr('name') === 'prefs_export' && $('#export_local_storage')[0].checked) {
807+
e.preventDefault();
808+
// use AJAX to read JSON settings and save them
809+
savePrefsToLocalStorage($form);
810+
} else if ($form.attr('name') === 'prefs_import' && $('#import_local_storage')[0].checked) {
811+
// set 'json' input and submit form
812+
$form.find('input[name=json]').val(window.localStorage.config);
813+
}
814+
});
815+
816+
$(document).on('click', 'div.click-hide-message', function () {
817+
$(this)
818+
.hide()
819+
.parent('.card-body')
820+
.css('height', '')
821+
.next('form')
822+
.show();
823+
});
824+
};
825+
}
826+
};
827+
828+
window.AJAX.registerTeardown('config.js', window.Config.off());
829+
window.AJAX.registerOnload('config.js', window.Config.on());

0 commit comments

Comments
 (0)