events.PasteHandler Extends goog.events.EventTarget
A paste event detector. Gets an element as parameter and fires goog.events.PasteHandler.EventType.PASTE events when text is pasted in the element. Uses heuristics to detect paste events in FF2. See more details of the heuristic on #handleEvent_.

Inheritance

Constructor

goog.events.PasteHandler(element)

Parameters

element : Element
The textarea element we are listening on.

Instance Methods

Public Protected Private
checkUpdatedText_() boolean
Checks whether the element.value property was updated, and if so, dispatches the event that let clients know that the text is available.
Returns: boolean  Whether the polling should stop or not, based on whether we found a text change or not.
code »
dispatch_(e)
Dispatches the paste event.
Arguments:
e : goog.events.BrowserEvent
The underlying browser event.
code »
disposeInternal()
No description.
code »
getEventHandler() goog.events.EventHandler.<T>
Returns the event handler.
Returns: goog.events.EventHandler.<T>  The event handler.
code »
getState() goog.events.PasteHandler.State
Returns the current state of the paste detection algorithm. Used mostly for testing.
Returns: goog.events.PasteHandler.State  The current state of the class.
code »
handleEvent_(e)
The main event handler which implements a state machine. To handle FF2, we enumerate and cover all the known ways a user can paste: 1) ctrl+v, shift+insert, cmd+v 2) right click -> paste 3) edit menu -> paste 4) drag and drop 5) middle click (1) is easy and can be detected by listening for key events and finding out which keys are pressed. (2), (3), (4) and (5) do not generate a key event, so we need to listen for more than that. (2-5) all generate 'input' events, but so does key events. So we need to have some sort of 'how did the input event was generated' history algorithm. (2) is an interesting case in Opera on a Mac: since Macs does not have two buttons, right clicking involves pressing the CTRL key. Even more interesting is the fact that opera does NOT set the e.ctrlKey bit. Instead, it sets e.keyCode = 0. http://www.quirksmode.org/js/keys.html (1) is also an interesting case in Opera on a Mac: Opera is the only browser covered by this class that can detect the cmd key (FF2 can't apparently). And it fires e.keyCode = 17, which is the CTRL key code. http://www.quirksmode.org/js/keys.html NOTE(user, pbarry): There is an interesting thing about (5): on Linux, (5) pastes the last thing that you highlighted, not the last thing that you ctrl+c'ed. This code will still generate a PASTE event though. We enumerate all the possible steps a user can take to paste text and we implemented the transition between the steps in a state machine. The following is the design of the state machine: matching paths: (1) happens on INIT -> FOCUSED -> TYPING -> [e.ctrlKey & e.keyCode = 'v'] (2-3) happens on INIT -> FOCUSED -> [input event happened] (4) happens on INIT -> [mouseover && text changed] non matching paths: user is typing normally INIT -> FOCUS -> TYPING -> INPUT -> INIT
Arguments:
e : goog.events.BrowserEvent
The underlying browser event.
code »
handleUnderFocused_(e)
goog.events.PasteHandler.EventType.FOCUSED is typically the second state the textarea will be, which is followed by the INIT state. On this state, users can paste in three different ways: edit -> paste, right click -> paste and drag and drop. The latter will generate a goog.events.EventType.MOUSEOVER event, which we match by making sure the textarea text changed. The first two will generate an 'input', which we match by making sure it was NOT generated by a key event (which also generates an 'input' event). Unfortunately, in Firefox, if you type fast, some KEYDOWN events are swallowed but an INPUT event may still happen. That means we need to differentiate between two consecutive INPUT events being generated either by swallowed key events OR by a valid edit -> paste -> edit -> paste action. We do this by checking a minimum time between the two events. This heuristic seems to work well, but it is obviously a heuristic :).
Arguments:
e : goog.events.BrowserEvent
The underlying browser event.
code »
handleUnderInit_(e)
goog.events.PasteHandler.EventType.INIT is the first initial state the textarea is found. You can only leave this state by setting focus on the textarea, which is how users will input text. You can also paste things using drag and drop, which will not generate a goog.events.EventType.FOCUS event, but will generate a goog.events.EventType.MOUSEOVER. For browsers that support the 'paste' event, we match it and stay on the same state.
Arguments:
e : goog.events.BrowserEvent
The underlying browser event.
code »
handleUnderTyping_(e)
goog.events.PasteHandler.EventType.TYPING is the third state this class can be. It exists because each KEYPRESS event will ALSO generate an INPUT event (because the textarea value changes), and we need to differentiate between an INPUT event generated by a key event and an INPUT event generated by edit -> paste actions. This is the state that we match the ctrl+v pattern.
Arguments:
e : goog.events.BrowserEvent
The underlying browser event.
code »
addEventListener(typeopt_captureopt_handlerScope)
Use #listen instead, when possible. Otherwise, use goog.events.listen if you are passing Object (instead of Function) as handler. Adds an event listener to the event target. The same handler can only be added once per the type. Even if you add the same handler multiple times using the same type then it will only be called once when the event is dispatched.
Arguments:
type : string
The type of the event to listen for.
: ?function():? | ?{handleEvent:function():?
No description.
opt_capture : boolean=
In DOM-compliant browsers, this determines whether the listener is fired during the capture or bubble phase of the event.
opt_handlerScope : Object=
Object in whose scope to call the listener.
code »
assertInitialized_()
Asserts that the event target instance is initialized properly.
code »
dispatchEvent()
No description.
code »
disposeInternal()
Removes listeners from this object. Classes that extend EventTarget may need to override this method in order to remove references to DOM Elements and additional listeners.
code »
fireListeners()
No description.
code »
getListener()
No description.
code »
getListeners()
No description.
code »
getParentEventTarget() goog.events.EventTarget
Returns the parent of this event target to use for bubbling.
Returns: goog.events.EventTarget  The parent EventTarget or null if there is no parent.
code »
hasListener()
No description.
code »
listen()
No description.
code »
listenOnce()
No description.
code »
removeAllListeners()
No description.
code »
removeEventListener(typeopt_captureopt_handlerScope)
Use #unlisten instead, when possible. Otherwise, use goog.events.unlisten if you are passing Object (instead of Function) as handler. Removes an event listener from the event target. The handler must be the same object as the one added. If the handler has not been added then nothing is done.
Arguments:
type : string
The type of the event to listen for.
: ?function():? | ?{handleEvent:function():?
No description.
opt_capture : boolean=
In DOM-compliant browsers, this determines whether the listener is fired during the capture or bubble phase of the event.
opt_handlerScope : Object=
Object in whose scope to call the listener.
code »
setParentEventTarget(parent)
Sets the parent of this event target to use for capture/bubble mechanism.
Arguments:
parent : goog.events.EventTarget
Parent listenable (null if none).
code »
setTargetForTesting(target)
Sets the target to be used for event.target when firing event. Mainly used for testing. For example, see goog.testing.events.mixinListenable.
Arguments:
target : !Object
The target.
code »
unlisten()
No description.
code »
unlistenByKey()
No description.
code »
addOnDisposeCallback(callbackopt_scope)
Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added.
Arguments:
callback : function(this:T):?
The callback function.
opt_scope : T=
An optional scope to call the callback in.
code »
dispose() void
Disposes of the object. If the object hasn't already been disposed of, calls #disposeInternal. Classes that extend goog.Disposable should override #disposeInternal in order to delete references to COM objects, DOM nodes, and other disposable objects. Reentrant.
Returns: void  Nothing.
code »
disposeInternal()
Deletes or nulls out any references to COM objects, DOM nodes, or other disposable objects. Classes that extend goog.Disposable should override this method. Not reentrant. To avoid calling it twice, it must only be called from the subclass' disposeInternal method. Everywhere else the public dispose method must be used. For example:
  mypackage.MyClass = function() {
    mypackage.MyClass.base(this, 'constructor');
    // Constructor logic specific to MyClass.
    ...
  };
  goog.inherits(mypackage.MyClass, goog.Disposable);

  mypackage.MyClass.prototype.disposeInternal = function() {
    // Dispose logic specific to MyClass.
    ...
    // Call superclass's disposeInternal at the end of the subclass's, like
    // in C++, to avoid hard-to-catch issues.
    mypackage.MyClass.base(this, 'disposeInternal');
  };
code »
getDisposed() boolean
Use #isDisposed instead. No description.
Returns: boolean  Whether the object has been disposed of.
code »
isDisposed() boolean
No description.
Returns: boolean  Whether the object has been disposed of.
code »
registerDisposable(disposable)
Associates a disposable object with this object so that they will be disposed together.
Arguments:
disposable : goog.disposable.IDisposable
that will be disposed when this object is disposed.
code »

Instance Properties

constructor :
No description.
Code »
ConditionalDelay used to poll for changes in the text element once users paste text. Browsers fire paste events BEFORE the text is actually present in the element.value property.
Code »
element_ :
The element that you want to listen for paste events on.
Code »
eventHandler_ : goog.events.EventHandler
Handler for events.
Code »
lastTime_ :
The last time an event occurred on the element. Kept to check whether the last event was generated by two input events or by multiple fast key events that got swallowed. See more details on #handleEvent_.
Code »
logger_ : goog.debug.Logger
A logger, used to help us debug the algorithm.
Code »
oldValue_ :
The last known value of the element. Kept to check if things changed. See more details on #handleEvent_.
Code »
previousEvent_ :
The previous event that caused us to be on the current state.
Code »
state_ :
The initial state of the paste detection algorithm.
Code »
actualEventTarget_ : goog.events.EventTarget
The object to use for event.target. Useful when mixing in an EventTarget to another object.
Code »
constructor :
No description.
Code »
eventTargetListeners_ : goog.events.ListenerMap
Maps of event type to an array of listeners.
Code »
parentEventTarget_ : goog.events.EventTarget
Parent event target, used during event bubbling. TODO(user): Change this to goog.events.Listenable. This currently breaks people who expect getParentEventTarget to return goog.events.EventTarget.
Code »
creationStack :
If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.
Code »
disposed_ :
Whether the object has been disposed of.
Code »
onDisposeCallbacks_ :
Callbacks to invoke when this object is disposed.
Code »

Static Properties

goog.events.PasteHandler.MANDATORY_MS_BETWEEN_INPUT_EVENTS_TIE_BREAKER :
The mandatory delay we expect between two input events, used to differentiated between non key paste events and key events.
Code »
goog.events.PasteHandler.PASTE_POLLING_PERIOD_MS_ :
The period between each time we check whether the pasted text appears in the text element or not.
Code »
goog.events.PasteHandler.PASTE_POLLING_TIMEOUT_MS_ :
The maximum amount of time we want to poll for changes.
Code »
goog.events.PasteHandler.superClass_ :
No description.
Code »

Enumerations

goog.events.PasteHandler.EventType :
The types of events fired by this class.
Constants:
AFTER_PASTE
No description.
PASTE
No description.
Code »
goog.events.PasteHandler.State :
The states that this class can be found, on the paste detection algorithm.
Constants:
FOCUSED
No description.
INIT
No description.
TYPING
No description.
Code »

Package events

Package Reference