MascotTools =
init: ->
return if !Conf['Mascots'] or (g.VIEW is 'catalog' and Conf['Hide Mascots on Catalog'])
if Conf['Click to Toggle']
$.on @el, 'mousedown', MascotTools.click
$.on doc, 'QRDialogCreation', MascotTools.reposition
$.asap (-> d.body), =>
$.add d.body, @el
MascotTools.toggle()
el: $.el 'div',
id: "mascot"
innerHTML: ""
change: (mascot) ->
if Conf['Mascot Position'] is 'default'
$.rmClass doc, 'mascot-position-above-post-form'
$.rmClass doc, 'mascot-position-bottom'
$.rmClass doc, 'mascot-position-default'
$.addClass doc, if mascot.position is 'bottom'
'mascot-position-bottom'
else
'mascot-position-above-post-form'
$[if mascot.silhouette or Conf['Silhouettize Mascots']
'addClass'
else
'rmClass'
] doc, 'silhouettize-mascots'
el = $.el 'img'
$.on el, 'error', MascotTools.error
el.src = mascot.image
$.off img = @el.firstElementChild, 'error', MascotTools.error
$.replace img, el
Style.sheets.mascots.textContent = """<%= grunt.file.read('src/General/css/mascot.css') %>"""
error: ->
@src = MascotTools.imageError if MascotTools.imageError
el = $.el 'canvas',
width: 248
height: 100
ctx = el.getContext('2d')
ctx.font = "50px #{Conf['Font']}"
ctx.fillStyle = (new Style.color (Themes[Conf['theme']] or Themes['Yotsuba B'])['Text']).hex
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'
ctx.fillText "Mascot 404", 124, 50
el.toBlob (blob) =>
@src = MascotTools.imageError = URL.createObjectURL blob
toggle: ->
string = g.MASCOTSTRING
enabled = Conf[string]
return unless len = enabled.length
Conf['mascot'] = name = enabled[i = Math.floor(Math.random() * len)]
unless mascot = Mascots[name]
enabled.splice i, 1
$.replace el, $.el 'img' if el = @el.firstElementChild
$.set string, Conf[string] = enabled
return MascotTools.toggle()
MascotTools.change mascot
categories: [
'Anime'
'Ponies'
'Questionable'
'Silhouette'
'Western'
]
dialog: (key) ->
Conf['editMode'] = 'mascot'
if Mascots[key]
editMascot = JSON.parse JSON.stringify Mascots[key]
else
editMascot = {}
editMascot.name = key or ''
layout =
name: [
"Mascot Name"
""
"text"
]
image: [
"Image"
""
"text"
]
category: [
"Category"
MascotTools.categories[0]
"select"
MascotTools.categories
]
position: [
"Position"
"default"
"select"
["default", "top", "bottom"]
]
height: [
"Height"
"auto"
"text"
]
width: [
"Width"
"auto"
"text"
]
vOffset: [
"Vertical Offset"
"0"
"number"
]
hOffset: [
"Horizontal Offset"
"0"
"number"
]
center: [
"Center Mascot"
false
"checkbox"
]
silhouette: [
"Silhouette"
false
"checkbox"
]
dialog = $.el "div",
id: "mascotConf"
className: "reply dialog"
innerHTML: """<%= grunt.file.read('src/General/html/Features/MascotDialog.html').replace(/>\s+<').trim() %>"""
container = $ "#mascotcontent", dialog
fileRice = (e) ->
if e.shiftKey
@nextSibling.click()
updateMascot = ->
MascotTools.change editMascot
saveVal = ->
editMascot[@name] = @value
updateMascot()
imageFn = ->
if MascotTools.URL is @value
return MascotTools.change editMascot
else if MascotTools.URL
URL.revokeObjectURL MascotTools.URL
delete MascotTools.URL
saveVal.call @
nameFn = ->
@value = @value.replace /[^a-z-_0-9]/ig, "_"
if (@value isnt "") and !/^[a-z]/i.test @value
return alert "Mascot names must start with a letter."
saveVal.call @
saveCheck = ->
editMascot[@name] = if @checked then true else false
updateMascot()
for name, item of layout
value = editMascot[name] or= item[1]
switch item[2]
when "text"
div = @input item, name
input = $ 'input', div
switch name
when 'image'
$.on input, 'blur', imageFn
fileInput = $.el 'input',
type: "file"
accept: "image/*"
title: "imagefile"
hidden: "hidden"
$.on input, 'click', FileRice
$.on fileInput, 'change', MascotTools.uploadImage
$.after input, fileInput
when 'name'
$.on input, 'blur', nameFn
else
$.on input, 'blur', saveVal
when "number"
div = @input item, name
$.on $('input', div), 'blur', saveVal
when "select"
optionHTML = "