1
0
Fork 0
mirror of https://github.com/Luzifer/password.git synced 2024-11-13 19:52:43 +00:00
password/frontend/assets/application.coffee

72 lines
1.9 KiB
CoffeeScript
Raw Normal View History

2015-05-10 14:38:17 +00:00
if window.Storage and window.JSON
window.$storage = (key) ->
set: (value) ->
localStorage.setItem(key, JSON.stringify(value))
get: ->
item = localStorage.getItem(key)
JSON.parse(item) if item
$ ->
window.lastLoad = now()
window.refreshPassword = 30000
window.tickerInterval = 200
window.ticker = setInterval tick, window.tickerInterval
$('#focusedInput').bind 'click', stopRefresh
$('#focusedInput').bind 'blur', restartRefresh
$('#optionSave').bind 'click', saveOptions
loadOptions()
loadPassword()
now = () ->
d = new Date()
d.getTime()
stopRefresh = () ->
clearInterval(window.ticker)
$('#focusedInput').select()
false
restartRefresh = () ->
window.lastLoad = now()
window.ticker = setInterval tick, window.tickerInterval
setProgress = (perc) ->
$('.progress-bar').css('width', "#{perc}%")
loadPassword = () ->
options = loadOptions()
$.get "/v1/getPassword?length=#{options.passwordLength}&special=#{options.useSpecial}&xkcd=#{options.useXKCD}", (data) ->
2015-05-10 14:38:17 +00:00
$('#focusedInput').val(data)
window.lastLoad = now()
saveOptions = () ->
options =
passwordLength: $('#passwordLengthOption').val()
useSpecial: $('#useSpecialOption')[0].checked
useXKCD: $('#useXKCDOption')[0].checked
2015-05-10 14:38:17 +00:00
window.$storage('SecurePasswordOptions').set(options)
$('#settingsModal').modal('hide')
loadPassword()
loadOptions = () ->
options = window.$storage('SecurePasswordOptions').get()
if options == undefined
options =
passwordLength: 20
useSpecial: false
useXKCD: false
2015-05-10 14:38:17 +00:00
$('#passwordLengthOption').val(options.passwordLength)
$('#useSpecialOption')[0].checked = options.useSpecial
$('#useXKCDOption')[0].checked = options.useXKCD
2015-05-10 14:38:17 +00:00
options
tick = () ->
diff = now() - window.lastLoad
perc = (window.refreshPassword - diff) / window.refreshPassword * 100
setProgress perc
if diff >= window.refreshPassword
loadPassword()