Skip to content

Commit

Permalink
[js] Improved patch #23
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Dec 20, 2017
1 parent 715b1b6 commit b5506ff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
2 changes: 1 addition & 1 deletion django_x509/base/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Media:

class AbstractCertForm(forms.ModelForm):
OPERATION_CHOICES = (
('-----', '-----'),
('', '----- {0} -----'.format(_('Please select an option'))),
('new', _('Create new')),
('import', _('Import Existing'))
)
Expand Down
68 changes: 31 additions & 37 deletions django_x509/static/django-x509/js/switcher.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
(function($){
django.jQuery(function ($) {
'use strict';
var operationType = $(".field-operation_type select");
// enable switcher only in add forms
if ($('form .deletelink-box').length > 0) {
operationType.hide();
return;
}
// function for operation_type switcher
var filter_div = function(operation_type){
// define fields for each operation_type
var import_fields = $('.form-row:not(.field-certificate, .field-operation_type, .field-private_key, .field-name, .field-ca)'),
new_fields = $('.form-row:not(.field-certificate, .field-private_key)'),
default_fields = $('.form-row:not(.field-operation_type)'),
all_fields = $('.form-row');
if(operation_type === '-----'){
all_fields.show()
default_fields.hide()
}
if(operation_type === 'Create new'){
all_fields.hide()
new_fields.show()
var showFields = function () {
// define fields for each operation
var importFields = $('.form-row:not(.field-certificate, .field-operation_type, ' +
'.field-private_key, .field-name, .field-ca)'),
newFields = $('.form-row:not(.field-certificate, .field-private_key)'),
defaultFields = $('.form-row:not(.field-operation_type)'),
allFields = $('.form-row'),
value = operationType.val();
if (!value) {
allFields.show();
defaultFields.hide();
}
if(operation_type === 'Import Existing'){
all_fields.show()
import_fields.hide()
if (value === 'new') {
allFields.hide();
newFields.show();
}
}
$(document).ready(function(){
// dont hide if edit cert
var path = window.location.pathname
if(path.indexOf("change") === -1){
// if field is set
if ($(".field operation_type" != "-----")){
filter_div($(".field-operation_type").find(":selected").text())
} else {
// filter default if field is not set
filter_div('-----')
}
$(".field-operation_type").on('change', function(){
switcher = $(".field-operation_type").find(":selected").text()
filter_div(switcher)
})
} else {
$(".field-operation_type").hide()
if (value === 'import') {
allFields.show();
importFields.hide();
}
})
})(django.jQuery);
};
showFields();
operationType.on('change', function (e) {
showFields();
});
});

0 comments on commit b5506ff

Please sign in to comment.