debian-mirror-gitlab/app/assets/javascripts/autosave.js.coffee

40 lines
704 B
CoffeeScript
Raw Normal View History

2015-04-26 12:48:37 +05:30
class @Autosave
constructor: (field, key) ->
@field = field
key = key.join("/") if key.join?
@key = "autosave/#{key}"
@field.data "autosave", this
@restore()
@field.on "input", => @save()
restore: ->
return unless window.localStorage?
try
text = window.localStorage.getItem @key
2016-06-02 11:05:42 +05:30
catch e
2015-04-26 12:48:37 +05:30
return
@field.val text if text?.length > 0
2016-06-02 11:05:42 +05:30
@field.trigger "input"
2015-04-26 12:48:37 +05:30
save: ->
return unless window.localStorage?
text = @field.val()
if text?.length > 0
try
window.localStorage.setItem @key, text
else
@reset()
reset: ->
return unless window.localStorage?
2016-06-02 11:05:42 +05:30
try
2015-04-26 12:48:37 +05:30
window.localStorage.removeItem @key