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