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
|
|
|
|
catch
|
|
|
|
return
|
|
|
|
|
2015-02-06 10:21:26 -05:00
|
|
|
@field.val text if text?.length > 0
|
|
|
|
@field.trigger "input"
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
2015-03-06 06:50:35 -05:00
|
|
|
try
|
|
|
|
window.localStorage.removeItem @key
|