Y.DataTable.BaseCellPopupEditor Class
This module defines an extension of View that includes a BASE Y.DataTable.BaseCellPopupEditor View class definition cell "pop-up" editor. This view class includes an editor with HTML inserted into an Overlay widget directly over the TD cell. Positioning, event management, creation/destruction and attribute changes are managed by this class.
Configuration
Y.DataTable.BaseCellPopupEditor by itself just creates an empty Overlay container, and really isn't meant to be used that way. The view class includes a number of attributes that are the key to defining a workable popup editor and it's behaviors.
The Y.DataTable.BaseCellPopupEditor View class communicates with the DataTable via the gallery-datatable-editable module, which sets up invocation and rendering of this View and establishes listeners for View events, thus the DataTable serves as a Controller.
Since the generic View class permits ad-hoc attributes, the implementer can pass many properties in during instantiation that will become available as run-time View attributes.
View Construction / Rendering
HTML content that will be set to the Overlay's bodyContent
is setup via the templateObject and
it's html
property. The base view class uses the YUI Template module, and specifically the Template.Micro module to
build out the HTML (Handlebars format is also available). For many use cases you won't need a fancy "template", and
in fact your templateObject.html may not include any "template parameters" at all -- which is perfectly fine.
Editing / Validation
This editor view creates the template'ed content, and attaches the _inputClass wherever the implementer assigns the class tag. The editor can also be configured to listen to the base view's editorCreated in order to attach or configure a Widget or another UI component within the View container.
Key listeners are provided to detect changes to the first Node within the container with _inputClass set. These keylisteners can be used prohibit invalid keystrokes (via the keyFiltering setting) and to allow validation upon a "save" entry (keyboard RTN stroke) where a validator can be prescribed to allow/disallow changes based upon the overall "value" of the INPUT control.
If the implementer has connected a Widget to this View, the widget should be configured by it's own "selection" mechanism to either call this View's saveEditor orcancelEditor methods to ensure proper saving / closing of the Overlay.
Navigation
The editor provides the capability to navigate from TD cell via key listeners on the following key combinations; * CTRL-arrow keys * TAB goes to RIGHT, SHIFT-TAB goes to left * ESC cancels editing * RTN saves cell
Key navigation can be disabled via the inputKeys attribute set to false
.
When a "key navigation" request is received it is passed to the keyDir as a change
in [row,col] that implementers can listen to "change" events on, to reposition and open editing on the
new relative cell. (NOTE: This view does not reposition, it simply fires a keyDirChange
event.
Events
Several events are fired by this View; which can be listened for and acted upon to achieve differing results. For example, the Y.DataTable.EditorOptions.inlineAC (inline autocompletion editor) listens for the editorCreated event and once received, it configures the autocomplete plugin onto the INPUT node.
Pre-Built Popup Editors
This Module includes several pre-defined editor configurations which are stored within the Y.DataTable.EditorOptions
namespace (presently there are popup editors for "textbox", "textarea", "checkbox", "radio", "dropdown", "autocomplete",
"calendar", "date", "number"). New popup editors can be created and added to this namespace at runtime,
and by defining the BaseViewClass:Y.DataTable.BaseCellPopupEditor
property.
This Y.DataTable.BaseCellinlineEditor class is similar to (and compatible with ) the Y.DataTable.BaseCellPopupEditor in another gallery module. Note that since the "inline" editor uses a simple INPUT[type=text] Node instead of an Overlay the codeline is quite a bit simpler.
The pre-built configuration options are stored in an Object variable Y.DataTable.EditorOptions within the DataTable namespace. The gallery-datatable-editable module uses the Y.DataTable.EditorOptions to create required editor View instances.
For example, the pre-built configuration object for the number popup editor
is stored as Y.DataTable.EditorOptions.number
.
To configure an editor on-the-fly (i.e within a DataTable column definition) just include the configuration object options
within DT's column editorConfig
object, which is Y.merge'ed with the pre-built configs;
// Column definition ... disabling keyfiltering and setting a CSS class
{ key:'firstName',
editor:"text", editorConfig:{ className:'align-right', keyFiltering:null }
}
KNOWN ISSUES:
- In-cell key navigation with scrolling DT's can put the View out of the DT limits, no bounds checking is currently done!
- Some problems have been encountered after "datatable.destroy()" and then immediate re-building of the same DT without a page refresh.
Item Index
Methods
Properties
- _classEditing static
- _classInput static
- _classItem static
- _classOverlay static
- _classOverlayBtn static
- _inputNode static
- _subscr static
- btnTemplate static
- overlay static
- template static
Attributes
Methods
_bindUI
()
private
Method that publishes the custom events and sets bindings for key handling and positioning changes
_clearDOMSelection
()
private
Helper method to clear DOM "selected" text or ranges
NOTE: could probably do this with CSS user-select: none;
, but anyway ...
_createOverlay
()
Y.Overlay
private
Method that creates the Editor's Overlay instance and populates the base content.
Returns:
_createOverlayButtons
-
overlay
Method creates a footer section within the Overlay and adds the buttons entered as the "buttons" config property of "overlayConfig".
Parameters:
-
overlay
Widget
_createTemplateContent
-
overlay
Method used to process the templateObject attribute and generate the
Overlay's bodyContent
. This method uses the Y.Template
module to prepare the HTML, which
is passed in via templateObject's html
property.
This method uses the Y.Template.Micro
module by default to process the template. Implementers
can pass in any different Y.Template supported-template engine via the templateEngine
attribute (i.e. templateEngine: Y.Handlebars
) but will have to modify the html
property of templateObject
appropriately for that engine.
Implementers can pass in "options" and other properties to the templateObject ATTR and
this function will normalize the this.options
to an Object with keys {value,text,title,raw} which can be
used more directly within HTML construction.
The templateObject ATTR options
property can be either an Array or an Object, this function converts and
normalizes the content to an output Array as this.options
within the template definitions.
Parameters:
-
overlay
WidgetOverlay instance for this View
_createUI
()
Y.Overlay
private
Creates this View's container, including instantiating the Overlay widget within the container, incorporating user-supplied overlay configs, creating buttons and creating the internal HTML content within the Overlay (using a Template-based method)
Returns:
_defEditorCancelFn
()
private
The defaultFn for the editorCancel
event
_defEditorSaveFn
-
e
The defaultFn for the editorSave
event
Parameters:
-
e
EventFacadeFor editorSave event
_isZeroOr
-
v
Utility method that checks if a value (include ZERO!!) is defined and not null (there's probably a much better way to do this)
Parameters:
-
v
Number | StringValue to check
Returns:
_mouseLeave
()
private
Listener to mouseleave event that closes the active editor
_setEditorXY
-
e
This method can be used to quickly reset the current View editor's position, used for scrollable DataTables.
Parameters:
-
e
EventFacadeThe xy attribute change event facade
_setInputValue
-
val
Parameters:
-
val
Number | String | Date
Returns:
_unbindUI
()
private
Detaches the listeners that were set on this view, any widgets that were created and on the View's Overlay instance.
cancelEditor
()
public
Called when the user has requested to cancel, and abort any changes to the DT cell, usually signified by a keyboard ESC or "Cancel" button, etc..
destructor
()
protected
hideEditor
-
keep
Hides the current editor View instance. If the optional hideMe
param is true this View will
be temporarily "hidden" (used for scrolling DT's when the TD is scrolled off/on to the page)
Parameters:
-
keep
BooleanKeep visible set to true/false
initializer
()
protected
chainable
Creates the View instance and sets the container and bindings
Returns:
processKeyDown
-
e
Key listener for the INPUT inline editor, "keydown" is checked for non-printing key strokes, navigation or ESC.
This method is intended to overridden by implementers in order to customize behaviors.
Parameters:
-
e
EventFacadeKeydown event facade
processKeyPress
-
e
Provides a method to process keypress entries and validate or prevent invalid inputs. This method is meant to be overrideable by implementers to customize behaviors.
Parameters:
-
e
EventFacadeKey press event object
saveEditor
-
val
Saves the View's value
setting (usually after keyboard RTN or other means) and fires the
editorSave event so consumers (i.e. DataTable) can make final changes to the
Model or dataset.
Thank you to Satyam for his guidance on configuring the event publishing, defaultFn related to this technique!
Parameters:
-
val
String | Number | DateValue to save to the current editor's value attribute
showEditor
-
tar
Method that displays the editor configured for the input TD node, this is done
in lieu of a render
method for this View. If no td
is provided then the
last configured cell attribute's td
property is used.
Parameters:
-
tar
NodeTarget TD cell that editing takes place on
Properties
_classEditing
String
protected
static
CSS classname to identify the individual input collection HTML nodes within the View container
Default: 'editing'
_classInput
String
protected
static
CSS classname to identify the input HTML node within the View container
Default: 'yui3-datatable-editor-input'
_classItem
String
protected
static
CSS classname to identify the individual input collection HTML nodes within the View container
Default: 'yui3-datatable-editor-input-item'
_classOverlay
String
protected
static
CSS classname for the Overlay content within the View container
Default: 'yui3-datatable-editor-overlay'
_classOverlayBtn
String
protected
static
CSS classname used for Overlay BUTTON elements within the View container
Default: 'yui3-datatable-editor-overlay-button'
_inputNode
Node
protected
static
Placeholder for the created Input element contained within the Overlay and View container
Default: null
_subscr
Array of EventHandles
protected
static
Placeholder for listener handles created from this View
Default: []
btnTemplate
String
static
Defines the HTML content "template" for BUTTON elements that are added to the Overlay via the overlayButtons attribute.
Default: See Code
overlay
Widget
static
Placeholder property for the Overlay that is created by this View
Default: null
template
String
static
Defines the HTML content "template" for the containing Overlay of this editor, this property is also set by default to the attribute
Default: See Code
Attributes
cell
Object
A cell reference object populated by the calling DataTable, contains the following key properties:
{td,value,recClientId,colKey}
Default: {}
hostDT
Y.DataTable
Maintains a reference back to the calling DataTable instance (not really used yet)
Default: null
inputKeys
Boolean
A flag to indicate if cell-to-cell navigation should be implemented (currently setup for CTRL-arrow key, TAB and Shift-TAB) capability
Default: true
inputWidth
String | Number
Specifies a width attribute style to set the _classInput
Node element to upon rendering.
Default: null
keyDir
Array as row,col from current TD
Tracks navigation changes during keyboard input as relative [row,col] changes from the currently active cell TD.
Default: []
keyFiltering
RegExp | Function
Provides a keystroke filtering capability to restrict input into the editing area checked during the "keypress" event. This attribute is set to either a RegEx or a function that confirms if the keystroke was valid for this editor. (TRUE meaning valid, FALSE meaning invalid)
If a function is provided, the single argument is the keystroke event facade e
and if
the keystroke is valid it should return true, otherwise if invalid false;
Default: null
Example:
/\d/ // for numeric digit-only input
/\d|\-|\./ // for floating point numeric input
/\d|\// // for Date field entry in MM/DD/YYYY format
lastValue
String | Number | Date
Value that was contained in the cell when the Editor View was initiated
Default: null
name
String
Name for this View, this is useful because the name
attribute is prefixed to the
'Options' string for some Views (i.e. a cell editor named 'myRadio' will have a defined
set of options available of 'myRadioOptions'
Default: null
offsetXY
Array
Sets an offset of the XY coordinates that will be used for positioning the Overlay upon displaying the editor View
Default: [0,0]
overlayConfig
Object
Additional config parameters for the Overlay to be used in constructing the Editor. These configs are merged with the defaults required by the Editor.
Default: {}
overlayWidth
String | Number
Defines the width
parameter to set the Overlay widget to upon rendering, can also be overridden
by setting overlayConfig.width
.
Default: null
prepFn
Function
Function to execute on the "data" contents just prior to displaying in the Editor's main view (i.e. typically used for pre-formatting Date information from JS to mm/dd/YYYY format)
This function will receive one argument "value" which is the data value from the record, and the function runs in Editor scope.
Default: null
saveFn
Function
Function to execute when Editing is complete, prior to "saving" the data to the Record (Model)
This function will receive one argument "value" which is the data value from the INPUT or Widget, and the function runs in Editor scope.
Default: null
saveKeyRTN
Boolean
A flag to signify whether the editor View should be "saved" upon detecting the RTN keystroke within the INPUT area.
For example, textarea typically will not, to allow a newline to be added.
Default: true
template
String
private
Defines the Overlay's HTML template for the overall View (not recommended to change this!!) NOTE: This the Overlay structure template and not the bodyContent template for the Overlay, it is not recommended you change this attr.
Please see the templateObject attribute to define the HTML for your View!
templateEngine
Object
Defines the type of template engine that will be used to parse Templates, (via Y.Template).
Typically this would be set to Y.Template.Micro
or Y.Handlebars
NOTE: If you use Y.Handlebars (or any other YUI template engine) you MUST include it in your YUI.use
loader statement ("template" is included in this module's requires
by default)
Default: Y.Template.Micro
templateObject
Object
This attribute is used to define the HTML content that will be created / generated and inserted within
this View's Y.Overlay. The attribute definitions include an object with the following recognizable
properties: html, xxxOptions, propValue, propText, propTitle
Note that xxxOptions matches the name
attribute (i.e. the editor "name" you include on your column
definitions), where xxx is replaced with the name. For "radio" it is radioOptions
, for "select" it is
selectOptions
, "checkbox" it is checkboxOptions
, etc...
The method _createTemplateContent uses this attribute and processes the
template using the html
and other properties to generate the HTML. It then inserts the compiled HTML into
the Overlay's bodyContent
.
Default: null
Example:
templateObject: {
// set the template definition
html: '<select class="myselect">'
+ '<% Y.Array.each( data.options, function(r){ %>'
+ '<option value="<%= r.value %>" <% (r.title) ? \'title="r.title"\' : %>>'
+ '<%= r.text %></option>'
+ '<% },this); %>'
+ '</select>'
options: states // [ {value:'AZ', text:'Arizona}, {value:'DE', text:'Delaware' } ]
}
validator
RegExp | Function
Provides the capability to validate the final saved value after editing is finished. This attribute can be set to either a RegEx or a function, that operates on the entire "value" setting of the editor input (whereas keyFiltering performs validation checks on each key input).
If a function is provided, the single argument is the value setting of the editor. the keystroke is valid it should return true, otherwise if invalid false;
Default: null
Example:
/\d/ // for numeric digit-only input
/\d|\-|\.|\+/ // for floating point numeric input
/\d|\// // for Date field entry in MM/DD/YYYY format
Events
editorCancel
Fired when editing is cancelled (without saving) on this cell editor
Event Payload:
-
rtn
ObjectReturned object
-
td
NodeTD Node for the edited cell
-
cell
ObjectCurrent cell object
-
oldValue
String | Number | DateData value of this cell prior to editing
-
editorCreated
View event fired when the inline editor has been initialized and ready for usage. This event can be listened to in order to add additional content or widgets, etc onto the View's container.
Event Payload:
-
rtn
ObjectReturned object
-
inputNode
NodeThe created INPUT[text] node, if any
-
container
ObjectThe View container / Overlay instance
-
editorDestroyed
Event fired when the cell editor View is destroyed.
Implementers can listen for this event to check if any sub-components / widgets, etc.. they had created as part of this View need to be destroyed or listener unbound.
editorHide
Fired when the active cell editor is hidden
editorSave
Event that is fired when the user has finished editing the View's cell contents (signified by either a keyboard RTN entry or "Save" button, etc...).
This event is intended to be the PRIMARY means for implementers to know that the editing has been completed and validated. Consumers (i.e. DataTable) should listen to this event and process it's results to save to the Model and or dataset for the DT.
Event Payload:
-
rtn
ObjectReturned object
-
td
NodeTD Node for the edited cell
-
cell
ObjectCurrent cell object
-
oldValue
String | Number | DateData value of this cell prior to editing
-
newValue
String | Number | DateData value of this cell after editing
-
editorShow
Event fired when the cell editor is displayed and becomes visible.
Implementers may listen for this event if they have configured complex View's, that include other widgets or components, to update their UI upon displaying of the view.
Event Payload:
-
rtn
ObjectReturned object
-
inputNode
NodeThe editor's INPUT / TEXTAREA Node
-
value
String | Number | DateThe current "value" setting
-
cell
Objectobject
-
td
NodeTD Node undergoing editing -
recClientId
StringThe active record's "clientId" attribute setting -
colKey
StringThe active column's key or name setting
-
-