A simple storage scheme.

This commit is contained in:
Corey Johnson
2011-09-16 16:01:13 -07:00
parent 39080d0132
commit 64dafdfad9
2 changed files with 33 additions and 50 deletions

View File

@@ -16,9 +16,6 @@ class Project extends Pane
keymap:
'Command-Ctrl-N': 'toggle'
persistantProperties:
'openedPaths' : []
initialize: ->
@reload(File.workingDirectory())
@editor = activeWindow.document
@@ -29,44 +26,51 @@ class Project extends Pane
if File.isDirectory filename
@reload filename
else
if not _.include @openedPaths, filename
@openedPaths.push filename
@openedPaths = @openedPaths # How icky, need to do this to store it
openedPaths = @storage('openedPaths') ? []
if not _.include openedPaths, filename
openedPaths.push filename
@storage('openedPaths', openedPaths)
@editor.ace.on 'close', ({filename}) =>
if File.isFile filename
@openedPaths = _.without @openedPaths, filename
openedPaths = @storage('openedPaths') ? []
openedPaths = _.without openedPaths, filename
@storage('openedPaths', openedPaths)
@editor.ace.on 'loaded', =>
# Reopen files (remove ones that no longer exist)
for path in @openedPaths
openedPaths = @storage('openedPaths') ? []
for path in openedPaths
if File.isFile path
@editor.open path
else if not File.exists path
@openedPaths = _.without @openedPaths, path
openedPaths = _.without openedPaths, path
@storage('openedPaths', openedPaths)
$('#project li').live 'click', (event) =>
$('#project .active').removeClass 'active'
el = $(event.currentTarget)
path = decodeURIComponent el.attr 'path'
if File.isDirectory path
openedPaths = @storage('openedPaths') ? []
if el.hasClass 'open'
@openedPaths = _.without @openedPaths, path
openedPaths = _.without openedPaths, path
el.removeClass 'open'
el.children("ul").remove()
else
@openedPaths.push path unless _.include @openedPaths, path
@openedPaths = @openedPaths # How icky, need to do this to store it
openedPaths.push path unless _.include openedPaths, path
el.addClass 'open'
list = @createList path
el.append list
@storage('openedPaths', openedPaths)
else
el.addClass 'active'
activeWindow.open path
false # Don't bubble!
persistentanceNamespace: ->
storageNamespace: ->
@.constructor.name + @dir
reload: (dir) ->
@@ -85,7 +89,8 @@ class Project extends Pane
type = if File.isDirectory path then 'dir' else 'file'
encodedPath = encodeURIComponent path
listItem = $("<li class='#{type}' path='#{encodedPath}'>#{filename}</li>")
if _.include(@openedPaths, path) and type == 'dir'
openedPaths = @storage('openedPaths') ? []
if _.include(openedPaths, path) and type == 'dir'
listItem.append @createList path
listItem.addClass "open"
list.append listItem

View File

@@ -10,14 +10,7 @@ class Pane
keymap: {}
persistantProperties: {}
editableProperties: {}
constructor: (options={}) ->
@createPersistentProperty(k, v) for k, v of @persistantProperties
@createPersistentProperty(k, v) for k, v of @editableProperties
for option, value of options
@[option] = value
@@ -34,35 +27,20 @@ class Pane
@initialize options
createPersistentProperty: (property, defaultValue) ->
storedPropertyName = "__" + property + "__"
Object.defineProperty @, property,
get: ->
key = @persistentanceNamespace() + property
storage: (key, value) ->
try
object = JSON.parse(localStorage[@storageNamespace()] ? "{}")
catch error
error.message += "\n#{key}: #{value}"
console.log(error)
if @[storedPropertyName]
# Cool, just chill for awhile
else if localStorage[key]
try
@[storedPropertyName] = JSON.parse(localStorage[key] ? "null")
catch error
@[storedPropertyName] = defaultValue
error.message += "\n#{key}: #{JSON.stringify localStorage[key]}"
console.log(error)
else
@[storedPropertyName] = defaultValue
return @[storedPropertyName]
set: (value) ->
key = @persistentanceNamespace() + property
try
@[storedPropertyName] = value
localStorage[key] = JSON.stringify value
catch error
error.message += "\n value = #{JSON.stringify value}"
console.log(error)
if value?
# Putting data in
object[key] = value
localStorage[@storageNamespace()] = JSON.stringify(object)
else
# Getting data out
object[key]
toggle: ->
if @showing
@@ -78,4 +56,4 @@ class Pane
# Override these in your subclass
initialize: ->
persistentanceNamespace: -> @.constructor.name
storageNamespace: -> @.constructor.name