Sahli/sahli.coffee

212 lines
6 KiB
CoffeeScript
Raw Normal View History

###
.___________________________________, ___
| / | | . \ .(___):
| _______ | : \ : |
: |___/ / | | \__| |
| / | | | |
l__________/__________|___|______l__________j_____j
Ansi/Ansi Viewer in Ecmascript
Coded by Sir Garbagetruck / Accession 2013
Uses fonts by DMG, http://trueschool.se
Uses Andy Herbert's Ansilove.js for rendering.
###
2015-02-17 16:53:45 +02:00
class @Sahli
constructor: () ->
# I don't think we actually are going to have one, as we don't
# need instance variables (things used outside the function)
@loadpic = (picdata, inserthere) ->
switch picdata.filetype
when 'plain'
@loadplain picdata, inserthere
when 'ansi'
@loadhugeansi picdata, inserthere
when 'bin'
@loadansi picdata, inserthere
when 'xbin'
@loadansi picdata, inserthere
when 'ice'
@loadansi picdata, inserthere
when 'avatar'
@loadavatar picdata, inserthere
when 'pcboard'
@loadansi picdata, inserthere
when 'idf'
@loadansi picdata, inserthere
when 'adf'
@loadansi picdata, inserthere
when 'tundra'
@loadansi picdata, inserthere
else
@loadplain picdata, inserthere
@loadplain = (picdata, inserthere) ->
pdiv = $('<div>')
req = new XMLHttpRequest
fname = @location + '/' + picdata.file
ptxt = $('<pre></pre>')
color = @calccolor(picdata.color)
bgcolor = @calccolor(picdata.bg)
pdiv.addClass 'scrolly'
ptxt.addClass picdata.font.toLowerCase()
ptxt.css
'color': color
'background-color': bgcolor
'margin': 'auto'
ptxt.width picdata.width * 8
pdiv.width '100%'
pdiv.append ptxt
# this is going to be interesting when dealing with ansi files in UTF-8
# or SHIFT-JIS etc - is it needed now?
req.overrideMimeType 'text/plain; charset=ISO-8859-1'
req.onreadystatechange = ->
if req.readyState == req.DONE
if req.status == 200 or req.status == 0
ptxt.text @responseText
inserthere.after pdiv
$('body').scrollTop(0)
else
@loaderror inserthere, fname, req.statusText, req.status
req.open 'GET', fname, true
req.send null
@loadansi = (picdata, inserthere) ->
fname = @location + '/' + picdata.file
pdiv = $('<div>')
AnsiLove.render fname, ((canv, SAUCE) ->
pdiv.append canv
inserthere.after pdiv
@origwidth = canv.width
@origheight = canv.height
@SAUCE = SAUCE
),
'font': '80x25'
'bits': '8'
'columns': 160
'thumbnail': 0
@loadhugeansi = (picdata, inserthere) ->
fname = @location + '/' + picdata.file
pdiv = $('<div>')
calcheight = 0
canvwidth = 0
pdiv.css 'display', 'inline-block'
AnsiLove.splitRender fname, ((chunks, SAUCE) ->
chunks.forEach (canv) ->
canv.style.verticalAlign = 'bottom'
pdiv.append canv
calcheight = calcheight + canv.height
canvwidth = canv.width
inserthere.after pdiv
@SAUCE = SAUCE
@origwidth = canvwidth
@origheight = calcheight
pdiv.width canvwidth
), 30, 'bits': '8'
@loadavatar = (picdata, inserthere) ->
alert 'avatar', picdata, inserthere
@requestsahlifile = (url) ->
@loadkeys()
@DEBUG = false
@fullscreen = false
@scroll_speed= 5
@scroll_direction= 1
@asciiasgfx= false
@currentpic= 0
$.getJSON url, (json) =>
@filedata = json.filedata
@slides = json.slides
@location = json.location
alert "SAHLI READY TO GO\n#{@filedata.length} Entries"
@nextpic = =>
viewbox = $('div#sahliviewer')
viewbox.children().remove()
$('body').stop()
$('body').scrollTop(0)
@scroll_direction = 1
i = @currentpic
filedata = @filedata
filedata[i].pic = $('<h6>' + filedata[i].file + '</h6>')
viewbox.append filedata[i].pic
@loadpic filedata[i], filedata[i].pic
@currentpic += 1
if @currentpic > filedata.length - 1
@currentpic = 0
@togglefullscreen = ->
docElm = document.documentElement
if @fullscreen
if document.exitFullscreen
document.exitFullscreen()
else if document.mozCancelFullScreen
document.mozCancelFullScreen()
else if document.webkitCancelFullScreen
document.webkitCancelFullScreen()
@fullscreen = false
else
if docElm.requestFullscreen
docElm.requestFullscreen Element.ALLOW_KEYBOARD_INPUT
else if docElm.mozRequestFullScreen
docElm.mozRequestFullScreen Element.ALLOW_KEYBOARD_INPUT
else if docElm.webkitRequestFullScreen
docElm.webkitRequestFullScreen Element.ALLOW_KEYBOARD_INPUT
@fullscreen = true
@toggledebug = ->
$('h1#top').fadeToggle()
@DEBUG = !@DEBUG
@keycode = (char) ->
char.toUpperCase().charCodeAt 0
@calccolor = (colorset) ->
"rgba(#{colorset.toString()})"
@loaderror = (inserthere, fname, errortext, errorcode) ->
if errorcode == 404
errstr = "Unable to find #{fname}"
else
errstr = "error! #{errortext} / code #{errorcode}"
inserthere.after $("<h1>").text("#{errstr}")
@setscroll = ->
scrollbox = $('body')
bottom = $('body').height()
scrollto = bottom
# kill animations from before
scrollbox.stop true
if @scroll_direction == 1
@scroll_direction = -1
steps = bottom - scrollbox.scrollTop()
else
@scroll_direction = 1
scrollto = 0
steps = scrollbox.scrollTop()
console.log "#{@scroll_speed} | #{steps}"
scrollbox.animate { scrollTop: scrollto }, @scroll_speed * steps, 'linear'
@loadkeys = ->
$(document).on('keydown', (ev) =>
switch ev.which
when @keycode(' ')
@nextpic()
when @keycode('f')
@togglefullscreen()
when @keycode('s')
@setscroll()
when @keycode('t')
$('body').scrollTop(0)
when 8 # backspace
$('body').stop()
else
console.log ev.which
)