diff --git a/demo/demo.css b/demo/demo.css index 250edb7..23a0fdb 100644 --- a/demo/demo.css +++ b/demo/demo.css @@ -12,16 +12,20 @@ .info { min-width: 22rem; - max-width: 42rem; + max-width: 40rem; } .widget { } .options pre { - overflow:auto; + overflow: auto; font-size: 85%; padding: 0 0 1em 0; border-bottom: 1px #eee solid; } +.options pre.style { + padding-top: 1em; + border-top: 1px #eee solid; +} h1 { font-size: 1.85rem; @@ -33,6 +37,9 @@ h3 { font-size: 1.05rem; } +.keyshot summary { + cursor: pointer; +} .keyshot a { display: block; } diff --git a/demo/demo.js b/demo/demo.js index 7a4240e..2e428c5 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -1,12 +1,6 @@ function styleDefString(width, height) { - return ` -lastfm-tracks { - box-sizing: border-box; - border: 1px solid #000; - width: ${width}px; - height: ${height}px; -}`; + return `lastfm-tracks {\n box-sizing: border-box;\n border: 1px solid #000;\n width: ${width}px;\n height: ${height}px;\n}`; } /** @@ -50,11 +44,11 @@ function debounce(func, delay) { * @param {ResizeObserverEntry[]} [roea] - ResizeObserverEntry Array */ function updateStyleDef(roea) { - const styling = document.querySelector(('.options pre')); + const styleDef = document.querySelector(('.options pre.style')); const widget = document.querySelector('lastfm-tracks'); - if (styling && widget) { + if (styleDef && widget) { const { offsetWidth, offsetHeight } = widget; - styling.textContent = styleDefString(offsetWidth, offsetHeight); + styleDef.textContent = styleDefString(offsetWidth, offsetHeight); } } /** @@ -62,6 +56,19 @@ function updateStyleDef(roea) { */ const handleResizedWidget = throttle(updateStyleDef, 100); +function updateTagDef() { + const widget = document.querySelector('lastfm-tracks'); + const tagDef = document.querySelector(('.options pre.tag')); + const attribs = []; + widget.getAttributeNames().forEach(name => { + const val = widget.getAttribute(name); + if (name!=='style' && !(name === 'class' && val === '')) { + attribs.push(`\n ${name}="${val}"`); + } + }) + tagDef.textContent = `\n`; +} + window.addEventListener( 'DOMContentLoaded', function () { // TODO run when widget is inserted... @@ -69,17 +76,25 @@ window.addEventListener( const stopButton = document.querySelector('button'); const toggleDynaHeader = document.querySelector('input.dynaheader'); const toggleHideAlbums = document.querySelector('input.hidealbums'); + const dynaHeaderChanged = () => { + widget.classList.toggle('dynaheader', toggleDynaHeader.checked); + updateTagDef(); + } + const hideAlbumsChanged = () => { + widget.classList.toggle('no-albums', toggleHideAlbums.checked); + updateTagDef(); + } if (widget) { stopButton?.addEventListener('click', () => { widget.stopUpdating() }); - toggleDynaHeader?.addEventListener('change', () => { - widget.classList.toggle('dynaheader', this.checked); - }); - toggleHideAlbums?.addEventListener('change', () => { - widget.classList.toggle('hide-albums', this.checked); - }); + toggleDynaHeader?.addEventListener('change', dynaHeaderChanged); + toggleHideAlbums?.addEventListener('change', hideAlbumsChanged); new ResizeObserver(handleResizedWidget).observe(widget); + + // init + dynaHeaderChanged(); + hideAlbumsChanged(); } }, false diff --git a/demo/index.html b/demo/index.html index d58c098..e4b7b9b 100644 --- a/demo/index.html +++ b/demo/index.html @@ -23,28 +23,27 @@

🔴 Last.fm Tracks widget

By Stig Nygaard.

-

BETA-VERSION - Work In Progress!...

+

This page is Work In Progress!...

This page is a simple demonstration of my Last.fm Tracks widget showing recent "scrobbles" from a last.fm account. I plan to make this page a place where you can play with - configuration of the widget, but that is not fully ready yet. The widget itself is also still a "beta-version". - So for now, just some quick basics...

+ configuration of the widget, but that is not fully ready yet. For now, just some quick basics...

To use the widget, you need to import lastfm.js as a module. If you do that from html, remember the type attribute:

<script src="lastfm.js" type="module"></script> -

The script defines/registers the custom html-element <lastfm-tracks/>. Simply insert such +

The script defines the custom html-element <lastfm-tracks/>. Simply insert such element on a webpage where you want a Tracks widget to be.

-

When creating a widget, the script will read the tracks.css stylesheet file, which it expects +

When creating widget, the script will read the tracks.css stylesheet file, which it expects to find at same location as the script file itself.

The widget has 3 modes. The Demo and Basic modes are "standalone" (pure frontend) - modes where widget communicates directly with last.fm's API. The third mode is - Backend-supported mode where you need to have a backend "proxy-api" that the widget can + modes where widget communicates directly with last.fm's API to get the "scrobbles". The third + mode is Backend-supported mode where you need to have a backend "proxy-api" that the widget can communicate with. The idea is that the proxy-api should mirror/forward the two methods (user.getinfo and user.getrecenttracks) @@ -57,7 +56,7 @@

🔴 Last.fm Tracks widget

  • "Demo" mode

    - <lastfm-tracks user="your username"></lastfm-tracks> + <lastfm-tracks user="your username"></lastfm-tracks>

    In the "standalone" Demo mode, the widget is "static". Latest "scrobbles" will be loaded upon creation of the widget, but tracklist will not be refreshed after that.

    @@ -65,7 +64,7 @@

    "Demo" mode

  • "Basic" mode

    - <lastfm-tracks user="your username" apikey="your own api key" interval="60"></lastfm-tracks> + <lastfm-tracks user="your username" apikey="your own api key" interval="60"></lastfm-tracks>

    Adding your own API-key - which you can get for free - puts the widget into the Basic mode where it supports periodically refresh of the tracks-list. @@ -74,7 +73,7 @@

    "Basic" mode

  • "Backend-supported" mode

    - <lastfm-tracks backend="/proxy-api" interval="35"></lastfm-tracks> + <lastfm-tracks backend="/proxy-api" interval="35"></lastfm-tracks>

    Add the backend attribute pointing to a custom "proxy API" to enable Backend-supported mode. Depending on your proxy-api, you could also add other attributes like user and @@ -90,9 +89,13 @@

    Getting your own API key for Basic or Backend-supported mo non-authenticated use of the API, you don't need a secret, but you still need an API-key connected to your Last.fm account. So follow the "apply for a key" link. - Here you should enter an application name and description. I imagine something like this is very fine: + Here you should enter an application name and description.

    -
     [screenshot]
    +
    + I imagine something like this will be very fine:... +  [screenshot] +
    +

    Once submitted, you should get a personal API-key which can be used for the Tracks widget, to either enable the widget's Basic mode, or use it in a proxy-api with widget in Backend-supported mode.

    @@ -110,7 +113,9 @@

    Getting your own API key for Basic or Backend-supported mo

  • -
    +    
    +    
    +