      1 (function($, rf) {
      2     rf.ui = rf.ui || {};
      3
      4     var defaultOptions = {
      5         mode : "server",
      6         cssRoot : "ddm",
      7         cssClasses : {}
      8     }
      9
     10     // constructor definition
     11
     12     rf.ui.MenuItem = function(componentId, options) {
     13         this.options = {};
     14         $.extend(this.options, defaultOptions, options || {});
     15         $super.constructor.call(this, componentId);
     16         $.extend(this.options.cssClasses, buildCssClasses.call(this, this.options.cssRoot));
     17         this.attachToDom(componentId);
     18         this.element = $(rf.getDomElement(componentId));
     19         rf.Event.bindById(this.id, 'click', this.__clickHandler, this);
     20         rf.Event.bindById(this.id, 'mouseenter', this.select, this);
     21         rf.Event.bindById(this.id, 'mouseleave', this.unselect, this);
     22         this.selected = false;
     23     };
     24
     25     var buildCssClasses = function(cssRoot) {
     26         var cssClasses = {
     27             itemCss : "rf-" +cssRoot+ "-itm",
     28             selectItemCss : "rf-" +cssRoot+ "-itm-sel",
     29             unselectItemCss : "rf-" +cssRoot+ "-itm-unsel",
     30             labelCss: "rf-" +cssRoot+ "-lbl"
     31         }
     32         return cssClasses;
     33     }
     34
     35     rf.BaseComponent.extend(rf.ui.MenuItem);
     36
     37     // define super class link
     38     var $super = rf.ui.MenuItem.$super;
     39
     40     $.extend(rf.ui.MenuItem.prototype, (function() {
     41
     42         return {
     43             name : "MenuItem",
     44             select : function() {
     45                 this.element.removeClass(this.options.cssClasses.unselectItemCss);
     46                 this.element.addClass(this.options.cssClasses.selectItemCss);
     47                 this.selected = true;
     48             },
     49             unselect : function() {
     50                 this.element.removeClass(this.options.cssClasses.selectItemCss);
     51                 this.element.addClass(this.options.cssClasses.unselectItemCss);
     52                 this.selected = false;
     53             },
     54             activate : function() {
     55                 this.invokeEvent('click', rf.getDomElement(this.id));
     56             },
     57
     58             isSelected : function() {
     59                 return this.selected;
     60             },
     61
     62             __clickHandler : function(e) {
     63                 if ($(e.target).is(":input:not(:button):not(:reset):not(:submit)")) {
     64                     return;
     65                 }
     66
     67                 var parentMenu = this.__getParentMenu();
     68                 if (parentMenu) {
     69                     parentMenu.processItem(this.element);
     70                 }
     71
     72                 var item = rf.getDomElement(this.id);
     73                 var params = this.options.params;
     74                 var form = this.__getParentForm(item);
     75                 var itemId = {};
     76                 itemId[item.id] = item.id;
     77                 $.extend(itemId, params || {});
     78                 e.form = form;
     79                 e.itemId = itemId;
     80                 this.options.onClickHandler.call(this, e);
     81             },
     82
     83             __getParentForm : function(item) {
     84                 return $($(item).parents("form").get(0));
     85             },
     86
     87             __getParentMenu : function() {
     88                 var menu = this.element.parents('div.' + this.options.cssClasses.labelCss);
     89                 if (menu && menu.length > 0)
     90                     return rf.component(menu);
     91                 else
     92                     return null;
     93             }
     94         };
     95     })());
     96
     97 })(RichFaces.jQuery, RichFaces);