1 (function ($, rf) {
  2 
  3     rf.ui = rf.ui || {};
  4 
  5     rf.ui.DataTable = function(id, options) {
  6         $super.constructor.call(this, id);
  7         this.options = $.extend(this.options, options || {});
  8         this.element = this.attachToDom();
  9         var self = this;
 10         var header = $(this.element).find('.rf-dt-thd');
 11         header.find(".rf-dt-c-srt").each(function() {
 12             $(this).bind("click", {sortHandle: this}, $.proxy(self.sortHandler, self));
 13         });
 14         header.find(".rf-dt-flt-i").each(function() {
 15             $(this).bind("blur", {filterHandle: this}, $.proxy(self.filterHandler, self));
 16         });
 17     };
 18 
 19     rf.BaseComponent.extend(rf.ui.DataTable);
 20     var $super = rf.ui.DataTable.$super;
 21 
 22     $.extend(rf.ui.DataTable, {
 23             SORTING: "rich:sorting",
 24             FILTERING: "rich:filtering",
 25             SUBTABLE_SELECTOR:".rf-cst"
 26         });
 27 
 28     $.extend(rf.ui.DataTable.prototype, ( function () {
 29 
 30         var invoke = function(event, attributes) {
 31             rf.ajax(this.id, event, {"parameters" : attributes});
 32         };
 33 
 34         var createParameters = function(type, id, arg1, arg2) {
 35             var parameters = {};
 36             var key = this.id + type;
 37             parameters[key] = (id + ":" + (arg1 || "") + ":" + arg2);
 38 
 39             var eventOptions = this.options.ajaxEventOption;
 40             for (key in eventOptions) {
 41                 if (!parameters[key]) {
 42                     parameters[key] = eventOptions[key];
 43                 }
 44             }
 45             return parameters;
 46         };
 47 
 48 
 49         return {
 50 
 51             name : "RichFaces.ui.DataTable",
 52 
 53             sort: function(columnId, direction, isClear) {
 54                 invoke.call(this, null, createParameters.call(this, rf.ui.DataTable.SORTING, columnId, direction, isClear));
 55             },
 56 
 57             clearSorting: function() {
 58                 this.sort("", "", true);
 59             },
 60 
 61             sortHandler: function(event) {
 62                 var sortHandle = $(event.data.sortHandle);
 63                 var button = sortHandle.find('.rf-dt-srt-btn');
 64                 var columnId = button.data('columnid');
 65                 var sortOrder = button.hasClass('rf-dt-srt-asc') ? 'descending' : 'ascending';
 66                 this.sort(columnId, sortOrder, false);
 67             },
 68 
 69             filter: function(columnId, filterValue, isClear) {
 70                 invoke.call(this, null, createParameters.call(this, rf.ui.DataTable.FILTERING, columnId, filterValue, isClear));
 71             },
 72 
 73             clearFiltering: function() {
 74                 this.filter("", "", true);
 75             },
 76 
 77             filterHandler: function(event) {
 78                 var filterHandle = $(event.data.filterHandle);
 79                 var columnId = filterHandle.data('columnid');
 80                 var filterValue = filterHandle.val();
 81                 this.filter(columnId, filterValue, false);
 82             },
 83 
 84             expandAllSubTables: function() {
 85                 this.invokeOnSubTables('expand');
 86             },
 87 
 88             collapseAllSubTables: function() {
 89                 this.invokeOnSubTables('collapse');
 90             },
 91 
 92             switchSubTable: function(id) {
 93                 this.getSubTable(id).switchState();
 94             },
 95 
 96             getSubTable: function(id) {
 97                 return rf.component(id);
 98             },
 99 
100             invokeOnSubTables: function(funcName) {
101                 var elements = $(document.getElementById(this.id)).children(rf.ui.DataTable.SUBTABLE_SELECTOR);
102                 var invokeOnComponent = this.invokeOnComponent;
103                 elements.each(
104                     function() {
105                         if (this.firstChild && this.firstChild[rf.RICH_CONTAINER] && this.firstChild[rf.RICH_CONTAINER].component) {
106                             var component = this.firstChild[rf.RICH_CONTAINER].component;
107                             if (component instanceof RichFaces.ui.CollapsibleSubTable) {
108                                 invokeOnComponent(component, funcName);
109                             }
110                         }
111                     }
112                 );
113             },
114 
115             invokeOnSubTable: function(id, funcName) {
116                 var subtable = this.getSubTable(id);
117                 this.invokeOnComponent(subtable, funcName);
118             },
119 
120             invokeOnComponent: function(component, funcName) {
121                 if (component) {
122                     var func = component[funcName];
123                     if (typeof func == 'function') {
124                         func.call(component);
125                     }
126                 }
127             },
128             destroy: function() {
129                 $super.destroy.call(this);
130             }
131         }
132 
133     })());
134 
135 })(RichFaces.jQuery, window.RichFaces);
136 
137