Only show events w/ descriptions in EventPalette

Also, auto-generate human readable event name in editor. This is a good example of how we could do this automatically for some kind of `onCommand`, `command`, `onInteractiveEvent` method that combines the event name, documentation string, and handler in one shot.
This commit is contained in:
Corey Johnson & Nathan Sobo
2012-10-19 11:24:40 -06:00
parent 2b8c946a65
commit de3bbce29f
8 changed files with 61 additions and 32 deletions

View File

@@ -155,10 +155,14 @@ class Editor extends View
'editor:toggle-line-comments': @toggleLineCommentsInSelection
'editor:log-cursor-scope': @logCursorScope
documentation = {}
for name, method of editorBindings
documentation[name] = _.humanizeEventName(name)
do (name, method) =>
@on name, => method.call(this); false
@document(documentation)
getCursor: (index) -> @activeEditSession.getCursor(index)
getCursors: -> @activeEditSession.getCursors()
getLastCursor: -> @activeEditSession.getLastCursor()

View File

@@ -13,7 +13,7 @@ class EventPalette extends SelectList
@viewClass: ->
"#{super} event-palette"
filterKey: 0 # filter on the event name for now
filterKey: 'eventDescription'
initialize: (@rootView) ->
@on 'event-palette:toggle', => @cancel()
@@ -21,29 +21,22 @@ class EventPalette extends SelectList
attach: ->
@previouslyFocusedElement = $(':focus')
@setArray(@previouslyFocusedElement.events())
events = []
for eventName, eventDescription of @previouslyFocusedElement.events()
events.push({eventName, eventDescription}) if eventDescription
@setArray(events)
@appendTo(@rootView)
@miniEditor.setText('')
@miniEditor.focus()
itemForElement: ([eventName, description]) ->
itemForElement: ({eventName, eventDescription}) ->
$$ ->
@li class: 'event', =>
@li class: 'event', 'data-event-name': eventName, =>
@div eventDescription, class: 'event-description'
@div eventName, class: 'event-name'
@div description, class: 'event-description'
@div class: 'clear-float'
populateEventList: ->
events = @previouslyFocusedElement.events()
table = $$ ->
@table =>
for [event, description] in events
@tr class: 'event', =>
@td event, class: 'event-name'
@td description if description
@eventList.html(table)
confirmed: ([eventName, description]) ->
confirmed: ({eventName}) ->
@cancel()
@previouslyFocusedElement.trigger(eventName)

View File

@@ -6,3 +6,13 @@
.event-palette ol {
max-height: 300px;
}
.event-palette ol .event-description {
float: left;
display: inline-block;
}
.event-palette ol .event-name {
float: right;
display: inline-block;
}

View File

@@ -56,10 +56,12 @@ $.fn.document = (eventDescriptions) ->
$.fn.events = ->
documentation = @data('documentation') ? {}
events = _.keys(@data('events') ? {}).map (eventName) ->
_.compact([eventName, documentation[eventName]])
events = {}
for eventName of @data('events') ? {}
events[eventName] = documentation[eventName] ? null
if @hasParent()
events.concat(@parent().events())
_.extend(@parent().events(), events)
else
events

View File

@@ -51,3 +51,14 @@ _.mixin
# even though only some strictly require it when inside of []
regex = RegExp('[' + specials.join('\\') + ']', 'g')
string.replace(regex, "\\$&");
humanizeEventName: (eventName) ->
if /:/.test(eventName)
[namespace, name] = eventName.split(':')
return "#{@humanizeEventName(namespace)}: #{@humanizeEventName(name)}"
words = eventName.split('-')
words.map(_.capitalize).join(' ')
capitalize: (word) ->
word[0].toUpperCase() + word[1..]