149 lines
		
	
	
	
		
			4.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			149 lines
		
	
	
	
		
			4.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| <!DOCTYPE html5>
 | |
| <html>
 | |
| <head>
 | |
|     {% import "bookmark_card.html" as bookmark_card %}
 | |
|     {% import "header.html" as header %}
 | |
|     <title>Bookmarks</title>
 | |
|     <meta charset="UTF-8">
 | |
|     <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
 | |
|     <link rel="stylesheet" href="/static/css/common.css">
 | |
|     <link rel="stylesheet" href="/static/css/etiquette.css">
 | |
|     <link rel="stylesheet" href="/static/css/cards.css">
 | |
|     {% if theme %}<link rel="stylesheet" href="/static/css/theme_{{theme}}.css">{% endif %}
 | |
|     <script src="/static/js/common.js"></script>
 | |
|     <script src="/static/js/api.js"></script>
 | |
|     <script src="/static/js/spinner.js"></script>
 | |
|     <script src="/static/js/editor.js"></script>
 | |
| 
 | |
| <style>
 | |
| #bookmark_list
 | |
| {
 | |
|     display: flex;
 | |
|     flex: 0 0 auto;
 | |
|     flex-direction: column;
 | |
| }
 | |
| </style>
 | |
| </head>
 | |
| 
 | |
| <body>
 | |
|     {{header.make_header(session=session)}}
 | |
|     <div id="content_body">
 | |
|         <div id="bookmark_list">
 | |
|             <h2>Bookmarks</h2>
 | |
|             {% for bookmark in bookmarks %}
 | |
|             {{bookmark_card.create_bookmark_card(bookmark, add_delete_button=True)}}
 | |
|             {% endfor %}
 | |
| 
 | |
|             <div class="bookmark_card new_bookmark_card">
 | |
|                 <input id="new_bookmark_title" type="text" placeholder="title (optional)">
 | |
|                 <input id="new_bookmark_url" type="text" placeholder="url">
 | |
|                 <div class="bookmark_toolbox">
 | |
|                     <button id="new_bookmark_button" class="green_button" onclick="return create_bookmark_form();">Create</button>
 | |
|                 </div>
 | |
|             </div>
 | |
|         </div>
 | |
|     </div>
 | |
| </body>
 | |
| 
 | |
| <script type="text/javascript">
 | |
| function create_bookmark_form()
 | |
| {
 | |
|     const url = document.getElementById("new_bookmark_url").value.trim();
 | |
|     const title = document.getElementById("new_bookmark_title").value.trim();
 | |
|     if (!url)
 | |
|     {
 | |
|         return;
 | |
|     }
 | |
|     return api.bookmarks.create(url, title, common.refresh_or_alert);
 | |
| }
 | |
| 
 | |
| function delete_bookmark_form(event)
 | |
| {
 | |
|     const card = event.target.closest(".bookmark_card");
 | |
|     const id = card.dataset.id;
 | |
|     function callback(response)
 | |
|     {
 | |
|         if (response.meta.status !== 200)
 | |
|         {
 | |
|             alert(JSON.stringify(response));
 | |
|             return;
 | |
|         }
 | |
|         card.parentElement.removeChild(card);
 | |
|     }
 | |
|     api.bookmarks.delete(id, callback);
 | |
| }
 | |
| 
 | |
| ed_on_open = undefined;
 | |
| 
 | |
| function ed_on_save(ed)
 | |
| {
 | |
|     function callback(response)
 | |
|     {
 | |
|         ed.hide_spinner();
 | |
| 
 | |
|         if (response.meta.status != 200)
 | |
|         {
 | |
|             ed.show_error("Status: " + response.meta.status);
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         // The data coming back from the server will have been normalized.
 | |
|         ed.elements["title"].edit.value = response.data.title;
 | |
| 
 | |
|         ed.save();
 | |
| 
 | |
|         ed.elements["title"].display.href = response.data.url;
 | |
|         ed.elements["url"].display.href = response.data.url;
 | |
|     }
 | |
| 
 | |
|     ed.elements["url"].edit.value = ed.elements["url"].edit.value.trim();
 | |
|     if (! ed.elements["url"].edit.value)
 | |
|     {
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     const bookmark_id = ed.misc_data["bookmark_id"];
 | |
|     const title = ed.elements["title"].edit.value;
 | |
|     const url = ed.elements["url"].edit.value;
 | |
| 
 | |
|     ed.show_spinner();
 | |
|     api.bookmarks.edit(bookmark_id, title, url, callback);
 | |
| }
 | |
| 
 | |
| ed_on_cancel = undefined;
 | |
| 
 | |
| function create_editors()
 | |
| {
 | |
|     const cards = document.getElementsByClassName("bookmark_card");
 | |
|     for (const card of cards)
 | |
|     {
 | |
|         if (card.classList.contains("new_bookmark_card"))
 | |
|         {
 | |
|             continue;
 | |
|         }
 | |
|         const ed_elements = [
 | |
|             {
 | |
|                 "id": "title",
 | |
|                 "element": card.getElementsByClassName("bookmark_title")[0],
 | |
|                 "placeholder": "title (optional)",
 | |
|                 "empty_text": card.dataset.id,
 | |
|                 "autofocus": true,
 | |
|             },
 | |
|             {
 | |
|                 "id": "url",
 | |
|                 "element": card.getElementsByClassName("bookmark_url")[0],
 | |
|                 "placeholder": "url",
 | |
|             },
 | |
|         ];
 | |
|         ed = new editor.Editor(ed_elements, ed_on_open, ed_on_save, ed_on_cancel);
 | |
|         ed.misc_data["bookmark_id"] = card.dataset.id;
 | |
|     }
 | |
| }
 | |
| 
 | |
| function on_pageload()
 | |
| {
 | |
|     create_editors();
 | |
| }
 | |
| document.addEventListener("DOMContentLoaded", on_pageload);
 | |
| </script>
 | |
| </html>
 |