Skip to content

Commit

Permalink
added javascript file autoupdates.js to automate references and made…
Browse files Browse the repository at this point in the history
… changes to include file field
  • Loading branch information
MelosaRao committed May 26, 2024
1 parent 498ad34 commit 3ce76ca
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion materials/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ def __init__(self, model=None, field=None, *args, **kwargs):


class AddReferenceForm(forms.Form):
inputRISFile = forms.FileField(
required=False,
widget=forms.ClearableFileInput(attrs={'multiple': True, 'onchange':'handleFileSelect();' }),
help_text='Upload RIS file reference'
)
title = forms.CharField(
widget=forms.TextInput(attrs={'class': 'form-control'}),
widget=forms.TextInput(attrs={'class': 'form-control', 'id':'title'}),
max_length=1000,
help_text='Article title')
journal = forms.CharField(
Expand Down
29 changes: 29 additions & 0 deletions materials/static/materials/javascript/autoupdates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function handleFileSelect() {
const fileInput = document.querySelector('[name="inputRISFile"]');
const textInput = document.querySelector('[name="title"]');

if (fileInput.files.length > 0) {
const file = fileInput.files[0];
const reader = new FileReader();

reader.onload = function (event) {
const fileContent = event.target.result;
const lines = fileContent.split('\n'); // Split content into lines

// Search for lines starting with "T1 -" or "TI -"
const relevantLines = lines.filter(line =>
line.trim().startsWith("TI -") || line.trim().startsWith("T1 -")
);

// Extract the text after "T1 -" or "T1 -"
const extractedText = relevantLines.map(line =>
line.trim().replace(/^(T1|TI) -/, "")
).join("\n");

// Set the extracted text to textInput.value
textInput.value = extractedText;
};

reader.readAsText(file); // Read the file as text
}
}
5 changes: 5 additions & 0 deletions materials/templates/materials/add_data.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ <h4>Add Data</h4>
<div class="card-header">
Add new reference
</div>
<div class="form-group">
<label for="{{ reference_form.inputRISFile.id_for_label }}">Upload RIS File:</label>
{{ reference_form.inputRISFile }}
</div>
<div class="card-body">
<form id="reference-form" method="post">
<div class="card authors-card">
Expand Down Expand Up @@ -547,6 +551,7 @@ <h4>Add Data</h4>
{% block script %}
{% load static %}
<script src="{% static 'materials/javascript/selectize.js' %}"></script>
<script src="{% static 'materials/javascript/autoupdates.js' %}"></script>
<script>
// Initial values for selectize elements
let initial_reference = '{{ main_form.select_reference.value }}';
Expand Down

0 comments on commit 3ce76ca

Please sign in to comment.