mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Merge pull request #11057 from atom/mkt-improve-pane-add-item-options
Move Pane::addItem 'pending' option to options object
(cherry picked from commit 53a9c22554)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
Grim = require 'grim'
|
||||
{find, compact, extend, last} = require 'underscore-plus'
|
||||
{CompositeDisposable, Emitter} = require 'event-kit'
|
||||
Model = require './model'
|
||||
@@ -394,30 +395,42 @@ class Pane extends Model
|
||||
# Public: Make the given item *active*, causing it to be displayed by
|
||||
# the pane's view.
|
||||
#
|
||||
# * `pending` (optional) {Boolean} indicating that the item should be added
|
||||
# in a pending state if it does not yet exist in the pane. Existing pending
|
||||
# items in a pane are replaced with new pending items when they are opened.
|
||||
activateItem: (item, pending=false) ->
|
||||
# * `options` (optional) {Object}
|
||||
# * `pending` (optional) {Boolean} indicating that the item should be added
|
||||
# in a pending state if it does not yet exist in the pane. Existing pending
|
||||
# items in a pane are replaced with new pending items when they are opened.
|
||||
activateItem: (item, options={}) ->
|
||||
if item?
|
||||
if @getPendingItem() is @activeItem
|
||||
index = @getActiveItemIndex()
|
||||
else
|
||||
index = @getActiveItemIndex() + 1
|
||||
@addItem(item, index, false, pending)
|
||||
@addItem(item, extend({}, options, {index: index}))
|
||||
@setActiveItem(item)
|
||||
|
||||
# Public: Add the given item to the pane.
|
||||
#
|
||||
# * `item` The item to add. It can be a model with an associated view or a
|
||||
# view.
|
||||
# * `index` (optional) {Number} indicating the index at which to add the item.
|
||||
# If omitted, the item is added after the current active item.
|
||||
# * `pending` (optional) {Boolean} indicating that the item should be
|
||||
# added in a pending state. Existing pending items in a pane are replaced with
|
||||
# new pending items when they are opened.
|
||||
# * `options` (optional) {Object}
|
||||
# * `index` (optional) {Number} indicating the index at which to add the item.
|
||||
# If omitted, the item is added after the current active item.
|
||||
# * `pending` (optional) {Boolean} indicating that the item should be
|
||||
# added in a pending state. Existing pending items in a pane are replaced with
|
||||
# new pending items when they are opened.
|
||||
#
|
||||
# Returns the added item.
|
||||
addItem: (item, index=@getActiveItemIndex() + 1, moved=false, pending=false) ->
|
||||
addItem: (item, options={}) ->
|
||||
# Backward compat with old API:
|
||||
# addItem(item, index=@getActiveItemIndex() + 1)
|
||||
if typeof options is "number"
|
||||
Grim.deprecate("Pane::addItem(item, #{options}) is deprecated in favor of Pane::addItem(item, {index: #{options}})")
|
||||
options = index: options
|
||||
|
||||
index = options.index ? @getActiveItemIndex() + 1
|
||||
moved = options.moved ? false
|
||||
pending = options.pending ? false
|
||||
|
||||
throw new Error("Pane items must be objects. Attempted to add item #{item}.") unless item? and typeof item is 'object'
|
||||
throw new Error("Adding a pane item with URI '#{item.getURI?()}' that has already been destroyed") if item.isDestroyed?()
|
||||
|
||||
@@ -437,7 +450,7 @@ class Pane extends Model
|
||||
@setPendingItem(item) if pending
|
||||
|
||||
@emitter.emit 'did-add-item', {item, index, moved}
|
||||
@destroyItem(lastPendingItem) if lastPendingItem?
|
||||
@destroyItem(lastPendingItem) if lastPendingItem? and not moved
|
||||
@setActiveItem(item) unless @getActiveItem()?
|
||||
item
|
||||
|
||||
@@ -467,7 +480,7 @@ class Pane extends Model
|
||||
# Returns an {Array} of added items.
|
||||
addItems: (items, index=@getActiveItemIndex() + 1) ->
|
||||
items = items.filter (item) => not (item in @items)
|
||||
@addItem(item, index + i, false) for item, i in items
|
||||
@addItem(item, {index: index + i}) for item, i in items
|
||||
items
|
||||
|
||||
removeItem: (item, moved) ->
|
||||
@@ -516,7 +529,7 @@ class Pane extends Model
|
||||
# given pane.
|
||||
moveItemToPane: (item, pane, index) ->
|
||||
@removeItem(item, true)
|
||||
pane.addItem(item, index, true)
|
||||
pane.addItem(item, {index: index, moved: true})
|
||||
|
||||
# Public: Destroy the active item and activate the next item.
|
||||
destroyActiveItem: ->
|
||||
|
||||
@@ -509,7 +509,7 @@ class Workspace extends Model
|
||||
return item if pane.isDestroyed()
|
||||
|
||||
@itemOpened(item)
|
||||
pane.activateItem(item, options.pending) if activateItem
|
||||
pane.activateItem(item, {pending: options.pending}) if activateItem
|
||||
pane.activate() if activatePane
|
||||
|
||||
initialLine = initialColumn = 0
|
||||
|
||||
Reference in New Issue
Block a user