From 9141ce0d8b163317ae36ec31a43ff66a2e77bd23 Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 23 Dec 2016 20:18:02 +0100 Subject: [PATCH] remove inline js from profile --- resources/assets/typescript/Yuuno/Image.ts | 73 ++++++++++ resources/assets/typescript/Yuuno/Profile.ts | 73 ++++++++++ resources/views/yuuno/user/profile.twig | 10 +- .../views/yuuno/user/profile_javascript.twig | 127 ------------------ .../views/yuuno/user/profile_macros.twig | 4 +- 5 files changed, 154 insertions(+), 133 deletions(-) create mode 100644 resources/assets/typescript/Yuuno/Image.ts create mode 100644 resources/assets/typescript/Yuuno/Profile.ts delete mode 100644 resources/views/yuuno/user/profile_javascript.twig diff --git a/resources/assets/typescript/Yuuno/Image.ts b/resources/assets/typescript/Yuuno/Image.ts new file mode 100644 index 0000000..4629359 --- /dev/null +++ b/resources/assets/typescript/Yuuno/Image.ts @@ -0,0 +1,73 @@ +namespace Yuuno +{ + export class Image + { + private static Client: Sakura.AJAX = new Sakura.AJAX; + + public static Set(file: File, url: string, selector: string): void { + var formData: FormData = new FormData; + formData.append('session', Sakura.Config.SessionId); + formData.append('file', file, file.name); + + this.Client.SetFormData(formData); + this.Client.SetUrl(url); + + this.Client.AddCallback(0, Image.Error); + this.Client.AddCallback(200, (ajax: Sakura.AJAX) => { + var result: any = ajax.JSON(); + + if (result.error) { + Image.Error(result.error); + } else { + Image.Reload(url, selector); + } + + ajax.RemoveCallback(200); + }); + + this.Client.Start(Sakura.HTTPMethod.POST); + } + + public static Delete(url: string, selector: string): void { + this.Client.SetUrl(url + "?session=" + Sakura.Config.SessionId); + + this.Client.AddCallback(0, () => { + Image.Reload(url, selector); + }); + + var confirm: Sakura.Dialogue = new Sakura.Dialogue; + confirm.SetType(Sakura.DialogueType.ConfirmNegative); + confirm.Title = "Deleting image"; + confirm.Text = "Are you sure?"; + + confirm.AddCallback(Sakura.DialogueButton.Yes, () => { + Image.Client.Start(Sakura.HTTPMethod.DELETE); + confirm.Close(); + }); + + confirm.Display(); + } + + private static Error(msg: string = null): void { + var diag: Sakura.Dialogue = new Sakura.Dialogue; + diag.Title = "Upload error!"; + diag.Text = msg ? msg : "Something happened!"; + diag.Display(); + } + + private static Reload(url: string, selector: string): void { + var elements = Sakura.DOM.Query(selector); + + 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 + ")"; + } + } + } + } +} diff --git a/resources/assets/typescript/Yuuno/Profile.ts b/resources/assets/typescript/Yuuno/Profile.ts new file mode 100644 index 0000000..1aff494 --- /dev/null +++ b/resources/assets/typescript/Yuuno/Profile.ts @@ -0,0 +1,73 @@ +namespace Yuuno +{ + export class Profile + { + // now playing + private static NPClient: Sakura.AJAX; + private static NPInterval: number; + + public static Load(userpage: boolean, npUrl: string = null): void { + window.addEventListener("hashchange", () => { + this.Mode(location.hash.slice(1)); + }); + + if (location.hash) { + location.hash = location.hash.slice(1); + } else { + location.hash = userpage ? 'userpage' : 'comments'; + } + + if (npUrl !== null) { + this.NPStart(npUrl); + } + } + + public static Mode(id: string): void { + // 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 and hide them + if (current) { + for (var i in current) { + current[i].className = 'hidden'; + } + } + + newMode.className = 'profile-mode-current'; + } + + private static NPStart(url: string): void { + this.NPClient = new Sakura.AJAX; + this.NPClient.SetUrl(url); + this.NPClient.AddCallback(200, () => { + var data: any = this.NPClient.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' : ''; + }); + this.NPUpdate(); + } + + private static NPUpdate(): void { + if (!Profile.NPInterval) { + Profile.NPInterval = setInterval(Profile.NPUpdate, 20000); + } + + Profile.NPClient.Start(Sakura.HTTPMethod.GET); + } + } +} diff --git a/resources/views/yuuno/user/profile.twig b/resources/views/yuuno/user/profile.twig index 804bf76..e8f7d9c 100644 --- a/resources/views/yuuno/user/profile.twig +++ b/resources/views/yuuno/user/profile.twig @@ -105,10 +105,6 @@ }, } %} -{% block js %} - {% include 'user/profile_javascript.twig' %} -{% endblock %} - {% block content %} {% if (user.id == profile.id and not user.restricted and user.activated and user.perms.changeBackground) or user.perms.manageProfileImages %} {{ profile_image_changer(route('user.background', profile.id), '.container') }} @@ -193,4 +189,10 @@ + {% endblock %} diff --git a/resources/views/yuuno/user/profile_javascript.twig b/resources/views/yuuno/user/profile_javascript.twig deleted file mode 100644 index 2d22639..0000000 --- a/resources/views/yuuno/user/profile_javascript.twig +++ /dev/null @@ -1,127 +0,0 @@ - diff --git a/resources/views/yuuno/user/profile_macros.twig b/resources/views/yuuno/user/profile_macros.twig index ff95237..dc6e46e 100644 --- a/resources/views/yuuno/user/profile_macros.twig +++ b/resources/views/yuuno/user/profile_macros.twig @@ -1,8 +1,8 @@ {% macro profile_image_changer(url, query) %}
- +
{% endmacro %}