Skip to content

Commit

Permalink
zmi: monotonized multiple insert_row actions for langs, lang_dict, me…
Browse files Browse the repository at this point in the history
…tas, metaobjs
  • Loading branch information
drfho committed Sep 6, 2024
1 parent 6976d75 commit 62508a1
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 59 deletions.
3 changes: 2 additions & 1 deletion Products/zms/ZMSMetaobjManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,8 @@ def manage_changeProperties(self, lang, btn='', key='all', REQUEST=None, RESPONS
if (len(attr_id) > 0 and len(newName) > 0 and len(newType) > 0) or newType in self.getMetadictAttrs():
message += self.setMetaobjAttr( id, None, attr_id, newName, newMandatory, newMultilang, newRepetitive, newType, newKeys, newCustom, newDefault)
message += self.getZMILangStr('MSG_INSERTED')%attr_id
# Lang-Dict.
# Insert (multiple) new language keys at once.
# Ref: _multilangmanager.py#L647
for key in REQUEST.form.keys():
if key.startswith('_lang_dict_key_'):
i = int(key[len('_lang_dict_key_'):])
Expand Down
58 changes: 34 additions & 24 deletions Products/zms/_multilangmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,23 +506,28 @@ def manage_changeLanguages(self, lang, btn, REQUEST, RESPONSE):
# Change.
# -------
elif btn == 'BTN_SAVE':
newId = REQUEST.get('language_id','').strip()
# Change available languages.
for id in self.getLangIds():
if id != newId:
newLabel = REQUEST.get('%s_label'%id).strip()
newParent = REQUEST.get('%s_parent'%id).strip()
newManage = REQUEST.get('%s_manage'%id).strip()
self.setLanguage(id, newLabel, newParent, newManage)
# Insert
if len(newId) > 0:
newLabel = REQUEST.get('language_label').strip()
if len(self.getLangIds()) == 0:
newParent = ''
else:
newParent = REQUEST.get('language_parent').strip()
newManage = REQUEST.get('language_manage').strip()
self.setLanguage(newId, newLabel, newParent, newManage)

newLabel = REQUEST.get('%s_label'%id).strip()
newParent = REQUEST.get('%s_parent'%id).strip()
newManage = REQUEST.get('%s_manage'%id).strip()
self.setLanguage(id, newLabel, newParent, newManage)
# Insert new languages
# Ref: _multilangmanager.py#L647
for key in REQUEST.form.keys():
if key.startswith('_lang_id_'):
i = int(key[len('_lang_id_'):])
if REQUEST[key]:
newId = REQUEST[key].strip()
if newId not in self.getLangIds():
newLabel = REQUEST.get('_lang_label_%i'%i).strip()
if len(self.getLangIds()) == 0:
newParent = ''
else:
newParent = REQUEST.get('_lang_parent_%i'%i).strip()
newManage = REQUEST.get('_lang_manage_%i'%i).strip()
self.setLanguage(newId, newLabel, newParent, newManage)

# Return with message.
message = standard.url_quote(self.getZMILangStr('MSG_CHANGED'))
return RESPONSE.redirect('manage_customizeLanguagesForm?lang=%s&manage_tabs_message=%s'%(lang, message))
Expand Down Expand Up @@ -639,14 +644,19 @@ def manage_changeLangDictProperties(self, lang, btn, REQUEST, RESPONSE=None):
enabled = lang_id not in d[key].get('acquired', [])
if enabled:
lang_dict[key][lang_id] = REQUEST['%s_value_%s'%(key, lang_id)].strip()
# Insert
key = REQUEST['_key'].strip()
if len(key) > 0:
lang_dict = self.get_lang_dict()
lang_dict[key] = {}
for lang_id in self.getLangIds():
lang_dict[key][lang_id] = REQUEST['_value_%s'%lang_id].strip()
self.set_lang_dict(lang_dict)
# Insert (multiple) new language keys at once.
# Ref: ZMSMetaobjManager.py#L1294
for key in REQUEST.form.keys():
if key.startswith('_lang_dict_key_'):
i = int(key[len('_lang_dict_key_'):])
if REQUEST[key]:
k = REQUEST[key].strip()
lang_dict[k] = {}
for key2 in REQUEST.form.keys():
if key2.startswith('_lang_dict_value_%i_'%i):
lang_id = key2[len('_lang_dict_value_%i_'%i):]
lang_dict[k][lang_id] = REQUEST[key2].strip()
self.set_lang_dict(lang_dict)

# Export.
# -------
Expand Down
11 changes: 5 additions & 6 deletions Products/zms/plugins/www/zmi.core.css
Original file line number Diff line number Diff line change
Expand Up @@ -1076,8 +1076,12 @@ div.zmi-manage-main-change {
border-bottom-left-radius: .25rem;
border-top-left-radius: .25rem;
padding-right: .25em;
padding-left: 0;
}
.zmi.metas.metaobj form table.table td.meta-sort select.zmi-sort {
width: 3em;
}
.zmi form.manage_metas table.table td.meta-sort select.zmi-sort {
.zmi.metas:not(.metaobj) form table.table td.meta-sort select.zmi-sort {
width: 3.9em;
}
.zmi table.table .input-group {
Expand Down Expand Up @@ -1693,11 +1697,6 @@ table.table-sm tr.row_metadata td,
table.table-sm tr.row_insert td {
background-color: #e9ecef !important
}
table.table-sm tr.row_metadata td .btn-add,
table.table-sm tr.row_insert td .btn-add {
padding-right: 0.4rem;
color: #888;
}
.zmi table.table-sm td.meta-id input {
background:rgba(53, 79, 103, 0.10);
}
Expand Down
116 changes: 106 additions & 10 deletions Products/zms/zpt/ZMS/manage_customizelanguagesform.zpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function zmiFormSubmit(sender,d) {
tal:content="python:here.getZMILangStr('TAB_LANGUAGES')">
Languages
</legend>
<table class="table table-sm table-striped table-bordered">
<table id="langs" class="table table-sm table-striped table-bordered">
<thead>
<tr>
<th class="meta-sort"></th>
Expand Down Expand Up @@ -101,16 +101,16 @@ function zmiFormSubmit(sender,d) {
</tal:block>
</tal:block>
<tr class="row_insert">
<td class="meta-sort text-center"><span class="btn btn-add mr-1"><i class="fas fa-plus"></i></span></td>
<td class="language_id"><input class="form-control form-control-sm" type="text" name="language_id" /></td>
<td class="language_label"><input class="form-control form-control-sm" type="text" name="language_label" /></td>
<td class="meta-sort text-center"><span class="btn btn-secondary btn-sm btn-add mr-1 w-100"><i class="fas fa-plus"></i></span></td>
<td class="language_id"><input class="form-control form-control-sm" placeholder="ISO-639/2" minlength="2" maxlength="3" required="true" type="text" name="_lang_id_0" /></td>
<td class="language_label"><input class="form-control form-control-sm" required="true"type="text" name="_lang_label_0" /></td>
<td class="language_parent">
<select class="form-control form-control-sm" name="language_parent">
<select class="form-control form-control-sm" name="_lang_parent_0" required="true">
<option tal:repeat="lang langIds" tal:attributes="value lang" tal:content="python:here.getLanguageLabel(lang)">lang</option>
</select>
</td>
<td>
<select class="form-control form-control-sm" name="language_manage">
<select class="form-control form-control-sm" name="_lang_manage_0" required="true">
<option tal:repeat="manage_lang python:here.get_manage_langs()" tal:attributes="value manage_lang" tal:content="python:here.getLangStr('LANG',manage_lang)">manage_lang</option>
</select>
</td>
Expand All @@ -130,7 +130,8 @@ function zmiFormSubmit(sender,d) {
tal:content="python:here.getZMILangStr('ATTR_DICTIONARY')">
Dictionary
</legend>
<table class="table table-sm table-striped table-bordered table-hover table-responsive"
<table id="lang_dict"
class="table table-sm table-striped table-bordered table-hover table-responsive"
tal:attributes="class python:'table table-sm table-striped table-bordered table-hover %s'%(len(langIds)>6 and 'table-responsive' or '')">
<thead>
<tr>
Expand Down Expand Up @@ -159,9 +160,9 @@ function zmiFormSubmit(sender,d) {
<td tal:repeat="langId langIds"><div class="single-line"><textarea class="form-control form-control-sm" tal:attributes="name python:'%s_value_%s'%(langDict['key'],langId); disabled python:['','disabled'][int(langId in langDict.get('acquired',[]))]" tal:content="python:langDict.get(langId,'')"></textarea></div></td>
</tr>
<tr class="row_insert">
<td class="meta-sort text-center"><span class="btn btn-add mr-1"><i class="fas fa-plus"></i></span></td>
<td><input class="form-control form-control-sm" type="text" name="_key" /></td>
<td tal:repeat="langId langIds"><div class="single-line"><textarea class="form-control form-control-sm" tal:attributes="name python:'_value_%s'%(langId)"></textarea></div></td>
<td class="meta-sort text-center"><span class="btn btn-secondary btn-sm btn-add mr-1 w-100"><i class="fas fa-plus"></i></span></td>
<td><input class="form-control form-control-sm" type="text" name="_lang_dict_key_0" /></td>
<td tal:repeat="langId langIds"><div class="single-line"><textarea class="form-control form-control-sm" tal:attributes="name python:'_lang_dict_value_0_%s'%(langId)"></textarea></div></td>
</tr>
</tbody>
</table>
Expand Down Expand Up @@ -203,6 +204,101 @@ function zmiFormSubmit(sender,d) {
</tal:block>

</div><!-- #zmi-tab -->

<style>
/* <!-- */
tr.row_insert > td > .btn-add {
width: 100%;
color:#333 !important;
padding: 4px 8px;
}
tr.row_insert > td > .btn-add:hover {
color:white !important;
}
tr.row_insert > td:not(.meta-sort) > * {
opacity:.35
}
/* --> */
</style>
<script>
// <!--
$(function(){
// New field set: initially disable inputs
$('input, textarea, select','tr.row_insert').attr('disabled',true);

// ++++++++++++
// Add rows to tables #meta_properties on button click
// ++++++++++++
let new_row_counter = 0;

// Get number of language terms
let lang_dict = ZMI.prototype.getCachedValue('get_lang_dict');
let lang_count = 0;
if ( lang_dict!='undefined' ) {
if ( typeof lang_dict == 'object' ) {
lang_count = Object.keys(ZMI.prototype.getCachedValue('get_lang_dict')).length
} else {
lang_count = Object.keys(JSON.parse(ZMI.prototype.getCachedValue('get_lang_dict'))).length
}
};

// Add click event function to add-buttons
$(".row_insert .btn-add").click(function(){
new_row_counter++;
// New field set: clone with enabled inputs
$('input, textarea, select','tr.row_insert').attr('disabled',false);

// Where to insert the new row
let $where_insert = $(this).closest('tr');

// Set variables
let table_id = $(this).closest('table').attr('id'); // meta_properties or meta_languages
let new_row_name = `new_row_${table_id}_${new_row_counter}`;
let old_id_html = '';
let sort_options_html = '';
let sort_options_len = 0;
let new_btn_html = `
<span class="btn btn-secondary btn-sm mr-1 w-100" style="color:#999"
onclick="javascript:$(this).closest('tr').remove()">
<i class="fas fa-times"></i>
</span>
`;

// Clone(true) to get a deep copy including select options
let $new_row = $where_insert.clone(true);

// Process table cells of the clone like "old" row
$new_row.find('td').each(function() {
$(this).find('input,select,textarea').each(function() {
let tagname = $(this).prop('tagName');
let defname = $(this).attr('name');
let deftype = $(this).attr('type');
let counter = table_id == 'lang_dict' ? lang_count + new_row_counter : new_row_counter;
let newname = $(this).attr('name').replace('_0', '_' + counter);
let newval = 'new'+counter;
$(this).attr('name',newname);
$(this).val(newval);
});
});

// Process td:first-child of the clone
$new_row.find('td.meta-sort').html(new_btn_html);
$new_row.removeAttr('class').attr('id',new_row_name);

// Insert the new row
$new_row.insertBefore($where_insert);
// Reset the clone template
$where_insert.find('input:not([type="checkbox"]),select,textarea').each(function() {
$(this).val(undefined);
});
// New field set: reset to disabled inputs
$('input, textarea, select','tr.row_insert').attr('disabled',true);
});

});
// -->
</script>

<footer tal:replace="structure python:here.zmi_body_footer(here,request)">zmi_body_footer</footer>
</body>
</html>
4 changes: 2 additions & 2 deletions Products/zms/zpt/ZMSFilterManager/manage_main.zpt
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
<!-- INSERT NEW ROW -->
<tr class="row_insert">
<td class="meta-sort text-right">
<span class="btn btn-add mr-1">
<span class="btn btn-secondary btn-add mr-1 w-100">
<i class="fas fa-plus"></i>
</span>
</td>
Expand Down Expand Up @@ -568,7 +568,7 @@
let table_id = $(this).closest('table').attr('id'); // meta_properties or meta_languages
let new_row_name = `new_row_${table_id}_${new_row_counter}`;
let new_btn_html = `
<span class="btn btn-default btn-sm mr-1" style="color:#999"
<span class="btn btn-secondary mr-1 w-100" style="color:#999"
onclick="javascript:$(this).closest('tr').remove()">
<i class="fas fa-times"></i>
</span>
Expand Down
Loading

0 comments on commit 62508a1

Please sign in to comment.