Refactor into a monolithic js function

This commit is contained in:
Oliver Walters 2022-04-10 17:53:09 +10:00
parent 957494f483
commit 641785a02b
2 changed files with 77 additions and 60 deletions

View File

@ -419,70 +419,14 @@
// Load the "notes" tab // Load the "notes" tab
onPanelLoad('part-notes', function() { onPanelLoad('part-notes', function() {
var notes = null; setupNotesField(
'part-notes',
var readOnly = false;
inventreeGet(
'{% url "api-part-detail" part.pk %}', '{% url "api-part-detail" part.pk %}',
{},
{ {
async: false, editable: true,
success: function(response) { save_button: '#save-notes',
notes = response.notes;
},
} }
); );
var toolbar_icons = ['preview'];
if (!readOnly) {
// Heading icons
toolbar_icons.push('heading-1', 'heading-2', 'heading-3', '|');
// Font style
toolbar_icons.push('bold', 'italic', 'strikethrough', '|');
// Text formatting
toolbar_icons.push('unordered-list', 'ordered-list', 'code', 'quote', '|');
// Elements
toolbar_icons.push('table', 'link', 'image');
}
const mde = new EasyMDE({
element: document.getElementById('part-notes'),
initialValue: notes,
toolbar: toolbar_icons,
shortcuts: [],
});
mde.togglePreview();
if (readOnly) {
mde.codemirror.setOption('readOnly', true);
$('#editor-toolbar').hide();
}
$('#save-notes').click(function() {
inventreePut(
'{% url "api-part-detail" part.pk %}',
{
notes: mde.value(),
},
{
method: 'PATCH',
success: function(response) {
showMessage('{% trans "Notes updated" %}', {style: 'success'});
},
error: function(xhr) {
showApiError(xhr, url);
}
}
)
});
}); });
// Load the "scheduling" tab // Load the "scheduling" tab

View File

@ -10,6 +10,7 @@
makeProgressBar, makeProgressBar,
renderLink, renderLink,
select2Thumbnail, select2Thumbnail,
setupNotesField,
thumbnailImage thumbnailImage
yesNoLabel, yesNoLabel,
*/ */
@ -221,3 +222,75 @@ function renderLink(text, url, options={}) {
return `<a href="${url}">${text}</a>`; return `<a href="${url}">${text}</a>`;
} }
function setupNotesField(element, url, options={}) {
var editable = options.editable || false;
// Read initial notes value from the URL
var initial = null;
inventreeGet(url, {}, {
async: false,
success: function(response) {
initial = response[options.notes_field || 'notes'];
},
});
var toolbar_icons = [
'preview',
];
if (editable) {
// Heading icons
toolbar_icons.push('heading-1', 'heading-2', 'heading-3', '|');
// Font style
toolbar_icons.push('bold', 'italic', 'strikethrough', '|');
// Text formatting
toolbar_icons.push('unordered-list', 'ordered-list', 'code', 'quote', '|');
// Elements
toolbar_icons.push('table', 'link', 'image');
}
// Markdown syntax guide
toolbar_icons.push('|', 'guide');
const mde = new EasyMDE({
element: document.getElementById(element),
initialValue: initial,
toolbar: toolbar_icons,
shortcuts: [],
});
mde.togglePreview();
if (!editable) {
mde.codemirror.setOption('readOnly', true);
$('#editor-toolbar').hide();
}
// Add callback for "save" button
if (options.save_button) {
$(options.save_button).click(function() {
var data = {};
data[options.notes_field || 'notes'] = mde.value();
inventreePut(url, data, {
method: 'PATCH',
success: function(response) {
showMessage('{% trans "Notes updated" %}', {style: 'success'});
},
error: function(xhr) {
showApiError(xhr, url);
}
});
});
}
}