jquery.DOMWindow.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. (function($){
  2. //closeDOMWindow
  3. $.fn.closeDOMWindow = function(settings){
  4. if(!settings){settings={};}
  5. var run = function(passingThis){
  6. if(settings.anchoredClassName){
  7. var $anchorClassName = $('.'+settings.anchoredClassName);
  8. $anchorClassName.fadeOut('fast',function(){
  9. if($.fn.draggable){
  10. $anchorClassName.draggable('destory').trigger("unload").remove();
  11. }else{
  12. $anchorClassName.trigger("unload").remove();
  13. }
  14. });
  15. if(settings.functionCallOnClose){settings.functionCallAfterClose();}
  16. }else{
  17. var $DOMWindowOverlay = $('#DOMWindowOverlay');
  18. var $DOMWindow = $('#DOMWindow');
  19. $DOMWindowOverlay.fadeOut('fast',function(){
  20. $DOMWindowOverlay.trigger('unload').unbind().remove();
  21. });
  22. $DOMWindow.fadeOut('fast',function(){
  23. if($.fn.draggable){
  24. $DOMWindow.draggable("destroy").trigger("unload").remove();
  25. }else{
  26. $DOMWindow.trigger("unload").remove();
  27. }
  28. });
  29. $(window).unbind('scroll.DOMWindow');
  30. $(window).unbind('resize.DOMWindow');
  31. if($.fn.openDOMWindow.isIE6){$('#DOMWindowIE6FixIframe').remove();}
  32. if(settings.functionCallOnClose){settings.functionCallAfterClose();}
  33. }
  34. };
  35. if(settings.eventType){//if used with $().
  36. return this.each(function(index){
  37. $(this).bind(settings.eventType, function(){
  38. run(this);
  39. return false;
  40. });
  41. });
  42. }else{//else called as $.function
  43. run();
  44. }
  45. };
  46. //allow for public call, pass settings
  47. $.closeDOMWindow = function(s){$.fn.closeDOMWindow(s);};
  48. //openDOMWindow
  49. $.fn.openDOMWindow = function(instanceSettings){
  50. var shortcut = $.fn.openDOMWindow;
  51. //default settings combined with callerSettings////////////////////////////////////////////////////////////////////////
  52. shortcut.defaultsSettings = {
  53. anchoredClassName:'',
  54. anchoredSelector:'',
  55. borderColor:'#ccc',
  56. borderSize:'4',
  57. draggable:0,
  58. eventType:null, //click, blur, change, dblclick, error, focus, load, mousedown, mouseout, mouseup etc...
  59. fixedWindowY:100,
  60. functionCallOnOpen:null,
  61. functionCallOnClose:null,
  62. height:500,
  63. loader:0,
  64. loaderHeight:0,
  65. loaderImagePath:'',
  66. loaderWidth:0,
  67. modal:0,
  68. overlay:1,
  69. overlayColor:'#000',
  70. overlayOpacity:'85',
  71. positionLeft:0,
  72. positionTop:0,
  73. positionType:'centered', // centered, anchored, absolute, fixed
  74. width:500,
  75. windowBGColor:'#fff',
  76. windowBGImage:null, // http path
  77. windowHTTPType:'get',
  78. windowPadding:10,
  79. windowSource:'inline', //inline, ajax, iframe
  80. windowSourceID:'',
  81. windowSourceURL:'',
  82. windowSourceAttrURL:'href'
  83. };
  84. var settings = $.extend({}, $.fn.openDOMWindow.defaultsSettings , instanceSettings || {});
  85. //Public functions
  86. shortcut.viewPortHeight = function(){ return self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;};
  87. shortcut.viewPortWidth = function(){ return self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;};
  88. shortcut.scrollOffsetHeight = function(){ return self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;};
  89. shortcut.scrollOffsetWidth = function(){ return self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;};
  90. shortcut.isIE6 = typeof document.body.style.maxHeight === "undefined";
  91. //Private Functions/////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. var sizeOverlay = function(){
  93. var $DOMWindowOverlay = $('#DOMWindowOverlay');
  94. if(shortcut.isIE6){//if IE 6
  95. var overlayViewportHeight = document.documentElement.offsetHeight + document.documentElement.scrollTop - 4;
  96. var overlayViewportWidth = document.documentElement.offsetWidth - 21;
  97. $DOMWindowOverlay.css({'height':overlayViewportHeight +'px','width':overlayViewportWidth+'px'});
  98. }else{//else Firefox, safari, opera, IE 7+
  99. $DOMWindowOverlay.css({'height':'100%','width':'100%','position':'fixed'});
  100. }
  101. };
  102. var sizeIE6Iframe = function(){
  103. var overlayViewportHeight = document.documentElement.offsetHeight + document.documentElement.scrollTop - 4;
  104. var overlayViewportWidth = document.documentElement.offsetWidth - 21;
  105. $('#DOMWindowIE6FixIframe').css({'height':overlayViewportHeight +'px','width':overlayViewportWidth+'px'});
  106. };
  107. var centerDOMWindow = function() {
  108. var $DOMWindow = $('#DOMWindow');
  109. if(settings.height + 50 > shortcut.viewPortHeight()){//added 50 to be safe
  110. $DOMWindow.css('left',Math.round(shortcut.viewPortWidth()/2) + shortcut.scrollOffsetWidth() - Math.round(($DOMWindow.outerWidth())/2));
  111. }else{
  112. $DOMWindow.css('left',Math.round(shortcut.viewPortWidth()/2) + shortcut.scrollOffsetWidth() - Math.round(($DOMWindow.outerWidth())/2));
  113. $DOMWindow.css('top',Math.round(shortcut.viewPortHeight()/2) + shortcut.scrollOffsetHeight() - Math.round(($DOMWindow.outerHeight())/2));
  114. }
  115. };
  116. var centerLoader = function() {
  117. var $DOMWindowLoader = $('#DOMWindowLoader');
  118. if(shortcut.isIE6){//if IE 6
  119. $DOMWindowLoader.css({'left':Math.round(shortcut.viewPortWidth()/2) + shortcut.scrollOffsetWidth() - Math.round(($DOMWindowLoader.innerWidth())/2),'position':'absolute'});
  120. $DOMWindowLoader.css({'top':Math.round(shortcut.viewPortHeight()/2) + shortcut.scrollOffsetHeight() - Math.round(($DOMWindowLoader.innerHeight())/2),'position':'absolute'});
  121. }else{
  122. $DOMWindowLoader.css({'left':'50%','top':'50%','position':'fixed'});
  123. }
  124. };
  125. var fixedDOMWindow = function(){
  126. var $DOMWindow = $('#DOMWindow');
  127. $DOMWindow.css('left', settings.positionLeft + shortcut.scrollOffsetWidth());
  128. $DOMWindow.css('top', + settings.positionTop + shortcut.scrollOffsetHeight());
  129. };
  130. var showDOMWindow = function(instance){
  131. if(arguments[0]){
  132. $('.'+instance+' #DOMWindowLoader').remove();
  133. $('.'+instance+' #DOMWindowContent').fadeIn('fast',function(){if(settings.functionCallOnOpen){settings.functionCallOnOpen();}});
  134. $('.'+instance+ '.closeDOMWindow').click(function(){
  135. $.closeDOMWindow();
  136. return false;
  137. });
  138. }else{
  139. $('#DOMWindowLoader').remove();
  140. $('#DOMWindow').fadeIn('fast',function(){if(settings.functionCallOnOpen){settings.functionCallOnOpen();}});
  141. $('#DOMWindow .closeDOMWindow').click(function(){
  142. $.closeDOMWindow();
  143. return false;
  144. });
  145. }
  146. };
  147. var urlQueryToObject = function(s){
  148. var query = {};
  149. s.replace(/b([^&=]*)=([^&=]*)b/g, function (m, a, d) {
  150. if (typeof query[a] != 'undefined') {
  151. query[a] += ',' + d;
  152. } else {
  153. query[a] = d;
  154. }
  155. });
  156. return query;
  157. };
  158. //Run Routine ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  159. var run = function(passingThis){
  160. //get values from element clicked, or assume its passed as an option
  161. settings.windowSourceID = $(passingThis).attr('href') || settings.windowSourceID;
  162. settings.windowSourceURL = $(passingThis).attr(settings.windowSourceAttrURL) || settings.windowSourceURL;
  163. settings.windowBGImage = settings.windowBGImage ? 'background-image:url('+settings.windowBGImage+')' : '';
  164. var urlOnly, urlQueryObject;
  165. if(settings.positionType == 'anchored'){//anchored DOM window
  166. var anchoredPositions = $(settings.anchoredSelector).position();
  167. var anchoredPositionX = anchoredPositions.left + settings.positionLeft;
  168. var anchoredPositionY = anchoredPositions.top + settings.positionTop;
  169. $('body').append('<div class="'+settings.anchoredClassName+'" style="'+settings.windowBGImage+';background-repeat:no-repeat;padding:'+settings.windowPadding+'px;overflow:auto;position:absolute;top:'+anchoredPositionY+'px;left:'+anchoredPositionX+'px;height:'+settings.height+'px;width:'+settings.width+'px;background-color:'+settings.windowBGColor+';border:'+settings.borderSize+'px solid '+settings.borderColor+';z-index:10001"><div id="DOMWindowContent" style="display:none"></div></div>');
  170. //loader
  171. if(settings.loader && settings.loaderImagePath !== ''){
  172. $('.'+settings.anchoredClassName).append('<div id="DOMWindowLoader" style="width:'+settings.loaderWidth+'px;height:'+settings.loaderHeight+'px;"><img src="'+settings.loaderImagePath+'" /></div>');
  173. }
  174. if($.fn.draggable){
  175. if(settings.draggable){$('.' + settings.anchoredClassName).draggable({cursor:'move'});}
  176. }
  177. switch(settings.windowSource){
  178. case 'inline'://////////////////////////////// inline //////////////////////////////////////////
  179. $('.' + settings.anchoredClassName+" #DOMWindowContent").append($(settings.windowSourceID).children());
  180. $('.' + settings.anchoredClassName).unload(function(){// move elements back when you're finished
  181. $('.' + settings.windowSourceID).append( $('.' + settings.anchoredClassName+" #DOMWindowContent").children());
  182. });
  183. showDOMWindow(settings.anchoredClassName);
  184. break;
  185. case 'iframe'://////////////////////////////// iframe //////////////////////////////////////////
  186. $('.' + settings.anchoredClassName+" #DOMWindowContent").append('<iframe frameborder="0" hspace="0" wspace="0" src="'+settings.windowSourceURL+'" name="DOMWindowIframe'+Math.round(Math.random()*1000)+'" style="width:100%;height:100%;border:none;background-color:#fff;" class="'+settings.anchoredClassName+'Iframe" ></iframe>');
  187. $('.'+settings.anchoredClassName+'Iframe').load(showDOMWindow(settings.anchoredClassName));
  188. break;
  189. case 'ajax'://////////////////////////////// ajax //////////////////////////////////////////
  190. if(settings.windowHTTPType == 'post'){
  191. if(settings.windowSourceURL.indexOf("?") !== -1){//has a query string
  192. urlOnly = settings.windowSourceURL.substr(0, settings.windowSourceURL.indexOf("?"));
  193. urlQueryObject = urlQueryToObject(settings.windowSourceURL);
  194. }else{
  195. urlOnly = settings.windowSourceURL;
  196. urlQueryObject = {};
  197. }
  198. $('.' + settings.anchoredClassName+" #DOMWindowContent").load(urlOnly,urlQueryObject,function(){
  199. showDOMWindow(settings.anchoredClassName);
  200. });
  201. }else{
  202. if(settings.windowSourceURL.indexOf("?") == -1){ //no query string, so add one
  203. settings.windowSourceURL += '?';
  204. }
  205. $('.' + settings.anchoredClassName+" #DOMWindowContent").load(
  206. settings.windowSourceURL + '&random=' + (new Date().getTime()),function(){
  207. showDOMWindow(settings.anchoredClassName);
  208. });
  209. }
  210. break;
  211. }
  212. }else{//centered, fixed, absolute DOM window
  213. //overlay & modal
  214. if(settings.overlay){
  215. $('body').append('<div id="DOMWindowOverlay" style="z-index:10000;display:none;position:absolute;top:0;left:0;background-color:'+settings.overlayColor+';filter:alpha(opacity='+settings.overlayOpacity+');-moz-opacity: 0.'+settings.overlayOpacity+';opacity: 0.'+settings.overlayOpacity+';"></div>');
  216. if(shortcut.isIE6){//if IE 6
  217. $('body').append('<iframe id="DOMWindowIE6FixIframe" src="blank.html" style="width:100%;height:100%;z-index:9999;position:absolute;top:0;left:0;filter:alpha(opacity=0);"></iframe>');
  218. sizeIE6Iframe();
  219. }
  220. sizeOverlay();
  221. var $DOMWindowOverlay = $('#DOMWindowOverlay');
  222. $DOMWindowOverlay.fadeIn('fast');
  223. if(!settings.modal){$DOMWindowOverlay.click(function(){$.closeDOMWindow();});}
  224. }
  225. //loader
  226. if(settings.loader && settings.loaderImagePath !== ''){
  227. $('body').append('<div id="DOMWindowLoader" style="z-index:10002;width:'+settings.loaderWidth+'px;height:'+settings.loaderHeight+'px;"><img src="'+settings.loaderImagePath+'" /></div>');
  228. centerLoader();
  229. }
  230. //add DOMwindow
  231. $('body').append('<div id="DOMWindow" style="background-repeat:no-repeat;'+settings.windowBGImage+';overflow:auto;padding:'+settings.windowPadding+'px;display:none;height:'+settings.height+'px;width:'+settings.width+'px;background-color:'+settings.windowBGColor+';border:'+settings.borderSize+'px solid '+settings.borderColor+'; position:absolute;z-index:10001"></div>');
  232. var $DOMWindow = $('#DOMWindow');
  233. //centered, absolute, or fixed
  234. switch(settings.positionType){
  235. case 'centered':
  236. centerDOMWindow();
  237. if(settings.height + 50 > shortcut.viewPortHeight()){//added 50 to be safe
  238. $DOMWindow.css('top', (settings.fixedWindowY + shortcut.scrollOffsetHeight()) + 'px');
  239. }
  240. break;
  241. case 'absolute':
  242. $DOMWindow.css({'top':(settings.positionTop+shortcut.scrollOffsetHeight())+'px','left':(settings.positionLeft+shortcut.scrollOffsetWidth())+'px'});
  243. if($.fn.draggable){
  244. if(settings.draggable){$DOMWindow.draggable({cursor:'move'});}
  245. }
  246. break;
  247. case 'fixed':
  248. fixedDOMWindow();
  249. break;
  250. case 'anchoredSingleWindow':
  251. var anchoredPositions = $(settings.anchoredSelector).position();
  252. var anchoredPositionX = anchoredPositions.left + settings.positionLeft;
  253. var anchoredPositionY = anchoredPositions.top + settings.positionTop;
  254. $DOMWindow.css({'top':anchoredPositionY + 'px','left':anchoredPositionX+'px'});
  255. break;
  256. }
  257. $(window).bind('scroll.DOMWindow',function(){
  258. if(settings.overlay){sizeOverlay();}
  259. if(shortcut.isIE6){sizeIE6Iframe();}
  260. if(settings.positionType == 'centered'){centerDOMWindow();}
  261. if(settings.positionType == 'fixed'){fixedDOMWindow();}
  262. });
  263. $(window).bind('resize.DOMWindow',function(){
  264. if(shortcut.isIE6){sizeIE6Iframe();}
  265. if(settings.overlay){sizeOverlay();}
  266. if(settings.positionType == 'centered'){centerDOMWindow();}
  267. });
  268. switch(settings.windowSource){
  269. case 'inline'://////////////////////////////// inline //////////////////////////////////////////
  270. $DOMWindow.append($(settings.windowSourceID).children());
  271. $DOMWindow.unload(function(){// move elements back when you're finished
  272. $(settings.windowSourceID).append($DOMWindow.children());
  273. });
  274. showDOMWindow();
  275. break;
  276. case 'iframe'://////////////////////////////// iframe //////////////////////////////////////////
  277. $DOMWindow.append('<iframe frameborder="0" hspace="0" wspace="0" src="'+settings.windowSourceURL+'" name="DOMWindowIframe'+Math.round(Math.random()*1000)+'" style="width:100%;height:100%;border:none;background-color:#fff;" id="DOMWindowIframe" ></iframe>');
  278. $('#DOMWindowIframe').load(showDOMWindow());
  279. break;
  280. case 'ajax'://////////////////////////////// ajax //////////////////////////////////////////
  281. if(settings.windowHTTPType == 'post'){
  282. if(settings.windowSourceURL.indexOf("?") !== -1){//has a query string
  283. urlOnly = settings.windowSourceURL.substr(0, settings.windowSourceURL.indexOf("?"));
  284. urlQueryObject = urlQueryToObject(settings.windowSourceURL);
  285. }else{
  286. urlOnly = settings.windowSourceURL;
  287. urlQueryObject = {};
  288. }
  289. $DOMWindow.load(urlOnly,urlQueryObject,function(){
  290. showDOMWindow();
  291. });
  292. }else{
  293. if(settings.windowSourceURL.indexOf("?") == -1){ //no query string, so add one
  294. settings.windowSourceURL += '?';
  295. }
  296. $DOMWindow.load(
  297. settings.windowSourceURL + '&random=' + (new Date().getTime()),function(){
  298. showDOMWindow();
  299. });
  300. }
  301. break;
  302. }
  303. }//end if anchored, or absolute, fixed, centered
  304. };//end run()
  305. if(settings.eventType){//if used with $().
  306. return this.each(function(index){
  307. $(this).bind(settings.eventType,function(){
  308. run(this);
  309. return false;
  310. });
  311. });
  312. }else{//else called as $.function
  313. run();
  314. }
  315. };//end function openDOMWindow
  316. //allow for public call, pass settings
  317. $.openDOMWindow = function(s){$.fn.openDOMWindow(s);};
  318. })(jQuery);