Permalink
Browse files

gallery-2010.09.08-19-45 apipkin gallery-plugin-simple-datatable-row-…

…select
  • Loading branch information...
YUI Builder
YUI Builder committed Sep 8, 2010
1 parent a33d74e commit d0332a9a3596d5d81405e9b3f6117c56df618c41
@@ -0,0 +1,27 @@
+# Simple Datatable Row Select Plugin Build Properties
+
+# As long as the 'builder' project is cloned to the default folder
+# next to the 'yui3-gallery' project folder, the 'builddir' property does not
+# need to be changed
+#
+# If the 'builder' project is checked out to an alternate location, this
+# property should be updated to point to the checkout location.
+builddir=../../../builder/componentbuild
+
+# The name of the component. E.g. event, attribute, widget
+component=gallery-plugin-simple-datatable-row-select
+
+# The list of files which should be concatenated to create the component.
+# NOTE: For a css component (e.g. cssfonts, cssgrids etc.) use component.cssfiles instead.
+# component.jsfiles=plugin-simple-datatable-row-select.js, plugin-simple-datatable-row-selectHelperClass.js, plugin-simple-datatable-row-selectSubComponentClass.js
+component.jsfiles=plugin-simple-datatable-row-select.js
+
+# The list of modules this component requires. Used to set up the Y.add module call for YUI 3.
+component.requires=plugin, collection
+
+# The list of modules this component supersedes. Used to set up the Y.add module call for YUI 3.
+component.supersedes=
+
+# The list of modules that are optional for this module. Used to set up the Y.add module call for YUI 3.
+component.optional=
+
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- YUI 3 Gallery Component Build File -->
+<project name="Simple Datatable Row Select Plugin" default="local">
+ <description>Simple Datatable Row Select Plugin Build File</description>
+ <property file="build.properties" />
+ <import file="${builddir}/3.x/bootstrap.xml" description="Default Build Properties and Targets" />
+</project>
@@ -0,0 +1,211 @@
+
+var SELECTED_ROWS = 'selectedRows',
+ EVENTS = {
+ ROW_SELECTED : 'rowSelected'
+ };
+
+ /**
+ * Adds row select functionality to Simple Datatable
+ *
+ * @class SDTRowSelect
+ * @extends Plugin.Base
+ * @version 1.3.0
+ */
+Y.Plugin.SDTRowSelect = Y.Base.create('simple-datatable-row-select', Y.Plugin.Base, [], {
+
+ // P U B L I C //
+ _host : null,
+ /**
+ * Classname of the widget. Used to prevent multiple look ups
+ * @since 1.0.0
+ */
+ className : null,
+
+ /**
+ * Provides a reference to the table body
+ * @since 1.0.0
+ */
+ tBody : null,
+
+ _delegateEnter : null,
+ _delegateLeave : null,
+ _delegateClick : null,
+
+
+ /**
+ * Binds event to host render for setup kickoff. Binds to host
+ * rowsUpdated to reselect the row after changes
+ * @since 1.0.0
+ * @public
+ * @method initializer
+ */
+ initializer : function() {
+ Y.log('initializer','info','plugin-row-select');
+ this._host = this.get('host');
+ this._host.get('boundingBox').addClass(this._host.getClassName('row-select'));
+ this.afterHostMethod('setRows', this._afterHostSetRows);
+ if(this._host.rowsSet) {
+ this._afterHostSetRows();
+ }
+ this.afterHostEvent('render', this._setup);
+ },
+
+ /**
+ * House keeping after a we are unplugged
+ *
+ */
+ destructor : function() {
+ Y.log('destructor', 'info', 'plugin-row-select');
+ var rowSelectClass = this._host.getClassName('row-select'),
+ overClass = this._host.getClassName('over');
+
+ this._host.get('boundingBox').removeClass(rowSelectClass);
+ this._delegateEnter.detach();
+ this._delegateLeave.detach();
+ this._delegateClick.detach();
+
+ this._host.get('boundingBox').all('.' + overClass).removeClass(overClass);
+
+ this.get('selectedRows').each(function(row) {
+ this._unselectRow(row);
+ }, this);
+ },
+
+ /**
+ * Returns the index in the array list of the row. Returns -1 if
+ * the row is not found
+ * @since 1.0.0
+ * @public
+ * @method isSelected
+ * @param row Y.Node
+ */
+ isSelected : function(row) {
+ Y.log('isSelected','info','plugin-row-select');
+ return this.get(SELECTED_ROWS).indexOf(row);
+ },
+
+ // P R O T E C T E D //
+ /**
+ * Publishes rowSelected event. Delegates mouse events to rows.
+ * @since 1.0.0
+ * @protected
+ * @method _setup
+ */
+ _setup : function() {
+ Y.log('_setup','info','plugin-row-select');
+ var cb = this._host.get('contentBox'),
+ overClass;
+ this.className = this.get('host').getClassName();
+ this.publish(EVENTS.ROW_SELECTED, {defaultFn: this._defSelectedFn});
+
+ overClass = this.className + '-over';
+
+ // allow for row highlight
+ this._delegateEnter = cb.delegate('mouseenter', function(e){
+ e.currentTarget.addClass(overClass);
+ },'tbody tr:not([selectable="false"])');
+
+ this._delegateLeave = cb.delegate('mouseleave', function(e){
+ e.currentTarget.removeClass(overClass);
+ },'tbody tr:not([selectable="false"])');
+
+ // update selected row
+ this._delegateClick = cb.delegate('click', function(e){
+ this.fire(EVENTS.ROW_SELECTED, {rowTarget: e.currentTarget});
+ },'tbody tr:not([selectable="false"])',this);
+ },
+
+ /**
+ * Default function when a row is selected. When selected row is
+ * clicked again, removes the row from being selected.
+ * @since 1.0.0
+ * @protected
+ * @method _defSelectedFn
+ * @param e Event
+ */
+ _defSelectedFn : function(e) {
+ Y.log('_defSelectedFn','info','plugin-row-select');
+
+ if(this.isSelected(e.rowTarget) > -1 && this.get('toggleSelect')) {
+ this._unselectRow(e.rowTarget);
+ }else{
+ this._selectRow(e.rowTarget);
+ }
+ },
+
+ /**
+ * Called when the host rows data has changed
+ * @since 1.0.0
+ * @protected
+ * @method _afterHostRowsChanged
+ * @param e Event
+ */
+ _afterHostSetRows : function(e) {
+ Y.log('_afterHostSetRows','info','plugin-row-select');
+ var discoveredRow = null,
+ currentRows = this.get(SELECTED_ROWS);
+
+ // empty the array list
+ this.set(SELECTED_ROWS, new Y.ArrayList() );
+
+ // update tbody
+ this.tBody = this._host.get('contentBox').one('tbody');
+
+ // loop through rows and reselect it if it's present
+ currentRows.each(function(row) {
+ discoveredRow = this.tBody.one('#' + row.get('id'));
+ if(discoveredRow) {
+ this._selectRow(discoveredRow);
+ }
+ discoveredRow = null;
+ },this);
+ },
+
+ /**
+ * Adds the row to the selectedRows list. If multiSelect is false,
+ * removes all other rows first.
+ * @since 1.0.0
+ * @see _unselectRow
+ * @protected
+ * @method _selectRow
+ * @param row Y.Node
+ */
+ _selectRow : function(row) {
+ Y.log('_selectRow','info','plugin-row-select');
+ if(!this.get('multiSelect')) {
+ this.get(SELECTED_ROWS).each(function(item) {
+ this._unselectRow(item);
+ }, this);
+ }
+ this.get(SELECTED_ROWS).add(row);
+ row.addClass(this.className + '-selected');
+ },
+
+ /**
+ * Removes row from the selectedRows list.
+ * @since 1.0.0
+ * @protected
+ * @method _selectRow
+ * @param row Y.Node
+ */
+ _unselectRow : function(row) {
+ Y.log('_unselectRow','info','plugin-row-select');
+ this.get(SELECTED_ROWS).remove(row, true);
+ row.removeClass(this.className + '-selected');
+ }
+
+}, {
+ NS : 'rowSelect',
+ EVENTS : EVENTS,
+ ATTRS : {
+ selectedRows : {
+ value : new Y.ArrayList()
+ },
+ multiSelect : {
+ value : false
+ },
+ toggleSelect : {
+ value : true
+ }
+ }
+});

0 comments on commit d0332a9

Please sign in to comment.