Move methods related to item destruction and saving to PaneModel

This commit is contained in:
Nathan Sobo
2014-01-08 11:12:27 -07:00
parent 626e22e4ae
commit a379d47230
2 changed files with 96 additions and 88 deletions

View File

@@ -1,4 +1,5 @@
{find, compact, clone} = require 'underscore-plus'
{dirname} = require 'path'
{Model} = require 'theorist'
Serializable = require 'serializable'
@@ -92,3 +93,86 @@ class PaneModel extends Model
moveItemToPane: (item, pane, index) ->
pane.addItem(item, index)
@removeItem(item, true)
# Public: Remove the currently active item.
destroyActiveItem: ->
@destroyItem(@activeItem)
false
# Public: Remove the specified item.
destroyItem: (item, options) ->
@emit 'before-item-destroyed', item
if @promptToSaveItem(item)
@emit 'item-destroyed', item
@removeItem(item, options)
item.destroy?()
true
else
false
# Public: Remove and delete all items.
destroyItems: ->
@destroyItem(item) for item in @getItems()
# Public: Remove and delete all but the currently focused item.
destroyInactiveItems: ->
@destroyItem(item) for item in @getItems() when item isnt @activeItem
# Public: Prompt the user to save the given item.
promptToSaveItem: (item) ->
return true unless item.shouldPromptToSave?()
uri = item.getUri()
chosen = atom.confirm
message: "'#{item.getTitle?() ? item.getUri()}' has changes, do you want to save them?"
detailedMessage: "Your changes will be lost if you close this item without saving."
buttons: ["Save", "Cancel", "Don't Save"]
switch chosen
when 0 then @saveItem(item, -> true)
when 1 then false
when 2 then true
# Public: Saves the currently focused item.
saveActiveItem: =>
@saveItem(@activeItem)
# Public: Save and prompt for path for the currently focused item.
saveActiveItemAs: =>
@saveItemAs(@activeItem)
# Public: Saves the specified item and call the next action when complete.
saveItem: (item, nextAction) ->
if item.getUri?()
item.save?()
nextAction?()
else
@saveItemAs(item, nextAction)
# Public: Prompts for path and then saves the specified item. Upon completion
# it also calls the next action.
saveItemAs: (item, nextAction) ->
return unless item.saveAs?
itemPath = item.getPath?()
itemPath = dirname(itemPath) if itemPath
path = atom.showSaveDialogSync(itemPath)
if path
item.saveAs(path)
nextAction?()
# Public: Saves all items in this pane.
saveItems: =>
@saveItem(item) for item in @getItems()
# Public: Finds the first item that matches the given uri.
itemForUri: (uri) ->
find @items, (item) -> item.getUri?() is uri
# Public: Focuses the first item that matches the given uri.
showItemForUri: (uri) ->
if item = @itemForUri(uri)
@showItem(item)
true
else
false

View File

@@ -1,6 +1,4 @@
{dirname} = require 'path'
{$, View} = require './space-pen-extensions'
_ = require 'underscore-plus'
Serializable = require 'serializable'
Delegator = require 'delegato'
@@ -28,7 +26,9 @@ class Pane extends View
@delegatesProperties 'items', 'activeItem', toProperty: 'model'
@delegatesMethods 'getItems', 'showNextItem', 'showPreviousItem', 'getActiveItemIndex',
'showItemAtIndex', 'showItem', 'addItem', 'itemAtIndex', 'removeItem', 'removeItemAtIndex',
'moveItem', 'moveItemToPane', toProperty: 'model'
'moveItem', 'moveItemToPane', 'destroyItem', 'destroyItems', 'destroyActiveItem',
'destroyInactiveItems', 'saveActiveItem', 'saveActiveItemAs', 'saveItem', 'saveItemAs',
'saveItems', 'itemForUri', 'showItemForUri', 'promptToSaveItem', toProperty: 'model'
previousActiveItem: null
@@ -48,6 +48,8 @@ class Pane extends View
@subscribe @model, 'item-added', @onItemAdded
@subscribe @model, 'item-removed', @onItemRemoved
@subscribe @model, 'item-moved', @onItemMoved
@subscribe @model, 'before-item-destroyed', @onBeforeItemDestroyed
@subscribe @model, 'item-destroyed', @onItemDestroyed
@subscribe this, 'focus', => @activeView?.focus(); false
@subscribe this, 'focusin', => @makeActive()
@@ -147,95 +149,17 @@ class Pane extends View
onItemMoved: (item, newIndex) =>
@trigger 'pane:item-moved', [item, newIndex]
onBeforeItemDestroyed: (item) =>
@unsubscribe(item) if typeof item.off is 'function'
@trigger 'pane:before-item-destroyed', [item]
onItemDestroyed: (item) =>
@getContainer()?.itemDestroyed(item)
# Private:
activeItemTitleChanged: =>
@trigger 'pane:active-item-title-changed'
# Public: Remove the currently active item.
destroyActiveItem: =>
@destroyItem(@activeItem)
false
# Public: Remove the specified item.
destroyItem: (item, options) ->
@unsubscribe(item) if _.isFunction(item.off)
@trigger 'pane:before-item-destroyed', [item]
if @promptToSaveItem(item)
@getContainer()?.itemDestroyed(item)
@removeItem(item, options)
item.destroy?()
true
else
false
# Public: Remove and delete all items.
destroyItems: ->
@destroyItem(item) for item in @getItems()
# Public: Remove and delete all but the currently focused item.
destroyInactiveItems: ->
@destroyItem(item) for item in @getItems() when item isnt @activeItem
# Public: Prompt the user to save the given item.
promptToSaveItem: (item) ->
return true unless item.shouldPromptToSave?()
uri = item.getUri()
chosen = atom.confirm
message: "'#{item.getTitle?() ? item.getUri()}' has changes, do you want to save them?"
detailedMessage: "Your changes will be lost if you close this item without saving."
buttons: ["Save", "Cancel", "Don't Save"]
switch chosen
when 0 then @saveItem(item, -> true)
when 1 then false
when 2 then true
# Public: Saves the currently focused item.
saveActiveItem: =>
@saveItem(@activeItem)
# Public: Save and prompt for path for the currently focused item.
saveActiveItemAs: =>
@saveItemAs(@activeItem)
# Public: Saves the specified item and call the next action when complete.
saveItem: (item, nextAction) ->
if item.getUri?()
item.save?()
nextAction?()
else
@saveItemAs(item, nextAction)
# Public: Prompts for path and then saves the specified item. Upon completion
# it also calls the next action.
saveItemAs: (item, nextAction) ->
return unless item.saveAs?
itemPath = item.getPath?()
itemPath = dirname(itemPath) if itemPath
path = atom.showSaveDialogSync(itemPath)
if path
item.saveAs(path)
nextAction?()
# Public: Saves all items in this pane.
saveItems: =>
@saveItem(item) for item in @getItems()
# Public: Finds the first item that matches the given uri.
itemForUri: (uri) ->
_.detect @items, (item) -> item.getUri?() is uri
# Public: Focuses the first item that matches the given uri.
showItemForUri: (uri) ->
if item = @itemForUri(uri)
@showItem(item)
true
else
false
# Private:
cleanupItemView: (item, detach) ->
if item instanceof $