Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Replace jquery UIs with smaller library

Roland Geidera5c892f0d287
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 7e59e04b9..86174366b 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -19,12 +19,16 @@ New features:
* Use the metricsgraphics library to more easily draw charts `#188`_
* Add extended PDF options to schedules as well (thanks `@alelevinas`_ ) `#272`_
* Show trained secondary muscles in workout view (thanks `@alokhan`_ ) `#282`_
-* Remove hard-coded CC licence from documentation and website `#247`_
+Improvements:
+
+* Replace jquery UI's autocompleter, this reduces size of JS (part of) `#78`_
+* Remove hard-coded CC licence from documentation and website `#247`_
Other improvements and bugfixes: `#279`_, `#275`_, `#270`_, `#258`_, `#257`_, `#269`_, `#296_`
+.. _#78: https://github.com/wger-project/wger/issues/78
.. _#188: https://github.com/wger-project/wger/issues/188
.. _#216: https://github.com/wger-project/wger/issues/216
.. _#217: https://github.com/wger-project/wger/issues/217
diff --git a/wger/core/static/css/jquery-ui-custom.css b/wger/core/static/css/jquery-ui-custom.css
index f22168911..4bb31604a 100644
--- a/wger/core/static/css/jquery-ui-custom.css
+++ b/wger/core/static/css/jquery-ui-custom.css
@@ -24,14 +24,8 @@
**************************************************************/
-/* Add a max-height for the autocompleter */
-.ui-autocomplete {
- max-height: 300px;
- overflow-y: auto;
- overflow-x: hidden; /* prevent horizontal scrollbar */
-}
/* Increase the z-index so bootstrap's modals, etc. don't interfere */
.ui-front {
z-index: 9999;
-}
\ No newline at end of file
+}
diff --git a/wger/core/static/css/workout-manager-common.css b/wger/core/static/css/workout-manager-common.css
index 2fc64c917..a73817407 100644
--- a/wger/core/static/css/workout-manager-common.css
+++ b/wger/core/static/css/workout-manager-common.css
@@ -329,4 +329,62 @@ table.month td.session-good {
table.month td.session-good:hover {
background-color: #449D44;
-}
\ No newline at end of file
+}
+
+
+/*
+ * Autocompleter
+ */
+.autocomplete-wrapper {
+ margin: 44px auto 44px;
+ max-width: 600px;
+}
+
+.autocomplete-wrapper label {
+ display: block;
+ margin-bottom: .75em;
+ color: #3f4e5e;
+ font-size: 1.25em;
+}
+.autocomplete-wrapper .text-field {
+ padding: 0 15px;
+ width: 100%;
+ height: 40px;
+ border: 1px solid #CBD3DD;
+ font-size: 1.125em;
+}
+.autocomplete-wrapper ::-webkit-input-placeholder,
+.autocomplete-wrapper :-moz-placeholder,
+.autocomplete-wrapper ::-moz-placeholder,
+.autocomplete-wrapper :-ms-input-placeholder {
+ color: #CBD3DD;
+ font-style: italic;
+ font-size: 18px;
+}
+.autocomplete-suggestions {
+ overflow: auto;
+ border: 1px solid #CBD3DD;
+ background: #FFF; }
+
+.autocomplete-suggestion {
+ overflow: hidden;
+ padding: 5px 15px;
+ white-space: nowrap;
+}
+
+.autocomplete-selected {
+ background: #F0F0F0;
+}
+
+.autocomplete-suggestions strong {
+ color: #3465a4;
+ font-weight: bold;
+}
+
+.autocomplete-group {
+ background-color: #d3d7cf;
+}
+.autocomplete-group strong {
+ color: black;
+ font-weight: bold;
+}
diff --git a/wger/core/static/js/wger-core.js b/wger/core/static/js/wger-core.js
index 3cfa8103b..7b9fe19dd 100644
--- a/wger/core/static/js/wger-core.js
+++ b/wger/core/static/js/wger-core.js
@@ -72,34 +72,6 @@ function get_current_language() {
return $('#current-language').data('currentLanguage');
}
-/*
- * Define an own widget, which is basically an autocompleter that groups
- * results by category
- */
-$.widget("custom.catcomplete", $.ui.autocomplete, {
- _renderMenu: function (ul, items) {
- var that = this,
- currentCategory = "";
- $.each(items, function (index, item) {
- if (item.category !== currentCategory) {
-
- ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
- currentCategory = item.category;
- }
- that._renderItemData(ul, item);
- });
- },
- _renderItem: function (ul, item) {
- var li_style = '';
- if (item.image) {
- li_style = "style='background-image:url(" + item.image_thumbnail + ");background-size:30px 30px;background-repeat:no-repeat;'";
- }
-
- return $("<li " + li_style + ">")
- .append("<a style='margin-left:30px;'>" + item.name + "</a>")
- .appendTo(ul);
- }
-});
/*
@@ -508,23 +480,36 @@ function init_remove_exercise_formset() {
function init_edit_set() {
// Initialise the autocompleter (our widget, defined above)
if (jQuery.ui) {
- $("#exercise-search").catcomplete({
- source: '/api/v2/exercise/search/?language=' + get_current_language(),
- minLength: 2,
- select: function (event, ui) {
-
- // Add the exercise to the list
- add_exercise(ui.item);
-
- // Load formsets
- get_exercise_formset(ui.item.id);
-
- // Init the remove buttons
- init_remove_exercise_formset();
-
- // Reset the autocompleter
- $(this).val("");
- return false;
+ $('#exercise-search').devbridgeAutocomplete({
+ serviceUrl: '/api/v2/exercise/search/?language=' + get_current_language(),
+ onSelect: function (suggestion) {
+ // Add the exercise to the list
+ add_exercise({id: suggestion.data.id,
+ value: suggestion.value});
+
+ // Load formsets
+ get_exercise_formset(suggestion.data.id);
+
+ // Init the remove buttons
+ init_remove_exercise_formset();
+
+ // Reset the autocompleter
+ $(this).val("");
+ return false;
+ },
+ groupBy: 'category',
+ paramName: 'term',
+ transformResult: function(response) {
+ // why is response not already a JSON object??
+ var jsonResponse = $.parseJSON(response);
+ return {
+ suggestions: $.map(jsonResponse, function(item) {
+ return {value: item.value, data: {id: item.id,
+ category: item.category,
+ image: item.image,
+ thumbnail: item.image_thumbnail}};
+ })
+ };
}
});
}
@@ -712,4 +697,4 @@ $(document).ready(function() {
'/' + token;
window.location.href = targetUrl;
});
-});
\ No newline at end of file
+});
diff --git a/wger/core/templates/template.html b/wger/core/templates/template.html
index 7f208fb2c..023010237 100644
--- a/wger/core/templates/template.html
+++ b/wger/core/templates/template.html
@@ -70,6 +70,7 @@
<script src="{% static 'js/exercises.js' %}"></script>
<script src="{% static 'js/nutrition.js' %}"></script>
<script src="{% static 'bower_components/metrics-graphics/dist/metricsgraphics.min.js' %}"></script>
+ <script src="{% static 'bower_components/devbridge-autocomplete/dist/jquery.autocomplete.min.js' %}"></script>
{% endcompress %}
{% block header %}{% endblock %}
diff --git a/wger/exercises/templates/equipment/overview.html b/wger/exercises/templates/equipment/overview.html
index 9056a16b6..438a462b6 100644
--- a/wger/exercises/templates/equipment/overview.html
+++ b/wger/exercises/templates/equipment/overview.html
@@ -15,15 +15,25 @@
<script>
$(function() {
// Init the autocompleter
- $("#exercise-search").catcomplete({
- source: "{% url 'exercise-search' %}",
- minLength: 2,
- select: function(event, ui) {
-
- // Redirect to exercise after clicking on a result
- // Path is hard coded here, because we can't use the url-tag
- window.location.href = '/exercise/' + ui.item.id + '/view/'
- }
+ $('#exercise-search').devbridgeAutocomplete({
+ serviceUrl: '{% url 'exercise-search' %}',
+ onSelect: function (suggestion) {
+ window.location.href = '/exercise/' + suggestion.data.id + '/view/'
+ },
+ groupBy: 'category',
+ paramName: 'term',
+ transformResult: function(response) {
+ // why is response not already a JSON object??
+ var jsonResponse = $.parseJSON(response);
+ return {
+ suggestions: $.map(jsonResponse, function(item) {
+ return {value: item.value, data: {id: item.id,
+ category: item.category,
+ image: item.image,
+ thumbnail: item.image_thumbnail}};
+ })
+ };
+ }
});
});
</script>
@@ -115,15 +125,10 @@ $(function() {
<h4>{% trans "Search" %}</h4>
-<form action="{% url 'exercise-search' %}"
- method="get"
- id="exercise_search_form">
- {% csrf_token %}
<input name="term"
type="search"
id="exercise-search"
class="ajax-form-element form-control"
placeholder="{% trans 'exercise name' %}"
style="width:100%;">
-</form>
{% endblock %}
diff --git a/wger/exercises/templates/exercise/overview.html b/wger/exercises/templates/exercise/overview.html
index 2788f4995..1ffa87da4 100644
--- a/wger/exercises/templates/exercise/overview.html
+++ b/wger/exercises/templates/exercise/overview.html
@@ -16,20 +16,27 @@
<script>
$(document).ready(function() {
- // Prevent the form from being submited if JavaScript is enabled
- $("#exercise_search_form").submit(function() {return false;});
-
// Init the autocompleter
- $("#exercise-search").catcomplete({
- source: "{% url 'exercise-search' %}",
- minLength: 2,
- select: function(event, ui) {
-
- // Redirect to exercise after clicking on a result
- // Path is hard coded here, because we can't use the url-tag
- window.location.href = '/exercise/' + ui.item.id + '/view/'
- }
- });
+ $('#exercise-search').devbridgeAutocomplete({
+ serviceUrl: '{% url 'exercise-search' %}',
+ onSelect: function (suggestion) {
+ window.location.href = '/exercise/' + suggestion.data.id + '/view/'
+ },
+ groupBy: 'category',
+ paramName: 'term',
+ transformResult: function(response) {
+ // why is response not already a JSON object??
+ var jsonResponse = $.parseJSON(response);
+ return {
+ suggestions: $.map(jsonResponse, function(item) {
+ return {value: item.value, data: {id: item.id,
+ category: item.category,
+ image: item.image,
+ thumbnail: item.image_thumbnail}};
+ })
+ };
+ }
+ });
});
</script>
{% endblock %}
@@ -124,16 +131,9 @@ $(document).ready(function() {
</p>
<h4>{% trans "Search" %}</h4>
-
-<form action="{% url 'exercise-search' %}"
- method="get"
- id="exercise_search_form">
- {% csrf_token %}
<input name="term"
type="search"
id="exercise-search"
class="ajax-form-element form-control"
- placeholder="{% trans 'exercise name' %}"
- >
-</form>
+ placeholder="{% trans 'exercise name' %}">
{% endblock %}
diff --git a/wger/exercises/templates/mobile/exercise/overview.html b/wger/exercises/templates/mobile/exercise/overview.html
index 1cebf5415..72a67174e 100644
--- a/wger/exercises/templates/mobile/exercise/overview.html
+++ b/wger/exercises/templates/mobile/exercise/overview.html
@@ -12,23 +12,29 @@
{% block header %}
<script>
- $(document).ready(function() {
-
- // Prevent the form from being submited if JavaScript is enabled
- $("#exercise_search_form").submit(function() {return false;});
-
- // Init the autocompleter
- $("#exercise-search").catcomplete({
- source: "{% url 'exercise-search' %}",
- minLength: 2,
- select: function(event, ui) {
-
- // Redirect to exercise after clicking on a result
- // Path is hard coded here, because we can't use the url-tag
- window.location.href = '/exercise/' + ui.item.id + '/view/'
- }
- });
+$(document).ready(function() {
+ // Init the autocompleter
+ $('#exercise-search').devbridgeAutocomplete({
+ serviceUrl: '{% url 'exercise-search' %}',
+ onSelect: function (suggestion) {
+ window.location.href = '/exercise/' + suggestion.data.id + '/view/'
+ },
+ groupBy: 'category',
+ paramName: 'term',
+ transformResult: function(response) {
+ // why is response not already a JSON object??
+ var jsonResponse = $.parseJSON(response);
+ return {
+ suggestions: $.map(jsonResponse, function(item) {
+ return {value: item.value, data: {id: item.id,
+ category: item.category,
+ image: item.image,
+ thumbnail: item.image_thumbnail}};
+ })
+ };
+ }
});
+});
</script>
{% endblock %}
@@ -99,17 +105,11 @@
-->
{% block sidebar %}
<h4>{% trans "Search" %}</h4>
-
- <form action="{% url 'exercise-search' %}"
- method="get"
- id="exercise_search_form">
- {% csrf_token %}
<input name="term"
type="search"
id="exercise-search"
class="ajax-form-element form-control"
placeholder="{% trans 'exercise name' %}">
- </form>
<p>
diff --git a/wger/settings_global.py b/wger/settings_global.py
index e79e0fbbf..7776a0ea5 100644
--- a/wger/settings_global.py
+++ b/wger/settings_global.py
@@ -99,7 +99,8 @@ BOWER_INSTALLED_APPS = (
'DataTables',
'components-font-awesome',
'tinymce',
- 'metrics-graphics'
+ 'metrics-graphics',
+ 'devbridge-autocomplete#1.2.x'
)