2015-02-06 10:21:26 -05:00
|
|
|
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?
|
|
|
|
|
2015-03-06 06:50:35 -05:00
|
|
|
try
|
|
|
|
text = window.localStorage.getItem @key
|
2016-02-26 07:22:35 -05:00
|
|
|
catch e
|
2015-03-06 06:50:35 -05:00
|
|
|
return
|
|
|
|
|
2015-02-06 10:21:26 -05:00
|
|
|
@field.val text if text?.length > 0
|
2016-02-26 07:22:35 -05:00
|
|
|
@field.trigger "input"
|
2015-02-06 10:21:26 -05:00
|
|
|
|
|
|
|
save: ->
|
|
|
|
return unless window.localStorage?
|
|
|
|
|
|
|
|
text = @field.val()
|
|
|
|
if text?.length > 0
|
2015-03-06 06:50:35 -05:00
|
|
|
try
|
|
|
|
window.localStorage.setItem @key, text
|
2015-02-06 10:21:26 -05:00
|
|
|
else
|
|
|
|
@reset()
|
|
|
|
|
|
|
|
reset: ->
|
|
|
|
return unless window.localStorage?
|
|
|
|
|
2016-02-26 07:22:35 -05:00
|
|
|
try
|
2015-03-06 06:50:35 -05:00
|
|
|
window.localStorage.removeItem @key
|