Ver Mensaje Individual
  #6 (permalink)  
Antiguo 17/04/2009, 03:09
Avatar de ZiTAL
ZiTAL
 
Fecha de Ingreso: marzo-2004
Ubicación: Bermio (Bizkaia)
Mensajes: 1.545
Antigüedad: 20 años, 10 meses
Puntos: 62
Respuesta: Aporte: zmodal ventana modal

zmodal.js
Código javascript:
Ver original
  1. /**
  2.  * @author zital
  3.  *
  4.  * zmodal: modal box
  5.  * license: GPL 3.0
  6.  * version: 0.0.1
  7.  * based on subModal: can be downloaded from [url]http://gabrito.com/files/subModal/[/url]
  8.  */
  9.  
  10. var zmodal = function()
  11. {
  12.     // public properties
  13.     this.title;
  14.     this.width;
  15.     this.height;
  16.     this.type;
  17.     this.content;
  18.     this.hiddeTags = new Array('object', 'embed', 'select');
  19.    
  20.     // private properties
  21.     this.browser;
  22.     this.background;
  23.     this.modal;
  24.     this.events;
  25.  
  26.     // public events
  27.  
  28.     // main event
  29.     zmodal.prototype.main = function()
  30.     {
  31.         // hidde elements
  32.         setClassByTags(document.body, this.hiddeTags, 'zhidden', 'add');
  33.         // get browser
  34.         this.browser = getBrowser();
  35.         // set new events for web
  36.         setEvents.call(this);
  37.         // set modal window
  38.         setModal.call(this);
  39.     };
  40.    
  41.     // event to redefine, whe close modal do this
  42.     zmodal.prototype.onClose = function(){};
  43.  
  44.     // private events  
  45.    
  46.     // set Modal window
  47.     var setModal = function()
  48.     {
  49.         // create main div and add to page
  50.         var div = document.createElement('div');
  51.         div.id = 'zmodal';
  52.         document.body.appendChild(div);
  53.        
  54.         // create background div
  55.         this.background = document.createElement('div');
  56.         this.background.className = 'background';
  57.  
  58.         // create modal div
  59.         this.modal = document.createElement('div');
  60.         this.modal.className = 'modal';
  61.  
  62.         // set modal position
  63.         setPosition.call(this);
  64.  
  65.         // add divs to modal div
  66.         div.appendChild(this.background);
  67.         div.appendChild(this.modal);
  68.  
  69.         // set Modal Content
  70.         setModalContent.call(this, this.type, this.content);
  71.     };
  72.  
  73.     // set Modal position
  74.     var setPosition = function()
  75.     {
  76.         // get Scroll positions
  77.         var scroll = getScroll();
  78.         // get page width
  79.         var width = getPageSize('width');
  80.         // get page height
  81.         var height = getPageSize('height');
  82.         // set background position
  83.         setBackgroundPosition.call(this, this.background, scroll, width, height);
  84.         // set modal position
  85.         setModalPosition.call(this, this.modal, scroll, width, height);
  86.     };
  87.  
  88.     // set background position
  89.     var setBackgroundPosition = function(elem, scroll, width, height)
  90.     {
  91.         // depend of scroll position
  92.         elem.style.left = scroll[0]+"px";
  93.         elem.style.top = scroll[1]+"px";
  94.         // if browser internet explorer
  95.         if(this.browser==='msie')
  96.         {
  97.             elem.style.width = width+"px";
  98.             elem.style.height = height+"px";
  99.         }
  100.         // no ie browser
  101.         else
  102.         {
  103.             elem.style.width = '100%';
  104.             elem.style.height = '100%';
  105.         }
  106.     };
  107.  
  108.     // set Modal position
  109.     var setModalPosition = function(elem, scroll, width, height)
  110.     {
  111.         elem.style.width = this.width+"px";
  112.         elem.style.height = this.height+"px";
  113.         elem.style.left = scroll[0]+(width-this.width)/2+"px";
  114.         elem.style.top = scroll[1]+(height-this.height)/2+"px";
  115.     };
  116.  
  117.     // set modal content
  118.     var setModalContent = function(type, content)
  119.     {
  120.         // add close img
  121.         setCloseImg.call(this);
  122.  
  123.         // modal out div (overflow: hidden)
  124.         var _out = document.createElement('div');
  125.         _out.className = 'out';
  126.         this.modal.appendChild(_out);
  127.  
  128.         // modal in div (overflow: auto)
  129.         var _in = document.createElement('div');
  130.         _in.className = 'in';
  131.         if(this.title!==undefined)
  132.         {
  133.             // set title to out div
  134.             setTitle(_out, this.title);
  135.             var titlediv = getByClass(document.getElementById('zmodal'), 'title');
  136.             // get title Height: is necessary to be added to web before get Height
  137.             var titleHeight = getSize(titlediv, 'height');
  138.             // substract title height to 'in' div
  139.             _in.style.height = (this.height-titleHeight)+"px"; 
  140.         }
  141.         // if no title full height
  142.         else
  143.             _in.style.height = this.height+"px";
  144.        
  145.         // full width
  146.         _in.style.width = this.width+"px";
  147.         // add in div to out div
  148.         _out.appendChild(_in);
  149.  
  150.         // modal content types
  151.         if(type==='html')
  152.             _in.innerHTML = content;
  153.         else if(type==='dom')
  154.             _in.appendChild(content);
  155.         // get DOM ID content and put in the modal, onclose restore div
  156.         else if(type==='id')
  157.             setElementChilds(_in, document.getElementById(content).childNodes);
  158.     };
  159.    
  160.     // set Modal title to out div
  161.     var setTitle = function(elem, title)
  162.     {
  163.         var div = document.createElement('div');
  164.         div.className = 'title';
  165.         var span = document.createElement('span');
  166.         span.appendChild(document.createTextNode(title));
  167.         div.appendChild(span);
  168.         elem.appendChild(div);
  169.     };
  170.  
  171.     // get Scroll info
  172.     var getScroll = function()
  173.     {
  174.         var scroll = [];
  175.         if (self.pageYOffset)
  176.         {
  177.             scroll[0] = self.pageXOffset;
  178.             scroll[1] = self.pageYOffset;
  179.         }
  180.         else if (document.documentElement && document.documentElement.scrollTop)
  181.         {
  182.             scroll[0] = document.documentElement.scrollLeft;
  183.             scroll[1] = document.documentElement.scrollTop;
  184.         }
  185.         else if (document.body)
  186.         {
  187.             scroll[0] = document.body.scrollLeft;
  188.             scroll[1] = document.body.scrollTop;
  189.         }
  190.         return scroll;
  191.     };
  192.  
  193.     // set Events to page
  194.     var setEvents = function()
  195.     {
  196.         var t = this;
  197.         t.events = [];
  198.         // get old Events and save
  199.         t.events[0] = window.onscroll;
  200.         t.events[1] = window.onresize;
  201.         // set new events
  202.         window.onscroll = function()
  203.         {
  204.             setPosition.call(t);
  205.         };
  206.         window.onresize = function()
  207.         {
  208.             setPosition.call(t);
  209.         };
  210.     };
  211.    
  212.     // restore page old events
  213.     var restoreEvents = function(t)
  214.     {
  215.         window.onscroll = t.events[0];
  216.         window.onresize = t.events[1];
  217.     }
  218.  
  219.     // get page size by type
  220.     var getPageSize = function(type)
  221.     {
  222.         var a;
  223.         if(type==='width')
  224.         {
  225.             if (window.innerWidth!=window.undefined) a = window.innerWidth;
  226.             else if (document.compatMode=='CSS1Compat') a = document.documentElement.clientWidth;
  227.             else if (document.body) a = document.body.clientWidth;
  228.             else a = window.undefined;
  229.         }
  230.         else if(type==='height')
  231.         {
  232.             if (window.innerHeight!=window.undefined) a = window.innerHeight;
  233.             else if (document.compatMode=='CSS1Compat') a = document.documentElement.clientHeight;
  234.             else if (document.body) a = document.body.clientHeight;
  235.             else a = window.undefined;
  236.         }
  237.         return a;
  238.     };
  239.  
  240.     // get Element size in page
  241.     var getSize = function(elem, type)
  242.     {
  243.         if(type==='width')
  244.             return (elem.offsetWidht || elem.style.pixelWidth);
  245.         else if(type==='height')
  246.         {
  247.             return (elem.offsetHeight || elem.style.pixelHeigth);
  248.         }
  249.     };
  250.  
  251.     // get browser type
  252.     var getBrowser = function()
  253.     {
  254.         var a;
  255.         if(navigator.appName==='Microsoft Internet Explorer')
  256.             a = 'msie';
  257.         else
  258.             a = 'default';
  259.         return a;
  260.     };
  261.  
  262.     // set close button
  263.     var setCloseImg= function()
  264.     {
  265.         var t = this;
  266.         var div = document.createElement('div');
  267.         div.className = 'close';
  268.         var img = document.createElement('img');
  269.         img.src = 'img/x-trans.png';
  270.         img.title = 'close';
  271.         img.alt = 'close';
  272.        
  273.         // add event
  274.         img.onclick = function()
  275.         {
  276.             privateClose(t);
  277.         };
  278.         // if ie browser fix PNG
  279.         if(t.browser==='msie')
  280.             img = fixPng(img);
  281.         div.appendChild(img);
  282.         t.modal.appendChild(div);
  283.     };
  284.  
  285.     // remove element from page
  286.     var removeElement = function(elem)
  287.     {
  288.         elem.parentNode.removeChild(elem);
  289.     };
  290.  
  291.     // fix png for ie6
  292.     var fixPng = function(img)
  293.     {
  294.         var png = /\.png$/i;
  295.         if(png.test(img.src))
  296.         {
  297.             img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\""+img.src+"\",sizingMethod=\"image\")";
  298.             img.src = 'img/trans.gif';
  299.         }
  300.         return img;
  301.     };
  302.  
  303.     // add childs to element
  304.     var setElementChilds = function(elem, obj)
  305.     {
  306.         var len = obj.length;
  307.         for(var i=0;i<len;i++)
  308.             elem.appendChild(obj[i]);
  309.     };
  310.  
  311.     // onclose private method
  312.     var privateClose = function(t)
  313.     {
  314.         var zmodal = document.getElementById('zmodal');
  315.         // if type ID restore ID content to div
  316.         if(t.type==='id')
  317.         {
  318.             var _in = getByClass(zmodal, 'in');
  319.             setElementChilds(document.getElementById(t.content), _in.childNodes);
  320.         }
  321.         // remove modal
  322.         removeElement(zmodal);
  323.         // restore hidden elements
  324.         setClassByTags(document.body, t.hiddeTags, 'zhidden', 'remove');
  325.         // restore events
  326.         restoreEvents(t);
  327.         // execute public onClose method
  328.         t.onClose();
  329.     };
  330.  
  331.     // get element by class (only returns 1)
  332.     var getByClass = function(elem, _class)
  333.     {
  334.         var childs = elem.childNodes;
  335.         var len = childs.length;
  336.         for(var i=0;i<len;i++)
  337.             if(childs[i].className===_class)
  338.                 return childs[i];
  339.             else if(childs[i].hasChildNodes())
  340.                 var a = getByClass(childs[i], _class);
  341.         return a;
  342.     };
  343.     var addClass = function(elem, _class)
  344.     {
  345.         elem.className+= " "+_class;
  346.     };
  347.     var removeClass = function(elem, _class)
  348.     {
  349.         var aClass = elem.className.split(' ');
  350.         var len = aClass.length;
  351.         var tmp = '';
  352.         for(var i=0;i<len;i++)
  353.             if(aClass[i]!==_class)
  354.                 tmp+=" "+aClass[i];
  355.         elem.className = tmp;
  356.     };
  357.     var setClassByTags = function(_parent, aTags, _class, _type)
  358.     {
  359.         var childs = _parent.childNodes;
  360.         var clen = childs.length;
  361.         var tlen = aTags.length;
  362.         for(var i=0;i<clen;i++)
  363.         {
  364.             for(var j=0;j<tlen;j++)
  365.             {
  366.                 if(childs[i].nodeName.toLowerCase()===aTags[j])
  367.                 {
  368.                     if(_type==='add')
  369.                         addClass(childs[i], _class);
  370.                     else if(_type==='remove')
  371.                         removeClass(childs[i], _class);
  372.                 }
  373.                 if(childs[i].hasChildNodes())
  374.                     setClassByTags(childs[i], aTags, _class, _type);
  375.             }
  376.         }      
  377.     };
  378. };
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan