persistence, but it's too magic

This commit is contained in:
Corey Johnson
2011-09-15 16:53:18 -07:00
parent 8011e2fc7c
commit 7347f4c8a4
2 changed files with 60 additions and 41 deletions

View File

@@ -16,58 +16,59 @@ class Project extends Pane
keymap:
'Command-Ctrl-N': 'toggle'
persistantProperties:
'openedPaths' : []
initialize: ->
@reload(File.workingDirectory())
@editor = activeWindow.document
window.x = @
@editor.ace.on 'open', ({filename}) =>
if File.isDirectory filename
@reload filename
else
openedPaths = @get 'openedPaths', []
if not _.include openedPaths, filename
openedPaths.push filename
@set 'openedPaths', openedPaths
if not _.include @openedPaths, filename
@openedPaths.push filename
@openedPaths = @openedPaths # How icky, need to do this to store it
@editor.ace.on 'close', ({filename}) =>
if File.isFile filename
openedPaths = _.without @get('openedPaths', []), filename
@set 'openedPaths', openedPaths
@openedPaths = _.without @openedPaths, filename
@editor.ace.on 'loaded', =>
# Reopen files (remove ones that no longer exist)
openedPaths = @get 'openedPaths', []
for path in openedPaths
for path in @openedPaths
if File.isFile path
@editor.open path
else if not File.exists path
openedPaths = _.without(openedPaths, path)
@set "openedPaths", openedPaths
@openedPaths = _.without @openedPaths, path
$('#project li').live 'click', (event) =>
$('#project .active').removeClass 'active'
el = $(event.currentTarget)
path = decodeURIComponent el.attr 'path'
if File.isDirectory path
openedPaths = @get('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.push path unless _.include @openedPaths, path
@openedPaths = @openedPaths # How icky, need to do this to store it
el.addClass 'open'
list = @createList path
el.append list
@set 'openedPaths', openedPaths
else
el.addClass 'active'
activeWindow.open path
false # Don't bubble!
persistentanceNamespace: ->
@.constructor.name + @dir
reload: (dir) ->
@dir = dir
@html.children('#project .cwd').text _.last @dir.split '/'
@@ -78,37 +79,15 @@ class Project extends Pane
createList: (dir) ->
paths = File.list dir
openedPaths = @get('openedPaths', [])
list = $('<ul>')
for path in paths
filename = path.replace(dir, "").substring 1
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'
if _.include(@openedPaths, path) and type == 'dir'
listItem.append @createList path
listItem.addClass "open"
list.append listItem
list
# HATE
# This needs to be replaced with a more generalized method like
# Atomicity.store or better yet, add it to Pane so each pane has it's
# own namespaced storage
set: (key, value) ->
try
object = JSON.parse localStorage[@dir]
catch error
console.log error
object = {}
if value == undefined then delete object[key] else object[key] = value
localStorage[@dir] = JSON.stringify object
get: (key, defaultValue=null) ->
try
JSON.parse(localStorage[@dir])[key] or defaultValue
catch error
console.log error
defaultValue

View File

@@ -10,7 +10,14 @@ 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
@@ -24,10 +31,38 @@ class Pane
@[method]()
else
console.error "keymap: no '#{method}' method found"
@initialize options
# Override in your subclass
initialize: ->
createPersistentProperty: (property, defaultValue) ->
storedPropertyName = "__" + property + "__"
Object.defineProperty @, property,
get: ->
key = @persistentanceNamespace() + property
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)
toggle: ->
if @showing
@@ -39,3 +74,8 @@ class Pane
activeWindow.addPane this
@showing = not @showing
# Override these in your subclass
initialize: ->
persistentanceNamespace: -> @.constructor.name