1 (function ($, rf) {
  2 
  3     rf.ui = rf.ui || {};
  4 
  5     rf.ui.CollapsibleSubTable = function(id, f, options) {
  6         this.id = id;
  7         this.stateInput = options.stateInput;
  8         this.optionsInput = options.optionsInput;
  9         this.expandMode = options.expandMode || rf.ui.CollapsibleSubTable.MODE_CLNT;
 10         this.eventOptions = options.eventOptions;
 11         this.formId = f;
 12         this.isNested = options.isNested;
 13 
 14         this.attachToDom();
 15     };
 16 
 17     $.extend(rf.ui.CollapsibleSubTable, {
 18             MODE_AJAX: "ajax",
 19             MODE_SRV: "server",
 20             MODE_CLNT: "client",
 21             collapse: 0,
 22             expand: 1
 23         });
 24 
 25     rf.BaseComponent.extend(rf.ui.CollapsibleSubTable);
 26     var $super = rf.ui.CollapsibleSubTable.$super;
 27 
 28     $.extend(rf.ui.CollapsibleSubTable.prototype, (function () {
 29 
 30         var element = function() {
 31             if (! this.isNested) {
 32                 //use parent tbody as parent dom elem
 33                 return $(document.getElementById(this.id)).parent();
 34             } else {
 35                 var regex = new RegExp(this.id + "\\:\\d\\:b");
 36                 return $(document.getElementById(this.id)).parent().find("tr").filter(function() {
 37                     return this.id.match(regex);
 38                 });
 39             }
 40         };
 41 
 42         var stateInputElem = function() {
 43             return $(document.getElementById(this.stateInput));
 44         };
 45 
 46         var optionsInputElem = function() {
 47             return $(document.getElementById(this.optionsInput));
 48         };
 49 
 50         var ajax = function(e, options) {
 51             this.__switchState();
 52             rf.ajax(this.id, e, options);
 53         };
 54 
 55         var server = function(options) {
 56             this.__switchState();
 57             $(document.getElementById(this.formId)).submit();
 58         };
 59 
 60         var client = function(options) {
 61             if (this.isExpanded()) {
 62                 this.collapse(options);
 63             } else {
 64                 this.expand(options);
 65             }
 66         };
 67 
 68 
 69         return {
 70 
 71             name: "CollapsibleSubTable",
 72 
 73             switchState: function(e, options) {
 74                 if (this.expandMode == rf.ui.CollapsibleSubTable.MODE_AJAX) {
 75                     ajax.call(this, e, this.eventOptions, options);
 76                 } else if (this.expandMode == rf.ui.CollapsibleSubTable.MODE_SRV) {
 77                     server.call(this, options);
 78                 } else if (this.expandMode == rf.ui.CollapsibleSubTable.MODE_CLNT) {
 79                     client.call(this, options);
 80                 }
 81             },
 82 
 83             collapse: function(options) {
 84                 this.setState(rf.ui.CollapsibleSubTable.collapse);
 85                 element.call(this).hide();
 86             },
 87 
 88             expand: function(options) {
 89                 this.setState(rf.ui.CollapsibleSubTable.expand);
 90                 element.call(this).show();
 91             },
 92 
 93             isExpanded: function() {
 94                 return (parseInt(this.getState()) == rf.ui.CollapsibleSubTable.expand);
 95             },
 96 
 97             __switchState: function(options) {
 98                 var state = this.isExpanded() ? rf.ui.CollapsibleSubTable.collapse : rf.ui.CollapsibleSubTable.expand;
 99                 this.setState(state);
100             },
101 
102             getState: function() {
103                 return stateInputElem.call(this).val();
104             },
105 
106             setState: function(state) {
107                 stateInputElem.call(this).val(state)
108             },
109 
110             setOption: function(option) {
111                 optionsInputElem.call(this).val(option);
112             },
113 
114             getMode: function() {
115                 return this.expandMode;
116             },
117             destroy: function() {
118                 $super.destroy.call(this);
119             }
120         };
121 
122     })());
123 
124 })(RichFaces.jQuery, window.RichFaces);