From 6516c4af1d637d1725c3e9570378798addf28f34 Mon Sep 17 00:00:00 2001 From: Michiel Scholten Date: Mon, 26 Mar 2018 15:33:07 +0200 Subject: [PATCH] Moved autocompletion items to seperate javascript call --- digimarks.py | 13 +++++++++++++ templates/bookmarks.html | 8 +------- templates/bookmarks.js | 11 +++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 templates/bookmarks.js diff --git a/digimarks.py b/digimarks.py index 7de8ffb..0cda344 100644 --- a/digimarks.py +++ b/digimarks.py @@ -582,6 +582,19 @@ def bookmarks_page(userkey, filtermethod=None, sortmethod=None): ) +@app.route('//js') +def bookmarks_js(userkey): + """ Return list of bookmarks with their favicons, to be used for autocompletion """ + bookmarks = Bookmark.select().where( + Bookmark.userkey == userkey, + Bookmark.status == Bookmark.VISIBLE + ).order_by(Bookmark.created_date.desc()) + return render_template( + 'bookmarks.js', + bookmarks=bookmarks + ) + + @app.route('/r//') def bookmark_redirect(userkey, urlhash): """ Securely redirect a bookmark to its url, stripping referrer (if browser plays nice) """ diff --git a/templates/bookmarks.html b/templates/bookmarks.html index 4498131..485cbd5 100644 --- a/templates/bookmarks.html +++ b/templates/bookmarks.html @@ -90,13 +90,6 @@ minLength: 3, limit: 10, data: { - {% for bookmark in bookmarks %} - {% if bookmark.favicon %} - "{{ bookmark.title | replace('\n', '') }}": "{{ url_for('static', filename='favicons/' + bookmark.favicon) }}", - {% else %} - "{{ bookmark.title | replace('\n', '') }}": null, - {% endif %} - {% endfor %} }, } var elem = document.querySelector('.autocomplete'); @@ -106,4 +99,5 @@ }); */ + {% endblock %} diff --git a/templates/bookmarks.js b/templates/bookmarks.js new file mode 100644 index 0000000..4c748a7 --- /dev/null +++ b/templates/bookmarks.js @@ -0,0 +1,11 @@ +var elem = document.querySelector('.autocomplete'); +var instance = M.Autocomplete.getInstance(elem); +instance.updateData({ + {% for bookmark in bookmarks %} + {% if bookmark.favicon %} + "{{ bookmark.title | replace('"', '\\"') | replace('\n', '') }}": "{{ url_for('static', filename='favicons/' + bookmark.favicon) }}", + {% else %} + "{{ bookmark.title | replace('"', '\\"') | replace('\n', '') }}": null, + {% endif %} + {% endfor %} +});