1(function($){ 2 3 var popupviewer = function() { 4 }; 5 6 /* singleton */ 7 var instance = null; 8 $.popupviewer = function() { 9 return instance || (instance = new popupviewer()); 10 }; 11 12 // Static functions 13 (function(_){ 14 15 var viewer = null; 16 var content = null; 17 var additionalContent = null; 18 var BASE_URL = DOKU_BASE + 'lib/exe/ajax.php'; 19 var viewerIsFixed = false; 20 var next = null; 21 var previous = null; 22 var internal = {}; 23 24 _.popupImageStack = null; 25 26 internal.log = function(message) { 27 console.log(message); 28 }; 29 30 _.showViewer = function() { 31 32 if ( viewer == null ) { 33 34 viewer = $('<div id="popupviewer"/>').click(_.hideViewer).appendTo('body'); 35 content = $('<div class="content"/>').click(function(e){e.stopPropagation()}); 36 content.current = $(); 37 38 additionalContent = $('<div class="additionalContent dokuwiki"/>'); 39 viewerIsFixed = viewer.css('position'); 40 41 $('<div class="controls"/>'). 42 append(content). 43 append(additionalContent). 44 append(previous = $('<a class="previous"/>').click({'direction': -1}, _.skipImageInDirection)). 45 append(next = $('<a class="next"/>').click({'direction': 1}, _.skipImageInDirection)). 46 append($('<a class="close"/>').addClass('visible').click(_.hideViewer)). 47 appendTo(viewer); 48 49 $(document).keydown(internal.globalKeyHandler); 50 51 } 52 53 content.empty(); 54 additionalContent.empty(); 55 $('body').css('overflow', 'hidden'); 56 viewer.show(); 57 return _; 58 }; 59 60 _.hideViewer = function(e, finalFunction) { 61 if ( viewer != null ) { 62 $('body').css('overflow', 'auto'); 63 64 additionalContent.animate({ 65 opacity: 0, 66 height: 0 67 }); 68 69 content.animate({ 70 width : 208, 71 height : 13, 72 }).parent('.controls').animate({ 73 top : '50%', 74 left : '50%', 75 'margin-left' : -104 76 }).parent('#popupviewer').animate({ 77 opacity: finalFunction ? 1 : 0 78 }, function(){ 79 viewer.hide(); 80 81 content.empty(); 82 additionalContent.empty(); 83 content.current = null; 84 85 additionalContent.css({ 86 opacity: 1, 87 height: '' 88 }); 89 90 content.css({ 91 width : '', 92 height : '', 93 }).parent('.controls').css({ 94 top : '', 95 left : '', 96 'margin-left' : '' 97 }).parent('#popupviewer').css({ 98 opacity : 1 99 }); 100 101 if ( typeof finalFunction == 'function' ) { 102 finalFunction(e); 103 } 104 }); 105 } 106 107 return _; 108 }; 109 110 internal.globalKeyHandler = function(e) { 111 112 if ( !viewer.is(":visible") ) return; 113 114 switch(e.keyCode) { 115 case 39: // Right 116 e.stopPropagation(); 117 next.click(); 118 break; 119 case 37: // Left 120 e.stopPropagation(); 121 previous.click(); 122 break; 123 case 27: // Escape 124 e.stopPropagation(); 125 _.hideViewer(); 126 break; 127 } 128 }; 129 130 _.presentViewerWithContent = function(e, popupData) { 131 132 popupData = popupData || this.popupData || e.target.popupData; // Either as param or from object 133 134 /* 135 popupData = { 136 isImage: boolean, 137 call: ajax_handler, 138 src: URL, 139 id: alternate_wiki_page, 140 width: width_of_window, 141 height: height_of_window 142 } 143 */ 144 145 if ( !popupData ) { return; } 146 147 e && e.preventDefault(); 148 _.showViewer(); 149 150 content.current = $(this); 151 152 internal.log(popupData); 153 154 if ( popupData.isImage ) { 155 156 // Load image routine 157 internal.log("loading an image"); 158 popupData.call = popupData.call || '_popup_load_image_meta'; 159 $(new Image()).attr('src', popupData.src || this.href).waitForImages(function(){ 160 161 var image = $(this); 162 163 var wrapper = $('<div/>').load(BASE_URL, popupData, function() { 164 165 // Force size for the moment 166 content.css({ 167 width: content.width(), 168 height: content.height(), 169 overflow: 'hidden' 170 }) 171 172 content.append(image); 173 content.popupData = jQuery.extend(true, {}, popupData); 174 175 additionalContent.html(wrapper.html()); 176 _.resizePopup(popupData.width, popupData.height, additionalContent.innerHeight(), image); 177 }); 178 }); 179 180 } else { 181 182 popupData.call = popupData.call || '_popup_load_file'; 183 popupData.src = popupData.src || BASE_URL; 184 var wrapper = $('<div/>').load(popupData.src, popupData, function(response, status, xhr) { 185 186 var success = function(node) 187 { 188 node.find('div.dokuwiki,body').first().waitForImages({ 189 finished: function() { 190 191 // Force size for the moment 192 content.css({ 193 width: content.width(), 194 height: content.height() 195 }) 196 197 node.find('a[href],form[action]'). 198 each(function(){ 199 // Replace all event handler 200 201 var element = $(this); 202 203 urlpart = element.attr('href') || element.attr('action') || ""; 204 if ( urlpart.match(new RegExp("^#.*?$")) ) { 205 // Scroll to anchor 206 element.click(function(){ 207 content.get(0).scrollTop( urlpart == '#' ? 0 : $(urlpart).offset().top); 208 }); 209 } 210 211 if ( this.getAttribute('popupviewerdata') ) { 212 this.popupData = $.parseJSON(this.getAttribute('popupviewerdata')); 213 this.removeAttribute('popupviewerdata'); 214 } else { 215 this.popupData = jQuery.extend(true, {}, popupData); 216 this.popupData.src = urlpart; 217 delete(this.popupData.id); // or it will always load this file. 218 } 219 220 $(this).bind('click', function(e){ 221 e.stopPropagation(); e.preventDefault(); 222 _.hideViewer(e, _.presentViewerWithContent); 223 }); 224 }); 225 226 content.html(this); 227 228 // Check for Javascript to execute 229 var script = ""; 230 node.find('popupscript'). 231 each(function() { 232 script += $.parseJSON((this.innerHTML || this.innerText)); 233 }) 234 235 var newContext = "jQuery.noConflict(); containerContext = this; ___ = function( selector, context ){return new jQuery.fn.init(selector,context||containerContext);}; ___.fn = ___.prototype = jQuery.fn;jQuery.extend( ___, jQuery );jQuery = ___;\n" 236 237 if ( script.length > 0 ) { 238 var randomID = Math.ceil(Math.random()*1000000); 239 content.attr('id', randomID); 240 241 var newContext = "jQuery.noConflict(); containerContext = this; ___ = function( selector, context ){return new jQuery.fn.init(selector,context||containerContext);}; ___.fn = ___.prototype = jQuery.fn;jQuery.extend( ___, jQuery );jQuery = ___;\n" 242 243 try{ 244 $.globalEval("try{\n(function(){\n"+newContext+script+"\n}).call(jQuery('div#"+randomID+"').get(0));\n}catch(e){}\n//"); 245 } catch (e) { 246 internal.log("Exception!"); 247 internal.log(e); 248 } 249 } 250 251 _.resizePopup(popupData.width, popupData.height, null, content, true); 252 253 }, waitForAll: true}); 254 } 255 256 257 if ( status == "error") { 258 // Go for an iframe 259 var finished = false; 260 var iframe = null; 261 262 var messageFunction = function(event) { 263 264 finished = true; 265 var data = event.data || event.originalEvent.data; 266 // If this message does not come with what we want, discard it. 267 if ((typeof data).toLowerCase() == "string" || !data.message 268 || data.message != 'frameContent') { 269 alert("Could not load page via popupviewer. The page responded with a wrong message."); 270 return; 271 } 272 273 iframe.remove(); 274 275 // Clear the window Event after we are done! 276 $(window).unbind("message", messageFunction); 277 278 success($(data.body)); 279 }; 280 281 var iframe = $('<iframe/>').load(function(){ 282 283 var frame = this; 284 if ( frame.contentWindow.postMessage ) { 285 286 // Register the Message Event for PostMessage receival 287 $(window).bind("message", messageFunction); 288 289 // Send a message 290 var message = "getFrameContent"; 291 frame.contentWindow.postMessage(message, "*"); 292 } 293 294 }).hide().attr('src', internal.getCurrentLocation() ).appendTo('body'); 295 296 window.setTimeout(function() { 297 if (!finished) { 298 iframe.remove(); 299 alert("Could not load page via popupviewer. The page is not available."); 300 } 301 }, 30000); 302 303 } else { 304 success(wrapper) 305 } 306 307 }); 308 } 309 }; 310 311 internal.getCurrentLocation = function() { 312 return content.current.attr('href') || content.current.attr('src') || content.current.attr('action'); 313 }; 314 315 internal.optimalSize = function(offsetElement) { 316 317 var prevWidth = content.width(); 318 var prevHeight = content.height(); 319 320 offsetElement.css({width:'', height: ''}); 321 322 width = offsetElement.width(); 323 height = offsetElement.height(); 324 325 // Reset to previous size so the whole thing will animate from the middle 326 offsetElement.css({width:prevWidth, height: prevHeight}); 327 328 return {width: width, height: height}; 329 } 330 331 _.resizePopup = function(width, height, additionalHeight, offsetElement, isPageContent) { 332 333 if ( offsetElement && !width && !height) { 334 var optimalSize = internal.optimalSize(offsetElement); 335 width = optimalSize.width; 336 height = optimalSize.height; 337 } 338 339 width = parseInt(width) || ($(window).width() * 0.7); 340 height = parseInt(height) || ($(window).height() * 0.8); 341 342 var ratio = width / height; 343 var maxHeight = ( $(window).height() * 0.99 ) - 60; 344 var maxWidth = ( $(window).width() * 0.99 ) - 40; 345 346 additionalHeight = additionalHeight || 0; 347 height += additionalHeight; 348 349 if ( height > maxHeight ) { 350 height = maxHeight; 351 if ( !isPageContent ) { // If this is an image we will have to fix the size 352 width = (height - additionalHeight) * ratio; 353 } else { 354 width += 20; // For the scroller Bar that will apear; 355 } 356 } 357 358 if ( width > maxWidth ) { 359 width = maxWidth; 360 if ( !isPageContent ) { // If this is an image we will have to fix the size 361 height = width / ratio + additionalHeight; 362 } 363 } 364 365 var xOffset = viewerIsFixed ? 0 : $(document).scrollLeft() || 0; 366 var yOffset = viewerIsFixed ? 0 : $(document).scrollTop() || 0; 367 368 yOffset = Math.max(($(window).height() - height) * 0.5 + yOffset, 5); 369 xOffset += ($(window).width() - width) * 0.5; 370 371 internal.log(width + " " + height); 372 internal.log(xOffset + " " + yOffset); 373 374 if ( !isPageContent && offsetElement.is('img') ) { 375 376 offsetElement.animate({ 377 width : width, 378 height : height - additionalHeight 379 }); 380 381 content.css({ 382 width : '', 383 height : '', 384 overflow: '' 385 }); 386 387 } else { 388 content.animate({ 389 width : width, 390 height : isPageContent ? height : 'auto', 391 }); 392 } 393 394 content.parent().animate({ 395 top : yOffset, 396 left : xOffset, 397 'margin-left' : 0 398 }); 399 400 if ( isPageContent ) { 401 content.removeClass('isImage'); 402 } else { 403 content.addClass('isImage'); 404 } 405 406 _.handleNextAndPrevious(!isPageContent); 407 return _; 408 }; 409 410 _.skipImageInDirection = function(e) 411 { 412 e.stopPropagation(); 413 414 if ( !$(this).is(':visible') ) { return; } 415 416 var skipTo = $.inArray(content.current.get(0), _.popupImageStack) + e.data.direction; 417 skipTo = Math.min(_.popupImageStack.length-1, Math.max(skipTo, 0)); 418 419 internal.log("skipping " + (e.data.direction < 0 ? 'previous' : 'next') + ' ' + skipTo ); 420 return _.skipToImage(skipTo, e.data.direction); 421 }; 422 423 _.skipToImage = function(skipTo, inDirection) 424 { 425 if ( !$(_.popupImageStack[skipTo]).is(content.current) ) { 426 _.hideViewer(null, function() { 427 // Deliver extra functionality to clicked item. 428 var nextItem = _.popupImageStack[skipTo]; 429 (nextItem.popupData && nextItem.popupData.click && nextItem.popupData.click(skipTo, inDirection)) || $(nextItem).click(); 430 }); 431 } 432 433 return _; 434 } 435 436 _.isFirst = function() { 437 return _.popupImageStack.first().is(content.current); 438 } 439 440 _.isLast = function() { 441 return _.popupImageStack.last().is(content.current); 442 } 443 444 _.handleNextAndPrevious = function(currentIsImage) { 445 446 if ( currentIsImage && _.popupImageStack && _.popupImageStack.length > 1) { 447 448 if ( _.isFirst() ) { 449 previous.addClass('inactive'); 450 } else { 451 previous.removeClass('inactive'); 452 } 453 454 if ( _.isLast() ) { 455 next.addClass('inactive'); 456 } else { 457 next.removeClass('inactive'); 458 } 459 460 next.addClass('visible'); 461 previous.addClass('visible'); 462 } else { 463 next.removeClass('visible'); 464 previous.removeClass('visible'); 465 } 466 467 return _; 468 }; 469 470 _.init = function(popupImageStack) { 471 472 _.popupImageStack = $(popupImageStack || 'a[popupviewerdata]').each(function(){ 473 this.popupData = this.popupData || $.parseJSON(this.getAttribute('popupviewerdata')); 474 if (this.removeAttribute) this.removeAttribute('popupviewerdata'); 475 $(this).unbind('click').click(_.presentViewerWithContent); 476 }).filter(function(){ 477 // Only images allowed in Stack. 478 return this.popupData.isImage; 479 }); 480 481 return _; 482 }; 483 484 })(popupviewer.prototype); 485 486 // Namespace all events. 487 var eventNamespace = 'waitForImages'; 488 489 // CSS properties which contain references to images. 490 $.waitForImages = { 491 hasImageProperties: ['backgroundImage', 'listStyleImage', 'borderImage', 'borderCornerImage', 'cursor'] 492 }; 493 494 // Custom selector to find `img` elements that have a valid `src` attribute and have not already loaded. 495 $.expr[':'].uncached = function (obj) { 496 // Ensure we are dealing with an `img` element with a valid `src` attribute. 497 if (!$(obj).is('img[src!=""]')) { 498 return false; 499 } 500 501 // Firefox's `complete` property will always be `true` even if the image has not been downloaded. 502 // Doing it this way works in Firefox. 503 var img = new Image(); 504 img.src = obj.src; 505 return !img.complete; 506 }; 507 508 $.fn.waitForImages = function (finishedCallback, eachCallback, waitForAll) { 509 510 var allImgsLength = 0; 511 var allImgsLoaded = 0; 512 513 // Handle options object. 514 if ($.isPlainObject(arguments[0])) { 515 waitForAll = arguments[0].waitForAll; 516 eachCallback = arguments[0].each; 517 // This must be last as arguments[0] 518 // is aliased with finishedCallback. 519 finishedCallback = arguments[0].finished; 520 } 521 522 // Handle missing callbacks. 523 finishedCallback = finishedCallback || $.noop; 524 eachCallback = eachCallback || $.noop; 525 526 // Convert waitForAll to Boolean 527 waitForAll = !! waitForAll; 528 529 // Ensure callbacks are functions. 530 if (!$.isFunction(finishedCallback) || !$.isFunction(eachCallback)) { 531 throw new TypeError('An invalid callback was supplied.'); 532 } 533 534 return this.each(function () { 535 // Build a list of all imgs, dependent on what images will be considered. 536 var obj = $(this); 537 var allImgs = []; 538 // CSS properties which may contain an image. 539 var hasImgProperties = $.waitForImages.hasImageProperties || []; 540 // To match `url()` references. 541 // Spec: http://www.w3.org/TR/CSS2/syndata.html#value-def-uri 542 var matchUrl = /url\(\s*(['"]?)(.*?)\1\s*\)/g; 543 544 if (waitForAll) { 545 546 // Get all elements (including the original), as any one of them could have a background image. 547 obj.find('*').addBack().each(function () { 548 var element = $(this); 549 550 // If an `img` element, add it. But keep iterating in case it has a background image too. 551 if (element.is('img:uncached')) { 552 allImgs.push({ 553 src: element.attr('src'), 554 element: element[0] 555 }); 556 } 557 558 $.each(hasImgProperties, function (i, property) { 559 var propertyValue = element.css(property); 560 var match; 561 562 // If it doesn't contain this property, skip. 563 if (!propertyValue) { 564 return true; 565 } 566 567 // Get all url() of this element. 568 while (match = matchUrl.exec(propertyValue)) { 569 allImgs.push({ 570 src: match[2], 571 element: element[0] 572 }); 573 } 574 }); 575 }); 576 } else { 577 // For images only, the task is simpler. 578 obj.find('img:uncached') 579 .each(function () { 580 allImgs.push({ 581 src: this.src, 582 element: this 583 }); 584 }); 585 } 586 587 allImgsLength = allImgs.length; 588 allImgsLoaded = 0; 589 590 // If no images found, don't bother. 591 if (allImgsLength === 0) { 592 finishedCallback.call(obj[0]); 593 } 594 595 $.each(allImgs, function (i, img) { 596 597 var image = new Image(); 598 599 // Handle the image loading and error with the same callback. 600 $(image).on('load.' + eventNamespace + ' error.' + eventNamespace, function (event) { 601 allImgsLoaded++; 602 603 // If an error occurred with loading the image, set the third argument accordingly. 604 eachCallback.call(img.element, allImgsLoaded, allImgsLength, event.type == 'load'); 605 606 if (allImgsLoaded == allImgsLength) { 607 finishedCallback.call(obj[0]); 608 return false; 609 } 610 611 }); 612 613 image.src = img.src; 614 }); 615 }); 616 }; 617 618 $(function(){ 619 $.popupviewer().init(); 620 }); 621 622})(jQuery); 623 624 625/* Loading the content for locally exported content */ 626(function($){ 627 $(window).bind("message", function(event){ 628 629 var data = event.data || event.originalEvent.data; 630 var source = event.source || event.originalEvent.source; 631 if (data != "getFrameContent") { 632 return; 633 } 634 635 try { 636 source.postMessage({ 637 message : "frameContent", 638 body : jQuery('html').html() 639 }, "*"); 640 } catch (e) { 641 alert("Fatal Exception! Could not load page via popupviewer.\n" + e); 642 } 643 }); 644})(jQuery);