      1 /*
      2  * JBoss, Home of Professional Open Source
      3  * Copyright ${year}, Red Hat, Inc. and individual contributors
      4  * by the @authors tag. See the copyright.txt in the distribution for a
      5  * full listing of individual contributors.
      6  *
      7  * This is free software; you can redistribute it and/or modify it
      8  * under the terms of the GNU Lesser General Public License as
      9  * published by the Free Software Foundation; either version 2.1 of
     10  * the License, or (at your option) any later version.
     11  *
     12  * This software is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
     15  * Lesser General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Lesser General Public
     18  * License along with this software; if not, write to the Free
     19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
     20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
     21  */
     22
     23 (function ($, rf) {
     24
     25     rf.ui = rf.ui || {};
     26
     27     rf.ui.Popup = function(id, options) {
     28         $super.constructor.call(this, id);
     29         this.options = $.extend({}, defaultOptions, options);
     30         this.positionOptions = {type: this.options.positionType, from:this.options.jointPoint, to:this.options.direction, offset: this.options.positionOffset};
     31
     32         this.popup = $(document.getElementById(id));
     33
     34         this.visible = this.options.visible;
     35         this.attachTo = this.options.attachTo;
     36         this.attachToBody = this.options.attachToBody;
     37         this.positionType = this.options.positionType;
     38         this.positionOffset = this.options.positionOffset;
     39     };
     40
     41     rf.BaseComponent.extend(rf.ui.Popup);
     42     var $super = rf.ui.Popup.$super;
     43
     44     var defaultOptions = {
     45         visible: false
     46     };
     47
     48     $.extend(rf.ui.Popup.prototype, {
     49
     50             name : "popup",
     51
     52             show: function(event) {
     53                 if (!this.visible) {
     54                     if (this.attachToBody) {
     55                         this.parentElement = this.popup.parent().get(0);
     56                         document.body.appendChild(this.popup.get(0));
     57                     }
     58                     this.visible = true;
     59                 }
     60
     61                 this.popup.setPosition(event || {id: this.attachTo}, this.positionOptions).show();
     62             },
     63
     64             hide: function() {
     65                 if (this.visible) {
     66                     this.popup.hide();
     67                     this.visible = false;
     68                     if (this.attachToBody && this.parentElement) {
     69                         this.parentElement.appendChild(this.popup.get(0));
     70                         this.parentElement = null;
     71                     }
     72                 }
     73             },
     74
     75             isVisible: function() {
     76                 return this.visible;
     77             },
     78
     79             getId: function() {
     80                 return this.id;
     81             },
     82
     83             destroy: function() {
     84                 if (this.attachToBody && this.parentElement) {
     85                     this.parentElement.appendChild(this.popup.get(0));
     86                     this.parentElement = null;
     87                 }
     88             }
     89         });
     90
     91 })(RichFaces.jQuery, window.RichFaces);