ImageGrid/legacy/ui/experiments/infinite-ribbon.html
Alex A. Naanou 5f47d6da7b restructured the repo moving the legacy out of the way...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2020-08-28 17:32:02 +03:00

187 lines
3.5 KiB
HTML
Executable File

<html>
<script src="jquery.js"></script>
<script>
function focus(elem){
$('.current.square').removeClass('current')
$(elem)
.closest('.square')
.addClass('current')
.trigger('focus')
}
function clickHandler(){
focus(this)
}
/* Focus next/prev container element
*
* This will shift focus within a container.
*
* If we reach the start/end of the container, we will "warp" around it
* and focus an element from the other end.
*
*/
// XXX make this customizable (selector-wise)...
function shiftFocus(n){
var cur = $('.current.square')
if(n > 0){
direction = 'next'
} else if(n < 0){
direction = 'prev'
} else {
return cur
}
// trigger the event...
cur.closest('.container')
.trigger('shiftfocus', n)
// XXX will adding a singular [direction] branch make things faster
// for n = +/-1 ???
var dir = cur[direction+'All']('.square')
// see if we need to warp arund...
if(dir.length < Math.abs(n)){
var sq = cur.closest('.container').children('.square')
var i = sq.index(cur)
return focus($(sq[ n > 0
? (i + n) % sq.length
: sq.length - (Math.abs(i + n) % sq.length) ]))
}
// shift the current element...
return focus($(dir[Math.abs(n)-1]))
}
// these will roll the container...
// XXX make this container context-aware...
function rollContainer(n){
var sq = $('.current.square')
.closest('.container')
.children('.square')
// right...
if(n > 0){
return $(sq.splice(0, n)).appendTo($('.container'))
// left...
} else if(n < 0){
var l = sq.length
return $(sq.splice(l+n, l)).prependTo($('.container'))
// 0...
} else {
return
}
}
// misc demo stuff...
var UPDATE_ELEMENT_TEXT = true
function toggleNumberUpdate(){
if(UPDATE_ELEMENT_TEXT){
UPDATE_ELEMENT_TEXT = false
} else {
UPDATE_ELEMENT_TEXT = true
}
}
// init things...
$(function(){
$('.container')
// NOTE: this event will get completed BEFORE the element is focused.
.on('shiftfocus', function(evt, n){
// update squares -- numbers...
if(UPDATE_ELEMENT_TEXT){
var sq = $(this).children('.square')
if(n > 0){
var s = sq.last().text()*1 + 1
} else if(n < 0){
var s = sq.first().text()*1 + n
} else {
return
}
rollContainer(n).each(function(i, e){
$(this).text(i + s)
})
// roll as-is...
} else {
rollContainer(n)
}
})
$('.square')
.click(clickHandler)
})
</script>
<style>
.container {
position: relative;
display: inline-block;
height: auto;
min-width: 0px;
overflow: visible;
white-space: nowrap;
font-size: 0;
background: gray;
padding: 10px;
}
.square {
position: relative;
display: inline-block;
vertical-align: middle;
text-align;left;
width: 100px;
height: 100px;
background: silver;
font-size: 12pt;
}
.current.square {
background: red;
}
</style>
<body>
<button onclick="shiftFocus(-5)">&lt;&lt;</button>
<button onclick="shiftFocus(-1)">&lt;</button>
<button onclick="shiftFocus(1)">&gt;</button>
<button onclick="shiftFocus(5)">&gt;&gt;</button>
&nbsp;
<button onclick="toggleNumberUpdate()">toggle square number update</button>
<div class="meta-container">
<div class="container">
<div class="square">1</div>
<div class="square">2</div>
<div class="square current">3</div>
<div class="square">4</div>
<div class="square">5</div>
<div class="square">6</div>
<div class="square">7</div>
</div>
</div>
</body>
<!-- vim:set ts=4 sw=4 : -->
</html>