This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/resources/views/yuuno/user/profile_javascript.twig
2016-12-18 23:00:24 +01:00

128 lines
4 KiB
Twig

<script type="text/javascript">
window.addEventListener('load', function () {
{% if profile.lastfm %}
var np = new Sakura.AJAX();
np.SetUrl("{{ route('user.nowplaying', profile.id) }}");
np.AddCallback(200, function () {
var data = np.JSON(),
artist = Sakura.DOM.ID('np-artist'),
track = Sakura.DOM.ID('np-track'),
state = Sakura.DOM.ID('np-state'),
by = Sakura.DOM.ID('np-by');
artist.href = data.artist_url;
artist.textContent = data.artist;
track.href = data.track_url;
track.textContent = data.track;
state.className = 'fa ' + (data.listening ? 'fa-play-circle' : 'fa-history');
by.className = data.track === '' || data.artist === '' ? 'hidden' : '';
});
setInterval(function () { np.Start(Sakura.HTTPMethod.GET); }, 20000);
np.Start(Sakura.HTTPMethod.GET);
{% endif %}
// Check if location.hash is set
if (location.hash) {
var open = location.hash.slice(1);
// Check if the element exists
if (document.getElementById('profile-mode-' + open)) {
profileMode(open);
return;
}
}
var profileUserpage = document.getElementById('profile-mode-userpage');
location.hash = profileUserpage.children[0].innerHTML.trim().length ? 'userpage' : 'comments';
});
window.addEventListener("hashchange", function () {
profileMode(location.hash.slice(1));
});
// Switch to a different mode
function profileMode(id) {
// Get other active modes and fetch the new element
var current = document.getElementsByClassName('profile-mode-current'),
newMode = document.getElementById('profile-mode-' + id);
// Check if the new mode exists
if (!newMode) {
return;
}
// Check if there's any active
if (current) {
// Hide them all
for (i in current) {
current[i].className = 'hidden';
}
}
// Set the new to active
newMode.className = 'profile-mode-current';
}
function handleImageChange(file, url, query) {
var ajax = new Sakura.AJAX,
formData = new FormData;
formData.append('session', Sakura.Config.SessionId);
formData.append('file', file, file.name);
ajax.SetFormData(formData);
ajax.SetUrl(url);
ajax.AddCallback(200, function () {
var result = ajax.JSON();
if (result.error) {
var diag = new Sakura.Dialogue;
diag.Title = "Error";
diag.Text = result.error;
diag.Display();
} else {
refreshImage(url, query);
}
});
ajax.Start(Sakura.HTTPMethod.POST);
}
function handleImageDelete(url, query) {
var confirm = new Sakura.Dialogue;
confirm.SetType(Sakura.DialogueType.ConfirmNegative);
confirm.Title = "Deleting image";
confirm.Text = "Are you sure?";
confirm.AddCallback(Sakura.DialogueButton.Yes, function () {
var client = new Sakura.AJAX;
client.SetUrl(url + "?session=" + Sakura.Config.SessionId);
client.AddCallback(200, function () {
refreshImage(url, query);
});
client.Start(Sakura.HTTPMethod.DELETE);
this.Close();
});
confirm.Display();
}
function refreshImage(url, query) {
var elements = document.querySelectorAll(query);
for (var i = 0; i < elements.length; i++) {
var element = elements[i],
url = url + "?" + Date.now();
if (element.tagName === "IMG") {
element.src = url;
} else {
element.style.backgroundImage = "url('" + url + ")";
}
}
}
</script>