Sith/core/templates/core/user_pictures.jinja

142 lines
4.5 KiB
Django/Jinja
Raw Normal View History

2016-11-20 09:40:49 +00:00
{% extends "core/base.jinja" %}
{%- block additional_css -%}
<link rel="stylesheet" href="{{ scss('sas/css/album.scss') }}">
{%- endblock -%}
2024-07-23 10:11:19 +00:00
{% block additional_js %}
<script defer type="module">
import { showSaveFilePicker } from "{{ static('vendored/native-file-system-adapter/mod.js') }}";
2024-07-23 10:11:19 +00:00
window.showSaveFilePicker = showSaveFilePicker; /* Export function to normal javascript */
</script>
<script defer type="text/javascript" src="{{ static('vendored/zipjs/zip-fs-full.min.js') }}"></script>
2024-07-23 10:11:19 +00:00
{% endblock %}
2016-11-20 09:40:49 +00:00
{% block title %}
2024-07-23 10:11:19 +00:00
{% trans user_name=profile.get_display_name() %}{{ user_name }}'s pictures{% endtrans %}
2016-11-20 09:40:49 +00:00
{% endblock %}
{% block content %}
<main x-data="user_pictures">
{% if user.id == object.id %}
<div x-show="pictures.length > 0" x-cloak>
2024-07-30 17:23:48 +00:00
<button
:disabled="is_downloading"
2024-07-30 17:57:56 +00:00
class="btn btn-blue"
2024-08-05 17:25:30 +00:00
@click="download_zip()"
2024-07-30 17:23:48 +00:00
>
2024-07-30 17:57:56 +00:00
<i class="fa fa-download"></i>
2024-07-30 17:23:48 +00:00
{% trans %}Download all my pictures{% endtrans %}
</button>
<progress x-ref="progress" x-show="is_downloading"></progress>
2024-07-30 17:23:48 +00:00
</div>
{% endif %}
2024-08-16 20:52:20 +00:00
<template x-for="[album, pictures] in Object.entries(albums)" x-cloak>
<section>
<br />
<h4 x-text="album"></h4>
<div class="photos">
<template x-for="picture in pictures">
<a :href="`/sas/picture/${picture.id}`">
2024-08-16 20:52:20 +00:00
<div
class="photo"
:class="{not_moderated: !picture.is_moderated}"
:style="`background-image: url(${picture.thumb_url})`"
>
<template x-if="!picture.is_moderated">
<div class="overlay">&nbsp;</div>
<div class="text">{% trans %}To be moderated{% endtrans %}</div>
</template>
<template x-if="picture.is_moderated">
<div class="text">&nbsp;</div>
</template>
</div>
</a>
</template>
</div>
2024-08-10 13:16:37 +00:00
</section>
</template>
<div class="photos" :aria-busy="loading"></div>
2024-07-30 17:23:48 +00:00
</main>
2024-07-30 17:57:56 +00:00
{% endblock content %}
2024-07-30 17:23:48 +00:00
{% block script %}
2024-07-30 17:23:48 +00:00
{{ super() }}
<script>
2024-09-03 18:15:37 +00:00
/**
* @typedef UserProfile
* @property {number} id
* @property {string} first_name
* @property {string} last_name
* @property {string} nick_name
* @property {string} display_name
* @property {string} profile_url
* @property {string} profile_pict
*/
2024-07-30 17:23:48 +00:00
/**
* @typedef Picture
* @property {number} id
* @property {string} name
* @property {number} size
* @property {string} date
2024-09-03 18:15:37 +00:00
* @property {UserProfile} owner
2024-07-30 17:23:48 +00:00
* @property {string} full_size_url
* @property {string} compressed_url
* @property {string} thumb_url
2024-07-31 09:56:38 +00:00
* @property {string} album
2024-09-03 18:15:37 +00:00
* @property {boolean} is_moderated
* @property {boolean} asked_for_removal
2024-07-30 17:23:48 +00:00
*/
document.addEventListener("alpine:init", () => {
Alpine.data("user_pictures", () => ({
is_downloading: false,
loading: true,
pictures: [],
albums: {},
async init() {
2024-09-03 18:15:37 +00:00
this.pictures = await fetch_paginated("{{ url("api:pictures") }}" + "?users_identified={{ object.id }}");
this.albums = this.pictures.reduce((acc, picture) => {
if (!acc[picture.album]){
acc[picture.album] = [];
}
acc[picture.album].push(picture);
return acc;
}, {});
this.loading = false;
},
async download_zip(){
this.is_downloading = true;
const bar = this.$refs.progress;
bar.value = 0;
bar.max = this.pictures.length;
const fileHandle = await window.showSaveFilePicker({
_preferPolyfill: false,
suggestedName: "{%- trans -%} pictures {%- endtrans -%}.zip",
types: {},
excludeAcceptAllOption: false,
})
const zipWriter = new zip.ZipWriter(await fileHandle.createWritable());
await Promise.all(this.pictures.map(p => {
const img_name = p.album + "/IMG_" + p.date.replaceAll(/[:\-]/g, "_") + p.name.slice(p.name.lastIndexOf("."));
return zipWriter.add(
img_name,
new zip.HttpReader(p.full_size_url),
{level: 9, lastModDate: new Date(p.date), onstart: () => bar.value += 1}
);
}));
await zipWriter.close();
this.is_downloading = false;
}
}))
});
</script>
2024-07-30 17:57:56 +00:00
{% endblock script %}